Created
December 8, 2023 15:22
-
-
Save kirugan/1450d2953bedebd6ac54796080281eb2 to your computer and use it in GitHub Desktop.
Sepolia balance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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