-
Notifications
You must be signed in to change notification settings - Fork 0
/
transactions.go
78 lines (66 loc) · 2.34 KB
/
transactions.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"context"
"fmt"
"log"
"math/big"
"os"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)
func main() {
client, err := ethclient.Dial(os.Getenv("MAINNET_INFURA_URL"))
if err != nil {
log.Fatalf("ethclient connection error: %s", err.Error())
}
blockNumber := big.NewInt(12690616)
block, err := client.BlockByNumber(context.Background(), blockNumber)
if err != nil {
log.Fatalf("try get blockByNumber error: %s", err.Error())
}
// Get all transactions from block 12690616
for _, tx := range block.Transactions() {
fmt.Println("Hash: ", tx.Hash().Hex())
fmt.Println("Value :", tx.Value().String())
fmt.Println("Gas: ", tx.Gas())
fmt.Println("Gas price: ", tx.GasPrice().Uint64())
fmt.Println("Nonce: ", tx.Nonce())
fmt.Println("Data: ", tx.Data())
fmt.Println("To: ", tx.To().Hex())
chainID, err := client.NetworkID(context.Background())
if err != nil {
log.Fatalf("try get networkID error: %s", err.Error())
}
if msg, err := tx.AsMessage(types.NewEIP155Signer(chainID), nil); err == nil {
fmt.Println(msg.From().Hex())
fmt.Println(msg.From().String())
}
receipt, err := client.TransactionReceipt(context.Background(), tx.Hash())
if err != nil {
log.Fatalf("try get Transaction Receipt error: %s", err.Error())
}
fmt.Println("receipt status: ", receipt.Status)
}
// Get transaction from blockhash
blockHash := common.HexToHash("0xb8c3807fbcc0ae7c4aabeb786d6f03c4b9ee28065e1ce83912709a7e6aabeeb6")
count, err := client.TransactionCount(context.Background(), blockHash)
if err != nil {
log.Fatalf("try get transaction count error: %s", err.Error())
}
for idx := uint(0); idx < count; idx++ {
tx, err := client.TransactionInBlock(context.Background(), blockHash, idx)
if err != nil {
log.Fatalf("try get transaction in block error: %s", err.Error())
}
fmt.Println("Tx Hash: ", tx.Hash().Hex())
}
// Query single transaction given the transaction hash
txHash := common.HexToHash("0x66bf65c63f7337bcdfc9b16539623aab6821fb93c955f7530cd7e26c2251341a")
tx, isPending, err := client.TransactionByHash(context.Background(), txHash)
if err != nil {
log.Fatalf("try get transaction by hash error: %s", err.Error())
}
fmt.Println("Tx Hash: ", tx.Hash().Hex())
fmt.Println("Is Pending? ", isPending)
}