-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
48 lines (41 loc) · 1.2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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://cloudflare-eth.com")
if err != nil {
log.Fatal(err)
}
// 读取一个账户的余额
account := common.HexToAddress("0xeF20B71f4C9a9036450E01D5361917D6C97262c7")
balance, err := client.BalanceAt(context.Background(), account, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(balance) // 25893180161173005034
// 传区块号能让您读取该区块时的账户余额
blockNumber := big.NewInt(5532993)
balanceAt, err := client.BalanceAt(context.Background(), account, blockNumber)
if err != nil {
log.Fatal(err)
}
fmt.Println(balanceAt) // 25729324269165216042
// 以太坊单位转换 wei/10^18
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)
if err != nil {
log.Fatal(err)
}
fmt.Println(pendingBalance) // 25729324269165216042
}