sdk.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. var router = require('koa-router')();
  2. var moralis = require('../model/moralis_sdk.js')
  3. var utils = require('../model/utils.js');
  4. var { reids_token_config, account_config } = require('../config/config.js');
  5. const logger = require('../model/logger.js');
  6. router.prefix('/sdk');
  7. const redis = require("../model/redis_db") //导入 db.js
  8. /**
  9. * 获取代币价格
  10. * @param {*} ctx
  11. */
  12. async function getAllTotkenPrice(ctx) {
  13. console.log('getTotkenPrice in:')
  14. var ret = await moralis.getAllTotkenPrice()
  15. console.log('getTotkenPrice result:', ret)
  16. if (ret)
  17. ctx.body = utils.toJson(0, ret, null);
  18. else ctx.body = utils.toJson(-1, null, "redis read error.");
  19. }
  20. /**
  21. * 获取交易记录
  22. * @param {*} ctx
  23. */
  24. async function getTransfers(ctx) {
  25. console.log("body", ctx);
  26. const obj = ctx.request.body;
  27. console.log("obj", obj);
  28. if (!obj.chain)//默认 bsc 币安链
  29. obj.chain = 'bsc'
  30. var temp_obj = { ...obj }
  31. await moralis.getTokenTransfers(obj).then((result) => {
  32. ctx.body = result;
  33. if (result) {
  34. //提交归集任务 native 能获取到 gas 、token 无法获取到 gas 费
  35. try {
  36. if (temp_obj.address && moralis.isTransferSucceed(result)) {
  37. var json_obj = JSON.parse(result);
  38. //缓存当前交易的 gas 费用
  39. var tr = moralis.getTransferRecordGasFree('native', json_obj, temp_obj.address)
  40. logger.log('getTransferRecordGasFree:', tr, temp_obj.address)
  41. if (tr && tr.totalGasFree > 0) {
  42. logger.log('getTransferRecordGasFree redis_set LAST_TOTAL_BNB_FREE:', tr.totalGasFree.toString())
  43. logger.log('getTransferRecordGasFree redis_set LAST_TOTAL_TOKEN_FREE:', (parseInt(tr.totalGasFree) * parseInt(account_config.TOKEN_GAS_LIMIT)).toString())
  44. redis.redis_set(reids_token_config.LAST_TOTAL_BNB_FREE, tr.totalGasFree.toString());
  45. // var tokenFee = await redis.readRedis(reids_token_config.LAST_TOTAL_TOKEN_FREE)
  46. // if (!tokenFee)
  47. redis.redis_set(reids_token_config.LAST_TOTAL_TOKEN_FREE, (parseInt(tr.gas_price) * parseInt(account_config.TOKEN_GAS_LIMIT)).toString());
  48. }
  49. //提交归集任务
  50. if (temp_obj.address) {
  51. moralis.pushCollectConisObj(temp_obj)
  52. }
  53. }
  54. } catch (error) {
  55. console.error('pushCollectConisObj error=', error)
  56. }
  57. }
  58. })
  59. }
  60. /**
  61. * 出金,入金交易
  62. * @param {*} ctx
  63. * @returns
  64. */
  65. async function transfer(ctx) {
  66. if (ctx.request == null || ctx.request.body == null) {
  67. ctx.body = utils.toJson(-1, null, "request error. ");
  68. return
  69. }
  70. const obj = ctx.request.body;
  71. await moralis.transfer(obj).then((result) => {
  72. ctx.body = result;
  73. });
  74. }
  75. async function getAllTokenWithdrawInfoLists(ctx) {
  76. if (ctx.request == null || ctx.request.body == null) {
  77. ctx.body = utils.toJson(-1, null, "request error. ");
  78. return
  79. }
  80. ctx.body = await moralis.getAllTokenWithdrawInfoLists(ctx);
  81. }
  82. //出金
  83. async function withdraw(ctx) {
  84. if (ctx.request == null || ctx.request.body == null) {
  85. ctx.body = utils.toJson(-1, null, "request error. ");
  86. return
  87. }
  88. const obj = ctx.request.body;
  89. await moralis.withdraw(obj).then((result) => {
  90. ctx.body = result;
  91. });
  92. }
  93. //获取交易记录
  94. router.post('/getTransfers', getTransfers)
  95. // 获取所有代币价格
  96. router.post('/getAllTotkenPrice', getAllTotkenPrice)
  97. // router.post('/transfer', transfer)
  98. //提现
  99. router.post('/withdraw', withdraw);
  100. //获取所有地址的所要消耗的最低提取费
  101. router.post('/getAllTokenWithdrawInfoLists', getAllTokenWithdrawInfoLists)
  102. module.exports = router;