Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intro to zk sync #42

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e62cab5
Add the LICENSE
HJVeronica Nov 26, 2019
caf2bff
Update README.md
HJVeronica Nov 26, 2019
13a923e
fix cryptozombies link
hankxdev Nov 27, 2019
7b9bb07
Lesson 17, Chapter 2: Let’s talk to zkSync
Jan 7, 2023
d216424
Lesson 17, Chapter 3: Create a new zkSync account
Jan 7, 2023
1897432
Lesson 17, Chapter 3: Create a new zkSync account (completed)
Jan 7, 2023
6af2716
Lesson 17, Chapter 4: Create a new zkSync account - continued
Jan 7, 2023
2526bdb
Lesson 17, Chapter 4: Create a new zkSync account - continued (comple…
Jan 7, 2023
5b5b66b
Lesson 17, Chapter 5: Deposit assets to zkSync
Jan 7, 2023
0aca6c4
Lesson 17, Chapter 5: Deposit assets to zkSync (completed)
Jan 7, 2023
80f99a4
Lesson 17, Chapter 6: Transfer assets on zkSync
Jan 7, 2023
434671a
Lesson 17, Chapter 6: Transfer assets on zkSync (completed)
Jan 7, 2023
b284d34
Lesson 17, Chapter 7: Transfer fees
Jan 7, 2023
68bd534
Lesson 17, Chapter 7: Transfer fees (completed)
Jan 14, 2023
a179b15
Lesson 17, Chapter 8: Withdraw to Ethereum
Jan 14, 2023
2e354dd
Lesson 17, Chapter 8: Withdraw to Ethereum (completed)
Jan 14, 2023
59ab5bf
Lesson 17, Chapter 9: Account balances
Jan 20, 2023
3f2e548
Lesson 17, Chapter 9: Account balances (completed)
Jan 20, 2023
01a58de
Lesson 17, Chapter 10: The shopkeeper - Talking to the blockchain
Jan 20, 2023
52293a0
Lesson 17, Chapter 10: The shopkeeper - Talking to the blockchain (co…
Jan 20, 2023
86e9bb7
Lesson 17, Chapter 11: The shopkeeper - Wallets and private keys
Jan 20, 2023
d789a6e
Lesson 17, Chapter 11: The shopkeeper - Wallets and private keys (com…
Jan 20, 2023
b34beae
Lesson 17, Chapter 12: Update balances
Jan 24, 2023
9c88e15
Lesson 17, Chapter 12: Update balances (completed)
Jan 24, 2023
99e8a17
Lesson 17, Chapter 13: Register an account
Jan 24, 2023
e016775
Lesson 17, Chapter 13: Register an account (completed)
Jan 24, 2023
31f6de6
Lesson 17, Chapter 14: Make a payment on zkSync
Jan 24, 2023
a3e52e9
Lesson 17, Chapter 14: Make a payment on zkSync (completed)
Jan 26, 2023
132f9ab
Lesson 17, Chapter 15: Putting everything together
Jan 26, 2023
77c0b30
Lesson 17, Chapter 15: Putting everything together (completed)
Jan 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
# cryptozombies-lesson-code
cryptozomebie lesson code

![CryptoZombies](https://user-images.githubusercontent.com/13703497/69648502-c8f3db80-10ae-11ea-9d52-ce4d4bbc426a.jpeg)


## Overview
This repository contains source codes for [Cryptozombies](https://cryptozombies.io/en/) lessons.

The source codes are divided into `courses` and `chapters` as folders and chapters contain Solidity sample codes for each `lesson`.

> _NOTE: Lesson 7, 8, 9 has been removed and currently no source code is available for those lessons._

## How To Use
You can simply clone the project to your local storage with following command:

```
git clone https://github.com/loomnetwork/cryptozombies-lesson-code.git
```

Or fork it to modify the sample codes for your own study.

If you found any error in the codes while you study, Feel free to submit pull requests or issues.

>_WARNING: Please note that these codes are just sample codes, so DO NOT use them on production._

## License
[![License shields](https://img.shields.io/badge/License-GPL%20v3-blue)](https://www.gnu.org/licenses/gpl-3.0)

This project is licensed under the GNU General Public License v3.0. See the [LICENSE](https://github.com/loomnetwork/cryptozombies-lesson-code/blob/master/LICENSE) file for details.
8 changes: 8 additions & 0 deletions lesson-17/chapter-10/src/bob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(async () => {
const ethers = require('ethers')
const zksync = require('zksync')
const utils = require('./utils')

const zkSyncProvider = await utils.getZkSyncProvider(zksync, process.env.NETWORK_NAME);
const ethersProvider = await utils.getEthereumProvider(ethers, process.env.NETWORK_NAME);
})()
120 changes: 120 additions & 0 deletions lesson-17/chapter-10/src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
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
}
13 changes: 13 additions & 0 deletions lesson-17/chapter-11/src/bob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(async () => {
const ethers = require('ethers')
const zksync = require('zksync')
const utils = require('./utils')

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);
})()
120 changes: 120 additions & 0 deletions lesson-17/chapter-11/src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
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
}
25 changes: 25 additions & 0 deletions lesson-17/chapter-12/src/bob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(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)

})()
Loading