-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3737777
commit a541b5f
Showing
6 changed files
with
640 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,256 @@ | ||
--- | ||
title: Getting Started with ZK Chains | ||
description: Create and run your first ZK chain. | ||
--- | ||
|
||
This tutorial shows you how to use the `zk_inception` CLI to run an Elastic chain ecosystem and custom zk chain locally: | ||
|
||
- You'll set up a local **Elastic chain ecosystem** | ||
- You'll create a standard **ZK chain** and deploy a smart contract to it | ||
- You'll create a second ZK chain that uses a **custom base token** | ||
|
||
## Prerequisites | ||
|
||
- Make sure your machine satisfies the [system | ||
requirements](https://github.com/matter-labs/era-compiler-solidity/tree/main#system-requirements). | ||
- If you aren't already familiar with deploying smart contracts on ZKsync Era, please refer to the first section of the [quickstart tutorial](https://docs.zksync.io/build/start-coding/quick-start/deploy-your-first-contract). | ||
- For background on the Elastic Chain or ZK chains, read the [ZK Chains](https://docs.zksync.io/zk-stack/concepts/zk-chains) section in our docs. | ||
- Install the dependencies for the `zksync-era` repo by following the instructions [here](https://github.com/matter-labs/zksync-era/blob/main/docs/guides/setup-dev.md) | ||
(you can skip the "Environment" section). | ||
|
||
::callout{icon="i-heroicons-light-bulb"} | ||
If you previously have `foundry-zksync` installed, | ||
reinstall the normal version of [foundry](https://book.getfoundry.sh/getting-started/installation) for this tutorial. | ||
:: | ||
|
||
## Setting up the Elastic chain ecosystem | ||
|
||
There are two components needed for running a ZK chain locally: | ||
|
||
1. An Elastic chain ecosystem to manage different chains | ||
2. A chain deployed within the ecosystem | ||
|
||
To setup both of these components, can use the `zk_inception` CLI. | ||
You can use `cargo` to install the CLI with the command below: | ||
|
||
```bash | ||
cargo install --git https://github.com/matter-labs/zksync-era/ --locked zk_inception --force | ||
``` | ||
|
||
Once you have that installed, the first step is to create a new ecosystem with the `zk_inception` command: | ||
|
||
```bash | ||
zk_inception ecosystem create | ||
``` | ||
|
||
::callout{icon="i-heroicons-light-bulb"} | ||
Make sure to have Docker open. | ||
:: | ||
|
||
You will be prompted with a series of options to customize your ecosystem and generate a new chain within the ecosystem. | ||
For this tutorial, use the options shown below: | ||
|
||
```bash | ||
$ zk_inception ecosystem create | ||
|
||
┌ ZKsync toolbox | ||
│ | ||
◇ What do you want to name the ecosystem? | ||
│ my_elastic_chain | ||
│ | ||
◇ Select the origin of zksync-era repository | ||
│ Clone for me (recommended) | ||
│ | ||
◇ Select the L1 network | ||
│ Localhost | ||
│ | ||
◇ What do you want to name the chain? | ||
│ zk_chain_1 | ||
│ | ||
◇ Whats the chain id? | ||
│ 271 | ||
│ | ||
◇ Select how do you want to create the wallet | ||
│ Localhost | ||
│ | ||
◇ Select the prover mode | ||
│ NoProofs | ||
│ | ||
◇ Select the commit data generator mode | ||
│ Rollup | ||
│ | ||
◇ Select the base token to use | ||
│ Eth | ||
│ | ||
◇ Do you want to start containers after creating the ecosystem? | ||
│ Yes | ||
``` | ||
|
||
By running this command and selecting these options, you just: | ||
|
||
- Created a new ecosystem called `my-elastic-chain`, which can contain many chains. | ||
- Cloned the `zksync-era` repository inside the `my_elastic_chain` folder. | ||
- Chose to use a local network to act as the L1. | ||
This means we'll have to run a local reth docker container as well (don't worry, we've setup everything you need for this too!). | ||
- Created a new chain called `zk_chain_1` and set it as your default chain. | ||
- Set the chain id to `271`. | ||
- Chose the default wallet configuration. | ||
- Selected to not use proofs, which makes testing more lightweight. | ||
- Chose a standard rollup for the data availability. | ||
- Selected ETH to use as the base token. | ||
- Started the containers for the ecosystem and `zk_chain_1` in Docker. | ||
|
||
Inside the generated `my_elastic_chain` folder, you should now have the following contents: | ||
|
||
- `ZkStack.yaml`: a config file for the ecosystem. | ||
- `chains`: a folder with configurations for each chain created | ||
- `configs`: configuration for the deployments and wallets | ||
- `volumes`: dependencies for running local nodes | ||
- `zksync-era`: a clone of the `zksync-era` repository | ||
- `docker-compose.yml`: a docker compose file to start up a local environment | ||
|
||
### Deploying the ecosystem | ||
|
||
You've just set up your ecosystem and chain, and have two Docker containers running: | ||
a postgres database for your chain, and a reth node for the local L1 chain. | ||
|
||
The L1 chain is already running, but your ecosystem and chain aren't deployed yet. | ||
The next step is to deploy your ecosystem contracts to the L1 and register your chain to the ecosystem. | ||
|
||
Move into the elastic chain folder: | ||
|
||
```bash | ||
cd my_elastic_chain | ||
``` | ||
|
||
Next, run the `init` command below to deploy the ecosystem: | ||
|
||
```bash | ||
zk_inception ecosystem init --dev | ||
``` | ||
|
||
The `--dev` flag will choose the default options for development. Using this flag is equivalent to answering the prompts like this: | ||
|
||
```bash | ||
◇ Do you want to deploy Paymaster contract? | ||
│ Yes | ||
│ | ||
◇ Do you want to deploy some test ERC20s? | ||
│ Yes | ||
│ | ||
◇ Do you want to deploy ecosystem contracts? (Not needed if you already have an existing one) | ||
│ Yes | ||
│ | ||
◆ What is the RPC URL of the L1 network? | ||
│ http://localhost:8545 (default) | ||
``` | ||
::callout{icon="i-heroicons-light-bulb"} | ||
If you have any issues at this step, try reinstalling the dependencies at the top. | ||
:: | ||
This process will take some time as there is a lot happening here. | ||
To see more detailed logs of what is happening at each step, you can add the `--verbose` flag to the command. | ||
To summarize, the `init` command: | ||
- Checks to see if your environment has the necessary dependencies. | ||
- Compiles and deploys all of necessary contracts for the ecosystem. | ||
- Deploys the `zk_chain_1` to the ecosystem. | ||
- Sets up a database for the default chain (in this case `zk_chain_1`). | ||
- Deploys a paymaster contract and some test ERC20 contracts (if selected) to use for development. | ||
You can find the paymaster contract used in the `zksync-era` repo in `contracts/l2-contracts/contracts/TestnetPaymaster.sol`, | ||
and the deployed address inside `my_elastic_chain/chains/zk_chain_1/configs/contracts.yaml` at `l2:testnet_paymaster_addr`. | ||
For the ERC20 contracts, you can find the deployed addresses inside `my_elastic_chain/configs/erc20.yaml`. | ||
### Understanding the chain configs | ||
Running the `init` command will also modify your chain configuration files in the ecosystem folder. | ||
The main configuration file for `zk_chain_1` can be found in `my_elastic_chain/chains/zk_chain_1/ZkStack.yaml`. | ||
It contains the most basic configurations for the chain. | ||
Inside `my_elastic_chain/chains/zk_chain_1/configs`, you can find six more configuration files: | ||
1. `contracts.yaml`: configurations for all the L1 & L2 contracts. | ||
1. `external_node.yaml`: configurations for the chain's node server. | ||
1. `general.yaml`: general configurations. | ||
1. `wallets.yaml`: all wallets that you are using for this chain. | ||
1. `secrets.yaml`: secrets that are individual for every chain. | ||
1. `genesis.yaml`: chain specific configurations with parameters that were used during genesis. | ||
::callout{icon="i-heroicons-light-bulb"} | ||
Never commit your private keys or sensitive secrets to a public repository! | ||
:: | ||
### Starting the chain server | ||
The last step here is to start a server for `zk_chain_1`: | ||
```bash | ||
zk_inception server | ||
``` | ||
With this, your L1 chain should be running at port `8545`, and the `zk_chain_1` should be running at port `3050`. | ||
### Funding a wallet on your chain | ||
Because you chose to use a local reth node for your L1 and selected ETH as the base asset, | ||
you have access to several rich wallets on the L1 that you can use to bridge ETH to `zk_chain_1`. | ||
You can do this by running the command below using the `zksync-cli`: | ||
```bash | ||
npx zksync-cli bridge deposit --rpc=http://localhost:3050 --l1-rpc=http://localhost:8545 | ||
``` | ||
For testing purposes, use the first rich wallet as both the sender and recipient: | ||
```shell | ||
? Amount to deposit 10 | ||
? Private key of the sender 0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110 | ||
? Recipient address on L2 0x36615Cf349d7F6344891B1e7CA7C72883F5dc049 | ||
``` | ||
To see that it's worked, let's check the balance of that address on the `zk_chain_1`: | ||
```bash | ||
zksync-cli wallet balance --address 0x36615Cf349d7F6344891B1e7CA7C72883F5dc049 --rpc http://localhost:3050 | ||
``` | ||
Now this address has ETH available on `zk_chain_1` to use for testing. | ||
## Deploying a contract to chain 1 | ||
Now that your chain is deployed and your wallet is funded, let's create a template contract and deploy it to chain `zk_chain_1`: | ||
Move out of your ecosystem folder and initialize a new hardhat project using the `zksync-cli`: | ||
```bash | ||
npx zksync-cli@latest create --template qs-hello-zksync zk-chain-test | ||
cd zk-chain-test | ||
``` | ||
In the `hardhat.config.ts` file, change the default network on line 6 to `dockerizedNode`: | ||
```bash | ||
defaultNetwork: "dockerizedNode", | ||
``` | ||
In the `.env` file, add the private key of the wallet you just funded on your chain: | ||
```env | ||
WALLET_PRIVATE_KEY=0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110 | ||
``` | ||
Finally, compile the contract and deploy to `zk_chain_1` | ||
```bash | ||
yarn compile && yarn deploy | ||
``` | ||
Nice - you just deployed a contract to your own local ZK chain! | ||
Next, let's take a look at customizing a chain. |
Oops, something went wrong.