How to Build on Layer 2 with zkEVM: A User-Friendly Guide

May 22, 2022 | Blockchain

Welcome to our comprehensive guide on how to build on Layer 2 using zkEVM, derived from the Intro to Layer 2s workshop preceding the ETH Denver event. This article is designed to be straightforward and user-centric, ensuring you can follow along easily.

Get Started with zkSync CLI

The zkSync CLI tool offers the simplest approach to kickstart your journey in developing applications and smart contracts on zkSync. To install it, run the following command:

sudo npm i -g zksync-cli@latest

Once installed, you can create a new project using the command:

zksync-cli create NAME_OF_YOUR_PROJECT

This generates a new folder containing a sample project. Move into the project directory with:

cd NAME_OF_YOUR_PROJECT

Understanding the Project Structure

Your project folder will include the following elements:

  • contracts: Contains your smart contracts.
  • deploy: Includes deployment and contract interaction scripts.
  • test: Contains your test files.
  • hardhat.config.ts: Your configuration file with zkSync-specific properties.

Compiling Your Contracts

To compile your smart contracts, use:

yarn hardhat compile

Keep an eye out for the creation of folders artifacts-zk and cache-zk, which store your compiled artifacts.

Deploying Your Contracts

To deploy a contract, first set your wallet’s private key in the .env file by renaming .env.example to .env, and entering your private key as follows:

 WALLET_PRIVATE_KEY=123cde574ccff.... 

Now, you can deploy your contract using:

yarn hardhat deploy-zksync --script deploy-greeter.ts

Deploying an ERC20 and ERC721 Contract

You can also create and deploy ERC20 and ERC721 contracts using contracts generated from the OpenZeppelin contract wizard. When you compile your project again, use the following commands accordingly:

  • ERC20 Deployment: yarn hardhat deploy-zksync –script deploy-erc20.ts
  • ERC721 Deployment: yarn hardhat deploy-zksync –script deploy-erc721.ts

Analogy for Understanding Deployment

Imagine you are an architect (the developer) designing a building (the smart contract). First, you need the proper blueprints (the code) to showcase your design. Once you have your blueprints ready, you call your construction crew (the zkSync CLI) to bring your vision to life on-site (the blockchain). You provide the crew with your schematics and necessary tools (the deployment scripts), and once they have everything set, they start building your space where it will stand permanently.

Troubleshooting Common Issues

If you encounter problems during development, consider these troubleshooting tips:

  • Ensure all required tools and packages are correctly installed.
  • Check your .env file for proper key configuration and permissions.
  • Confirm that you have enough GoerliETH to facilitate transactions on the zkSync blockchain.
  • Refer back to the zkSync CLI documentation for detailed guidance.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Final Thoughts

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox