-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy path02_transfer.go
75 lines (67 loc) · 1.99 KB
/
02_transfer.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
package main
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/zksync-sdk/zksync2-go/accounts"
"github.com/zksync-sdk/zksync2-go/clients"
"github.com/zksync-sdk/zksync2-go/utils"
"log"
"math/big"
"os"
)
func main() {
var (
PrivateKey1 = os.Getenv("PRIVATE_KEY")
PublicKey2 = "0x81E9D85b65E9CC8618D85A1110e4b1DF63fA30d9"
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(PrivateKey1), &client, nil)
if err != nil {
log.Panic(err)
}
// Show balances before transfer for both accounts
account1Balance, err := wallet.Balance(context.Background(), utils.EthAddress, nil)
if err != nil {
log.Panic(err)
}
account2Balance, err := client.BalanceAt(context.Background(), common.HexToAddress(PublicKey2), nil)
if err != nil {
log.Panic(err)
}
fmt.Println("Account1 balance before transfer: ", account1Balance)
fmt.Println("Account2 balance before transfer: ", account2Balance)
// Perform transfer
tx, err := wallet.Transfer(nil, accounts.TransferTransaction{
To: common.HexToAddress(PublicKey2),
Amount: big.NewInt(1_000_000_000),
Token: utils.EthAddress,
})
if err != nil {
log.Panic(err)
}
fmt.Println("Transaction: ", tx.Hash())
// Wait for transaction to be finalized on L2 network
_, err = client.WaitMined(context.Background(), tx.Hash())
if err != nil {
log.Panic(err)
}
// Show balances after transfer for both accounts
account1Balance, err = wallet.Balance(context.Background(), utils.EthAddress, nil)
if err != nil {
log.Panic(err)
}
account2Balance, err = client.BalanceAt(context.Background(), common.HexToAddress(PublicKey2), nil)
if err != nil {
log.Panic(err)
}
fmt.Println("Account1 balance after transfer: ", account1Balance)
fmt.Println("Account2 balance after transfer: ", account2Balance)
}