Skip to content

Instantly share code, notes, and snippets.

@futurechimp
Last active April 1, 2019 12:34
Show Gist options
  • Save futurechimp/b7c3dc43b6f5dbbcd8d7334c3723d18c to your computer and use it in GitHub Desktop.
Save futurechimp/b7c3dc43b6f5dbbcd8d7334c3723d18c to your computer and use it in GitHub Desktop.
Grab an account balance from the Ethereum blockchain
package main
import (
"context"
"fmt"
"log"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)
func main() {
// Infura is probably not super-secure but good enough for this test,
// it mimics a fullnode's RPC interface. Alternately we could run an
// Ethereum full node and connect to that.
conn, err := ethclient.Dial("https://mainnet.infura.io")
if err != nil {
log.Fatalf("Whoops something went wrong connecting to infura: %s", err)
}
// An address pulled randomly off Etherscan
addr := "0xc9d2e0a977983a9f09d9c2452b0d0aab42055773"
account := common.HexToAddress(addr)
balance, err := conn.BalanceAt(context.Background(), account, nil)
if err != nil {
log.Fatalf("Error getting account balance: %s", err)
}
fmt.Println(balance)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment