sdk.js 29 KB

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