Skip to content

wangqian/l2-intro-pre-ethdenver

 
 

Repository files navigation

Intro to Layer 2s: buidl on the zkEVM (pre ETH Denver edition)

This repository contains all the code examples used in the "Introduction to L2s" workshop previous to ETH Denver.

🎬 Watch the recording here

Slides are available here: https://docs.google.com/presentation/d/1_zWQRjUFX5ahiBMOjnoDiMAdjJ2-ozCs593HRCwflHU/edit?usp=sharing

This repo is no longer open to contributions & PRs.

Project structure

Projects created with the zkSync-CLI have the following structure.

  • /contracts: smart contracts.
  • /deploy: deployment and contract interaction scripts.
  • /test: test files
  • hardhat.config.ts: configuration file.

Commands

  • yarn hardhat compile will compile the contracts.
  • yarn hardhat deploy-zksync --script scriptFILE.ts will execute the script from the /deploy folder (e.g yarn hardhat deploy-zksync --script deploy-greeter.ts). Requires environment variable setup.

Environment variables

In order to prevent users to leak private keys, this project includes the dotenv package which is used to load environment variables. It's used to load the wallet private key, required to run the deploy script.

To use it, rename .env.example to .env and enter your private key.

WALLET_PRIVATE_KEY=123cde574ccff....

Workshop tasks

Workshop important links

1. zkSync portal and faucets

The zkSync Portal is the easiest way to deposit and withdraw funds from zkSync. If you have GoerliETH, you can use the bridge section to deposit or withdraw funds to and from the zkSync testnet.

However, if you don't have any GoerliETH, you can receive a small amount by using our faucet, which requires you to post a tweet as a way to verify your identity.

On the other hand, here are a few other faucets that you can use to get GoerLiETH, which then you can bridge to zkSync:

2. Create a project with zksync-cli

The zkSync CLI tool is the easiest way to start developing applications and smart contracts on zkSync. You can find the documentation here.

To install it, just run sudo npm i -g zksync-cli@latest (enter your system password).

To create a new project, just run zksync-cli create NAME_OF_YOUR_PROJECT. This will create a new folder with the project name and download a sample project inside it.

Note Once created, run cd NAME_OF_YOUR_PROJECT to enter the project directory. You'll have to run the commands to compile contracts and run scripts from this folder.

The project created is very similar to any other Hardhat project, but the hardhat.config.ts file includes some zkSync-specific properties.

First, it imports a few dependencies used to compile and deploy our contracts:

import "@matterlabs/hardhat-zksync-solc";
import "@matterlabs/hardhat-zksync-deploy";

Secondly, it includes the zksolc object which contains specific properties of the compiler. It comes with the minimal configuration but you can learn more about the zksolc configuration here.

zksolc: {
  version: "1.2.2",
  compilerSource: "binary",
  settings: {},
},

An last, the networks are defined with the following parameters:

  url: "https://zksync2-testnet.zksync.dev",
  ethNetwork: "goerli",
  zksync: true,

The url and ethNetwork are the RPC endpoints of the L2 and L1 and the zksync flag is used to indicate Hardhat if it should use the zksync compiler and deployment plugins.

Note With "goerli", the project will use the default providers from ethers, but you can change that for an RPC endpoint from

3. Deploy and verify the Greeter contract

The zkSync-CLI sample project includes a Greeter contract and a deploy script. The Greeter contract stores a message on chain which can be retrieved by calling the read method greet() and can be updated by calling the method setGreeting(_message).

To compile the contract, run yarn hardhat compile. You'll notice that the folders artifacts-zk and cache-zk will be created with the compiled artifact.

To deploy the contract, just set your wallet's private key in the .env file (you'll have to rename it first), and run the command yarn hardhat deploy-zksync --script deploy-greeter.ts.

To verify the contract you can use the zkSync Explorer. You'll have to select the solidity and zksolc compiler versions to match the ones from the hardhat.config.ts file and also enter the constructor params, which are printed in the terminal by the deploy-greeter.ts script.

Note Make sure you've configured your private key in the .env file as described above.

4. Create and deploy an ERC20 contract

To showcase the compatibility with the standard token contract, we'll use the OpenZeppeling contract wizard to create an ERC20 contract.

We'll choose an ERC20, Burnable, Pausable and Snapshot. We can copy the contract code and the contract and put it in the contracts folder as is (check out file zkToken.sol).

As this contract uses some dependencies from OpenZeppelin, we'll have to install them with yarn add -D @openzeppelin/contracts.

To compile the contract, just run yarn hardhat compile again.

  • The included deploy-erc20.ts script will deploy this contract.
  • The included use-erc20.ts script will do a transfer of tokens between two accounts and return its balances.

Note To verify contracts that include imports of other contracts and libraries (like Openzeppelin contracts 😉), you'd need to flatten it first! Learn more about flattening contracts in our docs

5. Create and deploy an ERC721 contract

To showcase the compatibility with the standard NFT token, we'll use the OpenZeppeling contract wizard to create an ERC721 contract.

We'll select the following options: Mintable (with auto increments), Burnable, Enumerable and enter a Base URI. We can copy this contract into the /contracts folder (check out file zkNFT.sol).

To compile the contract, just run yarn hardhat compile again.

  • The included deploy-erc721.ts script will deploy this contract.
  • The included use-erc721.ts script will mint a new NFT and return the total supply and balance.

Official Links

About

Introduction to layer two and zkSync

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 82.3%
  • Solidity 17.6%
  • Shell 0.1%