Deploy An ERC20 Contract To Polygon zkEVM Testnet With Brownie
How To Deploy To Polygon zkEVM Testnet Using Brownie With An ERC20 Token Contract
What Is zkEVM?
“Polygon zkEVM harnesses the power of ZK proofs to reduce transaction cost and massively increase throughput, all while inheriting the security of Ethereum L1.”
- https://polygon.technology/polygon-zkevm
Polygons’s Zero-Knowledge Ethereum Virtual Machine (zkEVM) is Layer 2 scaling solution for Ethereum that batches transactions (rolls them up with ZK-rollups) into a single transaction and posts them to Ethereum.
Do I Need Some Special Token Or Configuration To Work With zkEVM?
The beauty about Polygon zkEVM is that it’s EVM-equivalent, which means you can deploy the same solidity code that you would on Ethereum to zkEVM without adding any special cases to change or adapt the code to zkEVM.
We’ll be diving into how that’s done by deploying and ERC20 Token Contrat to Polygon zkEVM Testnet.
zkEVM Testnet Network Information
The testnet port and address has changed a few times since the first testnet came out, but it’s always good to visit the docs for the current network details.
Requirements
Before we begin, make sure you have the following installed on your computer.
- 🐍Python
v3.9.5
— ⚠️ Make sure to use this version of python for Brownie to work - Brownie
v1.19.3
Wallet Configuration
Our first step is to make sure that our wallet is configured with the zkEVM network. You could enter the details manually, but to save on time we’re just going to use the Polygon zkEVM Bridge site and add the network to our wallet.
Token Bridging
Now that we have the configuration set up for our wallet, we’ll need some native eth
to be able to perform transactions.
As there is no way to buy the tokens directly, we then need to bridge another Testnet’s token to zkEVM Testnet. Currently the bridge for zkEVM Testnet only supports Goerli.
You can find the bridge here: https://public.zkevm-test.net/.
Goerli Faucet Options
There are a few options to get Goerli token.
- Alchemy Goerli Faucet — https://goerlifaucet.com/
- You can also see a list of Faucets — https://faucetlink.to/goerli
Bridging Goerli To zkEVM Testnet
⚠️ NOTE: Briding can take up to 30 minutes to transfer your Goerli to zkEVM Testnet.
If you want to see the entire process, I recommend checking out my other article on How To Deploy A Contract To Polygon zkEVM Testnet.
Building & Deploying ERC20 To zkEVM Testnet
We’ll now go through the entire step-by-step in order to configure brownie to deploy a contract to zkEVM Testnet.
Python Version Sanity Check
To start, we need to verify that we have the correct version of python installed. A recommendation is to use pyenv to have multiple versions of python installed.
pyenv global 3.9.5;
python --version;
# Expected Output:
# Python 3.9.5
pip install eth-brownie;
brownie --version;
# Expected Output:
# Brownie v1.19.3 - Python development framework for Ethereum
Brownie ERC20 Template Project
Brownie has built in templates and for the sake of demonstrating how to use existing code to deploy directly to zkEVM Testnet, we’ll ask brownie to “bake” a template for us.
brownie bake token zkevm-brownie-erc20;
# Expected Output:
# Brownie v1.19.3 - Python development framework for Ethereum
#
# Downloading from https://github.com/brownie-mix/token-mix/archive/master.zip...
# 9.14kiB [00:00, 5.82MiB/s]
# SUCCESS: Brownie mix 'token' has been initiated at /Users/path/to/zkevm-brownie-erc20
A small tip to making sure that the right Python version is always setup with pyenv. is to create a .python-version
file in our repository. This will make sure that you always reference the correct version of python to work with brownie.
# FROM: ./zkevm-brownie-erc20
pyenv install 3.9.5;
echo "3.9.5" > .python-version
Adding zkEVM Testnet As A Brownie Network
If you run the following in your terminal, you’ll see that there is no Polygon zkEVM Testnet network listed.
brownie networks list;
# Expected Output:
# Brownie v1.19.3 - Python development framework for Ethereum
#
# The following networks are declared:
#
#...
#
# Polygon
# ├─Mainnet (Infura): polygon-main
# └─Mumbai Testnet (Infura): polygon-test
#
# ...
Following the official docs on how to add a network in Brownie, we can use the following command to add the network details to be supported by brownie.
Official zkEVM Docs — Connecting To zkEVM
- Network Name: Polygon zkEVM Testnet
- RPC URL: https://rpc.public.zkevm-test.net
- Chain ID: 1442
- Currency Symbol: ETH
- Block Explorer URL: https://explorer.public.zkevm-test.net
brownie networks add Polygon polygon-zkevm-test host="https://rpc.public.zkevm-test.net" name="zkEVM Testnet" chainid=1442 explorer="https://explorer.public.zkevm-test.net";
# Expected Output
# Brownie v1.19.3 - Python development framework for Ethereum
#
# SUCCESS: A new network 'zkEVM Testnet' has been added
# └─zkEVM Testnet
# ├─id: polygon-zkevm-test
# ├─chainid: 1442
# ├─explorer: https://explorer.public.zkevm-test.net
# └─host: https://rpc.public.zkevm-test.net
Add Wallet Private Key To Brownie
The next step will be to associate your wallet private key to brownie and give it an alias or an id.
NOTE: Avoid using any dots or special characters for your id. In case you make a mistake you can always remove the account by doing brownie accounts delete <ID-ENTERED>
.
brownie accounts new testingwithmanny;
# Expected Output
# Brownie v1.19.3 - Python development framework for Ethereum
#
# Enter the private key you wish to add: <YOUR-WALLET-PRIVATE-KEY>
# Enter the password to encrypt this account with: <YOUR-NEW-PASSWORD>
# SUCCESS: A new account '0xYourConfirmedWalletAddress' has been generated with the id 'testingwithmanny'
Associating Brownie Account To Deploy Script
After configuring the network and the account, we just need to make sure we modify our deploy script token.py
to make sure it references our account id.
File: ./scripts/token.py
#!/usr/bin/python3
from brownie import Token, accounts
def main():
# Add this next line
accounts.load('testingwithmanny') # or replace with the id you just created
return Token.deploy("zkEVM Test Token", "ZKTST", 18, 1e21, {'from': accounts[0]})
Deploying Our Contract To zkEVM Testnet With Brownie
Now that we have all the pieces, the last thing we need to do is just deploy our erc20 token to the PolygonzkEVM Testnet.
# FROM: ./zkevm-brownier-erc20
brownie run token.py --network polygon-zkevm-test;
# Expected Output:
# Brownie v1.19.3 - Python development framework for Ethereum
#
# Compiling contracts...
# Solc version: 0.6.12
# Optimizer: Enabled Runs: 200
# EVM Version: Istanbul
# Generating build data...
# - SafeMath
# - Token
#
# ZkevmBrownieErc20Project is the active project.
#
# Running 'scripts/token.py::main'...
# Enter password for "testingwithmanny":
# Transaction sent: 0x7fd0218b9f2963b54440df90e5480d42c537361788eb845f2eefed45376b80c9
# Gas price: 1.113493533 gwei Gas limit: 577595 Nonce: 2
# Token.constructor confirmed Block: 152186 Gas used: 521489 (90.29%)
# Token deployed at: 0xd23D6ec4BF2EA11A6d5a6eFB25ad1AAa6765fe66
We can see that we’ve successfully deployed with the following transaction details and the final contract address.
If we want to see this token in our wallet, all we need to do is copy the contract address and import the token into MetaMask.
That’s it, you’ve successfully deployed a contract to zkEVM Testnet with Brownie.
Full Code Repository
In case you want to see the full code, check out the python repository here.
What’s Next?
If you haven’t already read How To Deploy A Contract To Polygon zkEVM Testnet, please give it a read and if you want to get more in depth with zkEVM, check out Polygon University.
If you got value from this, please give it some love, and please also follow me on twitter (where I’m quite active) @codingwithmanny and instagram at @codingwithmanny.