|
@@ -4,8 +4,10 @@ var utils = require('../model/utils.js');
|
|
|
var { reids_token_config, account_config } = require('../config/config.js');
|
|
|
const logger = require('../model/logger.js');
|
|
|
router.prefix('/sdk');
|
|
|
-const redis = require("../model/redis_db") //导入 db.js
|
|
|
+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')
|
|
|
/**
|
|
|
* 获取代币价格
|
|
|
* @param {*} ctx
|
|
@@ -68,28 +70,117 @@ async function getTransfers(ctx) {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+async function getAllTokenWithdrawInfoLists(ctx) {
|
|
|
+ if (ctx.request == null || ctx.request.body == null) {
|
|
|
+ ctx.body = utils.toJson(-1, null, "request error. ");
|
|
|
+ return
|
|
|
+ }
|
|
|
+ ctx.body = await moralis.getAllTokenWithdrawInfoLists(ctx);
|
|
|
+}
|
|
|
+
|
|
|
+async function withdraw_task() {
|
|
|
+ var status = await redis.readRedis(reids_token_config.WITHDRAW_QUEUE_STATUS)
|
|
|
+ if (status && status == 1) {
|
|
|
+ logger.log('running...')
|
|
|
+ return
|
|
|
+ }
|
|
|
+ redis.redis_set(reids_token_config.WITHDRAW_QUEUE_STATUS, 1)
|
|
|
+
|
|
|
+ while (true) {
|
|
|
+ var exec_obj = await redis.redis_pop(reids_token_config.WITHDRAW_QUEUE_KEY)
|
|
|
+ try {
|
|
|
+ exec_obj = JSON.parse(exec_obj)
|
|
|
+ } catch (error) {
|
|
|
+ logger.log('item parse error', error);
|
|
|
+ break
|
|
|
+ }
|
|
|
+ logger.log('item', exec_obj);
|
|
|
+ if (!exec_obj) {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ var result = await withdraw_(exec_obj)
|
|
|
+ logger.log('withdraw_task=', result)
|
|
|
+ if (result && moralis.isTransferSucceed(result)) {
|
|
|
+ var obj = JSON.parse(result)
|
|
|
+ var nonce = obj.data.nonce
|
|
|
+ var curGasPrice = BigNumber(obj.data.gasPrice.hex).toNumber()
|
|
|
+ var curGasLimit = BigNumber(obj.data.gasLimit.hex).toNumber()
|
|
|
+ var value = BigNumber(obj.data.value.hex).toNumber()
|
|
|
+ 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 = value.toString()
|
|
|
+ update_obj.errorMsg = ''
|
|
|
+ await withdraw_db.update_withdraw_task(exec_obj.withdraw_id, update_obj)
|
|
|
+ } else {
|
|
|
+ var update_obj = {}
|
|
|
+ update_obj.withdraw_status = 3
|
|
|
+ update_obj.errorMsg = result
|
|
|
+ await withdraw_db.update_withdraw_task(exec_obj.withdraw_id, update_obj)
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ logger.log('withdraw_task error=', error)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ redis.redis_set(reids_token_config.WITHDRAW_QUEUE_STATUS, 0)
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
- * 出金,入金交易
|
|
|
+ * 队列版本
|
|
|
* @param {*} ctx
|
|
|
* @returns
|
|
|
*/
|
|
|
-async function transfer(ctx) {
|
|
|
+async function withdrawV3(ctx) {
|
|
|
+ logger.log('withdrawV3')
|
|
|
if (ctx.request == null || ctx.request.body == null) {
|
|
|
ctx.body = utils.toJson(-1, null, "request error. ");
|
|
|
return
|
|
|
}
|
|
|
const obj = ctx.request.body;
|
|
|
- await moralis.transfer(obj).then((result) => {
|
|
|
- ctx.body = result;
|
|
|
- });
|
|
|
+ var log_obj = { ...obj }
|
|
|
+ logger.log('withdrawV3', log_obj)
|
|
|
+ var obj_ = decrypt_withdraw_content(log_obj.content)
|
|
|
+ // 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 getAllTokenWithdrawInfoLists(ctx) {
|
|
|
- if (ctx.request == null || ctx.request.body == null) {
|
|
|
- ctx.body = utils.toJson(-1, null, "request error. ");
|
|
|
- return
|
|
|
+function decrypt_withdraw_content(content) {
|
|
|
+ // const encryptText = utils.encrypt(log_obj);
|
|
|
+ const encryptText = content;
|
|
|
+ logger.log("加密", encryptText);
|
|
|
+ let decryptObj = utils.decrypt(encryptText);
|
|
|
+ try {
|
|
|
+ logger.log("解密 before", decryptObj);
|
|
|
+ decryptObj = JSON.parse(decryptObj);
|
|
|
+ console.log("解密 json parse", decryptObj);
|
|
|
+
|
|
|
+ } catch (error) {
|
|
|
+ logger.log("json error:", error);
|
|
|
+ decryptObj = null;
|
|
|
}
|
|
|
- ctx.body = await moralis.getAllTokenWithdrawInfoLists(ctx);
|
|
|
+ return decryptObj;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -118,10 +209,30 @@ async function withdrawV2(ctx) {
|
|
|
})
|
|
|
} catch (error) {
|
|
|
logger.log("json error:", error);
|
|
|
- ctx.body = utils.toJson(-1,null,error.toString());
|
|
|
+ ctx.body = utils.toJson(-1, null, error.toString());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+function loop_test() {
|
|
|
+ var obj = {
|
|
|
+ type: "erc20",
|
|
|
+ contractAddress: "0x03716F32f72c692a0B355fa04639669E3341B94e",
|
|
|
+ amount: "112000000000000000000",
|
|
|
+ chain: "bsc_testnet",
|
|
|
+ receiver: "0x7C7401fcc82D1e53C4090561c3e6fde80d74e317"
|
|
|
+ }
|
|
|
+
|
|
|
+ var obj2 = {
|
|
|
+ type: "erc20",
|
|
|
+ contractAddress: "0x03716F32f72c692a0B355fa04639669E3341B94e",
|
|
|
+ amount: "113000000000000000000",
|
|
|
+ chain: "bsc_testnet",
|
|
|
+ receiver: "0x741050F44196Ad4b427AC5E97beB7715FD0127E7"
|
|
|
+ }
|
|
|
+ withdraw_(obj)
|
|
|
+ withdraw_(obj2)
|
|
|
+}
|
|
|
+
|
|
|
async function withdraw_(obj) {
|
|
|
console.log("withdraw_", obj);
|
|
|
var log_obj = { ...obj }
|
|
@@ -140,10 +251,9 @@ async function withdraw_(obj) {
|
|
|
var tr = moralis.getTransferGasFree('token', result)
|
|
|
log_obj.withdrawTotalGasFee = tr.totalGasFree.toString()
|
|
|
} else {
|
|
|
- var tr = moralis.getTransferGasFree('native', ret)
|
|
|
+ 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)
|
|
@@ -160,36 +270,51 @@ async function withdraw(ctx) {
|
|
|
return
|
|
|
}
|
|
|
const obj = ctx.request.body;
|
|
|
- var log_obj = { ...obj }
|
|
|
- var info = await moralis.queryCompanyInfoFromId(0);
|
|
|
- log_obj.company_address_total_balance_before = await moralis.queryCollectBalance(info.user_address, obj.chain)
|
|
|
- log_obj.company_public_key = info.user_address
|
|
|
- logger.log('withdraw log', log_obj,obj);
|
|
|
- await moralis.withdraw(obj).then((result) => {
|
|
|
+ await withdraw_(obj).then(result => {
|
|
|
ctx.body = result;
|
|
|
- if (moralis.isTransferSucceed(result)) {
|
|
|
- //提币日志上报
|
|
|
- log_obj.results = result
|
|
|
- log_obj.type = report.REPORT_TYPE.withdraw
|
|
|
-
|
|
|
- //缓存当前交易的 gas 费用
|
|
|
- if (result && log_obj.contractAddress) {
|
|
|
- var tr = moralis.getTransferGasFree('token', result)
|
|
|
- log_obj.withdrawTotalGasFee = tr.totalGasFree.toString()
|
|
|
- } else {
|
|
|
- var tr = moralis.getTransferGasFree('native', ret)
|
|
|
- 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)
|
|
|
- }
|
|
|
- });
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+/**
|
|
|
+ * 查询出金状态
|
|
|
+ * @param {*} ctx
|
|
|
+ */
|
|
|
+async function getWithdrawStatus(ctx) {
|
|
|
+ if (ctx.request == null || ctx.request.body == null) {
|
|
|
+ ctx.body = utils.toJson(-1, null, "request error. ");
|
|
|
+ return
|
|
|
+ }
|
|
|
+ const obj = ctx.request.body;
|
|
|
+ var info = await withdraw_db.queryWithdrawInfoFromWithdrawId(obj.withdraw_id)
|
|
|
+ logger.log('getWithdrawStatus info', JSON.stringify(info))
|
|
|
+ if (info) {
|
|
|
+ // try {
|
|
|
+ // var ret = await moralis.getTokenTransfers({
|
|
|
+ // transaction_hash: info.withdraw_hash,
|
|
|
+ // chain: utils.getChainIdToName(info.chain_id)
|
|
|
+ // })
|
|
|
+ // logger.log('getWithdrawStatus getTokenTransfers', ret)
|
|
|
+ // info.block_timestamp = ''
|
|
|
+ // if (ret) {
|
|
|
+ // if (typeof ret === 'string') {
|
|
|
+ // ret = JSON.parse(ret)
|
|
|
+ // info.block_timestamp = ret.data.result[0].block_timestamp
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // } catch (error) {
|
|
|
+ // logger.error('getWithdrawStatus getTokenTransfers error', error)
|
|
|
+ // }
|
|
|
+ ctx.body = utils.toJson(0, {
|
|
|
+ withdrawId: info.withdraw_id,
|
|
|
+ withdrawStatus: info.withdraw_status,
|
|
|
+ withdrawHash: info.withdraw_hash,
|
|
|
+ chainId: info.chain_id,
|
|
|
+ transferTimestamp: info.update_time,
|
|
|
+ }, null)
|
|
|
+ } else {
|
|
|
+ ctx.body = utils.toJson(-1, null, obj.withdraw_id + ' id does not exist.')
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
//获取交易记录
|
|
|
router.post('/getTransfers', getTransfers)
|
|
@@ -199,8 +324,13 @@ router.post('/getAllTotkenPrice', getAllTotkenPrice)
|
|
|
// router.post('/transfer', transfer)
|
|
|
//提现
|
|
|
router.post('/withdraw', withdraw);
|
|
|
-//提现
|
|
|
+//提现鉴权-body 加密
|
|
|
router.post('/withdrawV2', withdrawV2);
|
|
|
+//队列的形式
|
|
|
+router.post('/withdrawV3', withdrawV3);
|
|
|
+//查询出金服务
|
|
|
+router.post('/getWithdrawStatus', getWithdrawStatus);
|
|
|
+
|
|
|
//获取所有地址的所要消耗的最低提取费
|
|
|
router.post('/getAllTokenWithdrawInfoLists', getAllTokenWithdrawInfoLists)
|
|
|
|