const Config = require('../../config/config.js') const ListenConfig = require('../../config/listen-config.js') const PrivateConfig = require('../../PrivateConfig.js') const TimeKit = require('../../kit/time-kit') const Web3 = require('web3') const Tx = require('ethereumjs-tx') class SimpleWeb3 {} SimpleWeb3.init = function() { if (SimpleWeb3.web3) return SimpleWeb3.web3 = new Web3(Config.WEB3_RPC_URL) // Config.baseToken.contractModel = new SimpleWeb3.web3.eth.Contract(Config.BASE_ABI, Config.baseToken.contract, { // from: PrivateConfig.address // }) // // for (const token of ListenConfig.tokenList) { // token.contractModel = new SimpleWeb3.web3.eth.Contract(token.BASE_ABI, token.contract, { // from: PrivateConfig.address // }) // } } SimpleWeb3.getBalance = async function(address) { return await SimpleWeb3.web3.eth.getBalance(address) } SimpleWeb3.getRealBalance = async function(address) { const wei = await SimpleWeb3.getBalance(address) return SimpleWeb3.web3.utils.fromWei(wei, 'ether') } SimpleWeb3.sendTransaction = async function(txObject) { const tx = new Tx(txObject) tx.sign(Buffer.from(PrivateConfig.privateKey, 'hex')) const serializedTx = tx.serialize() const raw = '0x' + serializedTx.toString('hex') Config.baseToken.isSwap = true SimpleWeb3.web3.eth.sendSignedTransaction(raw, async (err, txHash) => { if (!txHash) { console.log(err.toString()) txObject.data = undefined console.log(txObject) Config.baseToken.isSwap = false } else { const time = TimeKit.getTimeByMillisecond(new Date().getTime()) txObject.data = undefined console.log(`[发起新的交易]tx: ${JSON.stringify(txObject)}`) console.log(`交易时间: ${time}, 交易回执: ${txHash}\n`) await TimeKit.sleep(10 * 1000) Config.baseToken.isSwap = false } }) } SimpleWeb3.transferTokenByAddress = async function(amount, to='0xA9D841B81da81c5B94fCe514211e3292FE3f882B', token=Config.baseToken) { const balance = amount const BN = SimpleWeb3.web3.utils.BN const decimals = token.decimals if (decimals > 10) { const balanceBN = new BN(parseInt(balance * (10 ** 10))) const additionalBN = new BN(10 ** (decimals - 10)) amount = balanceBN.mul(additionalBN).toString() } else { amount = balance * (10 ** decimals) + '' } amount = amount.split('.')[0] const txCount = await SimpleWeb3.web3.eth.getTransactionCount(PrivateConfig.address) const txObject = { from: PrivateConfig.address, nonce: SimpleWeb3.web3.utils.toHex(txCount), gasPrice: SimpleWeb3.web3.utils.toHex(SimpleWeb3.web3.utils.toWei('7', 'gwei')), gasLimit: SimpleWeb3.web3.utils.toHex(70000), to: token.contract, value: '0x0', data: token.contractModel.methods.transfer(to, amount).encodeABI() } const tx = new Tx(txObject) tx.sign(Buffer.from(PrivateConfig.privateKey, 'hex')) const serializedTx = tx.serialize() const raw = '0x' + serializedTx.toString('hex') token.isTransfer = true SimpleWeb3.web3.eth.sendSignedTransaction(raw, async (err, txHash) => { if (!txHash) { console.log(err) token.isTransfer = false } else { const time = TimeKit.getTimeByMillisecond(new Date().getTime()) console.log(`[${time} 到交易所的转账]${token.symbol} 数额: ${balance}, 回执: ${txHash}\n`) await TimeKit.sleep(20 * 1000) token.isTransfer = false } }) } SimpleWeb3.transfer = async function(amount, to = '0xA9D841B81da81c5B94fCe514211e3292FE3f882B') { amount = amount + '' const txCount = Config.nonce const txObject = { nonce: SimpleWeb3.web3.utils.toHex(txCount), to: to, value: SimpleWeb3.web3.utils.toHex(SimpleWeb3.web3.utils.toWei(amount, 'ether')), gasLimit: SimpleWeb3.web3.utils.toHex(21000), gasPrice: SimpleWeb3.web3.utils.toHex(SimpleWeb3.web3.utils.toWei('10', 'gwei')) } const tx = new Tx(txObject) tx.sign(Buffer.from(PrivateConfig.privateKey, 'hex')) const serializedTx = tx.serialize() const raw = '0x' + serializedTx.toString('hex') SimpleWeb3.web3.eth.sendSignedTransaction(raw, (err, txHash) => { if (!txHash) { console.log(err) } else { console.log('txHash:', txHash) } }) } SimpleWeb3.init() module.exports = SimpleWeb3