-
Notifications
You must be signed in to change notification settings - Fork 17
/
12_deploy_create2_with_constructor.go
121 lines (104 loc) · 2.96 KB
/
12_deploy_create2_with_constructor.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
package main
import (
"context"
"crypto/rand"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/zksync-sdk/zksync2-go/accounts"
"github.com/zksync-sdk/zksync2-go/clients"
"log"
"math/big"
"os"
"zksync2-examples/contracts/incrementer"
)
func main() {
var (
PrivateKey = os.Getenv("PRIVATE_KEY")
ZkSyncEraProvider = "https://sepolia.era.zksync.dev"
)
// Connect to zkSync network
client, err := clients.Dial(ZkSyncEraProvider)
if err != nil {
log.Panic(err)
}
defer client.Close()
// Create wallet
wallet, err := accounts.NewWallet(common.Hex2Bytes(PrivateKey), &client, nil)
if err != nil {
log.Panic(err)
}
// Read smart contract bytecode
bytecode, err := os.ReadFile("../solidity/incrementer/build/Incrementer.zbin")
if err != nil {
log.Panic(err)
}
// Get ABI
abi, err := incrementer.IncrementerMetaData.GetAbi()
if err != nil {
log.Panic(err)
}
// Encode constructor arguments
constructor, err := abi.Pack("", big.NewInt(2))
if err != nil {
log.Panicf("error while encoding constructor arguments: %s", err)
}
// Generate salt
salt := make([]byte, 32)
_, err = rand.Read(salt)
if err != nil {
log.Panicf("error while generating salt: %s", err)
}
// Deploy smart contract
hash, err := wallet.Deploy(nil, accounts.Create2Transaction{
Bytecode: bytecode,
Calldata: constructor,
Salt: salt,
})
if err != nil {
log.Panic(err)
// When contract is deployed twice without salt the following error occurs:
// panic: failed to EstimateGasL2: failed to query eth_estimateGas: execution reverted: Code hash is non-zero
}
fmt.Println("Transaction: ", hash)
// Wait unit transaction is finalized
receipt, err := client.WaitMined(context.Background(), hash)
if err != nil {
log.Panic(err)
}
contractAddress := receipt.ContractAddress
fmt.Println("Smart contract address: ", contractAddress.String())
// INTERACT WITH SMART CONTRACT
// Create instance of Incrementer contract
incrementerContract, err := incrementer.NewIncrementer(contractAddress, client)
if err != nil {
log.Panic(err)
}
// Execute Get method
value, err := incrementerContract.Get(nil)
if err != nil {
log.Panic(err)
}
fmt.Println("Value before Increment method execution: ", value)
// Start configuring transaction parameters
opts, err := bind.NewKeyedTransactorWithChainID(wallet.Signer().PrivateKey(), wallet.Signer().Domain().ChainId)
if err != nil {
log.Panic(err)
}
// Execute Set method from storage smart contract with configured transaction parameters
tx, err := incrementerContract.Increment(opts)
if err != nil {
log.Panic(err)
}
// Wait for transaction to be finalized
_, err = client.WaitMined(context.Background(), tx.Hash())
if err != nil {
log.Panic(err)
}
// Execute Get method again to check if state is changed
value, err = incrementerContract.Get(nil)
if err != nil {
log.Panic(err)
}
fmt.Println("Value after Increment method execution: ", value)
}