var router = require('koa-router')(); var moralis = require('../model/moralis_sdk.js') 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 report = require("../model/report") //导入 db.js /** * 获取代币价格 * @param {*} ctx */ async function getAllTotkenPrice(ctx) { console.log('getTotkenPrice in:') var ret = await moralis.getAllTotkenPrice() console.log('getTotkenPrice result:', ret) if (ret) ctx.body = utils.toJson(0, ret, null); else ctx.body = utils.toJson(-1, null, "redis read error."); } /** * 获取交易记录 * @param {*} ctx */ async function getTransfers(ctx) { console.log("body", ctx); const obj = ctx.request.body; console.log("obj", obj); if (!obj.chain)//默认 bsc 币安链 obj.chain = 'bsc' var temp_obj = { ...obj } await moralis.getTokenTransfers(obj).then((result) => { ctx.body = result; if (result) { //提交归集任务 native 能获取到 gas 、token 无法获取到 gas 费 try { if (temp_obj.address && moralis.isTransferSucceed(result)) { var log_obj = { ...obj } log_obj.results = result log_obj.type = report.REPORT_TYPE.transfer_record //埋点日志上报-入金检查 report.logReport(log_obj) var json_obj = JSON.parse(result); //缓存当前交易的 gas 费用 var tr = moralis.getTransferRecordGasFree('native', json_obj, temp_obj.address) logger.log('getTransferRecordGasFree:', tr, temp_obj.address) if (tr && tr.totalGasFree > 0) { logger.log('getTransferRecordGasFree redis_set LAST_TOTAL_BNB_FREE:', tr.totalGasFree.toString()) logger.log('getTransferRecordGasFree redis_set LAST_TOTAL_TOKEN_FREE:', (parseInt(tr.totalGasFree) * parseInt(account_config.TOKEN_GAS_LIMIT)).toString()) redis.redis_set(reids_token_config.LAST_TOTAL_BNB_FREE, tr.totalGasFree.toString()); // var tokenFee = await redis.readRedis(reids_token_config.LAST_TOTAL_TOKEN_FREE) // if (!tokenFee) redis.redis_set(reids_token_config.LAST_TOTAL_TOKEN_FREE, (parseInt(tr.gas_price) * parseInt(account_config.TOKEN_GAS_LIMIT)).toString()); } //提交归集任务 if (temp_obj.address) { moralis.pushCollectConisObj(temp_obj) } } } catch (error) { console.error('pushCollectConisObj error=', error) } } }) } /** * 出金,入金交易 * @param {*} ctx * @returns */ async function transfer(ctx) { 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; }); } 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(ctx) { if (ctx.request == null || ctx.request.body == null) { ctx.body = utils.toJson(-1, null, "request error. "); return } const obj = ctx.request.body; var log_obj = { ...obj } var info = await moralis.queryCompanyInfoFromId(0); log_obj.receiver_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); await moralis.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) } }); } //获取交易记录 router.post('/getTransfers', getTransfers) // 获取所有代币价格 router.post('/getAllTotkenPrice', getAllTotkenPrice) // router.post('/transfer', transfer) //提现 router.post('/withdraw', withdraw); //获取所有地址的所要消耗的最低提取费 router.post('/getAllTokenWithdrawInfoLists', getAllTokenWithdrawInfoLists) module.exports = router;