Skip to content

Latest commit

 

History

History
234 lines (202 loc) · 10 KB

13.md

File metadata and controls

234 lines (202 loc) · 10 KB
title actions requireLogin material
Register an account
checkAnswer
hints
false
editor
language startingCode answer
JavaScript
src/alice.js src/bob.js src/utils.js
(async () => { const ethers = require('ethers') const zksync = require('zksync') const utils = require('./utils') const token = 'ETH' const amountToDeposit = '0.05' const amountToTransfer = '0.02' const amountToWithdraw = '0.002' const zkSyncProvider = await utils.getZkSyncProvider(zksync, process.env.NETWORK_NAME) const ethersProvider = await utils.getEthereumProvider(ethers, process.env.NETWORK_NAME) console.log('Creating a new Rinkeby wallet for Alice') const aliceRinkebyWallet = new ethers.Wallet(process.env.ALICE_PRIVATE_KEY, ethersProvider) // Account #78 console.log(`Alice's Rinkeby address is: ${aliceRinkebyWallet.address}`) const aliceInitialRinkebyBalance = await aliceRinkebyWallet.getBalance() console.log(`Alice's initial balance on Rinkeby is: ${ethers.utils.formatEther(aliceInitialRinkebyBalance)}`) console.log('Creating a zkSync wallet for Alice') const aliceZkSyncWallet = await utils.initAccount(aliceRinkebyWallet, zkSyncProvider, zksync) console.log('Depositing') // Start here })()
(async () => { const ethers = require('ethers') const zksync = require('zksync') const utils = require('./utils') const SLEEP_INTERVAL = process.env.SLEEP_INTERVAL || 5000 const zkSyncProvider = await utils.getZkSyncProvider(zksync, process.env.NETWORK_NAME) const ethersProvider = await utils.getEthereumProvider(ethers, process.env.NETWORK_NAME) const bobRinkebyWallet = new ethers.Wallet(process.env.BOB_PRIVATE_KEY, ethersProvider) console.log(`Bob's Rinkeby address is: ${bobRinkebyWallet.address}`) console.log(`Bob's initial balance on Rinkeby is: ${ethers.utils.formatEther(await bobRinkebyWallet.getBalance())}`) const bobZkSyncWallet = await utils.initAccount(bobRinkebyWallet, zkSyncProvider, zksync) process.on('SIGINT', () => { console.log('Disconnecting') // Disconnect process.exit() }) setInterval(async () => { await utils.displayZkSyncBalance(bobZkSyncWallet, ethers) console.log('---') }, SLEEP_INTERVAL) })()
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 } async function initAccount (rinkebyWallet, zkSyncProvider, zksync) { const zkSyncWallet = await zksync.Wallet.fromEthSigner(rinkebyWallet, zkSyncProvider) return zkSyncWallet } 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() } console.log(`Account ${wallet.address()} registered`) } 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) } } 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) } async function getFee(transactionType, address, token, zkSyncProvider, ethers) { const feeInWei = await zkSyncProvider.getTransactionFee(transactionType, address, token) return ethers.utils.formatEther(feeInWei.totalFee.toString()) } 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') } 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 }
(async () => { const ethers = require('ethers') const zksync = require('zksync') const utils = require('./utils') const token = 'ETH' const amountToDeposit = '0.05' const amountToTransfer = '0.02' const amountToWithdraw = '0.002' const zkSyncProvider = await utils.getZkSyncProvider(zksync, process.env.NETWORK_NAME) const ethersProvider = await utils.getEthereumProvider(ethers, process.env.NETWORK_NAME) console.log('Creating a new Rinkeby wallet for Alice') const aliceRinkebyWallet = new ethers.Wallet(process.env.ALICE_PRIVATE_KEY, ethersProvider) // Account #78 console.log(`Alice's Rinkeby address is: ${aliceRinkebyWallet.address}`) const aliceInitialRinkebyBalance = await aliceRinkebyWallet.getBalance() console.log(`Alice's initial balance on Rinkeby is: ${ethers.utils.formatEther(aliceInitialRinkebyBalance)}`) console.log('Creating a zkSync wallet for Alice') const aliceZkSyncWallet = await utils.initAccount(aliceRinkebyWallet, zkSyncProvider, zksync) console.log('Depositing') await utils.depositToZkSync(aliceZkSyncWallet, token, amountToDeposit, ethers) await utils.displayZkSyncBalance(aliceZkSyncWallet, ethers) await utils.registerAccount(aliceZkSyncWallet) })()

Having coded the shopkeeper application, the next thing on your list should be the application Alice will use.

Take a look at the screen to the right. To get everything ready for you, we've:

  • Created a new JavaScript file called alice.js
  • Imported a few prerequisites
  • Defined the following const variables:
    const token = 'ETH'
    const amountToDeposit = '0.05'
    const amountToTransfer = '0.02'
    const amountToWithdraw = '0.002'
  • Filled in the code that connects your app to Rinkeby and zkSync.

Here's what we'll implement in the next few chapters:

  1. Alice will deposit some ETH to her zkSync account.
  2. Alice will register her public key with zkSync so she can transfer assets via zkSync.
  3. Alice will make a payment to Bob via zkSync.
  4. Alice will withdraw her remaining ETH from zkSync to Ethereum.

Let's take a stab at it.

Put it to the test

  1. Add a line of code that calls the utils.depositToZkSync function. It's an async function that takes the following parameters: aliceZkSyncWallet, token, amountToDeposit, ethers.
  2. To display Alice's balance on zkSync, invoke the await utils.displayZkSyncBalance function, passing it aliceZkSyncWallet and ethers as parameters. Don't forget it's an async function!
  3. Alice won't be able to make a payment unless she gets her public key registered on zkSync. Let's do that by calling the utils.registerAccount function, and passing it aliceZkSyncWallet as a parameter. As with all functions that interact with the blockchain, it returns a promise. You already know how to call this function!🤓