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

首先安装依赖插件

go mod init test
#复制下面代码放入 test.go 然后执行:
go mod tidy

下面是一段golang代码展示汇集eth的示例,请把私钥等配置成自己的

package main

import (
    "encoding/hex"
    "fmt"
    "github.com/yellomoon/web3tool"
    "github.com/yellomoon/web3tool/jsonrpc"
    "github.com/yellomoon/web3tool/wallet"
    "math"
    "math/big"
    "sync"
)

var RpcClient *jsonrpc.Client
var mut sync.Mutex

// var rpcUrl = "https://eth-mainnet.public.blastapi.io" // RPC URL  如果地址不能使用了 请去 https://infura.io 注册账号获取免费的
var rpcUrl = "http://localhost:7545"

func getClient() *jsonrpc.Client {
    mut.Lock()
    defer mut.Unlock()
    var err error
    if RpcClient == nil {
        RpcClient, err = jsonrpc.NewClient(rpcUrl)
        if err != nil {
            panic(err)
        }
    }
    return RpcClient
}

/*
获取指定账户的eth余额
address: 指定账户地址

:return:账户余额
*/
func getEthBalance(address string) *big.Int {
    client := getClient()
    number, err := client.Eth().BlockNumber()
    if err != nil {
        panic(err)
    }
    balance, e := client.Eth().GetBalance(web3tool.HexToAddress(address), web3tool.BlockNumber(number))
    if e != nil {
        panic(e)
    }
    return balance
}

var fromAccounts = map[string]string{
    "0xA3059b44852dF4c592d7916C19aC1B8EdF839C4C": "a5627de24f88d2e8586d314203acd4c8baf5bc710a76fdf000eeb4f54f692254",
    "0x2EE0B3Bb2A0222A9a424c861548e6b8d8fd49f65": "023bca35e1034ca3b073a5540fb036c3d1db13a599475afa603ccda5dbbdecea",
    "0x1f7537d14A8274C2e1F3B522D7025c1F765438FD": "fca4097f57fa73f18aec6ef04024fe27e277ba5de814520938ac3e17facc7717",
    "0xd27F9cA676d393432722Ae88D9e0cD9152e5Cb41": "7b96a28cd9487c2582197cccf4bb8f6a14a56d145f6c8895b3778a8caabc3eef",
    "0x5911d5b71E78261ba0D28f71017C9BF418d1e7a1": "671fc96178e91d2d20a341794ff7d9ba9595e4f623facdbe7b5d220c9485a312",
    "0x1a5CA207E3b6a4FAceADb20DfB7B3aAD3B98c0b8": "8ef1c6253183c968c5a1c85fc84a73b55de81d1ae8cc4aee761560ade71b6908",
}
var to_account = "0xbf8B5bc7Ea580ca7cEDa5F79F6ef3362134fC695"

func collect_funds() {
    for key, value := range fromAccounts {
        balance := getEthBalance(key)
        number, err := getClient().Eth().BlockNumber()
        if err != nil {
            panic(err)
        }
        nonce, _ := getClient().Eth().GetNonce(web3tool.HexToAddress(key), web3tool.BlockNumber(number))
        fmt.Printf("balance: %s", balance.String())
        to := web3tool.HexToAddress(to_account)
        oneAmount := new(big.Int).Sub(balance, big.NewInt(int64(math.Pow10(18))))
        txn := &web3tool.Transaction{
            To:       &to,
            Value:    oneAmount,
            GasPrice: 20000000000,
            Gas:      21000,
            Nonce:    nonce,
        }
        privateByte, _ := hex.DecodeString(value)
        privateKey, _ := wallet.NewWalletFromPrivKey(privateByte)
        //signer := wallet.NewEIP155Signer(1) //主网 chainID是1
        signer := wallet.NewEIP155Signer(1337)
        txn, _ = signer.SignTx(txn, privateKey)
        data, _ := txn.MarshalRLPTo(nil)
        hash, err := getClient().Eth().SendRawTransaction(data)
        if err != nil {
            fmt.Printf("%v", err)
        }
        fmt.Printf("%s\n", hash.String())
    }
}
func main() {
    collect_funds()
}


明公
1 声望0 粉丝