test.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. const withdraw_db = require('../model/db/withdraw_db')
  5. router.prefix('/denettest');
  6. const BigNumber = require('bignumber.js')
  7. function calculate_total_usdprice(amount, decimals, usdprice) {
  8. return parseInt(amount) / (10 ** parseInt(decimals)) * parseFloat(usdprice);
  9. // var new_decimals = 15;
  10. // var pos = 0;
  11. // var new_amount = 0;
  12. // var new_amount_bint = 0;
  13. // if (parseInt(decimals) > new_decimals) {
  14. // pos = parseInt(decimals) - parseInt(decimals) % new_decimals
  15. // new_amount = amount.substring(0, pos+1)
  16. // new_amount_bint = parseInt(new_amount)
  17. // var scale_dec = parseInt((10 ** new_decimals))
  18. // console.log('amount',amount)
  19. // console.log('pos',pos)
  20. // console.log('new_amount',new_amount)
  21. // console.log('new_amount_bint',new_amount_bint)
  22. // console.log('scale_dec',scale_dec)
  23. // console.log('usdprice',usdprice)
  24. // 1000000000000000
  25. // 1000000000000000
  26. // return new_amount_bint / scale_dec * parseInt(usdprice);
  27. // } else {
  28. // return BigInt(amount) / (10 ** BigInt(decimals) * BigInt(usdprice));
  29. // }
  30. }
  31. /**
  32. * 获取交易记录
  33. * @param {*} ctx
  34. */
  35. async function getTransfers(ctx) {
  36. const obj = ctx.request.body;
  37. console.log("obj", obj);
  38. if (!obj.chain)//默认 bsc 币安链
  39. obj.chain = 'bsc'
  40. var token_total_usdprice = calculate_total_usdprice('99999999999999999998', '18', 0.1);
  41. var data =
  42. {
  43. nonce: 118,
  44. gasPrice: { type: 'BigNumber', hex: '0x02540be400' },
  45. gasLimit: { type: 'BigNumber', hex: '0x5208' },
  46. to: '0x7C7401fcc82D1e53C4090561c3e6fde80d74e317',
  47. value: { type: 'BigNumber', hex: '0x039696f3392000' },
  48. data: '0x',
  49. chainId: 97,
  50. v: 230,
  51. r: '0x075149b50e81da71aa72ee73d1b07e8df6fa2416b97133dc3e2dea19b4ed4c88',
  52. s: '0x7d8ac906b85275d63233e8f8ba6c0f5b860e9ebea1bc896346fe81824d9ec9c3',
  53. from: '0xAD48D13E77011cFE03fF19729B6A247847AfD28E',
  54. hash: '0xb9a75efb6a17325f2decaec9fba30fead7664c13a589fe0ccac02f791a7c84c8',
  55. type: null,
  56. confirmations: 0
  57. }
  58. var curGasPrice = BigNumber(data.gasPrice.hex)
  59. var curGasLimit = BigNumber(data.gasLimit.hex)
  60. if (curGasLimit > 0) {
  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 withdraw(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 temp_ob = { ...obj }
  86. temp_ob.from_address = '0xxpaoifhjopiesaj'
  87. temp_ob.to_address = temp_ob.receiver
  88. var ret = await withdraw_db.create_withdraw_task(temp_ob);
  89. ctx.body = ret;
  90. var isExist = await withdraw_db.withdraw_id_exist(temp_ob.withdraw_id)
  91. if (isExist) {
  92. // [ret_obj.withdraw_status, ret_obj.withdraw_hash, ret_obj.nonce, ret_obj.update_time, ret_obj.gas_price, ret_obj.gas_limit, withdraw_id]
  93. // var update_obj = {
  94. // withdraw_status: 1,
  95. // withdraw_hash: 'asdfasdf',
  96. // nonce: 0,
  97. // update_time: 12989,
  98. // gas_price: '1200000',
  99. // gas_limit: '80000'
  100. // }
  101. var update_obj={}
  102. await withdraw_db.update_withdraw_task(temp_ob.withdraw_id, update_obj)
  103. }
  104. }
  105. async function crypto_test(ctx) {
  106. if (ctx.request == null || ctx.request.body == null) {
  107. ctx.body = utils.toJson(-1, null, "request error. ");
  108. return;
  109. }
  110. const obj = ctx.request.body;
  111. var crypt_key = obj.key;
  112. var message = obj.privateKey;
  113. //process.env.NODE_ENV
  114. console.log("NDK_HOME", process.env.NDK_HOME);
  115. var de_message = utils.decryptPrivityKey(crypt_key, message);
  116. console.log("decrypt message:", message);
  117. ctx.body = {
  118. decrypt: de_message
  119. }
  120. }
  121. router.post('/crypto_test', crypto_test)
  122. router.post('/withdraw', withdraw)
  123. module.exports = router