-
Notifications
You must be signed in to change notification settings - Fork 1
/
zkTransfer.js
71 lines (56 loc) · 1.91 KB
/
zkTransfer.js
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
import * as zksync from "zksync";
import * as ethers from "ethers";
import "dotenv/config";
// initialize the zksync provider
const initializeProvider = async () => {
const syncProvider = await zksync.Provider.newHttpProvider(
"https://api.zksync.io/jsrpc"
);
const ethProvider = new ethers.providers.InfuraProvider(
"mainnet",
process.env.PROJECT_ID
);
// get access to ethereum Wallet
const ethWallet = new ethers.Wallet(process.env.PRIVATE_KEY, ethProvider);
// get access to zkSync.signer from ethereum wallet
const zkWallet = await zksync.Wallet.fromEthSigner(ethWallet, syncProvider);
return { zkWallet };
};
// fees to pay: 0.00002 eth
// total eth: 0.00991162248 eth
// to transfer: 0.00889 eth
// addr_to: 0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
const runZKTransfer = async () => {
const { zkWallet } = await initializeProvider();
// set the amount to transfer
const amount = zksync.utils.closestPackableTransactionAmount(
ethers.utils.parseEther("0.0088")
);
// set the fees to be included in the transfer success
const fees = zksync.utils.closestPackableTransactionFee(
ethers.utils.parseEther("0.00002")
);
const transfer = await zkWallet.syncTransfer({
to: "0xbb256f544b8087596e8e6cdd7fe9726cc98cb400",
token: "ETH",
amount,
fees,
});
const txnReceipt = await transfer.awaitReceipt();
console.log("Is txn success: ", txnReceipt.success);
// Retreiving the current (committed) balance of an account
const committedEthBalance = await zkWallet.getBalance("ETH");
console.log("before", committedEthBalance.toNumber());
// Retreiving the finalized balance of an account
const finalizedEthBalance = await zkWallet.getBalance("ETH", "verified");
console.log("after", finalizedEthBalance.toNumber());
};
const run = async () => {
try {
await runZKTransfer();
} catch (error) {
console.log(error);
process.exit(1);
}
};
run();