|
@@ -6,7 +6,7 @@ var utils = require('./utils.js');
|
|
|
var { moralis_config, reids_token_config, account_config } = require('../config/config.js')
|
|
|
const redis = require("./redis_db") //导入 db.js
|
|
|
const mysql = require("./mysql_db")
|
|
|
-
|
|
|
+const logger = require('./logger')
|
|
|
/* Moralis init code */
|
|
|
var serverUrl = moralis_config.SERVER_URL;
|
|
|
var appId = moralis_config.APP_ID;
|
|
@@ -46,14 +46,14 @@ async function getAccountBalances(options) {
|
|
|
if (options.chain) {
|
|
|
options.chain = utils.getChainName(options.chain)
|
|
|
}
|
|
|
- console.log('getAccountBalances :', options)
|
|
|
+ logger.log('getAccountBalances :', options)
|
|
|
if (options.type == 'native') {
|
|
|
var opt_ret = await Moralis.Web3API.account.getNativeBalance(options);
|
|
|
- console.log('getNativeBalance=', opt_ret);
|
|
|
+ logger.log('getNativeBalance=', opt_ret);
|
|
|
return opt_ret
|
|
|
} else {
|
|
|
var aar = await Moralis.Web3API.account.getTokenBalances(options);
|
|
|
- console.log('getTokenBalances=', aar);
|
|
|
+ logger.log('getTokenBalances=', aar);
|
|
|
return aar;
|
|
|
}
|
|
|
}
|
|
@@ -101,35 +101,35 @@ async function computeTransferGasFree(my_account_all_coins, tokenPrices) {
|
|
|
//得到 20 币 满足 1美刀的 count
|
|
|
if (Array.isArray(my_account_all_coins.other) && my_account_all_coins.other.length > 0) {
|
|
|
my_account_all_coins.other.forEach(element => {
|
|
|
- console.debug('20 element=', element);
|
|
|
+ logger.debug('20 element=', element);
|
|
|
var find_transfer_item = findTokenPriceItem(element.token_address, tokenPrices);
|
|
|
var total_all_usdprice = calculate_total_usdprice(element.balance, element.decimals, find_transfer_item.usdPrice);
|
|
|
- console.debug('findTokenPriceItem ret=', element.token_address, find_transfer_item, total_all_usdprice);
|
|
|
+ logger.debug('findTokenPriceItem ret=', element.token_address, find_transfer_item, total_all_usdprice);
|
|
|
if (find_transfer_item && total_all_usdprice > 1.0) {
|
|
|
tokenCount += 1;
|
|
|
- console.debug('token > 1.0', tokenCount, element.token_address);
|
|
|
+ logger.debug('token > 1.0', tokenCount, element.token_address);
|
|
|
}
|
|
|
});
|
|
|
- console.log('account_config.TOKEN_GAS_LIMIT=', account_config.TOKEN_GAS_LIMIT);
|
|
|
+ logger.log('account_config.TOKEN_GAS_LIMIT=', account_config.TOKEN_GAS_LIMIT);
|
|
|
|
|
|
tokenGasPrice = parseInt(tokenCount) * parseInt(account_config.TOKEN_GAS_LIMIT) * parseInt(account_config.BNB_GAS_PRICE);
|
|
|
ret_a_gas = account_config.BNB_GAS_PRICE;
|
|
|
- console.log('tokenGasPrice=', tokenGasPrice);
|
|
|
+ logger.log('tokenGasPrice=', tokenGasPrice);
|
|
|
}
|
|
|
|
|
|
|
|
|
//计算 native 是否满足 1美刀
|
|
|
- console.log('isTransferGasFree token count:', tokenCount);
|
|
|
+ logger.log('isTransferGasFree token count:', tokenCount);
|
|
|
var nativeAllBalance = my_account_all_coins.native.balance;
|
|
|
var nativeCount = 0
|
|
|
var nativePriceItem = findTokenPriceItem('0x0000000000000000000000000000000000000000', tokenPrices);//todo 线上环境需要换
|
|
|
- console.debug('native nativePriceItem=', nativePriceItem, nativeAllBalance);
|
|
|
+ logger.debug('native nativePriceItem=', nativePriceItem, nativeAllBalance);
|
|
|
if (nativePriceItem) {
|
|
|
var total_all_usdprice = calculate_total_usdprice(nativeAllBalance, '18', nativePriceItem.usdPrice);
|
|
|
- console.debug('native total_all_usdprice=', total_all_usdprice);
|
|
|
+ logger.debug('native total_all_usdprice=', total_all_usdprice);
|
|
|
if (total_all_usdprice > 1.0) {
|
|
|
nativeCount = 1;
|
|
|
- console.debug('native > 1.0', tokenCount);
|
|
|
+ logger.debug('native > 1.0', tokenCount);
|
|
|
nativeGasPrice = parseInt(nativeCount) * parseInt(account_config.BNB_GAS_LIMIT) * parseInt(account_config.BNB_GAS_PRICE);
|
|
|
ret_a_gas = account_config.BNB_GAS_PRICE;
|
|
|
}
|
|
@@ -138,22 +138,22 @@ async function computeTransferGasFree(my_account_all_coins, tokenPrices) {
|
|
|
//计算所有币转账所需要的 gas
|
|
|
totalCount = nativeCount + tokenCount;
|
|
|
// var gasPrice = await redis.readRedis(reids_token_config.GASPRICE);
|
|
|
- console.log('nativeAllBalance', nativeAllBalance);
|
|
|
- console.log('totalCount', totalCount);
|
|
|
+ logger.log('nativeAllBalance', nativeAllBalance);
|
|
|
+ logger.log('totalCount', totalCount);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var total2Gas = nativeGasPrice + tokenGasPrice;
|
|
|
var service_charge = 0;
|
|
|
- console.log('total2Gas', total2Gas);
|
|
|
+ logger.log('total2Gas', total2Gas);
|
|
|
|
|
|
//如果当前的钱不够 gas
|
|
|
if (nativeAllBalance < total2Gas) {
|
|
|
if (tokenCount > 0) {//出现 token 需要转移手续费
|
|
|
service_charge = 1;
|
|
|
total2Gas = (total2Gas - nativeAllBalance);//充手续费
|
|
|
- console.log('需要转账=', total2Gas);
|
|
|
+ logger.log('需要转账=', total2Gas);
|
|
|
}
|
|
|
}
|
|
|
ret_total_gas_price = total2Gas.toString();
|
|
@@ -170,7 +170,7 @@ async function computeTransferGasFree(my_account_all_coins, tokenPrices) {
|
|
|
|
|
|
function findTokenPriceItem(token_address, tokenPrices) {
|
|
|
return tokenPrices.tokenPrice.find(element => {
|
|
|
- console.log('findTokenPriceItem find=', element.contract, token_address)
|
|
|
+ logger.log('findTokenPriceItem find=', element.contract, token_address)
|
|
|
return element.contract.toLowerCase() == token_address.toLowerCase();
|
|
|
})
|
|
|
}
|
|
@@ -188,7 +188,7 @@ async function updateNativeBalance(nativeBalance, obj) {
|
|
|
do {
|
|
|
//上面转账完 BNB 会减去,这里再获取一次
|
|
|
var native_ret = await Moralis.Web3API.account.getNativeBalance(temp);
|
|
|
- console.log('更新余额 :', native_ret, retryCount)
|
|
|
+ logger.log('更新余额 :', native_ret, retryCount)
|
|
|
if (nativeBalance != native_ret.balance) {
|
|
|
return native_ret.balance;
|
|
|
}
|
|
@@ -203,10 +203,10 @@ async function transfers(obj, my_account_all_coins) {
|
|
|
var address = obj.address;
|
|
|
var chain = obj.chain;
|
|
|
var tokenPrices = obj.tokenPrices;
|
|
|
- console.log('tokenPrices=', tokenPrices, my_account_all_coins);
|
|
|
+ logger.log('tokenPrices=', tokenPrices, my_account_all_coins);
|
|
|
if (!my_account_all_coins || !tokenPrices) return 'error.'
|
|
|
|
|
|
- console.log(' my_account_all_coins.other.lenth=', my_account_all_coins.other.length);
|
|
|
+ logger.log(' my_account_all_coins.other.lenth=', my_account_all_coins.other.length);
|
|
|
|
|
|
var isUpdateNativeBalance = 0;
|
|
|
|
|
@@ -228,14 +228,14 @@ async function transfers(obj, my_account_all_coins) {
|
|
|
type: 'erc20',
|
|
|
address: address,
|
|
|
}
|
|
|
- console.log('start_collectCoins erc20:', obj_20);
|
|
|
- console.log('calculate_total_usdprice 20', calculate_total_usdprice(transfer_item.balance, transfer_item.decimals, '0.1'));
|
|
|
+ logger.log('start_collectCoins erc20:', obj_20);
|
|
|
+ logger.log('calculate_total_usdprice 20', calculate_total_usdprice(transfer_item.balance, transfer_item.decimals, '0.1'));
|
|
|
isUpdateNativeBalance = 1;
|
|
|
var ret = await start_collectCoins(obj_20)
|
|
|
- console.log('start_collectCoins erc20 respose...', ret);
|
|
|
+ logger.log('start_collectCoins erc20 respose...', ret);
|
|
|
}
|
|
|
} else {
|
|
|
- console.error('token Must be greater than a dollar.', transfer_item.balance, transfer_item.decimals);
|
|
|
+ logger.error('token Must be greater than a dollar.', transfer_item.balance, transfer_item.decimals);
|
|
|
return toJson(-1, null, 'token Must be greater than a dollar.');
|
|
|
}
|
|
|
}
|
|
@@ -243,11 +243,11 @@ async function transfers(obj, my_account_all_coins) {
|
|
|
|
|
|
//native 归集
|
|
|
if (my_account_all_coins.native) {
|
|
|
- console.log('查询本地余额参数=', obj)
|
|
|
+ logger.log('查询本地余额参数=', obj)
|
|
|
if (obj.chain) {
|
|
|
obj.chain = utils.getChainName(obj.chain)
|
|
|
}
|
|
|
- console.log('查询本地余额 before', my_account_all_coins.native)
|
|
|
+ logger.log('查询本地余额 before', my_account_all_coins.native)
|
|
|
if (isUpdateNativeBalance) {
|
|
|
// do {
|
|
|
var updateBalance = await updateNativeBalance(my_account_all_coins.native.balance, obj)
|
|
@@ -267,29 +267,29 @@ async function transfers(obj, my_account_all_coins) {
|
|
|
// } while (native_ret.balance == my_account_all_coins.native.balance);
|
|
|
}
|
|
|
obj.chain = chain;
|
|
|
- console.log('查询本地余额 after', my_account_all_coins.native)
|
|
|
+ logger.log('查询本地余额 after', my_account_all_coins.native)
|
|
|
var find_native_item = findTokenPriceItem('0x0000000000000000000000000000000000000000', tokenPrices);//todo 线上环境需要换
|
|
|
var nativeCoins = calculate_total_usdprice(my_account_all_coins.native.balance, '18', find_native_item.usdPrice);
|
|
|
- console.log('start_collectCoins nativeCoins:', nativeCoins, obj);
|
|
|
- console.log('start_collectCoins obj:', obj);
|
|
|
- console.log('start_collectCoins native.balance:', my_account_all_coins.native.balance);
|
|
|
+ logger.log('start_collectCoins nativeCoins:', nativeCoins, obj);
|
|
|
+ logger.log('start_collectCoins obj:', obj);
|
|
|
+ logger.log('start_collectCoins native.balance:', my_account_all_coins.native.balance);
|
|
|
//todo 计算 token 币价格 * token美元单价
|
|
|
if (find_native_item && nativeCoins > 1.0) {
|
|
|
- console.log('native.balance', my_account_all_coins.native.balance)
|
|
|
- console.log('aGasPrice', obj.transFerGasFree.aGasPrice)
|
|
|
- console.log('gasLimint', account_config.BNB_GAS_LIMIT)
|
|
|
+ logger.log('native.balance', my_account_all_coins.native.balance)
|
|
|
+ logger.log('aGasPrice', obj.transFerGasFree.aGasPrice)
|
|
|
+ logger.log('gasLimint', account_config.BNB_GAS_LIMIT)
|
|
|
// var gasPrice = BigInt(obj.transFerGasFree.aGasPrice);
|
|
|
|
|
|
var gasPrice = BigInt(account_config.BNB_GAS_PRICE);
|
|
|
var gasLimit = BigInt(account_config.BNB_GAS_LIMIT);
|
|
|
var nativeBalance = BigInt(my_account_all_coins.native.balance);
|
|
|
|
|
|
- console.log('native.balance>>>', nativeBalance)
|
|
|
- console.log('aGasPrice>>>', gasPrice)
|
|
|
- console.log('gasLimint>>>', gasLimit)
|
|
|
+ logger.log('native.balance>>>', nativeBalance)
|
|
|
+ logger.log('aGasPrice>>>', gasPrice)
|
|
|
+ logger.log('gasLimint>>>', gasLimit)
|
|
|
|
|
|
var real_native_amount = nativeBalance - gasPrice * gasLimit;
|
|
|
- console.log('start_collectCoins native amount:', real_native_amount.toString());
|
|
|
+ logger.log('start_collectCoins native amount:', real_native_amount.toString());
|
|
|
|
|
|
obj = {
|
|
|
chain: chain,
|
|
@@ -298,11 +298,11 @@ async function transfers(obj, my_account_all_coins) {
|
|
|
type: 'native',
|
|
|
address: address,
|
|
|
}
|
|
|
-
|
|
|
- console.log('start_collectCoins native:', obj);
|
|
|
- console.log('calculate_total_usdprice native', nativeCoins, find_native_item);
|
|
|
+ logger.log('start_collectCoins native:', obj);
|
|
|
+ logger.log('calculate_total_usdprice native', nativeCoins, find_native_item);
|
|
|
return await start_collectCoins(obj)
|
|
|
} else {
|
|
|
+ logger.error('native Must be greater than a dollar.', obj);
|
|
|
return toJson(-1, null, 'native Must be greater than a dollar.');
|
|
|
}
|
|
|
}
|
|
@@ -338,7 +338,7 @@ const collectCoins = async (obj) => {
|
|
|
|
|
|
//计算 gas 费用 是否需要归集
|
|
|
var transFerGasFree = await computeTransferGasFree(my_account_all_coins, obj.tokenPrices);
|
|
|
- console.log('computeTransferGasFree=', transFerGasFree)
|
|
|
+ logger.log('computeTransferGasFree=', transFerGasFree)
|
|
|
|
|
|
//是否需要归集
|
|
|
if (transFerGasFree.totalCount > 0) {
|
|
@@ -351,9 +351,9 @@ const collectCoins = async (obj) => {
|
|
|
type: 'native',
|
|
|
address: account_config.WELLET_PUBLIC_KEY, //todo 正式环境需要替换从 mysql read
|
|
|
}
|
|
|
- console.log('开始充值 gas ', obj_wd)
|
|
|
+ logger.log('开始充值 gas ', obj_wd)
|
|
|
var ret = await withdraw(obj_wd)
|
|
|
- console.log('充值完成 gas ', ret)
|
|
|
+ logger.log('充值完成 gas ', ret)
|
|
|
|
|
|
//更新本地余额
|
|
|
var updateBalance = await updateNativeBalance(my_account_all_coins.native.balance, obj)
|
|
@@ -364,9 +364,9 @@ const collectCoins = async (obj) => {
|
|
|
|
|
|
obj.chain = chain;
|
|
|
obj.transFerGasFree = transFerGasFree;
|
|
|
- console.log('transfers--->', obj);
|
|
|
+ logger.log('transfers--->', obj);
|
|
|
var ret = await transfers(obj, my_account_all_coins);
|
|
|
- console.log('归集结果=', ret);
|
|
|
+ logger.log('归集结果=', ret);
|
|
|
return ret;
|
|
|
}
|
|
|
return obj.address + ':不满足归集条件';
|
|
@@ -379,10 +379,8 @@ async function execCollectCoinsTask() {
|
|
|
var obj = collectCoinsArrays.pop();
|
|
|
//开始收集用户地址里面的币到归集地址
|
|
|
var ret = await collectCoins(obj);
|
|
|
- console.log('execCollectCoinsTask=', obj, ret)
|
|
|
+ logger.log('execCollectCoinsTask=', obj, ret)
|
|
|
}
|
|
|
- // resolve('result');
|
|
|
- // })
|
|
|
}
|
|
|
|
|
|
|
|
@@ -395,27 +393,27 @@ function pushCollectConisObj(obj) {
|
|
|
async function readPriveteKeyFromMysql(address) {
|
|
|
return new Promise(resolve => {
|
|
|
mysql.queryUserPrivateKeyFromUserAddress(address).then(ret => {
|
|
|
- console.log('readPriveteKeyFromMysql=', ret);
|
|
|
+ logger.log('readPriveteKeyFromMysql=', ret);
|
|
|
resolve(ret);
|
|
|
})
|
|
|
})
|
|
|
}
|
|
|
|
|
|
const transfer = async (obj) => {
|
|
|
- console.debug("fun transfer in ", obj);
|
|
|
- console.debug("fun transfer serverUrl ", serverUrl);
|
|
|
- console.debug("fun transfer appId ", serverUrl);
|
|
|
- console.debug("fun transfer moralisSecret ", moralisSecret);
|
|
|
+ logger.debug("fun transfer in ", obj);
|
|
|
+ logger.debug("fun transfer serverUrl ", serverUrl);
|
|
|
+ logger.debug("fun transfer appId ", serverUrl);
|
|
|
+ logger.debug("fun transfer moralisSecret ", moralisSecret);
|
|
|
await initMoralisSecretSDK();
|
|
|
// initSDK(moralisSecret);
|
|
|
- console.debug("fun transfer start ok ");
|
|
|
+ logger.debug("fun transfer start ok ");
|
|
|
const opts = {};
|
|
|
opts.chainId = 'bsc_testnet';
|
|
|
opts.privateKey = moralis_config.DEFAULT_PRIVATE_KEY;
|
|
|
opts.type = "erc20"; //native erc20
|
|
|
|
|
|
if (!obj.receiver || !obj.amount || parseInt(obj.amount) <= 0) {
|
|
|
- console.error("fun transfer parameter error.");
|
|
|
+ logger.error("transfer fun transfer parameter error.",obj.receiver,obj.amount,obj.amount);
|
|
|
return toJson(ERROR_CODE_001, null, "please check receiver or amount parameter is ok ?");
|
|
|
}
|
|
|
if (obj.chain != null) {
|
|
@@ -445,8 +443,6 @@ const transfer = async (obj) => {
|
|
|
opts.privateKey = obj.privateKey;
|
|
|
}
|
|
|
|
|
|
- //解密私钥
|
|
|
- var privateKey = '';
|
|
|
//提币
|
|
|
if (obj.withdraw) {
|
|
|
//提币公司
|
|
@@ -466,7 +462,7 @@ const transfer = async (obj) => {
|
|
|
//解密
|
|
|
opts.privateKey = utils.decryptPrivityKey(opts.privateKey);
|
|
|
|
|
|
- console.log('decryptPrivityKey privateKey=', opts.privateKey);
|
|
|
+ logger.log('decryptPrivityKey privateKey=', opts.privateKey);
|
|
|
try {
|
|
|
// sending 0.5 DAI tokens with 18 decimals on BSC testnet
|
|
|
var options;
|
|
@@ -477,14 +473,14 @@ const transfer = async (obj) => {
|
|
|
receiver: opts.receiver, //接收钱包地址
|
|
|
contractAddress: opts.contractAddress //用户合约地址
|
|
|
};
|
|
|
- console.log("options 20 =", options);
|
|
|
+ logger.log("options 20 =", options);
|
|
|
} else { //ETH or BNB
|
|
|
options = Moralis.TransferOptions = {
|
|
|
type: opts.type,
|
|
|
amount: opts.amount,
|
|
|
receiver: opts.receiver, //接收钱包地址
|
|
|
};
|
|
|
- console.log("options native =", options, opts.chainId);
|
|
|
+ logger.log("options native =", options, opts.chainId);
|
|
|
}
|
|
|
|
|
|
// Enable web3
|
|
@@ -496,7 +492,7 @@ const transfer = async (obj) => {
|
|
|
var ret = await Moralis.transfer(options);
|
|
|
return toJson(SUCCEED_CODE, ret, "");
|
|
|
} catch (error) {
|
|
|
- console.log('transfer error:', error);
|
|
|
+ logger.error('transfer error:', error);
|
|
|
if (error.reason != null) {
|
|
|
return toJson(ERROR_CODE_001, null, error.toString());
|
|
|
} else {
|
|
@@ -522,13 +518,13 @@ const getAllTokenWithdrawInfoLists = async (obj) => {
|
|
|
*/
|
|
|
const getAllTotkenPrice = async () => {
|
|
|
try {
|
|
|
- console.log('当前环境:', process.env.NODE_ENV);
|
|
|
- console.log("getAllTotkenPrice in", reids_token_config); // Prints "value"
|
|
|
+ logger.log('当前环境:', process.env.NODE_ENV);
|
|
|
+ logger.log("getAllTotkenPrice in", reids_token_config); // Prints "value"
|
|
|
var token_price_key = reids_token_config.TOKENPRICE;
|
|
|
- console.log("getAllTotkenPrice token_price_key=", token_price_key);
|
|
|
+ logger.log("getAllTotkenPrice token_price_key=", token_price_key);
|
|
|
return await redis.readRedis(token_price_key)
|
|
|
} catch (error) {
|
|
|
- console.error("getTotkenPrice=", error);
|
|
|
+ logger.error("getTotkenPrice=", error);
|
|
|
return toJson(ERROR_CODE_001, null, error.toString());
|
|
|
}
|
|
|
}
|
|
@@ -546,14 +542,14 @@ function setTransfersDataType(type, ret) {
|
|
|
//hash 0xe09ba3a4c9f7a8902e01af68d0f1f91906f3f7db1195227e61c45c0e86b2630a
|
|
|
async function getTokenTransfers(opt) {
|
|
|
await initMasterSDK();
|
|
|
- console.debug("fun getTokenTransfers in ", opt);
|
|
|
+ logger.debug("fun getTokenTransfers in ", opt);
|
|
|
const options = {};
|
|
|
options.type = 'all';
|
|
|
options.chain = 'bsc_mainnet';
|
|
|
|
|
|
if (opt.chain != null) {
|
|
|
options.chain = utils.getChainName(opt.chain);
|
|
|
- console.log('getTokenTransfers=', options.chain);
|
|
|
+ logger.log('getTokenTransfers=', options.chain);
|
|
|
}
|
|
|
|
|
|
if (opt.order != null) {
|
|
@@ -581,7 +577,7 @@ async function getTokenTransfers(opt) {
|
|
|
options.type = 'transaction_hash';
|
|
|
}
|
|
|
|
|
|
- console.debug('getTokenTransfers-->>>', options);
|
|
|
+ logger.debug('getTokenTransfers-->>>', options);
|
|
|
if (options.type == 'all') {//查询主流币和 20 币所有的交易
|
|
|
try {
|
|
|
if (opt.address != null) {
|
|
@@ -612,7 +608,7 @@ async function getTokenTransfers(opt) {
|
|
|
|
|
|
return toJson(SUCCEED_CODE, t_1, null);
|
|
|
} catch (error) {
|
|
|
- console.error("getTransactions error:", error)
|
|
|
+ logger.error("getTransactions error:", error)
|
|
|
return toJson(ERROR_CODE_001, null, error);;
|
|
|
}
|
|
|
} else if (options.type == 'transaction_hash') {//根据哈希查询
|
|
@@ -625,7 +621,7 @@ async function getTokenTransfers(opt) {
|
|
|
var obj = { result: arr }
|
|
|
return toJson(SUCCEED_CODE, obj, null);
|
|
|
} catch (error) {
|
|
|
- console.error("native getTransaction error:", error)
|
|
|
+ logger.error("native getTransaction error:", error)
|
|
|
return toJson(ERROR_CODE_001, null, error);
|
|
|
}
|
|
|
} else {
|