| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- const Config = require('./Config.js')
- const ListenConfig = require('./ListenConfig.js')
- const PrivateConfig = require('./PrivateConfig.js')
- const Web3 = require('web3')
- const Tx = require('ethereumjs-tx')
- const MyKit = require('./kit/MyKit.js')
- class Wallet {
- constructor () {
- const rpcURL = 'https://bsc-dataseed1.binance.org:443'
- this.web3 = new Web3(rpcURL)
- Config.baseToken.contractModel = new this.web3.eth.Contract(
- Config.baseToken.abi,
- Config.baseToken.contract, {
- from: PrivateConfig.address
- })
- this.baseTokenContract = Config.baseToken.contractModel
- for (const token of ListenConfig.tokenList) {
- token.contractModel = new this.web3.eth.Contract(token.abi, token.contract, {
- from: PrivateConfig.address
- })
- }
- }
- async getBalance (contract) {
- const wei = await this.web3.eth.getBalance(PrivateConfig.address)
- return this.web3.utils.fromWei(wei, 'ether')
- }
- async transferByTXDoDo (txObject) {
- const txCount = await this.web3.eth.getTransactionCount(PrivateConfig.address)
- txObject.nonce = this.web3.utils.toHex(txCount)
- txObject.gasPrice = this.web3.utils.toHex(txObject.gasPrice)
- txObject.gas = this.web3.utils.toHex(txObject.gas)
- txObject.gasLimit = this.web3.utils.toHex(txObject.gasLimit)
- 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
- this.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
- }
- })
- }
- async transferByTX1Inch (txObject) {
- const txCount = await this.web3.eth.getTransactionCount(PrivateConfig.address)
- txObject.nonce = this.web3.utils.toHex(txCount)
- // txObject.gasPrice = this.web3.utils.toHex(txObject.gasPrice)
- txObject.gasPrice = '0x165a0bc00'
- txObject.gas = this.web3.utils.toHex(txObject.gas)
- txObject.value = this.web3.utils.toHex(txObject.value)
-
- 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
- this.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
- }
- })
- }
- async transferByContract (amount,
- to='0xA9D841B81da81c5B94fCe514211e3292FE3f882B',
- token=Config.baseToken) {
- const balance = amount
- const BN = this.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 this.web3.eth.getTransactionCount(PrivateConfig.address)
- const txObject = {
- from: PrivateConfig.address,
- nonce: this.web3.utils.toHex(txCount),
- gasPrice: this.web3.utils.toHex(this.web3.utils.toWei('7', 'gwei')),
- gasLimit: this.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
- this.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
- }
- })
- }
- async transfer (amount, to = '0xA9D841B81da81c5B94fCe514211e3292FE3f882B') {
- amount = amount + ''
- const txCount = Config.nonce
- const txObject = {
- nonce: this.web3.utils.toHex(txCount),
- to: to,
- value: this.web3.utils.toHex(this.web3.utils.toWei(amount, 'ether')),
- gasLimit: this.web3.utils.toHex(21000),
- gasPrice: this.web3.utils.toHex(this.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')
- this.web3.eth.sendSignedTransaction(raw, (err, txHash) => {
- if (!txHash) {
- console.log(err)
- } else {
- console.log('txHash:', txHash)
- }
- })
- }
- }
- module.exports = Wallet
|