Browse Source

增加czz出金兼容

DevYK 2 năm trước cách đây
mục cha
commit
bbf7d4f356
7 tập tin đã thay đổi với 169 bổ sung24 xóa
  1. 1 1
      config/dev_config.js
  2. 1 0
      config/prd_config.js
  3. 1 0
      config/test_config.js
  4. 28 2
      model/czz.js
  5. 2 2
      model/db/withdraw_db.js
  6. 71 11
      model/moralis_sdk.js
  7. 65 8
      routes/sdk.js

+ 1 - 1
config/dev_config.js

@@ -32,7 +32,7 @@ const reids_token_config = {
     COLLECT_CONIS_QUEUE_KEY:'COLLECT_CONIS_QUEUE_KEY_DEV',
     LAST_COLLECT_PUBLIC_KEY:'LAST_COLLECT_PUBLIC_LEY',
     LAST_COLLECT_TIME:'LAST_COLLECT_TIME',
-
+    CHECK_CZZ_WITHDRAW_STATUS_QUEUE:'CHECK_CZZ_WITHDRAW_STATUS_QUEUE',
 }
 
 // http 埋点上报配置

+ 1 - 0
config/prd_config.js

@@ -30,6 +30,7 @@ const reids_token_config = {
     COLLECT_CONIS_QUEUE_KEY:'COLLECT_CONIS_QUEUE_KEY',
     LAST_COLLECT_PUBLIC_KEY:'LAST_COLLECT_PUBLIC_LEY',
     LAST_COLLECT_TIME:'LAST_COLLECT_TIME',
+    CHECK_CZZ_WITHDRAW_STATUS_QUEUE:'CHECK_CZZ_WITHDRAW_STATUS_QUEUE',
 }
 
 // http 埋点上报配置

+ 1 - 0
config/test_config.js

@@ -31,6 +31,7 @@ const reids_token_config = {
     COLLECT_CONIS_QUEUE_KEY: 'COLLECT_CONIS_QUEUE_KEY',
     LAST_COLLECT_PUBLIC_KEY: 'LAST_COLLECT_PUBLIC_LEY',
     LAST_COLLECT_TIME: 'LAST_COLLECT_TIME',
+    CHECK_CZZ_WITHDRAW_STATUS_QUEUE:'CHECK_CZZ_WITHDRAW_STATUS_QUEUE',
 }
 
 

+ 28 - 2
model/czz.js

@@ -7,13 +7,14 @@ const logger = require('./logger.js');
 
 var url = account_config.CZZ_BASEURL
 
+
 const withdraw = async (params) => {
     params.privateKey = ''
     params.toAddress = params.receiver
     logger.info('withdraw_czz', process.env.NODE_ENV, url, ' params ', params)
     var data = { ...params }
     return new Promise(resolve => {
-        axios.post(url + '/withdraw', data, { timeout: 2 * 60 * 1000 })
+        axios.post(url + '/withdraw', data, { timeout: 5 * 60 * 1000 })
             .then(res => {
                 console.log('res=>', res.status, res.data);
                 resolve(res.data)
@@ -24,6 +25,31 @@ const withdraw = async (params) => {
     })
 }
 
+
+const check_withdraw_status = async (params) => {
+    logger.info('txn_status_czz', process.env.NODE_ENV, url, ' params ', params)
+    var pars = {
+        txn_hash: params.hash,
+        chain: "czz"
+    }
+    var data = pars
+    return new Promise(resolve => {
+        axios.post(url + '/txn_status', data, { timeout: 1 * 60 * 1000 })
+            .then(res => {
+                console.log('res=>', res.status, res.data);
+                resolve(res.data)
+            }).catch(err => {
+                logger.error('http_request_post check_withdraw_status error ', JSON.stringify(err));
+                resolve(JSON.stringify(err))
+            });
+    })
+}
+
+// check_withdraw_status({
+//     hash: "0x4561096623a1101b28e6b321f5bd4633dbed992aa9fc7b6d1776ef09581f045f",
+// })
+
 module.exports = {
-    withdraw
+    withdraw,
+    check_withdraw_status
 }

+ 2 - 2
model/db/withdraw_db.js

@@ -42,7 +42,7 @@ async function create_withdraw_task(task_obj) {
     }
     var create_withdraw_sql = 'INSERT INTO withdraw_manage (withdraw_id,withdraw_status,create_time,from_address,to_address,chain_id,type,contract_address,amount)' +
         'VALUES(?,?,?,?,?,?,?,?,?)'
-    var create_withdraw_params = [withdraw_id, 0, withdraw_create_time, from_address, to_address, chain_id, type, contract_address ? contract_address : '', amount]
+    var create_withdraw_params = [withdraw_id, 1, withdraw_create_time, from_address, to_address, chain_id, type, contract_address ? contract_address : '', amount]
 
     logger.log('create_withdraw_task create_withdraw_sql', create_withdraw_sql);
     return new Promise((resolve) => {
@@ -90,7 +90,7 @@ function update_withdraw_task(withdraw_id, ret_obj) {
         }
     }
     var update_withdraw_sql = 'update  withdraw_manage SET withdraw_status=?,withdraw_hash=?,nonce=?,update_time=?,gas_price=?,gas_limit=?,value=?,errorMsg=? WHERE withdraw_id=?'
-
+    
     var status = ret_obj.withdraw_status
     var hash = ret_obj.withdraw_hash
     var nonce = ret_obj.nonce

+ 71 - 11
model/moralis_sdk.js

@@ -37,9 +37,6 @@ const SHIB_AMOUNT_LIMIT = 20000
 const SHIB_AMOUNT_FEE_LIMIT = 10000
 const SHIB_ADDRESS = '0x2859e4544C4bB03966803b044A93563Bd2D0DD4D'
 
-
-
-
 /**
  * 初始化 moralis
  * https://st94nif1cq.feishu.cn/docs/doccnNxG2UwHPCdZXbywgbdy13f#
@@ -207,7 +204,18 @@ function isTransferSucceed(json) {
         return obj.code == 0
     }
     return false
+}
 
+/**
+ * 
+ * @param {转账是否成功} json 
+ */
+function getTransferCode(json) {
+    if (json) {
+        var obj = JSON.parse(json)
+        return obj.code
+    }
+    return -1
 }
 
 const withdraw = async (obj) => {
@@ -540,10 +548,9 @@ async function transfers(obj, my_account_all_coins, logParams) {
             if (ti.type == 'native') {
                 await updateNativeBalance(ti.amount, ti);
             }
-
             var ret = await start_collectCoins(ti)
             //更新 native 金额
-            if (isTransferSucceed(ret)) {
+            if (getTransferCode(ret) == 0) {
                 logger.tlog('start_collectCoins  respose...', ret);
                 t_i += 1
                 collects.push(ret);
@@ -558,6 +565,10 @@ async function transfers(obj, my_account_all_coins, logParams) {
                     totalGasFee += parseInt(tr.totalGasFree);
                 }
                 collects_mysql.push(changeJsonHexBignumberToString(ret))
+            } else if (getTransferCode(ret) == 1) {
+                t_i += 1
+                //todo 没有想到好办法如何整合在一起
+                // pushChainDetailTOQueue(0,)
             } else {
                 //缓存当前交易的 gas 费用
                 try {
@@ -637,7 +648,7 @@ const transfer_handle = async (obj) => {
         if (obj.privateKeyId)
             id = obj.privateKeyId
 
-        //读取用户充币地址对应的私钥
+        //读取归集对应的私钥
         var info = await queryCompanyInfoFromId(id);
         logger.log('transfer_handle queryCompanyInfoFromId=', info);
         //提币公司
@@ -677,7 +688,7 @@ const transfer_handle = async (obj) => {
     }
 
     var ret = await transfer(obj);
-    if (isTransferSucceed(ret)) {
+    if (getTransferCode(ret) == 0) {
         //缓存当前交易的 gas 费用
         if (ret && obj.contractAddress) {
             var tr = getTransferGasFree('token', ret)
@@ -690,6 +701,8 @@ const transfer_handle = async (obj) => {
             // redis.redis_set(reids_token_config.LAST_BNB_PRICE, tr.gasFee);
             redis.writeAppendRedis(reids_token_config.LAST_BNB_PRICE, obj.chain, '', tr.gasFee);
         }
+    } else if (getTransferCode(ret) == 1) {
+
     } else {
         var tm_obj = { ...obj }
         tm_obj.privateKey = '不对外暴露'
@@ -699,6 +712,9 @@ const transfer_handle = async (obj) => {
 }
 
 
+
+
+
 async function queryCollectBalance(address, chain) {
     // var t_chain = utils.getChainName(chain)
 
@@ -802,7 +818,9 @@ const collectCoins = async (obj) => {
             var ret = await withdraw(obj_wd)
             logger.log('充值完成 gas ', ret)
 
-            if (!isTransferSucceed(ret)) return ret;
+            if (getTransferCode(ret) != 0) return ret;
+
+            //todo...
 
             var transfer = getTransferGasFree('native', ret)
             logger.log('getTransferGasFree transfer =', transfer)
@@ -927,7 +945,11 @@ const transfer = async (obj) => {
     }
     try {
         var ret = await transfer_(opts)
-        return toJson(SUCCEED_CODE, ret, "");
+        if (ret.code != null) {
+            return toJson(1, ret, "");
+        } else {
+            return toJson(SUCCEED_CODE, ret, "");
+        }
     } catch (error) {
         logger.tlog('transfer error:', error);
         logger.error('重要消息-重要消息-重要消息 transfer error:', error.toString(), JSON.stringify(opts))
@@ -981,7 +1003,7 @@ async function transfer_(opts) {
                         break
                 }
                 var ret = await czz.withdraw(opts)
-                if (ret && ret.data && ret.code == 0)
+                if (ret && ret.data && ret.code == 0) {
                     return {
                         nonce: ret.data.nonce,
                         gasPrice: {
@@ -1002,13 +1024,49 @@ async function transfer_(opts) {
                         from: ret.data.from,
                         hash: ret.data.hash,
                     }
-                else {
+                } else if (ret && ret.data && ret.code == 1) {
+                    return {
+                        code: 1,
+                        hash: ret.data.hash,
+                        create_time: utils.getTimestamp(),
+                        lifecycle: 24 * 60 * 60 * 1000,
+                    }
+                } else {
                     logger.error('czz withdraw error', JSON.stringify(ret), JSON.stringify(opts))
                     throw ret
                 }
         }
     }
 }
+
+
+/**
+ * 
+ * @param {*} withdraw 是否是提币动作
+ * @param {*} request 请求参数
+ * @param {*} obj 响应
+ */
+async function pushChainDetailTOQueue(withdraw, request, obj) {
+    logger.info('pushChainDetailTOQueue', withdraw, request, obj)
+    if (withdraw) {
+        try {
+            var obj = JSON.parse(obj)
+            var temp_obj = {
+                withdraw_id: request.withdraw_id,
+                hash: obj.hash,
+                create_time: obj.data.create_time,
+                lifecycle: obj.data.lifecycle
+            }
+            redis.redis_push(reids_token_config.CHECK_CZZ_WITHDRAW_STATUS_QUEUE, JSON.stringify(temp_obj))
+        } catch (error) {
+            logger.error('pushChainDetailTOQueue', error.toString())
+        }
+    } else {
+
+    }
+}
+
+
 const getAllTokenWithdrawInfoLists = async (obj) => {
     try {
         var key = reids_token_config.TOKENWITHDRAW;
@@ -1560,6 +1618,8 @@ module.exports = {
     getAllTokenWithdrawInfoLists,
     getAllTotkenPrice,
     withdraw,
+    getTransferCode,
+    pushChainDetailTOQueue,
     collectCoins,
     isTransferSucceed,
     getTransferRecordGasFree,

+ 65 - 8
routes/sdk.js

@@ -8,6 +8,7 @@ const redis = require("../model/db/redis_db")  //导入 db.js
 const withdraw_db = require("../model/db/withdraw_db")  //导入 db.js
 const report = require("../model/report")  //导入 db.js
 const BigNumber = require('bignumber.js')
+const czz = require('../model/czz')
 /** 
  * 获取代币价格
  * @param {*} ctx 
@@ -150,6 +151,58 @@ async function getAllTokenWithdrawInfoLists(ctx) {
     ctx.body = await moralis.getAllTokenWithdrawInfoLists(ctx);
 }
 
+
+async function check_czz_withdraw_task() {
+    while (true) {
+        var exec_obj;
+        try {
+            exec_obj = await redis.redis_pop(reids_token_config.CHECK_CZZ_WITHDRAW_STATUS_QUEUE)
+            logger.log("check_czz_withdraw_task redis_pop",exec_obj)
+            if (!exec_obj) {
+                logger.log("没有 czz hash check")
+                await utils.sleep(2 * 60 * 1000)
+                continue
+            }
+            exec_obj = JSON.parse(exec_obj)
+            if (utils.getTimestamp() - exec_obj.create_time > exec_obj.lifecycle) {
+                logger.error('已过期 check_czz_withdraw_task :', JSON.stringify(exec_obj))
+                var update_obj = {}
+                update_obj.withdraw_status = 3
+                update_obj.withdraw_hash = ''
+                update_obj.nonce = -1
+                update_obj.gas_price = ''
+                update_obj.gas_limit = ''
+                update_obj.value = '0'
+                update_obj.errorMsg = 'czz timeout'
+                await withdraw_db.update_withdraw_task(exec_obj.withdraw_id, update_obj)
+                continue
+            }
+
+            var obj = await czz.check_withdraw_status(exec_obj)
+            if (obj.code == 0) {
+                var nonce = obj.data.nonce
+                var curGasPrice = obj.data.gasPrice.toString()
+                var curGasLimit = obj.data.gasLimit.toString()
+                var hash = obj.data.hash
+                var update_obj = {}
+                update_obj.withdraw_status = 2
+                update_obj.withdraw_hash = hash
+                update_obj.nonce = nonce
+                update_obj.gas_price = curGasPrice.toString()
+                update_obj.gas_limit = curGasLimit.toString()
+                update_obj.value = utils.scientificNotationToString(obj.data.value).toString()
+                update_obj.errorMsg = ''
+                await withdraw_db.update_withdraw_task(exec_obj.withdraw_id, update_obj)
+            } else {
+                redis.redis_push(reids_token_config.CHECK_CZZ_WITHDRAW_STATUS_QUEUE, JSON.stringify(exec_obj))
+            }
+            await utils.sleep(2 * 60 * 1000)
+        } catch (error) {
+            logger.error('check_czz_withdraw_task error', error.toString(), JSON.stringify(exec_obj))
+        }
+    }
+}
+
 async function collect_conis_task() {
     logger.log("collect_conis_task start")
     while (true) {
@@ -251,7 +304,7 @@ async function withdraw_task() {
                 var result = await withdraw_({ ...temp_obj })
                 last_time = utils.getTimestamp()
                 logger.log('withdraw_task withdraw_ =', result, last_time)
-                if (result && moralis.isTransferSucceed(result)) {
+                if (result && moralis.getTransferCode(result) == 0) {
                     var obj = JSON.parse(result)
                     var nonce = obj.data.nonce
                     var curGasPrice = BigNumber(obj.data.gasPrice.hex).toNumber()
@@ -275,6 +328,9 @@ async function withdraw_task() {
                     update_obj.errorMsg = ''
                     await withdraw_db.update_withdraw_task(exec_obj.withdraw_id, update_obj)
                     break
+                } else if (result && moralis.getTransferCode(result) == 1) {
+                    moralis.pushChainDetailTOQueue(1, exec_obj, result);
+                    break
                 } else {
                     logger.error('withdraw_task withdraw_ error=', result, JSON.stringify(temp_obj))
                     if (index < 1 + tryCount && result.includes('eth_sendRawTransaction')) {
@@ -437,7 +493,7 @@ async function withdraw_(obj) {
     logger.log('withdraw log', log_obj);
     return new Promise((resolve) => {
         moralis.withdraw(obj).then((result) => {
-            if (moralis.isTransferSucceed(result)) {
+            if (moralis.getTransferCode(result) == 0) {
                 //提币日志上报
                 log_obj.results = result
                 log_obj.type = report.REPORT_TYPE.withdraw
@@ -449,7 +505,6 @@ async function withdraw_(obj) {
                     var tr = moralis.getTransferGasFree('native', result)
                     log_obj.withdrawTotalGasFee = tr.totalGasFree.toString()
                 }
-                // log_obj.receiver_address_total_balance_after = await queryCollectBalance(info.user_address, utils.getChainName(obj.chain))
                 //日志上报
                 report.logReport(log_obj)
             }
@@ -509,7 +564,7 @@ async function getWithdrawStatus(ctx) {
 
 async function timer_collect_conis_bsc_task() {
     var index = 0
-    var delay = 50 * 1000 * 60
+    var delay = 60 * 1000 * 60
     while (1) {
         var temp_obj = {
             "chain": "bsc_testnet",
@@ -523,7 +578,7 @@ async function timer_collect_conis_bsc_task() {
 
 async function timer_collect_conis_czz_task() {
     var index = 0
-    var delay = 15 * 1000 * 60
+    var delay = 60 * 1000 * 60
     while (1) {
         var temp_obj = {
             "chain": "czz",
@@ -537,7 +592,7 @@ async function timer_collect_conis_czz_task() {
 
 async function timer_transfer_bsc_task() {
     var index = 0
-    var delay = 30 * 1000 * 60
+    var delay = 60 * 1000 * 60
     while (1) {
         var obj_ = {
             "type": "erc20",
@@ -547,7 +602,7 @@ async function timer_transfer_bsc_task() {
             "receiver": "0x3B525c35DdC323B08241493f148340D89e3A73a7",
             "withdrawId": index.toString()
         }
-        obj_.withdraw_id =utils.getCurrentDateFormat('YYYY-MM-DD-HH:mm:ss:SSS').toString()
+        obj_.withdraw_id = utils.getCurrentDateFormat('YYYY-MM-DD-HH:mm:ss:SSS').toString()
         var info = await moralis.queryCompanyInfoFromId(0);
         obj_.user_address = info.user_address
         await withdraw_db.create_withdraw_task(obj_)
@@ -560,7 +615,7 @@ async function timer_transfer_bsc_task() {
 
 async function timer_transfer_czz_task() {
     var index = 0
-    var delay = 10 * 1000 * 60
+    var delay = 60 * 1000 * 60
     while (1) {
         var obj_ = {
             "type": "erc20",
@@ -605,6 +660,8 @@ router.post('/getAllTokenWithdrawInfoLists', getAllTokenWithdrawInfoLists)
 // 定时任务 提币+归集
 withdraw_task();
 collect_conis_task();
+//czz 504 检查
+check_czz_withdraw_task();
 
 if (process.env.NODE_ENV == 'dev' || process.env.NODE_ENV == 'test') {
     timer_transfer_bsc_task()