-
Notifications
You must be signed in to change notification settings - Fork 17
/
15_deploy_token_create2.go
95 lines (81 loc) · 2.08 KB
/
15_deploy_token_create2.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
package main
import (
"context"
"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/token"
)
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)
}
tokenAbi, err := token.TokenMetaData.GetAbi()
if err != nil {
log.Panic(err)
}
constructor, err := tokenAbi.Pack("", "Crown", "Crown", uint8(18))
if err != nil {
log.Panic(err)
}
//Deploy smart contract
hash, err := wallet.Deploy(nil, accounts.Create2Transaction{
Bytecode: common.FromHex(token.TokenMetaData.Bin),
Calldata: constructor})
if err != nil {
log.Panic(err)
}
fmt.Println("Transaction: ", hash)
// Wait unit transaction is finalized
receipt, err := client.WaitMined(context.Background(), hash)
if err != nil {
log.Panic(err)
}
tokenAddress := receipt.ContractAddress
fmt.Println("Token address", tokenAddress.String())
// Create instance of token contract
tokenContract, err := token.NewToken(tokenAddress, client)
if err != nil {
log.Panic(err)
}
symbol, err := tokenContract.Symbol(nil)
if err != nil {
log.Panic(err)
}
fmt.Println("Symbol: ", symbol)
decimals, err := tokenContract.Decimals(nil)
if err != nil {
log.Panic(err)
}
fmt.Println("Decimals: ", decimals)
opts, err := bind.NewKeyedTransactorWithChainID(wallet.Signer().PrivateKey(), wallet.Signer().Domain().ChainId)
if err != nil {
log.Panic(err)
}
mint, err := tokenContract.Mint(opts, wallet.Address(), big.NewInt(5))
if err != nil {
log.Panic(err)
}
// Wait for transaction to be finalized
_, err = client.WaitMined(context.Background(), mint.Hash())
if err != nil {
log.Panic(err)
}
}