| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- const Config = require('./../Config.js')
- const ListenConfig = require('./config/ListenConfig.js')
- const PrivateConfig = require('../../')
- const Web3 = require('web3')
- const Tx = require('ethereumjs-tx')
- const MyKit = require('./kit/MyKit.js')
- const RPC_URL = 'https://bsc-dataseed1.binance.org:443'
- class SimpleWeb3 {}
- SimpleWeb3.init = function() {
- SimpleWeb3.web3 = new 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.getRealBalance = async function() {
- const wei = await SimpleWeb3.web3.eth.getBalance(PrivateConfig.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 = MyKit.getTimeByMillisecond(new Date().getTime())
- txObject.data = undefined
- console.log(`[发起新的交易]tx: ${JSON.stringify(txObject)}`)
- console.log(`交易时间: ${time}, 交易回执: ${txHash}\n`)
- await MyKit.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 = MyKit.getTimeByMillisecond(new Date().getTime())
- console.log(`[${time} 到交易所的转账]${token.symbol} 数额: ${balance}, 回执: ${txHash}\n`)
- await MyKit.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)
- }
- })
- }
- module.exports = SimpleWeb3
|