sdk.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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/db/redis_db") //导入 db.js
  8. const withdraw_db = require("../model/db/withdraw_db") //导入 db.js
  9. const report = require("../model/report") //导入 db.js
  10. const BigNumber = require('bignumber.js')
  11. /**
  12. * 获取代币价格
  13. * @param {*} ctx
  14. */
  15. async function getAllTotkenPrice(ctx) {
  16. console.log('getTotkenPrice in:')
  17. var ret = await moralis.getAllTotkenPrice()
  18. console.log('getTotkenPrice result:', ret)
  19. if (ret)
  20. ctx.body = utils.toJson(0, ret, null);
  21. else ctx.body = utils.toJson(-1, null, "redis read error.");
  22. }
  23. /**
  24. * 获取交易记录
  25. * @param {*} ctx
  26. */
  27. async function getTransfers(ctx) {
  28. const obj = ctx.request.body;
  29. console.log("getTransfers body", obj);
  30. if (!obj.chain)//默认 bsc 币安链
  31. obj.chain = 'bsc_mainnet'
  32. var temp_obj = { ...obj }
  33. var index = 0
  34. //提交归集任务
  35. if (temp_obj.address) {
  36. logger.log('pushCollectConisObj>>>', temp_obj.address)
  37. redis.redis_push(reids_token_config.COLLECT_CONIS_QUEUE_KEY, JSON.stringify(temp_obj))
  38. }
  39. // for (let index = 0; index < 30; index++) {
  40. await moralis.getTokenTransfers(obj).then((result) => {
  41. logger.log('getTransfers response', 'index=' + index, result)
  42. ctx.body = result;
  43. if (result) {
  44. //提交归集任务 native 能获取到 gas 、token 无法获取到 gas 费
  45. try {
  46. if (temp_obj.address && moralis.isTransferSucceed(result)) {
  47. var log_obj = { ...obj }
  48. log_obj.results = result
  49. log_obj.type = report.REPORT_TYPE.transfer_record
  50. //埋点日志上报-入金检查
  51. report.logReport(log_obj)
  52. var json_obj = JSON.parse(result);
  53. //缓存当前交易的 gas 费用
  54. var tr = moralis.getTransferRecordGasFree('native', json_obj, temp_obj.address)
  55. logger.log('getTransferRecordGasFree:', tr, temp_obj.address)
  56. if (tr && tr.totalGasFree > 0) {
  57. logger.log('getTransferRecordGasFree redis_set LAST_TOTAL_BNB_FREE:', tr.totalGasFree.toString())
  58. logger.log('getTransferRecordGasFree redis_set LAST_TOTAL_TOKEN_FREE:', (parseInt(tr.totalGasFree) * parseInt(account_config.TOKEN_GAS_LIMIT)).toString())
  59. redis.redis_set(reids_token_config.LAST_TOTAL_BNB_FREE, tr.totalGasFree.toString());
  60. redis.redis_set(reids_token_config.LAST_TOTAL_TOKEN_FREE, (parseInt(tr.gas_price) * parseInt(account_config.TOKEN_GAS_LIMIT)).toString());
  61. }
  62. }
  63. } catch (error) {
  64. console.error('pushCollectConisObj error=', error)
  65. }
  66. }
  67. })
  68. // }
  69. }
  70. async function getAllTokenWithdrawInfoLists(ctx) {
  71. if (ctx.request == null || ctx.request.body == null) {
  72. ctx.body = utils.toJson(-1, null, "request error. ");
  73. return
  74. }
  75. ctx.body = await moralis.getAllTokenWithdrawInfoLists(ctx);
  76. }
  77. async function collect_conis_task() {
  78. logger.log("collect_conis_task start")
  79. while (true) {
  80. var start_time = utils.getTimestamp()
  81. var exec_obj = await redis.redis_pop(reids_token_config.COLLECT_CONIS_QUEUE_KEY)
  82. if (!exec_obj) {
  83. await utils.sleep(30000)
  84. logger.log("没有归集任务")
  85. continue
  86. }
  87. try {
  88. exec_obj = JSON.parse(exec_obj)
  89. logger.log('collect_conis_task exec item>>>>', exec_obj);
  90. if (exec_obj.address == await redis.readRedis(reids_token_config.LAST_COLLECT_PUBLIC_KEY) && utils.getTimestamp() - await redis.readRedis(reids_token_config.LAST_COLLECT_TIME) < 60 * 2 * 1000) {
  91. logger.log('间隔不足 1 分钟', exec_obj);
  92. continue
  93. }
  94. redis.redis_set(reids_token_config.LAST_COLLECT_TIME, utils.getTimestamp())
  95. redis.redis_set(reids_token_config.LAST_COLLECT_PUBLIC_KEY, exec_obj.address)
  96. //开始收集用户地址里面的币到归集地址
  97. var ret = await moralis.collectCoins(exec_obj)
  98. logger.log('collect_conis_task ret =', exec_obj, ret)
  99. try {
  100. var ret_obj = JSON.parse(ret)
  101. if (ret_obj.code == 0) {
  102. logger.log('触发归集 delay collect_conis_task ret =', exec_obj, ret)
  103. //间隔 10s 归集,避免提交任务过多
  104. await utils.sleep(10000)
  105. }
  106. } catch (error) { }
  107. } catch (error) {
  108. logger.error('collect_conis_task error', error.toString());
  109. redis.redis_set(reids_token_config.LAST_COLLECT_TIME, 0)
  110. redis.redis_set(reids_token_config.LAST_COLLECT_PUBLIC_KEY, 0)
  111. }
  112. logger.log("collect_conis_task cost-time", utils.getTimestamp() - start_time, exec_obj)
  113. }
  114. }
  115. async function withdraw_task() {
  116. logger.log("withdraw_task start")
  117. let last_time = 0
  118. let last_hash = ''
  119. while (true) {
  120. var exec_obj = await redis.redis_pop(reids_token_config.WITHDRAW_QUEUE_KEY)
  121. if (!exec_obj) {
  122. await utils.sleep(60000)
  123. logger.log("没有出金任务")
  124. continue
  125. }
  126. try {
  127. exec_obj = JSON.parse(exec_obj)
  128. } catch (error) {
  129. logger.error('item parse error', error);
  130. break
  131. }
  132. var temp_obj = { ...exec_obj }
  133. if (utils.getTimestamp() - last_time < 60000) {
  134. //有可能上一个区块还未更新,这里做一个尝试限制
  135. //Error: Failed to make "eth_sendRawTransaction" request with networkConnector: "already known"
  136. //通过 交易 hash 获取块。last_hash
  137. if (last_hash) {
  138. var options = {
  139. transaction_hash: last_hash,
  140. chain: temp_obj.chain,
  141. endTime: '2099-01-01'
  142. }
  143. var tryCount = 10;
  144. do {
  145. try {
  146. //通过获取上一个交易记录来进行确认
  147. var transaction = await moralis.getTokenTransfers(options);
  148. logger.log('withdraw_task exectransaction', transaction, options, tryCount);
  149. transaction = JSON.parse(transaction)
  150. if (transaction.code == 0) {
  151. if (transaction.data.result.length <= 0) {
  152. logger.log('等待10s');
  153. await utils.sleep(10000)
  154. } else {
  155. logger.log('等待5s');
  156. await utils.sleep(5000)
  157. break
  158. }
  159. } else {
  160. break
  161. }
  162. tryCount -= 1
  163. } catch (error) {
  164. logger.error('withdraw_task exectransaction', error.toString());
  165. }
  166. } while (tryCount >= 0);
  167. }
  168. }
  169. //如果失败重试一次
  170. var tryCount = 1;
  171. for (let index = 0; index < 1 + tryCount; index++) {
  172. try {
  173. var result = await withdraw_({ ...temp_obj })
  174. last_time = utils.getTimestamp()
  175. logger.log('withdraw_task withdraw_ =', result, last_time)
  176. if (result && moralis.isTransferSucceed(result)) {
  177. var obj = JSON.parse(result)
  178. var nonce = obj.data.nonce
  179. var curGasPrice = BigNumber(obj.data.gasPrice.hex).toNumber()
  180. var curGasLimit = BigNumber(obj.data.gasLimit.hex).toNumber()
  181. var value = BigNumber(obj.data.value.hex).toNumber()
  182. var hash = obj.data.hash
  183. last_hash = hash
  184. var update_obj = {}
  185. update_obj.withdraw_status = 2
  186. update_obj.withdraw_hash = hash
  187. update_obj.nonce = nonce
  188. update_obj.gas_price = curGasPrice.toString()
  189. update_obj.gas_limit = curGasLimit.toString()
  190. update_obj.value = value.toString()
  191. update_obj.errorMsg = ''
  192. await withdraw_db.update_withdraw_task(exec_obj.withdraw_id, update_obj)
  193. break
  194. } else {
  195. logger.error('withdraw_task withdraw_ error=', result, JSON.stringify(temp_obj))
  196. if (index < 1 + tryCount && result.includes('eth_sendRawTransaction')) {
  197. logger.error('try withdraw_:', JSON.stringify(temp_obj), index)
  198. await utils.sleep(3000)
  199. continue
  200. }
  201. var update_obj = {}
  202. update_obj.withdraw_status = 3
  203. if (typeof result === 'string') {
  204. try {
  205. result = JSON.parse(result)
  206. update_obj.errorMsg = result.errMsg
  207. } catch (error) {
  208. logger.error('withdraw_task=', result)
  209. }
  210. }
  211. await withdraw_db.update_withdraw_task(exec_obj.withdraw_id, update_obj)
  212. break
  213. }
  214. } catch (error) {
  215. var update_obj = {}
  216. update_obj.withdraw_status = 3
  217. update_obj.errorMsg = error.toString()
  218. await withdraw_db.update_withdraw_task(exec_obj.withdraw_id, update_obj)
  219. logger.error('withdraw_task error=', error.toString())
  220. break
  221. }
  222. }
  223. }
  224. logger.log("withdraw_task end")
  225. }
  226. /**
  227. * 队列版本
  228. * @param {*} ctx
  229. * @returns
  230. */
  231. async function withdrawV3(ctx) {
  232. logger.log('withdrawV3')
  233. if (ctx.request == null || ctx.request.body == null) {
  234. ctx.body = utils.toJson(-1, null, "request error. ");
  235. return
  236. }
  237. const obj = ctx.request.body;
  238. // for (let index = 0; index < 10; index++) {
  239. var log_obj = { ...obj }
  240. logger.log('withdrawV3', log_obj)
  241. var obj_ = decrypt_withdraw_content(log_obj.content)
  242. obj_.withdraw_id = obj_.withdrawId;
  243. // obj_.withdraw_id = utils.getTimestamp().toString();
  244. // var obj_ = log_obj
  245. if (obj_.withdraw_id) {
  246. var isExist = await withdraw_db.withdraw_id_exist(obj_.withdraw_id)
  247. if (isExist) {
  248. logger.error('withdraw_id_exist', obj_.withdraw_id + ' is already in the queue.')
  249. ctx.body = utils.toJson(-2, null, obj_.withdraw_id + ' is already in the queue.')
  250. return
  251. }
  252. redis.redis_push(reids_token_config.WITHDRAW_QUEUE_KEY, JSON.stringify(obj_))
  253. var info = await moralis.queryCompanyInfoFromId(0);
  254. obj_.user_address = info.user_address
  255. await withdraw_db.create_withdraw_task(obj_)
  256. // withdraw_task()
  257. ctx.body = utils.toJson(0, obj_.withdraw_id, null)
  258. } else {
  259. return utils.toJson(-2, null, ' withdraw_id not empty.')
  260. }
  261. // }
  262. }
  263. async function withdrawV3Test(ctx) {
  264. logger.log('withdrawV3Test')
  265. if (ctx.request == null || ctx.request.body == null) {
  266. ctx.body = utils.toJson(-1, null, "request error. ");
  267. return
  268. }
  269. const obj = ctx.request.body;
  270. // for (let index = 0; index < 10; index++) {
  271. var log_obj = { ...obj }
  272. logger.log('withdrawV3', log_obj)
  273. var obj_ = decrypt_withdraw_content(log_obj.content)
  274. obj_.withdraw_id = obj_.withdrawId;
  275. obj_.withdraw_id = utils.getTimestamp().toString();
  276. // var obj_ = log_obj
  277. if (obj_.withdraw_id) {
  278. var isExist = await withdraw_db.withdraw_id_exist(obj_.withdraw_id)
  279. if (isExist) {
  280. logger.error('withdraw_id_exist', obj_.withdraw_id + ' is already in the queue.')
  281. ctx.body = utils.toJson(-2, null, obj_.withdraw_id + ' is already in the queue.')
  282. return
  283. }
  284. redis.redis_push(reids_token_config.WITHDRAW_QUEUE_KEY, JSON.stringify(obj_))
  285. var info = await moralis.queryCompanyInfoFromId(0);
  286. obj_.user_address = info.user_address
  287. await withdraw_db.create_withdraw_task(obj_)
  288. ctx.body = utils.toJson(0, obj_.withdraw_id, null)
  289. } else {
  290. return utils.toJson(-2, null, ' withdraw_id not empty.')
  291. }
  292. // }
  293. }
  294. function decrypt_withdraw_content(content) {
  295. // const encryptText = utils.encrypt(log_obj);
  296. const encryptText = content;
  297. logger.log("加密", encryptText);
  298. let decryptObj = utils.decrypt(encryptText);
  299. try {
  300. logger.log("解密 before", decryptObj);
  301. decryptObj = JSON.parse(decryptObj);
  302. console.log("解密 json parse", decryptObj);
  303. } catch (error) {
  304. logger.error("json error:", error);
  305. decryptObj = null;
  306. }
  307. return decryptObj;
  308. }
  309. /**
  310. *
  311. * @param {鉴权版本} ctx
  312. */
  313. async function withdrawV2(ctx) {
  314. if (ctx.request == null || ctx.request.body == null) {
  315. ctx.body = utils.toJson(-1, null, "request error. ");
  316. return
  317. }
  318. const obj = ctx.request.body;
  319. var log_obj = { ...obj }
  320. // const encryptText = utils.encrypt(log_obj);
  321. const encryptText = log_obj.content;
  322. logger.log("加密", encryptText);
  323. let decryptObj = utils.decrypt(encryptText);
  324. try {
  325. logger.log("解密 before", decryptObj);
  326. decryptObj = JSON.parse(decryptObj);
  327. // console.log("解密 json parse", decryptObj);
  328. await withdraw_(decryptObj).then(result => {
  329. ctx.body = result;
  330. })
  331. } catch (error) {
  332. logger.error("json error:", error);
  333. ctx.body = utils.toJson(-1, null, error.toString());
  334. }
  335. }
  336. async function withdraw_(obj) {
  337. console.log("withdraw_", obj);
  338. var log_obj = { ...obj }
  339. var info = await moralis.queryCompanyInfoFromId(0);
  340. log_obj.company_address_total_balance_before = await moralis.queryCollectBalance(info.user_address, obj.chain)
  341. log_obj.company_public_key = info.user_address
  342. logger.log('withdraw log', log_obj);
  343. return new Promise((resolve) => {
  344. moralis.withdraw(obj).then((result) => {
  345. if (moralis.isTransferSucceed(result)) {
  346. //提币日志上报
  347. log_obj.results = result
  348. log_obj.type = report.REPORT_TYPE.withdraw
  349. //缓存当前交易的 gas 费用
  350. if (result && log_obj.contractAddress) {
  351. var tr = moralis.getTransferGasFree('token', result)
  352. log_obj.withdrawTotalGasFee = tr.totalGasFree.toString()
  353. } else {
  354. var tr = moralis.getTransferGasFree('native', result)
  355. log_obj.withdrawTotalGasFee = tr.totalGasFree.toString()
  356. }
  357. // log_obj.receiver_address_total_balance_after = await queryCollectBalance(info.user_address, utils.getChainName(obj.chain))
  358. //日志上报
  359. report.logReport(log_obj)
  360. }
  361. resolve(result)
  362. });
  363. })
  364. }
  365. //出金
  366. async function withdraw(ctx) {
  367. if (ctx.request == null || ctx.request.body == null) {
  368. ctx.body = utils.toJson(-1, null, "request error. ");
  369. return
  370. }
  371. const obj = ctx.request.body;
  372. await withdraw_(obj).then(result => {
  373. ctx.body = result;
  374. })
  375. }
  376. /**
  377. * 查询出金状态
  378. * @param {*} ctx
  379. */
  380. async function getWithdrawStatus(ctx) {
  381. if (ctx.request == null || ctx.request.body == null) {
  382. ctx.body = utils.toJson(-1, null, "request error. ");
  383. return
  384. }
  385. const obj = ctx.request.body;
  386. var info = await withdraw_db.queryWithdrawInfoFromWithdrawId(obj.withdrawId)
  387. logger.log('getWithdrawStatus info', JSON.stringify(info))
  388. if (info) {
  389. if (info.withdraw_status != 3) {
  390. ctx.body = utils.toJson(0, {
  391. withdrawId: info.withdraw_id,
  392. withdrawStatus: info.withdraw_status,
  393. withdrawHash: info.withdraw_hash,
  394. chainId: info.chain_id,
  395. transferTimestamp: info.update_time,
  396. }, null)
  397. } else {
  398. ctx.body = utils.toJson(0, {
  399. withdrawId: info.withdraw_id,
  400. withdrawStatus: info.withdraw_status,
  401. withdrawHash: info.withdraw_hash,
  402. chainId: info.chain_id,
  403. transferTimestamp: info.update_time,
  404. errorMsg: info.errorMsg
  405. }, null)
  406. }
  407. } else {
  408. ctx.body = utils.toJson(-1, null, obj.withdraw_id + ' id does not exist.')
  409. }
  410. }
  411. //获取交易记录
  412. router.post('/getTransfers', getTransfers)
  413. // 获取所有代币价格
  414. router.post('/getAllTotkenPrice', getAllTotkenPrice)
  415. // router.post('/transfer', transfer)
  416. //提现
  417. router.post('/withdraw', withdraw);
  418. //提现鉴权-body 加密
  419. router.post('/withdrawV2', withdrawV2);
  420. //队列的形式
  421. router.post('/withdrawV3', withdrawV3);
  422. if (process.env.NODE_ENV == 'dev')
  423. router.post('/withdrawV3Test', withdrawV3Test);
  424. //查询出金服务
  425. router.post('/getWithdrawStatus', getWithdrawStatus);
  426. //获取所有地址的所要消耗的最低提取费
  427. router.post('/getAllTokenWithdrawInfoLists', getAllTokenWithdrawInfoLists)
  428. // 定时任务 提币+归集
  429. withdraw_task();
  430. collect_conis_task();
  431. module.exports = router