Commit 95024fb7 by haojie

Initial commit

parents
/node_modules
/env.js
import request from './src/axios.js';
import { login_api, login_params, task_api } from './env.js';
import { sleep, getTokenHeader } from './utils/tool.js';
import { sendToken, inscriptionTransfer } from './utils/ethers.js';
// 交易状态
const statusInfo = {
TRADE_STATUS_WAIT: 1, // 未开始
TRADE_STATUS_SUCCESS: 2, // 已完成
TRADE_STATUS_FAIL: 3, // 失败
TRADE_STATUS_FAIL_2: 4, // 支付成功,接收地址错误
TRADE_STATUS_ERROR: 5, // 检测失败
};
// 当前token
let login_token = '';
// 登录过期校验
async function checkToken(code) {
if (code == 403) {
// 重新登录
await login();
}
}
// 获取铭文列表
async function getInscriptionList(address, inscription_hash) {
let check_url =
'https://mainnet-api.ethscriptions.com/api/ethscriptions/owned_by/' +
address;
// 转小写
inscription_hash = inscription_hash.toLowerCase();
try {
// 检测五次
for (let check_time = 0; check_time < 5; check_time++) {
let result = await request.get(check_url);
if (result) {
if (typeof result === 'string') {
result = JSON.parse(result);
}
if (result.length) {
// 找到铭文
for (let i = 0; i < result.length; i++) {
let item = result[i];
let item_hash = item.transaction_hash.toLowerCase();
if (item_hash === inscription_hash) {
return item;
}
}
}
}
}
return false;
} catch (e) {
console.log(e);
return false;
}
}
// 登录
async function login() {
try {
const res = await request.post(login_api, login_params);
if (res.code == 0) {
console.log('登录成功');
login_token = res.data.access_token;
}
} catch (e) {
console.log(e);
}
}
// 获取后台任务
const get_task = async () => {
const header = getTokenHeader(login_token);
let res = await request.get(task_api, {
headers: {
...header,
},
});
if (res.code == 0) {
if (typeof res.data === 'string') {
return JSON.parse(res.data);
}
return res.data;
}
await checkToken(res.code);
return false;
};
// 交易回调
const Transfer_callBack = async (task) => {
let for_status = true;
let url = task.notify_url;
// 提交5次
for (let i = 0; i < 5; i++) {
const header = getTokenHeader(login_token);
try {
let res = await request.post(url, task, {
headers: {
...header,
},
});
if (res.code == 0) {
console.log(res);
for_status = false;
}
await checkToken(res.code);
if (!for_status) {
console.log('结束循环');
break;
}
} catch (e) {
console.log(e);
}
}
};
// 连接上之后开始循环获取任务
async function run() {
let status = true;
// 获取启动时间
let start_time = new Date().valueOf();
let max_runing_time = 1000 * 60 * 60 * 24;
while (status) {
let cur_time = new Date().valueOf();
// 一天关一次
if (cur_time - start_time >= max_runing_time) {
// 结束
status = false;
break;
}
// 获取队列任务
let task = await get_task();
if (task) {
let from = task.from;
let to = task.to;
try {
if (task.type == 1) {
let inscription_hash = task.inscription_hash;
// 铭文转移
console.log('铭文转移');
const result = await inscriptionTransfer(to, from, inscription_hash);
if (result.hash && result.status) {
task.hash = result.hash;
// 判断是否转移成功--并更新回传铭文信息
const inscription_info = await getInscriptionList(
task.to,
inscription_hash
);
if (inscription_info) {
task.status = statusInfo.TRADE_STATUS_SUCCESS;
task.inscriptionInfo = inscription_info;
} else {
task.status = statusInfo.TRADE_STATUS_FAIL;
}
Transfer_callBack(task);
} else {
// 失败
task.status = statusInfo.TRADE_STATUS_FAIL;
task.message = result.message ?? '';
task.hash = result.hash ?? '';
Transfer_callBack(task);
}
} else if (task.type == 2) {
let amount = task.amount;
// 转账
const result = await sendToken(to, from, amount);
if (result && result.hash) {
// 成功
task.status = statusInfo.TRADE_STATUS_SUCCESS;
task.hash = result.hash;
Transfer_callBack(task);
} else {
// 失败
task.status = statusInfo.TRADE_STATUS_FAIL;
task.message = result.message;
Transfer_callBack(task);
}
}
} catch (e) {}
} else {
console.log('等待');
sleep(2000);
}
}
}
await login();
await run();
// await Transfer_callBack();
// 登录接口
export const login_api = '';
// 用户名密码
export const login_params = {
username: '',
password: '',
};
// 获取任务接口
export const task_api = '';
export const private_key = [
{
address: '',
key: '',
},
];
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"scripts": {
"start": "node ./app.js"
},
"dependencies": {
"axios": "^1.3.4",
"ethereumjs-common": "^1.5.2",
"ethereumjs-tx": "^2.1.2",
"ethers": "^5.7.2",
"web3": "^1.8.1"
},
"type": "module"
}
import Web3 from 'web3';
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import TronWeb from 'tronweb';
import tx from 'ethereumjs-tx';
import common from 'ethereumjs-common';
const bsc = 'https://rpc.ankr.com/bsc';
const bsc_web3 = new Web3(new Web3.providers.HttpProvider(bsc));
const eth = 'https://rpc.ankr.com/eth';
const eth_web3 = new Web3(new Web3.providers.HttpProvider(eth));
const Tx = tx.Transaction;
const Common = common.default;
const BSC_FORK = Common.forCustomChain(
'mainnet',
{
name: 'Binance Smart Chain Mainnet',
networkId: 56,
chainId: 56,
},
'istanbul'
);
const ETH_FORK = Common.forCustomChain(
'mainnet',
{
name: 'Ethereum',
networkId: 1,
chainId: 1,
url: 'https://rpc.ankr.com/eth',
},
'istanbul'
);
const HttpProvider = TronWeb.providers.HttpProvider;
const fullNode = new HttpProvider('https://api.trongrid.io');
const solidityNode = new HttpProvider('https://api.trongrid.io');
const eventServer = new HttpProvider('https://api.trongrid.io');
const contractAbi = JSON.parse(
'[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"guy","type":"address"},{"name":"wad","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"src","type":"address"},{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"wad","type":"uint256"}],"name":"withdraw","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"deposit","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"},{"name":"","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"},{"indexed":true,"name":"guy","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"},{"indexed":true,"name":"dst","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"dst","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Deposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Withdrawal","type":"event"}]'
);
const WBNB = bsc_web3.utils.toChecksumAddress(
'0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c'
);
const WBNBContract = new bsc_web3.eth.Contract(contractAbi, WBNB);
const bnb_decimals = 18;
const TRXDecimals = 6;
const bsc_usdt_decimals = 18;
const eth_usdt_decimals = 6;
const trx_usdt_decimals = 6;
const tronWeb = new TronWeb(fullNode, solidityNode, eventServer);
// eth
let ethUsdtContractAbi = JSON.parse(
'[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_upgradedAddress","type":"address"}],"name":"deprecate","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"deprecated","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_evilUser","type":"address"}],"name":"addBlackList","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"upgradedAddress","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balances","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"maximumFee","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"_totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"unpause","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_maker","type":"address"}],"name":"getBlackListStatus","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"},{"name":"","type":"address"}],"name":"allowed","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"paused","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"who","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"pause","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getOwner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"newBasisPoints","type":"uint256"},{"name":"newMaxFee","type":"uint256"}],"name":"setParams","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"amount","type":"uint256"}],"name":"issue","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"amount","type":"uint256"}],"name":"redeem","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"basisPointsRate","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"isBlackListed","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_clearedUser","type":"address"}],"name":"removeBlackList","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"MAX_UINT","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_blackListedUser","type":"address"}],"name":"destroyBlackFunds","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"_initialSupply","type":"uint256"},{"name":"_name","type":"string"},{"name":"_symbol","type":"string"},{"name":"_decimals","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"Issue","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"Redeem","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"newAddress","type":"address"}],"name":"Deprecate","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"feeBasisPoints","type":"uint256"},{"indexed":false,"name":"maxFee","type":"uint256"}],"name":"Params","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"_blackListedUser","type":"address"},{"indexed":false,"name":"_balance","type":"uint256"}],"name":"DestroyedBlackFunds","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"_user","type":"address"}],"name":"AddedBlackList","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"_user","type":"address"}],"name":"RemovedBlackList","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[],"name":"Pause","type":"event"},{"anonymous":false,"inputs":[],"name":"Unpause","type":"event"}]'
);
let ethUsdt = eth_web3.utils.toChecksumAddress(
'0xdAC17F958D2ee523a2206206994597C13D831ec7'
);
let ethUsdtTokenContract = new eth_web3.eth.Contract(
ethUsdtContractAbi,
ethUsdt
);
// bsc
let bscUsdtContractAbi = JSON.parse(
'[{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"constant":true,"inputs":[],"name":"_decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"_name","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"_symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"burn","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"subtractedValue","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getOwner","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"addedValue","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"mint","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"renounceOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]'
);
let bscUsdt = bsc_web3.utils.toChecksumAddress(
'0x55d398326f99059fF775485246999027B3197955'
);
let bscUsdtTokenContract = new bsc_web3.eth.Contract(
bscUsdtContractAbi,
bscUsdt
);
// tron
const trc20ContractAddress = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t';
/**
* 转移 gas 费
* @param type 类型
* @param privateKey 私钥
* @param fromAddress
* @param toAddress
* @param amount 金额
* @returns {Promise<boolean|*>}
*/
async function transfer_gas(type, privateKey, fromAddress, toAddress, amount) {
if (type === 'bsc') {
try {
amount = BigInt(amount * 10 ** bnb_decimals).toString();
let count = await bsc_web3.eth.getTransactionCount(fromAddress);
let rawTransaction = {
from: fromAddress,
gasPrice: bsc_web3.utils.toHex(bsc_web3.utils.toWei('5', 'gwei')),
gasLimit: 21000,
to: toAddress,
value: bsc_web3.utils.toHex(amount),
nonce: count,
};
let transaction = new Tx(rawTransaction, { common: BSC_FORK });
privateKey = privateKey.slice(2);
transaction.sign(Buffer.from(privateKey, 'hex'));
let result = await bsc_web3.eth.sendSignedTransaction(
'0x' + transaction.serialize().toString('hex')
);
return {
hash: result['transactionHash'],
status: result['status'],
};
} catch (e) {
console.log(e);
return false;
}
} else if (type === 'tron') {
tronWeb.setPrivateKey(privateKey);
let from = tronWeb.address.fromPrivateKey(privateKey);
amount = amount * Math.pow(10, TRXDecimals);
try {
let tradeobj = await tronWeb.transactionBuilder.sendTrx(
toAddress,
amount,
from
);
let signedtxn = await tronWeb.trx.sign(tradeobj, privateKey);
let result = await tronWeb.trx.sendRawTransaction(signedtxn);
console.log(result);
// 交易 ID
return {
hash: result['txid'],
status: result['result'],
};
} catch (e) {
console.error(e);
return false;
}
}
}
/**
* 转移 usdt
* @param type 类型
* @param privateKey 私钥
* @param fromAddress
* @param toAddress
* @param amount 金额
* @returns {Promise<*|boolean>}
*/
async function transfer_usdt(type, privateKey, fromAddress, toAddress, amount) {
if (type === 'bsc') {
try {
amount = Number(amount).toFixed(2);
amount = BigInt(amount * 10 ** bsc_usdt_decimals).toString();
let count = await bsc_web3.eth.getTransactionCount(fromAddress);
let data = bscUsdtTokenContract.methods.transfer(toAddress, amount);
let rawTransaction = {
from: fromAddress,
gasPrice: bsc_web3.utils.toHex(bsc_web3.utils.toWei('5', 'gwei')),
gasLimit: 200000,
to: bscUsdt,
data: data.encodeABI(),
nonce: count,
};
let transaction = new Tx(rawTransaction, { common: BSC_FORK });
privateKey = privateKey.slice(2);
transaction.sign(Buffer.from(privateKey, 'hex'));
let result = await bsc_web3.eth.sendSignedTransaction(
'0x' + transaction.serialize().toString('hex')
);
return {
hash: result['transactionHash'],
status: result['status'],
};
} catch (e) {
console.log(e);
return false;
}
} else if (type === 'tron') {
try {
// 设置发起交易签名的地址
tronWeb.setPrivateKey(privateKey);
let tronContractInstance = await tronWeb
.contract()
.at(trc20ContractAddress);
amount = amount * Math.pow(10, trx_usdt_decimals);
amount = Math.floor(amount);
// 返回交易 ID
let hash = await tronContractInstance.transfer(toAddress, amount).send();
for (let i = 0; i < 20; i++) {
console.log('开始重试获取');
try {
let status = await tronWeb.trx.getTransaction(hash);
return {
hash: hash,
status: status.ret[0].contractRet,
};
} catch (e) {
console.log(e);
}
}
} catch (e) {
console.log(e);
return false;
}
}
}
const app = express();
var corsOptions = {
origin: '*',
};
app.use(cors(corsOptions));
// content-type:application/json
app.use(bodyParser.json());
// content-type:application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
/**
* 创建账号
*/
app.get('/api/create-account', async (req, res) => {
var bsc_account = bsc_web3.eth.accounts.create();
// var eth_account = eth_web3.eth.accounts.create()
var tron_account = await tronWeb.createAccount();
var tron = {
type: 'tron',
private_key: tron_account.privateKey,
address: tron_account.address.base58,
};
var bsc = {
type: 'bsc',
private_key: bsc_account.privateKey,
address: bsc_account.address,
};
// var eth = {
// 'type': 'eth',
// 'private_key': eth_account.privateKey,
// 'address': eth_account.address,
// }
res.send({
status: 0,
message: 'success',
data: [
tron,
bsc,
// eth
],
});
});
/**
* 获取余额
*/
app.get('/api/balance', async (req, res) => {
var params = req.query;
var address = params.address;
var private_key = params.private_key;
var balance = 0;
var other_balance = 0;
try {
if (params.type === 'eth') {
let usdt_decimals = 6;
let result = await ethUsdtTokenContract.methods.balanceOf(address).call();
balance = result / 10 ** usdt_decimals;
// let bnb_balance = await bsc_web3.eth.getBalance(address);
// other_balance = bnb_balance / Math.pow(10, 18);
}
if (params.type === 'bsc') {
let usdt_decimals = 18;
let result = await bscUsdtTokenContract.methods.balanceOf(address).call();
balance = result / 10 ** usdt_decimals;
balance = balance.toFixed(8);
// let bnb_balance = await bsc_web3.eth.getBalance(address);
// other_balance = bnb_balance / Math.pow(10, 18);
}
if (params.type === 'tron') {
await tronWeb.setPrivateKey(private_key);
let tronContractInstance = await tronWeb
.contract()
.at(trc20ContractAddress);
let ownerAddress = tronWeb.address.fromPrivateKey(private_key);
let result = await tronContractInstance.balanceOf(ownerAddress).call();
let num = tronWeb.toBigNumber(result['_hex']);
const usdtDecimals = 6;
balance = num['c'][0] / 10 ** usdtDecimals;
}
} catch (e) {
console.log(e);
}
console.log(balance);
res.send({
status: 0,
message: 'success',
data: {
balance: balance,
other_balance: other_balance,
},
});
});
/**
* 获取其他余额
*/
app.get('/api/other-balance', async (req, res) => {
var params = req.query;
var address = params.address;
var other_balance = 0;
console.log('同步钱包金额: ' + address);
try {
if (params.type === 'eth' || params.type === 'bsc') {
let bnb_balance = await bsc_web3.eth.getBalance(address);
other_balance = bnb_balance / Math.pow(10, 18);
}
if (params.type === 'tron') {
let trx_balance = await tronWeb.trx.getBalance(address);
other_balance = tronWeb.fromSun(trx_balance);
}
} catch (e) {
console.log(e);
}
res.send({
status: 0,
message: 'success',
data: {
other_balance: other_balance,
},
});
});
/**
* 转移usd
*/
app.get('/api/transfer-usd', async (req, res) => {
var params = req.query;
console.log(params);
let result = await transfer_usdt(
params.type,
params.private_key,
params.from,
params.to,
params.amount
);
console.log(result);
res.send({
status: 0,
message: 'success',
data: {
type: params.type,
hash: result['hash'],
status: result['status'],
address: params.from,
},
});
});
/**
* 转 gas
*/
app.get('/api/transfer-gas', async (req, res) => {
var params = req.query;
console.log(params);
let result = await transfer_gas(
params.type,
params.private_key,
params.from,
params.to,
params.amount
);
res.send({
status: 0,
message: 'success',
data: {
type: params.type,
status: result['status'],
hash: result['hash'],
},
});
});
// 设置监听端口
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`服务器运行端口: ${PORT}.`);
});
[
{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [{ "name": "", "type": "string" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [{ "name": "_upgradedAddress", "type": "address" }],
"name": "deprecate",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "name": "_spender", "type": "address" },
{ "name": "_value", "type": "uint256" }
],
"name": "approve",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "deprecated",
"outputs": [{ "name": "", "type": "bool" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [{ "name": "_evilUser", "type": "address" }],
"name": "addBlackList",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [{ "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "name": "_from", "type": "address" },
{ "name": "_to", "type": "address" },
{ "name": "_value", "type": "uint256" }
],
"name": "transferFrom",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "upgradedAddress",
"outputs": [{ "name": "", "type": "address" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [{ "name": "", "type": "address" }],
"name": "balances",
"outputs": [{ "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [{ "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "maximumFee",
"outputs": [{ "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "_totalSupply",
"outputs": [{ "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "unpause",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [{ "name": "_maker", "type": "address" }],
"name": "getBlackListStatus",
"outputs": [{ "name": "", "type": "bool" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{ "name": "", "type": "address" },
{ "name": "", "type": "address" }
],
"name": "allowed",
"outputs": [{ "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "paused",
"outputs": [{ "name": "", "type": "bool" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [{ "name": "who", "type": "address" }],
"name": "balanceOf",
"outputs": [{ "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "pause",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "getOwner",
"outputs": [{ "name": "", "type": "address" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "owner",
"outputs": [{ "name": "", "type": "address" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "symbol",
"outputs": [{ "name": "", "type": "string" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "name": "_to", "type": "address" },
{ "name": "_value", "type": "uint256" }
],
"name": "transfer",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "name": "newBasisPoints", "type": "uint256" },
{ "name": "newMaxFee", "type": "uint256" }
],
"name": "setParams",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [{ "name": "amount", "type": "uint256" }],
"name": "issue",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [{ "name": "amount", "type": "uint256" }],
"name": "redeem",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{ "name": "_owner", "type": "address" },
{ "name": "_spender", "type": "address" }
],
"name": "allowance",
"outputs": [{ "name": "remaining", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "basisPointsRate",
"outputs": [{ "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [{ "name": "", "type": "address" }],
"name": "isBlackListed",
"outputs": [{ "name": "", "type": "bool" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [{ "name": "_clearedUser", "type": "address" }],
"name": "removeBlackList",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "MAX_UINT",
"outputs": [{ "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [{ "name": "newOwner", "type": "address" }],
"name": "transferOwnership",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [{ "name": "_blackListedUser", "type": "address" }],
"name": "destroyBlackFunds",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{ "name": "_initialSupply", "type": "uint256" },
{ "name": "_name", "type": "string" },
{ "name": "_symbol", "type": "string" },
{ "name": "_decimals", "type": "uint256" }
],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [{ "indexed": false, "name": "amount", "type": "uint256" }],
"name": "Issue",
"type": "event"
},
{
"anonymous": false,
"inputs": [{ "indexed": false, "name": "amount", "type": "uint256" }],
"name": "Redeem",
"type": "event"
},
{
"anonymous": false,
"inputs": [{ "indexed": false, "name": "newAddress", "type": "address" }],
"name": "Deprecate",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{ "indexed": false, "name": "feeBasisPoints", "type": "uint256" },
{ "indexed": false, "name": "maxFee", "type": "uint256" }
],
"name": "Params",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{ "indexed": false, "name": "_blackListedUser", "type": "address" },
{ "indexed": false, "name": "_balance", "type": "uint256" }
],
"name": "DestroyedBlackFunds",
"type": "event"
},
{
"anonymous": false,
"inputs": [{ "indexed": false, "name": "_user", "type": "address" }],
"name": "AddedBlackList",
"type": "event"
},
{
"anonymous": false,
"inputs": [{ "indexed": false, "name": "_user", "type": "address" }],
"name": "RemovedBlackList",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{ "indexed": true, "name": "owner", "type": "address" },
{ "indexed": true, "name": "spender", "type": "address" },
{ "indexed": false, "name": "value", "type": "uint256" }
],
"name": "Approval",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{ "indexed": true, "name": "from", "type": "address" },
{ "indexed": true, "name": "to", "type": "address" },
{ "indexed": false, "name": "value", "type": "uint256" }
],
"name": "Transfer",
"type": "event"
},
{ "anonymous": false, "inputs": [], "name": "Pause", "type": "event" },
{ "anonymous": false, "inputs": [], "name": "Unpause", "type": "event" }
]
import axios from 'axios';
const request = axios.create({
timeout: 30000,
withCredentials: true,
});
request.interceptors.response.use(
(res) => {
const { data } = res;
if (data.code == 0) {
return data;
}
},
(err) => {
return err;
}
);
export default request;
import { private_key } from '../env.js';
import { ethers } from 'ethers';
import { readFile } from 'fs/promises';
import common from 'ethereumjs-common';
import Web3 from 'web3';
import tx from 'ethereumjs-tx';
const Common = common.default;
const Tx = tx.Transaction;
const json_abi = JSON.parse(
await readFile(new URL('../src/ERC20ABI.json', import.meta.url))
);
// 节点
const chain_node =
'https://eth-mainnet.g.alchemy.com/v2/fH-3go199OKvbKSovc84gAC6xa5Givlb';
// eth链的 usdt 地址
const usdt = '0xdAC17F958D2ee523a2206206994597C13D831ec7';
const ETH_FORK = Common.forCustomChain(
'mainnet',
{
name: 'Ethereum',
networkId: 1,
chainId: 1,
url: chain_node,
},
'istanbul'
);
// 获取私钥
export const getPrivateKey = (address) => {
for (let i = 0; i < private_key.length; i++) {
let item = private_key[i];
if (item.address == address) {
return item.key;
}
}
return '';
};
// 实例化节点
export const newWeb3 = () => {
return new Web3(new Web3.providers.HttpProvider(chain_node));
};
// 转账
export const sendToken = async (to, from, amount) => {
// 获取私钥地址
const key = getPrivateKey(from);
let is_local = true;
const eth_web3 = newWeb3();
// let eth_web3 = new ethers.providers.JsonRpcProvider(chain_node);
let bscUsdtTokenContract = new eth_web3.eth.Contract(json_abi, usdt);
if (is_local) {
amount = Number(amount).toFixed(2);
amount = BigInt(amount * 10 ** 6).toString();
let count = await eth_web3.eth.getTransactionCount(from);
let data = bscUsdtTokenContract.methods.transfer(to, amount);
let rawTransaction = {
from: from,
gasPrice: eth_web3.utils.toHex(eth_web3.utils.toWei('18', 'gwei')),
gasLimit: 80000,
to: usdt,
data: data.encodeABI(),
nonce: count,
};
let transaction = new Tx(rawTransaction, { common: ETH_FORK });
// let privateKey = key.slice(2);
transaction.sign(Buffer.from(key, 'hex'));
let result = await eth_web3.eth.sendSignedTransaction(
'0x' + transaction.serialize().toString('hex')
);
return {
hash: result['transactionHash'],
// status: result['status'],
};
} else {
// ether.js
if (key) {
const provider = new ethers.providers.JsonRpcProvider(chain_node);
const signer = provider.getSigner();
let wallet = new ethers.Wallet(key, provider);
// 转换
let tokenAmount = amount * Math.pow(10, 6);
let big_num = BigInt(Math.floor(tokenAmount));
if (usdt) {
try {
let contract = new ethers.Contract(usdt, json, wallet);
let data = contract.transfer(to, big_num);
const res = await signer.sendTransaction({
to: contract,
// value: ethers.utils.parseUnits('0.000', 'ether'),
data: data.encodeABI(),
from: wallet.getAddress(),
nonce: signer.getTransactionCount(),
maxPriorityFeePerGas: ethers.utils.parseUnits('5', 'gwei'),
gasLimit: 80000,
// chainId: 5,
});
// const res = await contract.transfer(to, big_num);
console.log(res);
if (res.hash) {
// 交易成功
return {
hash: res.hash,
message: '',
};
} else {
// 交易失败
return {
hash: '',
message: '没有hash',
};
}
} catch (e) {
return {
hash: '',
message: e,
};
}
} else {
console.log('未设置usdt地址');
}
} else {
console.log('未获取到私钥');
}
}
};
// 铭文转移
export const inscriptionTransfer = async (to, from, data) => {
try {
const eth_web3 = newWeb3();
const senderPrivateKey = getPrivateKey(from);
const senderAccount =
eth_web3.eth.accounts.privateKeyToAccount(senderPrivateKey);
eth_web3.eth.accounts.wallet.add(senderAccount);
const transfer = {
from: senderAccount.address,
to: to,
value: 0,
data: data,
gas: 80000,
gasPrice: eth_web3.utils.toWei('18', 'gwei'),
};
// 使用私钥对交易进行签名
const signedTransaction = await eth_web3.eth.accounts.signTransaction(
transfer,
senderPrivateKey
);
// 发起交易
const transactionReceipt = await eth_web3.eth.sendSignedTransaction(
signedTransaction.rawTransaction
);
if (transactionReceipt.transactionHash) {
return {
hash: transactionReceipt.transactionHash,
status: transactionReceipt.status,
};
}
} catch (e) {
console.log(e);
return {
hash: '',
message: e,
};
}
};
inscriptionTransfer(
'0x53d05d2f8bdbb5ce389d313cd6d320fb0fb1c397',
'0x46DD0A127C7819e21591927aFb62518284150860',
'0x911d92a48d489ad367a50688e15af6a49274045c919305f0601b7c8f78098295'
);
// 等待
export function sleep(time) {
var timeStamp = new Date().valueOf();
var endTime = timeStamp + time;
while (true) {
if (new Date().valueOf() >= endTime) {
return;
}
}
}
// 获取带token的请求头
export const getTokenHeader = (token) => {
return {
authorization: `Bearer ${token}`,
};
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment