| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- const Config = require('../../config/config.js')
- const Web3 = require('web3')
- class SimpleWeb3 {}
- SimpleWeb3.init = function() {
- if (SimpleWeb3.web3) return
- SimpleWeb3.web3 = new Web3(Config.WEB3_RPC_URL)
- }
- 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.baseIerc20Token.isSwap = true
- // SimpleWeb3.web3.eth.sendSignedTransaction(raw, async (err, txHash) => {
- // if (!txHash) {
- // console.log(err.toString())
- // txObject.data = undefined
- // console.log(txObject)
- //
- // Config.baseIerc20Token.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.baseIerc20Token.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
|