You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.js 2.0KB

3 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. var Decimal = require("decimal.js");
  2. Decimal8 = Decimal.clone({ precision:8, rounding:8 });
  3. function doSmartRedirect(req, res, defaultUrl) {
  4. if (req.session.redirectUrl) {
  5. res.redirect(req.session.redirectUrl);
  6. req.session.redirectUrl = null;
  7. } else {
  8. res.redirect(defaultUrl);
  9. }
  10. res.end();
  11. }
  12. function redirectToConnectPageIfNeeded(req, res) {
  13. if (!req.session.host) {
  14. req.session.redirectUrl = req.originalUrl;
  15. res.redirect("/");
  16. res.end();
  17. return true;
  18. }
  19. return false;
  20. }
  21. function hex2ascii(hex) {
  22. var str = "";
  23. for (var i = 0; i < hex.length; i += 2) {
  24. str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
  25. }
  26. return str;
  27. }
  28. function getBlockReward(blockHeight) {
  29. var nSubsidy = 10 * 100000000;
  30. var prevBlock = blockHeight -1;
  31. // yearly decline of production by 10% per year, projected 136m coins max by year 2050+.
  32. var i =0;
  33. for (i = 262800; i <= prevBlock ; i += 262800 ) {
  34. nSubsidy -= nSubsidy/10;
  35. }
  36. return nSubsidy/100000000;
  37. }
  38. function splitArrayIntoChunks(array, chunkSize) {
  39. var j = array.length;
  40. var chunks = [];
  41. for (var i = 0; i < j; i += chunkSize) {
  42. chunks.push(array.slice(i, i + chunkSize));
  43. }
  44. return chunks;
  45. }
  46. function getRandomString(length, chars) {
  47. var mask = '';
  48. if (chars.indexOf('a') > -1) {
  49. mask += 'abcdefghijklmnopqrstuvwxyz';
  50. }
  51. if (chars.indexOf('A') > -1) {
  52. mask += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  53. }
  54. if (chars.indexOf('#') > -1) {
  55. mask += '0123456789';
  56. }
  57. if (chars.indexOf('!') > -1) {
  58. mask += '~`!@#$%^&*()_+-={}[]:";\'<>?,./|\\';
  59. }
  60. var result = '';
  61. for (var i = length; i > 0; --i) {
  62. result += mask[Math.floor(Math.random() * mask.length)];
  63. }
  64. return result;
  65. }
  66. module.exports = {
  67. doSmartRedirect: doSmartRedirect,
  68. redirectToConnectPageIfNeeded: redirectToConnectPageIfNeeded,
  69. hex2ascii: hex2ascii,
  70. getBlockReward: getBlockReward,
  71. splitArrayIntoChunks: splitArrayIntoChunks,
  72. getRandomString: getRandomString
  73. };