-
Notifications
You must be signed in to change notification settings - Fork 0
/
contract_read_write.go
138 lines (123 loc) · 3.46 KB
/
contract_read_write.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"context"
"crypto/ecdsa"
"encoding/hex"
"fmt"
"log"
"math/big"
"os"
"strings"
"time"
ethereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
store "github.com/CryptoRbtree/goeth-client/contract-store"
)
func contractReadWrite() {
var (
ctx = context.Background()
url = "https://eth-goerli.g.alchemy.com/v2/" + os.Getenv("ALCHEMY_ID")
client, err = ethclient.DialContext(ctx, url)
)
if err != nil {
log.Fatal(err)
}
// 1. load contract
address := common.HexToAddress("0xd3047d5bbcbcfe4256f9c1668c57b3d875c4adea")
instance, err := store.NewStore(address, client)
if err != nil {
log.Fatal(err)
}
// 2. read contract
version, err := instance.Version(nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("store contract version =", version)
// 3. write contract
privateKey, err := crypto.HexToECDSA(os.Getenv("ACCOUNT_KEY"))
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("error casting public key to ECDSA")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
log.Fatal(err)
}
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
log.Fatal(err)
}
gasTipCap, err := client.SuggestGasTipCap(context.Background())
if err != nil {
log.Fatal(err)
}
chainID, err := client.NetworkID(context.Background())
if err != nil {
log.Fatal(err)
}
auth, err := bind.NewKeyedTransactorWithChainID(privateKey, chainID)
if err != nil {
log.Fatal(err)
}
auth.Nonce = big.NewInt(int64(nonce))
auth.Value = big.NewInt(0) // in wei
//auth.GasPrice = big.NewInt(1e9) // only for legacy transactions
auth.GasFeeCap = gasPrice
auth.GasTipCap = gasTipCap
key := [32]byte{}
value := [32]byte{}
copy(key[:], []byte("foo"))
copy(value[:], []byte("bar"))
if err != nil {
log.Fatal(err)
}
// estimate gas
parsed, err := abi.JSON(strings.NewReader(store.StoreABI))
if err != nil {
log.Fatal(err)
}
encodedData, err := parsed.Pack("setItem", key, value)
if err != nil {
log.Fatal(err)
}
estimatedGas, err := client.EstimateGas(context.Background(), ethereum.CallMsg{
From: fromAddress,
To: &address,
Data: encodedData,
GasFeeCap: gasPrice,
GasTipCap: gasTipCap,
})
if err != nil {
log.Fatal(err)
return
}
auth.GasLimit = estimatedGas // in units
tx, err := instance.SetItem(auth, key, value)
txHash := common.HexToHash(tx.Hash().Hex())
tx, isPending, err := client.TransactionByHash(context.Background(), txHash)
fmt.Println("contract address =", address.Hex())
fmt.Println("transaction hash:", tx.Hash().Hex())
fmt.Println("transaction gas limit:", tx.Gas())
fmt.Println("transaction fee cap per gas:", tx.GasFeeCap())
fmt.Println("transaction tip cap per gas:", tx.GasTipCap())
fmt.Println("transaction data:", hex.EncodeToString(tx.Data()))
fmt.Println("transaction to:", tx.To().Hex())
fmt.Println("isPending:", isPending)
for isPending {
time.Sleep(time.Second * 5)
fmt.Println("pending...")
_, isPending, _ = client.TransactionByHash(context.Background(), txHash)
}
result, err := instance.Items(nil, key)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(result[:])) // "bar"
}