miguelmota / go-ethereum-hdwallet

Ethereum HD Wallet derivations in Go (golang)
https://github.com/miguelmota/go-ethereum-hdwallet
MIT License
498 stars 273 forks source link

how get balance from wallet ? #18

Closed Phuong39 closed 2 years ago

Phuong39 commented 3 years ago

how get balance from wallet ?

miguelmota commented 2 years ago

Use the ethclient available from go-ethereum for reading data from chain or for doing any chain interactions, since reading balances is out-of-scope for this package.

Example

package main

import (
    "context"
    "fmt"
    "log"
    "math"
    "math/big"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/ethclient"
)

func main() {
    client, err := ethclient.Dial("https://mainnet.infura.io")
    if err != nil {
        log.Fatal(err)
    }

    account := common.HexToAddress("0x71c7656ec7ab88b098defb751b7401b5f6d8976f")
    balance, err := client.BalanceAt(context.Background(), account, nil)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(balance) // 25893860171173005034
}

Hope this helps!