使用js将ETH账户的资产打散其他账户web3

2024/9/27 23:02:56

本文主要是介绍使用js将ETH账户的资产打散其他账户web3,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

使用 js 将ETH账户的资产平均分散到其他账户

平时如果你需要把一个账户的eth打散到很多账户,其他账户批量操作的话,就需要一个个的发送,很麻烦,下面这段脚本可以把一个账户的eth打散发送到指定的很多账户

首先安装依赖插件

npm install -save web3@4.0.3
npm install -save @ethereumjs/common@3.2.0
npm install -save sign-tx

如下代码展示如何将0xbf8B5bc7Ea580ca7cEDa5F79F6ef3362134fC695 账户的ETH平均分散到其他账户

const {Web3} = require('web3')
const {Common, Chain, Hardfork} = require("@ethereumjs/common");
const Transaction = require('sign-tx').LegacyTransaction;
const rpcURL = "https://eth-mainnet.public.blastapi.io" // RPC URL  如果地址不能使用了 请去 https://infura.io 注册账号获取免费的
const your_private_key =  Buffer.from('ea9c312161c541758038e374a53147b933d39f504649b82f823285eb0b2ffd6e', 'hex');
const your_wallet_address = '0xbf8B5bc7Ea580ca7cEDa5F79F6ef3362134fC695'
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 dispersion_accounts = [
    "0xA3059b44852dF4c592d7916C19aC1B8EdF839C4C",
    "0x2EE0B3Bb2A0222A9a424c861548e6b8d8fd49f65",
    "0x1f7537d14A8274C2e1F3B522D7025c1F765438FD",
    "0xd27F9cA676d393432722Ae88D9e0cD9152e5Cb41",
    "0x5911d5b71E78261ba0D28f71017C9BF418d1e7a1",
    "0x1a5CA207E3b6a4FAceADb20DfB7B3aAD3B98c0b8"
];

function dispersion_funds() {
    web3.eth.getBalance(your_wallet_address).then(( wei) => {
        // 余额单位从wei转换为ether
        web3.eth.getTransactionCount(your_wallet_address).then((txCount) => {
            let one_amount =  wei-BigInt(10**18) // 预留一个ETH做手续费
            one_amount = one_amount/BigInt(dispersion_accounts.length)
            const txObject = {
                from:           your_wallet_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)
            for(let i=0;i<dispersion_accounts.length;i++){
                txObject['nonce'] = nonce
                txObject['to'] = dispersion_accounts[i]
                const common = Common.custom({ chainId: chainId })
                let tx = Transaction.fromTxData(txObject, { common })
                let signedTx = tx.sign(your_private_key)
                let serializedTx = signedTx.serialize()
                let tes = serializedTx.valueOf()
                let raw = '0x' + uint8ToHex(tes)
                web3.eth.sendSignedTransaction(raw).then(call_back)
                nonce++
            }
        })
    })
}
dispersion_funds()

每天学习一点点,遨游在区块链知识海洋里



这篇关于使用js将ETH账户的资产打散其他账户web3的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程