平时如果你需要把多个账户的eth汇集到一个账户,就需要一个个的发送,很麻烦,下面这段脚本可以把多个账户的eth汇集到一个账户
首先安装依赖插件
npm install -save web3@4.0.3 npm install -save @ethereumjs/common@3.2.0 npm install -save sign-tx
如下代码展示如何将
"0x962Ec4fAb7f283051622BE83f9C64c9DD12Ead73",
“0x5960596b614341af474A5bc9E240636687A5aE4E”,
“0x2B5Ae3C44703a7Bde45Da0a189cd74449c4A06ab”,
“0xA4505b256B44c98f9675614D731408f27D186556”,
“0xa4e43e2168B261d227F6216ca4e495516CF8406c”,
“0x7990CA5C9Ad5F1F579df2e3E24dd90305585D7e7” 账户的ETH平均汇集到0x900a2e6B7831e594CC0ec4eBA2dB044a565D38a4
const {Web3} = require('web3') const {Common, Chain, Hardfork} = require("@ethereumjs/common"); const Transaction = require('sign-tx').LegacyTransaction; const rpcURL = "http://localhost:7545" // RPC URL 如果地址不能使用了 请去 https://infura.io 注册账号获取免费的 const your_private_key = Buffer.from('0xd20278ac90c96d55cfab9cf6114f62f16e9e5f6de89af70d53d502ad10c26bbb', 'hex'); const your_wallet_address = '0x900a2e6B7831e594CC0ec4eBA2dB044a565D38a4' const web3 = new Web3(rpcURL); const chainId = 1///Chain.Mainnet function uint8ToHex(uint8arr) { let hexStr = ""; for (let i = 0; i < uint8arr.length; i++) { let hex = uint8arr[i].toString(16); hex = hex.length === 1 ? "0" + hex : hex; // 需要补0 hexStr += hex; } return hexStr; } const from_accounts_privateKey= [ "7102b8bee3969984ca0c0b774220267223aaa6f277add744100959cc7b8ec969", "ace0b6ad6d0343c8b496aef57ffaba0b0c0199fb4a97f538bf799966fcc5f45b", "0300119a51fe5185229680cd9828d1e91a5d75125ac152efb69950d344de4ff8", "3306db491dfba6055b8639dc65793a4a9857b2994734c01b1b55a1d02fd6f99b", "c41866f8a915fc4b237ebfea89bd16f791236b7c19b616ce2ba9e0869efc7873", "02a9d828665565180be507e049bd990a7891ef781e440c1bc9a999da2c33e7e0" ] const from_accounts_address= [ "0x962Ec4fAb7f283051622BE83f9C64c9DD12Ead73", "0x5960596b614341af474A5bc9E240636687A5aE4E", "0x2B5Ae3C44703a7Bde45Da0a189cd74449c4A06ab", "0xA4505b256B44c98f9675614D731408f27D186556", "0xa4e43e2168B261d227F6216ca4e495516CF8406c", "0x7990CA5C9Ad5F1F579df2e3E24dd90305585D7e7" ] function collect_funds() { for(var i=0;i<from_accounts_address.length;i++){ let index = i var from_address = from_accounts_address[i] web3.eth.getBalance(from_address).then(( wei) => { // 余额单位从wei转换为ether web3.eth.getTransactionCount(from_address).then((txCount) => { let one_amount = wei-BigInt(10**18) // 预留一个ETH做手续费 const txObject = { from: from_address, gasLimit: web3.utils.toHex(21000), gasPrice: web3.utils.toHex(web3.utils.toNumber(20000000000)), value: web3.utils.toHex(one_amount), } const call_back = function (txHash){ console.log('txHash:', txHash.transactionHash) } let nonce = web3.utils.toHex(txCount) txObject['nonce'] = nonce txObject['to'] = your_wallet_address const common = Common.custom({ chainId: chainId }) let tx = Transaction.fromTxData(txObject, { common }) console.log(from_accounts_privateKey[index]) var private_key =Buffer.from(from_accounts_privateKey[index], 'hex'); let signedTx = tx.sign(private_key) let serializedTx = signedTx.serialize() let tes = serializedTx.valueOf() let raw = '0x' + uint8ToHex(tes) web3.eth.sendSignedTransaction(raw).then(call_back) }) }) } } collect_funds()
每天学习一点点,遨游在区块链知识海洋里
本文由博客一文多发平台 OpenWrite 发布!