-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccountBalance.go
More file actions
53 lines (43 loc) · 1.27 KB
/
accountBalance.go
File metadata and controls
53 lines (43 loc) · 1.27 KB
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
49
50
51
52
53
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("http://127.0.0.1:8545/")
if err != nil {
log.Fatal(err)
}
account := common.HexToAddress("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266")
balance, err := client.BalanceAt(context.Background(), account, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(balance) // 25893180161173005034
// 获取最新区块上的 nonce 值
nonce, err := client.NonceAt(context.Background(), account, nil)
if err != nil {
log.Fatal(err)
}
// 打印 nonce 值
fmt.Printf("Nonce value: %d\n", nonce)
blockNumber := big.NewInt(37)
balanceAt, err := client.BalanceAt(context.Background(), account, blockNumber)
if err != nil {
log.Fatal(err)
}
fmt.Println(balanceAt) // 25729324269165216042
fbalance := new(big.Float)
fbalance.SetString(balanceAt.String())
ethValue := new(big.Float).Quo(fbalance, big.NewFloat(math.Pow10(18)))
fmt.Println(ethValue) // 25.729324269165216041
// PendingBalanceAt 获取的是挂起的余额
pendingBalance, err := client.PendingBalanceAt(context.Background(), account)
fmt.Println(pendingBalance) // 25729324269165216042
}