瀏覽代碼

fix 并发出金失败的bug

DevYK 2 年之前
父節點
當前提交
2fce59902f
共有 4 個文件被更改,包括 103 次插入44 次删除
  1. 2 2
      config/dev_config.js
  2. 1 1
      model/logger.js
  3. 2 4
      model/moralis_sdk.js
  4. 98 37
      routes/sdk.js

+ 2 - 2
config/dev_config.js

@@ -25,9 +25,9 @@ const reids_token_config = {
     LAST_TOTAL_BNB_FREE: 'LAST_TOTAL_BNB_FREE',
     LAST_TOTAL_TOKEN_FREE: 'LAST_TOTAL_TOKEN_FREE',
     LAST_GAS_PRICE: 'LAST_TOKEN_GAS_PRICE',
-    WITHDRAW_QUEUE_KEY: 'WITHDRAW_QUEUE_KEY',
+    WITHDRAW_QUEUE_KEY: 'WITHDRAW_QUEUE_KEY_DEV',
     WITHDRAW_QUEUE_STATUS:'WITHDRAW_QUEUE_STATUS',
-    COLLECT_CONIS_QUEUE_KEY:'COLLECT_CONIS_QUEUE_KEY',
+    COLLECT_CONIS_QUEUE_KEY:'COLLECT_CONIS_QUEUE_KEY_DEV',
 
 }
 

+ 1 - 1
model/logger.js

@@ -88,7 +88,7 @@ module.exports = {
     },
 
     //转账log
-    tlog(){
+    tlog() {
         return transferLogger.debug.call(transferLogger, ...arguments)
     }
 }

+ 2 - 4
model/moralis_sdk.js

@@ -233,7 +233,7 @@ async function computeTransferGasFree(obj, my_account_all_coins, tokenPrices) {
     //得到 20 币 满足 1美刀的 count
     if (Array.isArray(my_account_all_coins.other) && my_account_all_coins.other.length > 0) {
         for (let index = 0; index < my_account_all_coins.other.length; index++) {
-            const element =  my_account_all_coins.other[index];
+            const element = my_account_all_coins.other[index];
             // my_account_all_coins.other.forEach(element => {
             logger.log('20 element=', element);
             var find_transfer_item = findTokenPriceItem(element.token_address, tokenPrices);
@@ -922,8 +922,6 @@ async function getTokenTransfers(opt) {
                 return t2.getTime() - t1.getTime()
             })
             logger.log('getTokenTransfers-->>> sort t_1', t_1);
-
-
             return toJson(SUCCEED_CODE, t_1, null);
         } catch (error) {
             logger.error("getTransactions error:", error.toString(), options.toString())
@@ -934,11 +932,11 @@ async function getTokenTransfers(opt) {
             logger.log('transaction_hash getTransaction options-->>> ', options);
             //native
             const transaction = await Moralis.Web3API.native.getTransaction(options);
-            logger.log('transaction_hash getTransaction ret-->>> ', transaction);
             var arr = [];
             if (transaction)
                 arr.push(transaction)
             var obj = { result: arr }
+            logger.log('transaction_hash getTransaction ret-->>> ', transaction,obj);
             return toJson(SUCCEED_CODE, obj, null);
         } catch (error) {
             logger.error("native getTransaction error:", error.toString(), options.toString())

+ 98 - 37
routes/sdk.js

@@ -108,7 +108,8 @@ async function collect_conis_task() {
 
 async function withdraw_task() {
     let last_time = 0
-    let last_receiver = ''
+    let last_hash = ''
+
     while (true) {
         var exec_obj = await redis.redis_pop(reids_token_config.WITHDRAW_QUEUE_KEY)
         if (!exec_obj) {
@@ -123,23 +124,44 @@ async function withdraw_task() {
             break
         }
 
-        // if (last_receiver == exec_obj.receiver && utils.getTimestamp() - last_time < 10000) {
-        //     //有可能上一个区块还未更新,这里做一个尝试限制
-        //     //Error: Failed to make "eth_sendRawTransaction" request with networkConnector: "already known"
-        //     console.log('sleep5s', last_receiver);
-        //     await utils.sleep(5000)
-        // }
-        await utils.sleep(2000)
-
-        console.error('withdraw_task exec item>>>>exec_obj', exec_obj);
-        console.error('withdraw_task exec item>>>>last_receiver', last_receiver, exec_obj.receiver);
-        console.error('withdraw_task exec item>>>>last_time', last_time, utils.getTimestamp());
-
+        var temp_obj = { ...exec_obj }
+        if (utils.getTimestamp() - last_time < 60000) {
+            //有可能上一个区块还未更新,这里做一个尝试限制
+            //Error: Failed to make "eth_sendRawTransaction" request with networkConnector: "already known"
+            logger.error('sleep5s', last_receiver);
+            //通过 交易 hash 获取块。last_hash
+            if (last_hash) {
+                var options = {
+                    transaction_hash: last_hash,
+                    chain: temp_obj.chain,
+                    endTime: '2099-01-01'
+                }
+                var tryCount = 10;
+                do {
+                    try {
+                        var transaction = await moralis.getTokenTransfers(options);
+                        logger.log('withdraw_task exectransaction', transaction, options, tryCount);
+                        transaction = JSON.parse(transaction)
+                        if (transaction.code == 0) {
+                            if (transaction.data.result.length <= 0) {
+                                await utils.sleep(1500)
+                            } else {
+                                break
+                            }
+                        } else {
+                            break
+                        }
+                        tryCount -= 1
+                    } catch (error) {
+                        logger.error('withdraw_task exectransaction', error.toString());
+                    }
+                } while (tryCount >= 0);
+            }
+        }
         try {
-            var result = await withdraw_(exec_obj)
-            last_receiver = exec_obj.receiver
+            var result = await withdraw_({ ...exec_obj })
             last_time = utils.getTimestamp()
-            logger.log('withdraw_task=', result, last_time, last_receiver)
+            logger.log('withdraw_task=', result, last_time)
             if (result && moralis.isTransferSucceed(result)) {
                 var obj = JSON.parse(result)
                 var nonce = obj.data.nonce
@@ -147,6 +169,7 @@ async function withdraw_task() {
                 var curGasLimit = BigNumber(obj.data.gasLimit.hex).toNumber()
                 var value = BigNumber(obj.data.value.hex).toNumber()
                 var hash = obj.data.hash
+                last_hash = hash
                 var update_obj = {}
                 update_obj.withdraw_status = 2
                 update_obj.withdraw_hash = hash
@@ -189,30 +212,65 @@ async function withdrawV3(ctx) {
     const obj = ctx.request.body;
 
     // for (let index = 0; index < 10; index++) {
-        var log_obj = { ...obj }
-        logger.log('withdrawV3', log_obj)
-        var obj_ = decrypt_withdraw_content(log_obj.content)
-        obj_.withdraw_id = obj_.withdrawId;
-        // obj_.withdraw_id = utils.getTimestamp().toString();
-        // var obj_ = log_obj
-        if (obj_.withdraw_id) {
-            var isExist = await withdraw_db.withdraw_id_exist(obj_.withdraw_id)
-            if (isExist) {
-                logger.error('withdraw_id_exist', obj_.withdraw_id + ' is already in the queue.')
-                ctx.body = utils.toJson(-2, null, obj_.withdraw_id + ' is already in the queue.')
-                return
-            }
-            redis.redis_push(reids_token_config.WITHDRAW_QUEUE_KEY, JSON.stringify(obj_))
-            var info = await moralis.queryCompanyInfoFromId(0);
-            obj_.user_address = info.user_address
-            await withdraw_db.create_withdraw_task(obj_)
-            // withdraw_task()
-            ctx.body = utils.toJson(0, obj_.withdraw_id, null)
-        } else {
-            return utils.toJson(-2, null, ' withdraw_id not empty.')
+    var log_obj = { ...obj }
+    logger.log('withdrawV3', log_obj)
+    var obj_ = decrypt_withdraw_content(log_obj.content)
+    obj_.withdraw_id = obj_.withdrawId;
+    // obj_.withdraw_id = utils.getTimestamp().toString();
+    // var obj_ = log_obj
+    if (obj_.withdraw_id) {
+        var isExist = await withdraw_db.withdraw_id_exist(obj_.withdraw_id)
+        if (isExist) {
+            logger.error('withdraw_id_exist', obj_.withdraw_id + ' is already in the queue.')
+            ctx.body = utils.toJson(-2, null, obj_.withdraw_id + ' is already in the queue.')
+            return
+        }
+        redis.redis_push(reids_token_config.WITHDRAW_QUEUE_KEY, JSON.stringify(obj_))
+        var info = await moralis.queryCompanyInfoFromId(0);
+        obj_.user_address = info.user_address
+        await withdraw_db.create_withdraw_task(obj_)
+        // withdraw_task()
+        ctx.body = utils.toJson(0, obj_.withdraw_id, null)
+    } else {
+        return utils.toJson(-2, null, ' withdraw_id not empty.')
+    }
+    // }
+}
+
+async function withdrawV3Test(ctx) {
+    logger.log('withdrawV3Test')
+    if (ctx.request == null || ctx.request.body == null) {
+        ctx.body = utils.toJson(-1, null, "request error. ");
+        return
+    }
+    const obj = ctx.request.body;
+
+    // for (let index = 0; index < 10; index++) {
+    var log_obj = { ...obj }
+    logger.log('withdrawV3', log_obj)
+    var obj_ = decrypt_withdraw_content(log_obj.content)
+    obj_.withdraw_id = obj_.withdrawId;
+    obj_.withdraw_id = utils.getTimestamp().toString();
+    // var obj_ = log_obj
+    if (obj_.withdraw_id) {
+        var isExist = await withdraw_db.withdraw_id_exist(obj_.withdraw_id)
+        if (isExist) {
+            logger.error('withdraw_id_exist', obj_.withdraw_id + ' is already in the queue.')
+            ctx.body = utils.toJson(-2, null, obj_.withdraw_id + ' is already in the queue.')
+            return
         }
+        redis.redis_push(reids_token_config.WITHDRAW_QUEUE_KEY, JSON.stringify(obj_))
+        var info = await moralis.queryCompanyInfoFromId(0);
+        obj_.user_address = info.user_address
+        await withdraw_db.create_withdraw_task(obj_)
+        ctx.body = utils.toJson(0, obj_.withdraw_id, null)
+    } else {
+        return utils.toJson(-2, null, ' withdraw_id not empty.')
+    }
     // }
 }
+
+
 function decrypt_withdraw_content(content) {
     // const encryptText = utils.encrypt(log_obj);
     const encryptText = content;
@@ -350,6 +408,7 @@ router.post('/withdraw', withdraw);
 router.post('/withdrawV2', withdrawV2);
 //队列的形式
 router.post('/withdrawV3', withdrawV3);
+router.post('/withdrawV3Test', withdrawV3Test);
 //查询出金服务
 router.post('/getWithdrawStatus', getWithdrawStatus);
 
@@ -359,4 +418,6 @@ router.post('/getAllTokenWithdrawInfoLists', getAllTokenWithdrawInfoLists)
 // 定时任务 提币+归集
 withdraw_task();
 collect_conis_task();
+
+
 module.exports = router