Skip to main content

Go – ethclient

Overview

The ethclient package built to enable remote communication directly to an ethereum mode, including RPC access over HTTP.

Resources

Documentation

Basic Usage

package main

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

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

func main() {
url :=
client, err := ethclient.Dial("https://<CHAIN_PREFIX>.rpc.grove.city/v1/<APP_ID>")
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)

blockNumber := big.NewInt(5532993)
balanceAt, err := client.BalanceAt(context.Background(), account, blockNumber)
if err != nil {
log.Fatal(err)
}
fmt.Println(balanceAt)

fbalance := new(big.Float)
fbalance.SetString(balanceAt.String())
ethValue := new(big.Float).Quo(fbalance, big.NewFloat(math.Pow10(18)))
fmt.Println(ethValue) // 25.729324269165216041

pendingBalance, err := client.PendingBalanceAt(context.Background(), account)
fmt.Println(pendingBalance)
}