test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const router = require('koa-router')() //导入 koa-router
  2. const moralis = require("../model/moralis_sdk") //导入 db.js
  3. // https://github.com/luin/ioredis#readme
  4. router.prefix('/denettest');
  5. const BigNumber = require('bignumber.js')
  6. function calculate_total_usdprice(amount, decimals, usdprice) {
  7. return parseInt(amount) / (10 ** parseInt(decimals)) * parseFloat(usdprice);
  8. // var new_decimals = 15;
  9. // var pos = 0;
  10. // var new_amount = 0;
  11. // var new_amount_bint = 0;
  12. // if (parseInt(decimals) > new_decimals) {
  13. // pos = parseInt(decimals) - parseInt(decimals) % new_decimals
  14. // new_amount = amount.substring(0, pos+1)
  15. // new_amount_bint = parseInt(new_amount)
  16. // var scale_dec = parseInt((10 ** new_decimals))
  17. // console.log('amount',amount)
  18. // console.log('pos',pos)
  19. // console.log('new_amount',new_amount)
  20. // console.log('new_amount_bint',new_amount_bint)
  21. // console.log('scale_dec',scale_dec)
  22. // console.log('usdprice',usdprice)
  23. // 1000000000000000
  24. // 1000000000000000
  25. // return new_amount_bint / scale_dec * parseInt(usdprice);
  26. // } else {
  27. // return BigInt(amount) / (10 ** BigInt(decimals) * BigInt(usdprice));
  28. // }
  29. }
  30. /**
  31. * 获取交易记录
  32. * @param {*} ctx
  33. */
  34. async function getTransfers(ctx) {
  35. const obj = ctx.request.body;
  36. console.log("obj", obj);
  37. if (!obj.chain)//默认 bsc 币安链
  38. obj.chain = 'bsc'
  39. var token_total_usdprice = calculate_total_usdprice('99999999999999999998', '18', 0.1);
  40. var data =
  41. {
  42. nonce: 118,
  43. gasPrice: { type: 'BigNumber', hex: '0x02540be400' },
  44. gasLimit: { type: 'BigNumber', hex: '0x5208' },
  45. to: '0x7C7401fcc82D1e53C4090561c3e6fde80d74e317',
  46. value: { type: 'BigNumber', hex: '0x039696f3392000' },
  47. data: '0x',
  48. chainId: 97,
  49. v: 230,
  50. r: '0x075149b50e81da71aa72ee73d1b07e8df6fa2416b97133dc3e2dea19b4ed4c88',
  51. s: '0x7d8ac906b85275d63233e8f8ba6c0f5b860e9ebea1bc896346fe81824d9ec9c3',
  52. from: '0xAD48D13E77011cFE03fF19729B6A247847AfD28E',
  53. hash: '0xb9a75efb6a17325f2decaec9fba30fead7664c13a589fe0ccac02f791a7c84c8',
  54. type: null,
  55. confirmations: 0
  56. }
  57. var curGasPrice = BigNumber(data.gasPrice.hex)
  58. var curGasLimit = BigNumber(data.gasLimit.hex)
  59. if(curGasLimit > 0)
  60. {
  61. console.log('curGasLimit', curGasLimit.toNumber())
  62. }
  63. console.log('curGasPrice', curGasPrice.toNumber())
  64. var a = parseInt('10000000000000000')
  65. var b = parseInt('100000000000000000')
  66. console.log(token_total_usdprice, a / b);
  67. // ctx.body = await moralis.collectCoins(obj);
  68. //提交归集任务
  69. if (obj.address) {
  70. moralis.pushCollectConisObj(obj)
  71. }
  72. ctx.body = curGasPrice + '-' + curGasLimit;
  73. // await moralis.collectCoins(obj).then((result) => {
  74. // ctx.body = result;
  75. // })
  76. }
  77. //获取交易记录
  78. router.post('/getTransfers', getTransfers)
  79. async function crypto_test(ctx) {
  80. if (ctx.request == null || ctx.request.body == null) {
  81. ctx.body = utils.toJson(-1, null, "request error. ");
  82. return;
  83. }
  84. const obj = ctx.request.body;
  85. var crypt_key = obj.key;
  86. var message = obj.privateKey;
  87. //process.env.NODE_ENV
  88. console.log("NDK_HOME", process.env.NDK_HOME);
  89. var de_message = utils.decryptPrivityKey(crypt_key, message);
  90. console.log("decrypt message:", message);
  91. ctx.body = {
  92. decrypt: de_message
  93. }
  94. }
  95. router.post('/crypto_test', crypto_test)
  96. module.exports = router