This repository has been archived by the owner on Nov 19, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 421
/
contract_write_estimate_gas.go
70 lines (57 loc) 路 1.54 KB
/
contract_write_estimate_gas.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
package main
import (
"context"
"crypto/ecdsa"
"fmt"
"log"
"strings"
ethereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
store "github.com/miguelmota/ethereum-development-with-go-book/code/contracts"
)
func main() {
client, err := ethclient.Dial("http://rinkeby.infura.io")
if err != nil {
log.Fatal(err)
}
privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
if err != nil {
log.Fatal(err)
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
log.Fatal(err)
}
address := common.HexToAddress("0x147B8eb97fD247D06C4006D269c90C1908Fb5D54")
key := [32]byte{}
value := [32]byte{}
copy(key[:], []byte("foo"))
copy(value[:], []byte("bar"))
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,
GasPrice: gasPrice,
})
if err != nil {
log.Fatal(err)
}
fmt.Println(estimatedGas)
}