sdk.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. const czz = require('../model/czz')
  12. /**
  13. * 获取代币价格
  14. * @param {*} ctx
  15. */
  16. async function getAllTotkenPrice(ctx) {
  17. console.log('getTotkenPrice in:')
  18. var ret = await moralis.getAllTotkenPrice(ctx.request.body)
  19. console.log('getTotkenPrice result:', ret)
  20. if (ret)
  21. ctx.body = utils.toJson(0, ret, null);
  22. else ctx.body = utils.toJson(-1, null, "redis read error.");
  23. }
  24. async function getAllTokenPrice(ctx) {
  25. var ret = await moralis.getAllTotkenPrice(ctx.request.body)
  26. console.log('getTotkenPrice result:', ret)
  27. if (ret)
  28. ctx.body = utils.toJson(0, ret, null);
  29. else ctx.body = utils.toJson(-1, null, "redis read error.");
  30. }
  31. /**
  32. * 获取交易记录
  33. * @param {*} ctx
  34. */
  35. async function getTransfers(ctx) {
  36. const obj = ctx.request.body;
  37. console.log("getTransfers body", obj);
  38. if (!obj.chain)//默认 bsc 币安链
  39. obj.chain = 'bsc_mainnet'
  40. var temp_obj = { ...obj }
  41. var index = 0
  42. await moralis.getTokenTransfers(obj).then((result) => {
  43. logger.log('getTransfers response', 'index=' + index, result)
  44. ctx.body = result;
  45. if (result) {
  46. //提交归集任务 native 能获取到 gas 、token 无法获取到 gas 费
  47. try {
  48. if (temp_obj.address && moralis.isTransferSucceed(result)) {
  49. var log_obj = { ...obj }
  50. log_obj.results = result
  51. log_obj.type = report.REPORT_TYPE.transfer_record
  52. //埋点日志上报-入金检查
  53. report.logReport(log_obj)
  54. var json_obj = JSON.parse(result);
  55. //缓存当前交易的 gas 费用
  56. var tr = moralis.getTransferRecordGasFree('native', json_obj, temp_obj.address)
  57. logger.log('getTransferRecordGasFree:', tr, temp_obj.address)
  58. if (tr && tr.totalGasFree > 0) {
  59. logger.log('getTransferRecordGasFree redis_set LAST_PRICE:', tr)
  60. // redis.redis_set(reids_token_config.LAST_BNB_PRICE, tr.gas_price.toString());
  61. // redis.redis_set(reids_token_config.LAST_TOKEN_PRICE, tr.gas_price.toString());
  62. redis.writeAppendRedis(reids_token_config.LAST_BNB_PRICE, temp_obj.chain, '', tr.gas_price.toString());
  63. redis.writeAppendRedis(reids_token_config.LAST_TOKEN_PRICE, temp_obj.chain, '', tr.gas_price.toString());
  64. }
  65. if (json_obj.data.total > 0) {
  66. //提交归集任务
  67. if (temp_obj.address) {
  68. logger.log('pushCollectConisObj>>>', temp_obj.address)
  69. redis.redis_push(reids_token_config.COLLECT_CONIS_QUEUE_KEY, JSON.stringify(temp_obj))
  70. }
  71. }
  72. if (json_obj.data.total > 0) {
  73. //提交归集任务
  74. if (temp_obj.address) {
  75. logger.log('pushCollectConisObj>>>', temp_obj.address)
  76. redis.redis_push(reids_token_config.COLLECT_CONIS_QUEUE_KEY, JSON.stringify(temp_obj))
  77. }
  78. }
  79. }
  80. } catch (error) {
  81. console.error('pushCollectConisObj error=', error)
  82. }
  83. }
  84. })
  85. }
  86. /**
  87. * 获取交易记录
  88. * @param {*} ctx
  89. */
  90. async function getTransfersV2(ctx) {
  91. const obj = ctx.request.body;
  92. console.log("getTransfers body", obj);
  93. if (!obj.chain)//默认 bsc 币安链
  94. obj.chain = 'bsc_mainnet'
  95. var temp_obj = { ...obj }
  96. var index = 0
  97. await moralis.getTokenTransfersV2(obj).then((result) => {
  98. logger.log('getTokenTransfersV2 response', 'index=' + index, result)
  99. ctx.body = result;
  100. if (result) {
  101. //提交归集任务 native 能获取到 gas 、token 无法获取到 gas 费
  102. try {
  103. if (temp_obj.address && moralis.isTransferSucceed(result)) {
  104. var log_obj = { ...obj }
  105. log_obj.results = result
  106. log_obj.type = report.REPORT_TYPE.transfer_record
  107. //埋点日志上报-入金检查
  108. report.logReport(log_obj)
  109. var json_obj = JSON.parse(result);
  110. //缓存当前交易的 gas 费用
  111. var tr = moralis.getTransferRecordGasFree('native', json_obj, temp_obj.address)
  112. logger.log('getTransferRecordGasFree:', tr, temp_obj.address)
  113. if (tr && tr.totalGasFree > 0) {
  114. logger.log('getTransferRecordGasFree redis_set LAST_TOTAL_BNB_FREE:', tr)
  115. // redis.redis_set(reids_token_config.LAST_BNB_PRICE, tr.gas_price.toString());
  116. // redis.redis_set(reids_token_config.LAST_TOKEN_PRICE, tr.gas_price.toString().toString());
  117. redis.writeAppendRedis(reids_token_config.LAST_BNB_PRICE, temp_obj.chain, '', tr.gas_price.toString());
  118. redis.writeAppendRedis(reids_token_config.LAST_TOKEN_PRICE, temp_obj.chain, '', tr.gas_price.toString());
  119. }
  120. if (json_obj.data.total > 0) {
  121. //提交归集任务
  122. if (temp_obj.address) {
  123. logger.log('pushCollectConisObj>>>', temp_obj.address)
  124. redis.redis_push(reids_token_config.COLLECT_CONIS_QUEUE_KEY, JSON.stringify(temp_obj))
  125. }
  126. }
  127. }
  128. } catch (error) {
  129. console.error('pushCollectConisObj error=', error)
  130. }
  131. }
  132. })
  133. }
  134. async function getAllTokenWithdrawInfoLists(ctx) {
  135. if (ctx.request == null || ctx.request.body == null) {
  136. ctx.body = utils.toJson(-1, null, "request error. ");
  137. return
  138. }
  139. ctx.body = await moralis.getAllTokenWithdrawInfoLists(ctx);
  140. }
  141. async function check_czz_withdraw_task() {
  142. while (true) {
  143. var exec_obj;
  144. try {
  145. exec_obj = await redis.redis_pop(reids_token_config.CHECK_CZZ_WITHDRAW_STATUS_QUEUE)
  146. logger.log("check_czz_withdraw_task redis_pop", exec_obj)
  147. if (!exec_obj) {
  148. logger.log("没有 czz hash check")
  149. await utils.sleep(2 * 60 * 1000)
  150. continue
  151. }
  152. exec_obj = JSON.parse(exec_obj)
  153. if (utils.getTimestamp() - exec_obj.create_time > exec_obj.lifecycle) {
  154. logger.error('已过期 check_czz_withdraw_task :', JSON.stringify(exec_obj))
  155. var update_obj = {}
  156. update_obj.withdraw_status = 3
  157. update_obj.withdraw_hash = ''
  158. update_obj.nonce = -1
  159. update_obj.gas_price = ''
  160. update_obj.gas_limit = ''
  161. update_obj.value = '0'
  162. update_obj.errorMsg = 'czz timeout'
  163. await withdraw_db.update_withdraw_task(exec_obj.withdraw_id, update_obj)
  164. continue
  165. }
  166. var obj = await czz.check_withdraw_status(exec_obj)
  167. if (obj.code == 0) {
  168. var nonce = obj.data.nonce
  169. var curGasPrice = obj.data.gasPrice.toString()
  170. var curGasLimit = obj.data.gasLimit.toString()
  171. var hash = obj.data.hash
  172. var update_obj = {}
  173. update_obj.withdraw_status = 2
  174. update_obj.withdraw_hash = hash
  175. update_obj.nonce = nonce
  176. update_obj.gas_price = curGasPrice.toString()
  177. update_obj.gas_limit = curGasLimit.toString()
  178. update_obj.value = utils.scientificNotationToString(obj.data.value).toString()
  179. update_obj.errorMsg = ''
  180. await withdraw_db.update_withdraw_task(exec_obj.withdraw_id, update_obj)
  181. } else {
  182. redis.redis_push(reids_token_config.CHECK_CZZ_WITHDRAW_STATUS_QUEUE, JSON.stringify(exec_obj))
  183. }
  184. await utils.sleep(2 * 60 * 1000)
  185. } catch (error) {
  186. logger.error('check_czz_withdraw_task error', error.toString(), JSON.stringify(exec_obj))
  187. }
  188. }
  189. }
  190. async function collect_conis_task() {
  191. logger.log("collect_conis_task start")
  192. while (true) {
  193. var start_time = utils.getTimestamp()
  194. var exec_obj = await redis.redis_pop(reids_token_config.COLLECT_CONIS_QUEUE_KEY)
  195. if (!exec_obj) {
  196. await utils.sleep(30000)
  197. logger.log("没有归集任务")
  198. continue
  199. }
  200. try {
  201. exec_obj = JSON.parse(exec_obj)
  202. logger.log('collect_conis_task exec item>>>>', exec_obj);
  203. //开始收集用户地址里面的币到归集地址
  204. var ret = await moralis.collectCoins(exec_obj)
  205. logger.log('collect_conis_task ret =', exec_obj, ret)
  206. try {
  207. var ret_obj = JSON.parse(ret)
  208. if (ret_obj.code == 0) {
  209. logger.log('触发归集 delay collect_conis_task ret =', exec_obj, ret)
  210. //间隔 10s 归集,避免提交任务过多
  211. await utils.sleep(10000)
  212. }
  213. } catch (error) { }
  214. } catch (error) {
  215. logger.error('collect_conis_task error', error.toString());
  216. }
  217. logger.log("collect_conis_task cost-time", utils.getTimestamp() - start_time, exec_obj)
  218. }
  219. }
  220. async function withdraw_task() {
  221. logger.log("withdraw_task start")
  222. let last_time = 0
  223. let last_hash = ''
  224. let last_chain = ''
  225. while (true) {
  226. var exec_obj = await redis.redis_pop(reids_token_config.WITHDRAW_QUEUE_KEY)
  227. if (!exec_obj) {
  228. await utils.sleep(10000)
  229. logger.log("没有出金任务")
  230. continue
  231. }
  232. try {
  233. exec_obj = JSON.parse(exec_obj)
  234. } catch (error) {
  235. logger.error('item parse error', error);
  236. break
  237. }
  238. var temp_obj = { ...exec_obj }
  239. if (utils.getTimestamp() - last_time < 60000) {
  240. //有可能上一个区块还未更新,这里做一个尝试限制
  241. //Error: Failed to make "eth_sendRawTransaction" request with networkConnector: "already known"
  242. //通过 交易 hash 获取块。last_hash
  243. if (last_hash && last_chain) {
  244. var options = {
  245. transaction_hash: last_hash,
  246. chain: last_chain,
  247. endTime: '2099-01-01'
  248. }
  249. var tryCount = 3;
  250. do {
  251. try {
  252. //通过获取上一个交易记录来进行确认
  253. var transaction = await moralis.getTokenTransfersV2(options);
  254. logger.log('withdraw_task exectransaction', transaction, options, tryCount);
  255. if (typeof transaction === 'string')
  256. transaction = JSON.parse(transaction)
  257. if (transaction.code == 0) {
  258. if (transaction.data.results.length <= 0) {
  259. logger.log('等待10s');
  260. await utils.sleep(10000)
  261. } else {
  262. logger.log('等待5s');
  263. await utils.sleep(5000)
  264. break
  265. }
  266. } else {
  267. break
  268. }
  269. tryCount -= 1
  270. } catch (error) {
  271. logger.error('withdraw_task exectransaction err', error.toString());
  272. }
  273. if (tryCount < 0) {
  274. logger.error('withdraw_task getTokenTransfersV2 警告交易未更新:', JSON.stringify(options));
  275. }
  276. } while (tryCount >= 0);
  277. }
  278. }
  279. //如果失败重试一次
  280. var tryCount = 1;
  281. for (let index = 0; index < 1 + tryCount; index++) {
  282. var result;
  283. try {
  284. result = await withdraw_({ ...temp_obj })
  285. last_time = utils.getTimestamp()
  286. logger.log('withdraw_task withdraw_ =', result, last_time)
  287. if (result && moralis.getTransferCode(result) == 0) {
  288. var obj;
  289. var curGasPrice = 0;
  290. var curGasLimit = 0;
  291. if (typeof result === 'string') {
  292. obj = JSON.parse(result)
  293. }
  294. var nonce = obj.data.nonce
  295. try {
  296. curGasPrice = BigNumber(obj.data.gasPrice.hex).toNumber()
  297. curGasLimit = BigNumber(obj.data.gasLimit.hex).toNumber()
  298. } catch (error) {
  299. logger.error('BigNumber toNumber error')
  300. }
  301. var value = 0
  302. try {
  303. if (obj.data.chainId == 97 || obj.data.chainId == 56) {
  304. value = BigNumber(obj.data.value.hex).toNumber()
  305. } else {
  306. value = obj.data.value.number
  307. }
  308. } catch (error) {
  309. logger.error('BigNumber value toNumber error')
  310. }
  311. var hash = obj.data.hash
  312. last_hash = hash
  313. last_chain = temp_obj.chain
  314. var update_obj = {}
  315. update_obj.withdraw_status = 2
  316. update_obj.withdraw_hash = hash
  317. update_obj.nonce = nonce
  318. update_obj.gas_price = curGasPrice.toString()
  319. update_obj.gas_limit = curGasLimit.toString()
  320. try {
  321. update_obj.value = utils.scientificNotationToString(value).toString()
  322. } catch (error) {
  323. logger.error('scientificNotationToString error')
  324. }
  325. update_obj.errorMsg = ''
  326. await withdraw_db.update_withdraw_task(exec_obj.withdraw_id, update_obj)
  327. break
  328. } else if (result && moralis.getTransferCode(result) == 1) {
  329. moralis.pushChainDetailTOQueue(1, exec_obj, result);
  330. break
  331. } else {
  332. logger.error('withdraw_task withdraw_ error=', result, JSON.stringify(temp_obj))
  333. if (index < 1 + tryCount && result.includes('eth_sendRawTransaction')) {
  334. logger.error('try withdraw_:', JSON.stringify(temp_obj), index)
  335. await utils.sleep(3000)
  336. continue
  337. }
  338. var update_obj = {}
  339. update_obj.withdraw_status = 3
  340. if (typeof result === 'string') {
  341. try {
  342. result = JSON.parse(result)
  343. update_obj.errorMsg = result.errMsg
  344. } catch (error) {
  345. logger.error('withdraw_task=', result)
  346. }
  347. }
  348. await withdraw_db.update_withdraw_task(exec_obj.withdraw_id, update_obj)
  349. break
  350. }
  351. } catch (error) {
  352. var update_obj = {}
  353. update_obj.withdraw_status = 3
  354. update_obj.errorMsg = error.toString()
  355. await withdraw_db.update_withdraw_task(exec_obj.withdraw_id, update_obj)
  356. if (result)
  357. logger.error('withdraw_task error=', error.toString(), JSON.stringify(temp_obj), JSON.stringify(result))
  358. else {
  359. logger.error('withdraw_task error=', error.toString(), JSON.stringify(temp_obj))
  360. }
  361. break
  362. }
  363. }
  364. }
  365. logger.log("withdraw_task end")
  366. }
  367. /**
  368. * 队列版本
  369. * @param {*} ctx
  370. * @returns
  371. */
  372. async function withdrawV3(ctx) {
  373. logger.log('withdrawV3')
  374. if (ctx.request == null || ctx.request.body == null) {
  375. ctx.body = utils.toJson(-1, null, "request error. ");
  376. return
  377. }
  378. const obj = ctx.request.body;
  379. // for (let index = 0; index < 10; index++) {
  380. var log_obj = { ...obj }
  381. logger.log('withdrawV3', log_obj)
  382. var obj_ = decrypt_withdraw_content(log_obj.content)
  383. obj_.withdraw_id = obj_.withdrawId;
  384. // obj_.withdraw_id = utils.getTimestamp().toString();
  385. // var obj_ = log_obj
  386. if (obj_.withdraw_id) {
  387. var isExist = await withdraw_db.withdraw_id_exist(obj_.withdraw_id)
  388. if (isExist) {
  389. logger.error('withdraw_id_exist', obj_.withdraw_id + ' is already in the queue.')
  390. ctx.body = utils.toJson(-2, null, obj_.withdraw_id + ' is already in the queue.')
  391. return
  392. }
  393. var info = await moralis.queryCompanyInfoFromId(0);
  394. obj_.user_address = info.user_address
  395. await withdraw_db.create_withdraw_task(obj_)
  396. redis.redis_push(reids_token_config.WITHDRAW_QUEUE_KEY, JSON.stringify(obj_))
  397. // withdraw_task()
  398. ctx.body = utils.toJson(0, obj_.withdraw_id, null)
  399. } else {
  400. return utils.toJson(-2, null, ' withdraw_id not empty.')
  401. }
  402. // }
  403. }
  404. async function withdrawV3Test(ctx) {
  405. logger.log('withdrawV3Test')
  406. if (ctx.request == null || ctx.request.body == null) {
  407. ctx.body = utils.toJson(-1, null, "request error. ");
  408. return
  409. }
  410. const obj = ctx.request.body;
  411. // for (let index = 0; index < 10; index++) {
  412. var log_obj = { ...obj }
  413. logger.log('withdrawV3', log_obj)
  414. var obj_ = decrypt_withdraw_content(log_obj.content)
  415. obj_.withdraw_id = utils.getCurrentDateFormat('YYYY-MM-DD-HH:mm:ss:SSS').toString()
  416. // var obj_ = log_obj
  417. if (obj_.withdraw_id) {
  418. var isExist = await withdraw_db.withdraw_id_exist(obj_.withdraw_id)
  419. if (isExist) {
  420. logger.error('withdraw_id_exist', obj_.withdraw_id + ' is already in the queue.')
  421. ctx.body = utils.toJson(-2, null, obj_.withdraw_id + ' is already in the queue.')
  422. return
  423. }
  424. redis.redis_push(reids_token_config.WITHDRAW_QUEUE_KEY, JSON.stringify(obj_))
  425. var info = await moralis.queryCompanyInfoFromId(0);
  426. obj_.user_address = info.user_address
  427. await withdraw_db.create_withdraw_task(obj_)
  428. ctx.body = utils.toJson(0, obj_.withdraw_id, null)
  429. } else {
  430. return utils.toJson(-2, null, ' withdraw_id not empty.')
  431. }
  432. // }
  433. }
  434. function decrypt_withdraw_content(content) {
  435. // const encryptText = utils.encrypt(log_obj);
  436. const encryptText = content;
  437. logger.log("加密", encryptText);
  438. let decryptObj = utils.decrypt(encryptText);
  439. try {
  440. logger.log("解密 before", decryptObj);
  441. decryptObj = JSON.parse(decryptObj);
  442. console.log("解密 json parse", decryptObj);
  443. } catch (error) {
  444. logger.error("json error:", error);
  445. decryptObj = null;
  446. }
  447. return decryptObj;
  448. }
  449. /**
  450. *
  451. * @param {鉴权版本} ctx
  452. */
  453. async function withdrawV2(ctx) {
  454. if (ctx.request == null || ctx.request.body == null) {
  455. ctx.body = utils.toJson(-1, null, "request error. ");
  456. return
  457. }
  458. const obj = ctx.request.body;
  459. var log_obj = { ...obj }
  460. // const encryptText = utils.encrypt(log_obj);
  461. const encryptText = log_obj.content;
  462. logger.log("加密", encryptText);
  463. let decryptObj = utils.decrypt(encryptText);
  464. try {
  465. logger.log("解密 before", decryptObj);
  466. decryptObj = JSON.parse(decryptObj);
  467. // console.log("解密 json parse", decryptObj);
  468. await withdraw_(decryptObj).then(result => {
  469. ctx.body = result;
  470. })
  471. } catch (error) {
  472. logger.error("json error:", error);
  473. ctx.body = utils.toJson(-1, null, error.toString());
  474. }
  475. }
  476. async function withdraw_(obj) {
  477. console.log("withdraw_", obj);
  478. var log_obj = { ...obj }
  479. var info = await moralis.queryCompanyInfoFromId(0);
  480. // log_obj.company_address_total_balance_before = await moralis.queryCollectBalance(info.user_address, obj.chain)
  481. log_obj.company_public_key = info.user_address
  482. logger.log('withdraw log', log_obj);
  483. return new Promise((resolve) => {
  484. moralis.withdraw(obj).then((result) => {
  485. if (moralis.getTransferCode(result) == 0) {
  486. //提币日志上报
  487. log_obj.results = result
  488. log_obj.type = report.REPORT_TYPE.withdraw
  489. //缓存当前交易的 gas 费用
  490. if (result && log_obj.contractAddress) {
  491. var tr = moralis.getTransferGasFree('token', result)
  492. log_obj.withdrawTotalGasFee = tr.totalGasFree.toString()
  493. } else {
  494. var tr = moralis.getTransferGasFree('native', result)
  495. log_obj.withdrawTotalGasFee = tr.totalGasFree.toString()
  496. }
  497. //日志上报
  498. report.logReport(log_obj)
  499. }
  500. resolve(result)
  501. });
  502. })
  503. }
  504. //出金
  505. async function withdraw(ctx) {
  506. if (ctx.request == null || ctx.request.body == null) {
  507. ctx.body = utils.toJson(-1, null, "request error. ");
  508. return
  509. }
  510. const obj = ctx.request.body;
  511. await withdraw_(obj).then(result => {
  512. ctx.body = result;
  513. })
  514. }
  515. /**
  516. * 查询出金状态
  517. * @param {*} ctx
  518. */
  519. async function getWithdrawStatus(ctx) {
  520. if (ctx.request == null || ctx.request.body == null) {
  521. ctx.body = utils.toJson(-1, null, "request error. ");
  522. return
  523. }
  524. const obj = ctx.request.body;
  525. var info = await withdraw_db.queryWithdrawInfoFromWithdrawId(obj.withdrawId)
  526. logger.log('getWithdrawStatus info', JSON.stringify(info))
  527. if (info) {
  528. if (info.withdraw_status != 3) {
  529. ctx.body = utils.toJson(0, {
  530. withdrawId: info.withdraw_id,
  531. withdrawStatus: info.withdraw_status,
  532. withdrawHash: info.withdraw_hash,
  533. chainId: info.chain_id,
  534. transferTimestamp: info.update_time,
  535. }, null)
  536. } else {
  537. ctx.body = utils.toJson(0, {
  538. withdrawId: info.withdraw_id,
  539. withdrawStatus: info.withdraw_status,
  540. withdrawHash: info.withdraw_hash,
  541. chainId: info.chain_id,
  542. transferTimestamp: info.update_time,
  543. errorMsg: info.errorMsg
  544. }, null)
  545. }
  546. } else {
  547. ctx.body = utils.toJson(-1, null, obj.withdraw_id + ' id does not exist.')
  548. }
  549. }
  550. async function timer_collect_conis_bsc_task() {
  551. var index = 0
  552. var delay = 60 * 1000 * 60
  553. while (1) {
  554. var temp_obj = {
  555. "chain": "bsc_testnet",
  556. "address": "0x3B525c35DdC323B08241493f148340D89e3A73a7"
  557. }
  558. redis.redis_push(reids_token_config.COLLECT_CONIS_QUEUE_KEY, JSON.stringify(temp_obj))
  559. await utils.sleep(delay)
  560. index += 1
  561. }
  562. }
  563. async function timer_collect_conis_czz_task() {
  564. var index = 0
  565. var delay = 60 * 1000 * 60
  566. while (1) {
  567. var temp_obj = {
  568. "chain": "czz",
  569. "address": "0x39ACD9CC975D792D8160215Dc84fa00E4934F076",
  570. }
  571. redis.redis_push(reids_token_config.COLLECT_CONIS_QUEUE_KEY, JSON.stringify(temp_obj))
  572. await utils.sleep(delay)
  573. index += 1
  574. }
  575. }
  576. async function timer_transfer_bsc_task() {
  577. var index = 0
  578. var delay = 60 * 1000 * 60
  579. while (1) {
  580. var obj_ = {
  581. "type": "erc20",
  582. "contractAddress": "0xFF94950Ee8A79c52cC4B0Aa5178C8cEa48A3F3A6",
  583. "amount": "123000000000000000000",
  584. "chain": "bsc_testnet",
  585. "receiver": "0x3B525c35DdC323B08241493f148340D89e3A73a7",
  586. "withdrawId": index.toString()
  587. }
  588. obj_.withdraw_id = utils.getCurrentDateFormat('YYYY-MM-DD-HH:mm:ss:SSS').toString()
  589. var info = await moralis.queryCompanyInfoFromId(0);
  590. obj_.user_address = info.user_address
  591. await withdraw_db.create_withdraw_task(obj_)
  592. redis.redis_push(reids_token_config.WITHDRAW_QUEUE_KEY, JSON.stringify(obj_))
  593. await utils.sleep(delay)
  594. index += 1
  595. }
  596. }
  597. async function timer_transfer_czz_task() {
  598. var index = 0
  599. var delay = 60 * 1000 * 60
  600. while (1) {
  601. var obj_ = {
  602. "type": "erc20",
  603. "contractAddress": "0xfb16179d5e84b0e3e7524ed61a9cf7b98d039b20",
  604. "amount": "100000000000000000000",
  605. "chain": "czz",
  606. "receiver": "0x39ACD9CC975D792D8160215Dc84fa00E4934F076"
  607. }
  608. obj_.withdraw_id = utils.getCurrentDateFormat('YYYY-MM-DD-HH:mm:ss:SSS').toString()
  609. var info = await moralis.queryCompanyInfoFromId(0);
  610. obj_.user_address = info.user_address
  611. await withdraw_db.create_withdraw_task(obj_)
  612. redis.redis_push(reids_token_config.WITHDRAW_QUEUE_KEY, JSON.stringify(obj_))
  613. await utils.sleep(delay)
  614. index += 1
  615. }
  616. }
  617. //获取交易记录
  618. router.post('/getTransfers', getTransfers)
  619. router.post('/getTransfersV2', getTransfersV2)
  620. // 获取所有代币价格
  621. router.post('/getAllTotkenPrice', getAllTotkenPrice)
  622. router.post('/getAllTokenPrice', getAllTokenPrice)
  623. // router.post('/transfer', transfer)
  624. //提现
  625. router.post('/withdraw', withdraw);
  626. //提现鉴权-body 加密
  627. router.post('/withdrawV2', withdrawV2);
  628. //队列的形式
  629. router.post('/withdrawV3', withdrawV3);
  630. if (process.env.NODE_ENV == 'dev' || process.env.NODE_ENV == 'test') {
  631. router.post('/withdrawV3Test', withdrawV3Test);
  632. }
  633. //查询出金服务
  634. router.post('/getWithdrawStatus', getWithdrawStatus);
  635. //获取所有地址的所要消耗的最低提取费
  636. router.post('/getAllTokenWithdrawInfoLists', getAllTokenWithdrawInfoLists)
  637. // 定时任务 提币+归集
  638. withdraw_task();
  639. collect_conis_task();
  640. //czz 504 检查
  641. check_czz_withdraw_task();
  642. if (process.env.NODE_ENV == 'dev' || process.env.NODE_ENV == 'test') {
  643. // timer_transfer_bsc_task()
  644. // timer_transfer_czz_task()
  645. // timer_collect_conis_bsc_task()
  646. // timer_collect_conis_czz_task()
  647. }
  648. module.exports = router