为了账号安全,请及时绑定邮箱和手机立即绑定

使用PHP 将ETH账户的资产汇集到一个账户

标签:
PHP

使用PHP 将ETH账户的资产汇集到一个账户

平时如果你需要把多个账户的eth汇集到一个账户,就需要一个个的发送,很麻烦,下面这段脚本可以把多个账户的eth汇集到一个账户
首先安装依赖插件

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

如下代码展示

<?php
require('../vendor/autoload.php');
use Web3\Web3;
use Web3\Utils;
//use Web3\Providers\HttpProvider;
//use Web3\RequestManagers\HttpRequestManager;
use Blockchainethdev\EthereumTx\Transaction;
/*
 * 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("http://localhost:7545",10))); # 在https://infura.io注册获得免费的rpc地址  示例: https://mainnet.infura.io/v3/{密钥}
$Eth = $Web3->getEth();
$toaddress = "0xbf8B5bc7Ea580ca7cEDa5F79F6ef3362134fC695";
$from_accounts = [
    "0xA3059b44852dF4c592d7916C19aC1B8EdF839C4C"=> "a5627de24f88d2e8586d314203acd4c8baf5bc710a76fdf000eeb4f54f692254",
    "0x2EE0B3Bb2A0222A9a424c861548e6b8d8fd49f65"=> "023bca35e1034ca3b073a5540fb036c3d1db13a599475afa603ccda5dbbdecea",
    "0x1f7537d14A8274C2e1F3B522D7025c1F765438FD"=> "fca4097f57fa73f18aec6ef04024fe27e277ba5de814520938ac3e17facc7717",
    "0xd27F9cA676d393432722Ae88D9e0cD9152e5Cb41"=> "7b96a28cd9487c2582197cccf4bb8f6a14a56d145f6c8895b3778a8caabc3eef",
    "0x5911d5b71E78261ba0D28f71017C9BF418d1e7a1"=> "671fc96178e91d2d20a341794ff7d9ba9595e4f623facdbe7b5d220c9485a312",
    "0x1a5CA207E3b6a4FAceADb20DfB7B3aAD3B98c0b8"=> "8ef1c6253183c968c5a1c85fc84a73b55de81d1ae8cc4aee761560ade71b6908"
];
#将一个账户的所有eth分散到其他账户,留1eth做手续费
function collect_funds(){
    global $Eth;
    global $from_accounts;
    foreach ($from_accounts as $address =>$privatekey){
        $Eth->getBalance($address, function ($err, $balance) use($address,$privatekey) {
            global $Eth;
            if ($err == null) {
                $all_send = $balance->toString()-(10**18);//留1eth做手续费
                print_r($all_send);
                $Eth->getTransactionCount($address, function ($err, $transactionCount)use($Eth,$all_send,$privatekey) {
                    if ($err !== null) {
                        return print_r($err);
                    }
                    $Eth->gasPrice(function ($err, $gasPrice)use($transactionCount,$Eth,$all_send,$privatekey) {
                        global $toaddress;
                        if ($err !== null) {
                            return print_r($err);
                        }
                        $nonce = $transactionCount->toString();
                        $transaction = new Transaction([
                            'nonce' => Utils::toHex(intval($nonce),true),
                            'to' => $toaddress,
                            'gas' => Utils::toHex(21000,true),
                            'gasPrice' => Utils::toHex(intval($gasPrice->toString()),true),
                            'value' =>Utils::toHex(number_format($all_send, 0, '', ''),true),
                            'chainId' => chainId,
                        ]);
                        $sign_data = $transaction->sign($privatekey);
                        $Eth->sendRawTransaction("0x".$sign_data, function ($err, $transaction) {
                            if ($err !== null) {
                                print_r($err);
                            }
                            echo 'tx id: ' . $transaction . PHP_EOL;
                        });
                    });
                });
            }
        });
    }
}
collect_funds();


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

本文由博客一文多发平台 OpenWrite 发布!

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消