Skip to content

Instantly share code, notes, and snippets.

@kirugan
Created December 8, 2023 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirugan/1450d2953bedebd6ac54796080281eb2 to your computer and use it in GitHub Desktop.
Save kirugan/1450d2953bedebd6ac54796080281eb2 to your computer and use it in GitHub Desktop.
Sepolia balance
package main
import (
"context"
"crypto/ecdsa"
"fmt"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"os"
)
var ctx = context.Background()
func main() {
privKey := getKey()
pubKey := privKey.Public()
addr := crypto.PubkeyToAddress(*pubKey.(*ecdsa.PublicKey))
fmt.Printf("Your address is %v\n", addr)
client, err := ethclient.Dial("https://rpc.sepolia.org/")
check(err)
b, err := client.PendingBalanceAt(ctx, addr)
check(err)
fmt.Printf("Balance is %v wei (wei is 10^-18 ETH)\n", b)
}
func getKey() *ecdsa.PrivateKey {
const filename = "./private_key"
privKey, err := crypto.LoadECDSA(filename)
if err != nil {
if os.IsNotExist(err) {
privKey, err = crypto.GenerateKey()
check(err)
err = crypto.SaveECDSA(filename, privKey)
check(err)
} else {
panic(err)
}
}
return privKey
}
func check(err error) {
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment