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

chore: add test for custom zk chain #105

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
73 changes: 60 additions & 13 deletions content/tutorials/custom-zk-chain/10.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ Make sure Docker is running on your machine.

Move to a directory where you want your ecosystem folder to be, and run the command below to generate an ecosystem folder.

:test-action{actionId="project-folder"}
:test-action{actionId="create-ecosystem"}

```bash
zkstack ecosystem create
```
Expand All @@ -76,10 +79,10 @@ If you choose different names for your ecosystem or chain, remember to update th
│ my_elastic_chain
◇ Select the origin of zksync-era repository
│ Clone for me (recommended)
│ Clone for me (recommended)
◇ Select the L1 network
│ Localhost
│ Localhost
◇ What do you want to name the chain?
│ zk_chain_1
Expand All @@ -88,16 +91,19 @@ If you choose different names for your ecosystem or chain, remember to update th
│ 271
◇ Select how do you want to create the wallet
│ Localhost
│ Localhost
◇ Select the prover mode
│ NoProofs
│ NoProofs
◇ Select the commit data generator mode
│ Rollup
│ Rollup
◇ Select the base token to use
│ Eth
│ Eth
◇ Enable EVM emulator?
│ No
◇ Do you want to start containers after creating the ecosystem?
│ Yes
Expand All @@ -123,16 +129,16 @@ By running this command and selecting these options, you just:
You can read more about data availability options for ZK chains in the
[ZK chains](https://docs.zksync.io/zk-stack/concepts/zk-chains#data-availability-da) docs.
- Selected ETH to use as the base token.
- Opted to not enable the EVM emulator.
- Started the containers for the ecosystem in Docker.

Inside the generated `my_elastic_chain` folder, you should now have the following contents:

- `ZkStack.yaml`: a configuration 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.
- `configs`: configurations for the deployments and wallets.
- `zksync-era`: a clone of the `zksync-era` repository.
- `docker-compose.yml`: a Docker compose file to start up a local environment.
- `ZkStack.yaml`: a configuration file for the ecosystem.

## Deploying the ecosystem

Expand All @@ -144,12 +150,17 @@ The next step is to deploy your ecosystem contracts to the L1 and register your

Move into the ecosystem folder:

:test-action{actionId="move-to-ecosystem-folder"}

```bash
cd my_elastic_chain
```

Next, run the `zkstack ecosystem init` command below to deploy the ecosystem:

:test-action{actionId="add-tsconfig"}
:test-action{actionId="init-ecosystem"}

```bash
zkstack ecosystem init --dev
```
Expand Down Expand Up @@ -200,6 +211,8 @@ Never commit your private keys or sensitive secrets.

The last step here is to start a server for `zk_chain_1`:

:test-action{actionId="start-server"}

```bash
zkstack server
```
Expand All @@ -219,6 +232,8 @@ Never use these wallets in production or send real funds to them.

Open a new terminal and run the command below to bridge some ETH to `zk_chain_1` using ZKsync CLI:

:test-action{actionId="deposit-eth"}

```bash
npx zksync-cli bridge deposit --rpc=http://localhost:3050 --l1-rpc=http://localhost:8545
```
Expand All @@ -233,6 +248,8 @@ For testing purposes, we'll use one of the rich wallets as both the sender and r

To see that it worked, let's check the balance of that address on `zk_chain_1`:

:test-action{actionId="check-balance"}

```bash
npx zksync-cli wallet balance \
--address 0x36615Cf349d7F6344891B1e7CA7C72883F5dc049 \
Expand All @@ -247,31 +264,61 @@ Now that your chain is deployed and your wallet is funded, let's create a templa

Move out of your ecosystem folder and initialize a new hardhat project using ZKsync CLI:

:test-action{actionId="create-test-project"}

```bash
npx zksync-cli@latest create --template zksync-101 zk-chain-test
cd zk-chain-test
```

Use the same private key for the rich wallet:

:test-action{actionId="npm-install"}
:test-action{actionId="add-pk"}

```shell
? Private key of the wallet responsible for deploying contracts (optional)
0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110
```

In the `hardhat.config.ts` file, change the default network on line 6 to `dockerizedNode`,
which is already configured to connect to the local chain node running on port `3050`:
In the `hardhat.config.ts` file, let's add the local network and configure it as the default:

```bash
:test-action{actionId="change-network"}

```ts
defaultNetwork: "dockerizedNode",
networks: {
dockerizedNode: {
url: 'http://localhost:3050',
ethNetwork: 'http://localhost:8545',
zksync: true,
},
```

Finally, compile the contract and run the deploy script:

```bash
:test-action{actionId="compile-and-deploy"}

::code-group

```bash [npm]
npm run compile && npm run deploy:hello-zksync
```

```bash [yarn]
yarn compile && yarn deploy:hello-zksync
```

```bash [pnpm]
pnpm compile && pnpm deploy:hello-zksync
```

```bash [bun]
bun compile && bun deploy:hello-zksync
```

::

Nice - you just deployed a contract to your own local ZK chain!

Next, let's take a look at customizing a chain.
70 changes: 63 additions & 7 deletions content/tutorials/custom-zk-chain/20.customizing-your-chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ cd base-token-contract

Then, run the `hardhat init` command to generate a new project:

:test-action{actionId="new-hh-project"}

```bash
npx hardhat init
```
Expand All @@ -44,6 +46,8 @@ Select the option **Create a Typescript project** and accept the default options

Run the command below to install the necessary dependencies:

:test-action{actionId="install-token-deps"}

::code-group

```bash [npm]
Expand All @@ -60,7 +64,9 @@ yarn add -D typescript ts-node @openzeppelin/contracts @nomicfoundation/hardhat-

Once installed, replace your existing config in `hardhat.config.ts` with the config below:

```ts
:test-action{actionId="hh-config"}

```ts [hardhat.config.ts]
import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";

Expand All @@ -86,30 +92,38 @@ However, in the future this may change.

Next, create a `.env` file with:

:test-action{actionId="create-env"}

```bash
touch .env
```

Add the governor private key from our elastic_chain setup to ensure that this address becomes the owner of the token contract. Here's how
you can add the `WALLET_PRIVATE_KEY` environment variable:

```bash
:test-action{actionId="new-env"}

```bash [.env]
# Use the private key of the `governor` from wallet.yaml
WALLET_PRIVATE_KEY=<governor_private_key_from_wallet.yaml>
WALLET_PRIVATE_KEY=<governor_private_key_from_wallets.yaml>
```

### Deploying an ERC20 Contract

Now that we've configured hardhat and the deployer wallet, let's add the contract and deploy script.
Rename the example generated contract file to `CustomBaseToken.sol`:

:test-action{actionId="rename-contract-file"}

```bash
mv contracts/Lock.sol contracts/CustomBaseToken.sol
```

Open the `CustomBaseToken.sol` file and replace the example contract with the ERC20 contract below:

```solidity
:test-action{actionId="token-contract"}

```solidity [CustomBaseToken.sol]
// SPDX-License-Identifier: Unlicensed
pragma solidity ^0.8.20;

Expand All @@ -131,13 +145,17 @@ contract CustomBaseToken is ERC20, Ownable, ERC20Burnable {
Next, let's update the ignition module to deploy the ERC20 contract.
Rename the module file with the command below:

:test-action{actionId="rename-module"}

```bash
mv ignition/modules/Lock.ts ignition/modules/CustomBaseToken.ts
```

Then, replace the module file with the code below:

```ts
:test-action{actionId="new-deploy-module"}

```ts [CustomBaseToken.ts]
import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";

const CustomBaseTokenModule = buildModule("CustomBaseTokenModule", (m) => {
Expand All @@ -154,6 +172,9 @@ export default CustomBaseTokenModule;

To run the module and deploy the token contract, run:

:test-action{actionId="ignore-deploy-confirm"}
:test-action{actionId="deploy-token-contract"}

```bash
npx hardhat ignition deploy ./ignition/modules/CustomBaseToken.ts --network localRethNode
```
Expand All @@ -164,6 +185,9 @@ After deploying, the token contract address should be logged in your console.
The constructor function in the contract should have minted tokens to the deployer address.
Let's verify that the tokens were minted to the deployer address using the Foundry `cast` CLI:

:test-action{actionId="get-contract-address"}
:test-action{actionId="check-token-balance"}

```bash
cast balance --erc20 <0xYOUR_TOKEN_ADDRESS> 0x36615Cf349d7F6344891B1e7CA7C72883F5dc049 \
--rpc-url http://localhost:8545
Expand All @@ -175,8 +199,16 @@ Now that your ERC20 token is deployed, you can create a new chain.

First, shut down the node server running for `zk_chain_1` by terminating the process.

:test-action{actionId="shutdown-server"}

```bash
zkstack chain create
```

Move back into your elastic chain ecosystem folder and run the `zkstack chain create` subcommand:

:test-action{actionId="create-new-chain"}

```bash
zkstack chain create
```
Expand Down Expand Up @@ -224,22 +256,34 @@ For example, if we set the nominator to 20 and the denominator to 1, together th
this would mean that 20 tokens would be given the equivalent value as 1 ETH for gas.
For testing purposes, we'll just use a 1:1 ratio.

:test-action{actionId="get-gov-address"}
:test-action{actionId="fund-other"}
:test-action{actionId="fund-gov"}

```bash
cast send <0x_BASE_TOKEN_ADDRESS> "transfer(address,uint256)" <0x_GOVERNOR_ADDRESS> 500000000000000000 --private-key 0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110 --rpc-url http://localhost:8545 --gas-price 30000000000
```

### Initializing the chain

::callout{icon="i-heroicons-exclamation-triangle" color="amber"}
Make sure the server for `zk_chain_1` that you started in the previous section is shut down.
::

Next, initialize the chain in the ecosystem with the command below, and select the default options for the prompts.
Next, initialize the chain in the ecosystem with the command below.

:test-action{actionId="init-new-chain"}

```bash
zkstack chain init
zkstack chain init --dev
```

During the initialization process, your ERC20 token address gets added to the allowlist mentioned earlier.

Now that the chain is initialized, you can start the chain server:

:test-action{actionId="start-server-2"}

```bash
zkstack server
```
Expand All @@ -251,6 +295,8 @@ Base tokens can not be minted on the L2 without being backed by the correspondin

Open a new terminal and use ZKsync CLI to bridge the tokens with the command below:

:test-action{actionId="bridge-tokens"}

```bash
npx zksync-cli bridge deposit --token <0x_YOUR_TOKEN_ADDRESS> \
--rpc=http://localhost:3050 \
Expand All @@ -265,6 +311,8 @@ npx zksync-cli bridge deposit --token <0x_YOUR_TOKEN_ADDRESS> \

To verify that this worked, let's check the new balance of our address on the L2 chain:

:test-action{actionId="l2-token-balance"}

```bash
npx zksync-cli wallet balance \
--address 0x36615Cf349d7F6344891B1e7CA7C72883F5dc049 \
Expand All @@ -285,6 +333,8 @@ The answer is that if you try to bridge regular ETH to this chain, it will just

You can try this out by depositing ETH from the L1 to this new chain:

:test-action{actionId="bridge-ETH"}

```bash
npx zksync-cli bridge deposit \
--rpc=http://localhost:3050 \
Expand All @@ -304,13 +354,17 @@ To find the L2 token address for ETH, you can use the `l2TokenAddress` method av
To try this out, open your hardhat project `zk-chain-test` from the previous section,
and run the commands below to create a new script file:

:test-action{actionId="create-script"}

```bash
mkdir scripts
touch scripts/checkBalance.ts
```

Next, copy and paste the script below into the `checkBalance.ts` file:

:test-action{actionId="l2-eth-address-script"}

```ts
import { ETH_ADDRESS_IN_CONTRACTS } from "zksync-ethers/build/utils.js";
import { getWallet } from "../deploy/utils";
Expand All @@ -329,6 +383,8 @@ main();

Run the script with the `hardhat run` command:

:test-action{actionId="run-address-script"}

```bash
npx hardhat run scripts/checkBalance.ts
```
Expand Down
Loading
Loading