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

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

composer require web3p/web3.php
composer require blockchainethdev/ethereum-tx

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

<?php
require('vendor/autoload.php');
use Web3\Web3;
use Web3\Utils;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\HttpRequestManager;
use Blockchainethdev\EthereumTx\Transaction;
define('your_private_key','0xea9c312161c541758038e374a53147b933d39f504649b82f823285eb0b2ffd6e');
define('your_wallet_address','0xbf8B5bc7Ea580ca7cEDa5F79F6ef3362134fC695');
/*
 * Ethereum Mainnet : 1
 * BNB Smart Chain Mainnet : 56
 * Arbitrum One : 42161
 * Polygon Mainnet : 137
 * OP Mainnet : 10
 *
 */
define('chainId',1);
$Web3 = new Web3(new HttpProvider(new HttpRequestManager("https://eth-mainnet.public.blastapi.io"))); # 在https://infura.io注册获得免费的rpc地址  示例: https://mainnet.infura.io/v3/{密钥}

$Eth = $Web3->getEth();
$dispersion_accounts = [
    "0xA3059b44852dF4c592d7916C19aC1B8EdF839C4C",
    "0x2EE0B3Bb2A0222A9a424c861548e6b8d8fd49f65",
    "0x1f7537d14A8274C2e1F3B522D7025c1F765438FD",
    "0xd27F9cA676d393432722Ae88D9e0cD9152e5Cb41",
    "0x5911d5b71E78261ba0D28f71017C9BF418d1e7a1",
    "0x1a5CA207E3b6a4FAceADb20DfB7B3aAD3B98c0b8"
];


#将一个账户的所有eth分散到其他账户,留1eth做手续费
function dispersion_funds(){
    global $Eth;
    $Eth->getBalance(your_wallet_address, function ($err, $balance) {
        global $dispersion_accounts;
        global $Eth;
        if ($err == null) {
            $all_send = $balance->toString()-(10**18);//留1eth做手续费
            $send_one = $all_send/count($dispersion_accounts);
            $Eth->getTransactionCount(your_wallet_address, function ($err, $transactionCount)use($Eth,$send_one) {
                if ($err !== null) {
                    return print_r($err);
                }
                $Eth->gasPrice(function ($err, $gasPrice)use($transactionCount,$Eth,$send_one) {
                    global $dispersion_accounts;
                    if ($err !== null) {
                        return print_r($err);
                    }
                    $nonce = $transactionCount->toString();
                    foreach ($dispersion_accounts as $to_address){
                        $transaction = new Transaction([
                            'nonce' => Utils::toHex(intval($nonce),true),
                            'to' => $to_address,
                            'gas' => Utils::toHex(21000,true),
                            'gasPrice' => Utils::toHex(intval($gasPrice->toString()),true),
                            'value' =>Utils::toHex(number_format($send_one, 0, '', ''),true),
                            'chainId' => chainId,
                        ]);
                        $sign_data = $transaction->sign(your_private_key);
                        $Eth->sendRawTransaction("0x".$sign_data, function ($err, $transaction) {
                            if ($err !== null) {
                                print_r($err);
                            }
                            echo 'tx id: ' . $transaction . PHP_EOL;
                        });
                        $nonce++;
                    }
                });
            });
        }
    });
}
dispersion_funds();

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


明公
1 声望0 粉丝