-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
120 lines (111 loc) · 4.26 KB
/
utils.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
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
// Step 1. Get Providers for zkSync and Ethereum
async function getZkSyncProvider(zksync, networkName) {
let zkSyncProvider
try {
zkSyncProvider = await zksync.getDefaultProvider(networkName)
} catch (error) {
console.log('Unable to connect to zkSync.')
console.log(error)
}
return zkSyncProvider
}
async function getEthereumProvider(ethers, networkName) {
let ethersProvider
try {
// eslint-disable-next-line new-cap
ethersProvider = new ethers.getDefaultProvider(networkName)
} catch (error) {
console.log('Could not connect to Rinkeby')
console.log(error)
}
return ethersProvider
}
// Step 2. Initialize your wallet on zkSync
async function initAccount(rinkebyWallet, zkSyncProvider, zksync) {
const zkSyncWallet = await zksync.Wallet.fromEthSigner(rinkebyWallet, zkSyncProvider)
return zkSyncWallet
}
// Step 3. Register an account on zkSync
async function registerAccount(wallet) {
console.log(`Registering the ${wallet.address()} account on zkSync`)
if (!await wallet.isSigningKeySet()) {
if (await wallet.getAccountId() === undefined) {
throw new Error('Unknown account')
}
const changePubkey = await wallet.setSigningKey()
await changePubkey.awaitReceipt()
}
}
// Step 4. Deposit tokens from ethereum to zkSync
async function depositToZkSync(zkSyncWallet, token, amountToDeposit, ethers) {
const deposit = await zkSyncWallet.depositToSyncFromEthereum({
depositTo: zkSyncWallet.address(),
token: token,
amount: ethers.utils.parseEther(amountToDeposit)
})
try {
await deposit.awaitReceipt()
} catch (error) {
console.log('Error while awaiting confirmation from the zkSync operators.')
console.log(error)
}
}
// Step 5. Transfer from 1 account to another on zkSync
async function transfer(from, toAddress, amountToTransfer, transferFee, token, zksync, ethers) {
const closestPackableAmount = zksync.utils.closestPackableTransactionAmount(ethers.utils.parseEther(amountToTransfer))
const closestPackableFee = zksync.utils.closestPackableTransactionFee(ethers.utils.parseEther(transferFee))
const transfer = await from.syncTransfer({
to: toAddress,
token: token,
amount: closestPackableAmount,
fee: closestPackableFee
})
const transferReceipt = await transfer.awaitReceipt()
console.log('Got transfer receipt.')
console.log(transferReceipt)
}
// Step 6. Get Fee estimation for a transaction
async function getFee(transactionType, address, token, zkSyncProvider, ethers) {
const feeInWei = await zkSyncProvider.getTransactionFee(transactionType, address, token)
return ethers.utils.formatEther(feeInWei.totalFee.toString())
}
// Step 7. Withdraw your balance from zkSync
async function withdrawToEthereum(wallet, amountToWithdraw, withdrawalFee, token, zksync, ethers) {
const closestPackableAmount = zksync.utils.closestPackableTransactionAmount(ethers.utils.parseEther(amountToWithdraw))
const closestPackableFee = zksync.utils.closestPackableTransactionFee(ethers.utils.parseEther(withdrawalFee))
const withdraw = await wallet.withdrawFromSyncToEthereum({
ethAddress: wallet.address(),
token: token,
amount: closestPackableAmount,
fee: closestPackableFee
})
await withdraw.awaitVerifyReceipt()
console.log('ZKP verification is complete')
}
// Step 8. Show zkSync balances
async function displayZkSyncBalance(wallet, ethers) {
const state = await wallet.getAccountState()
if (state.committed.balances.ETH) {
console.log(`Commited ETH balance for ${wallet.address()}: ${ethers.utils.formatEther(state.committed.balances.ETH)}`)
}
else {
console.log(`Commited ETH balance for ${wallet.address()}: 0`)
}
if (state.verified.balances.ETH) {
console.log(`Verified ETH balance for ${wallet.address()}: ${ethers.utils.formatEther(state.verified.balances.ETH)}`)
}
else {
console.log(`Verified ETH balance for ${wallet.address()}: 0`)
}
}
module.exports = {
getZkSyncProvider,
getEthereumProvider,
depositToZkSync,
registerAccount,
displayZkSyncBalance,
transfer,
withdrawToEthereum,
getFee,
initAccount
}