TLDR:
Polygon zkEVM is the first ever EVM-compatible zero-knowledge rollup to hit the market. This means that developers can leverage existing Web3 tooling and zero-knowledge proofs to deploy smart contracts and execute transactions on the Ethereum network cheaper than ever before.
Polygon zkEVM is a layer 2 scaling solution that aims to make Ethereum transactions faster and more efficient using special math called zero-knowledge proofs to ensure transactions are valid and quickly finalized.
ZK-rollups execute smart contracts transparently by sharing zero-knowledge proofs of their validity, which allows Polygon zkEVM to work seamlessly with the Ethereum Virtual Machine. If you want to read more about ZK-EVMs and ZK-rollups, you can check out our blog article zkEVM and zk-rollups explained.
In this tutorial, we will go over how to bridge funds between Sepolia and zkEVM testnets, as well as how to deploy a smart contract to the testnet using Hardhat. Here is a brief overview of the tutorial:
.env
file to store the secrets.See Create a project.
See View node access and credentials.
Before diving into the project, make sure to top up your wallet with Sepolia ether. You can use our faucet for this.
We can easily move assets between Ethereum (L1) and Polygon zkEVM (L2) using the zkEVM bridge. The UI interface for the bridge is available at public.zkevm-test.net.
To bridge assets between L1 and L2, the user has to lock up any amount of those assets in the original network using the zkEVM bridge. An equivalent amount of wrapped tokens are then minted in the other chain.
Let us go through the process of obtaining zkEVM ETH:
Add the Polygon zkEVM to your MetaMask wallet.
You can do that by simply creating a Polygon zkEVM node with Chainstack, and adding the network to your MetaMask by clicking on the Add to MetaMask. See Chainstack console.
Make sure that your wallet is connected to the Sepolia Testnet and has sufficient Sepolia ETH.
Navigate to the zkEVM bridge interface.
Follow the instructions on the bridge to mint some ETH to the zkEVM testnet.
After submitting the transaction, it is necessary to wait until the Finalize button is activated. Without finalizing, the bridge operation won’t be complete.
Create a new directory for your project, then run the following from a terminal:
After Hardhat is installed, run:
This will launch the Hardhat CLI, which will prompt you to configure a starter project. For this tutorial, click Yes on all the prompts Hardhat offers you.
This project uses the dotenv package to safely use environment variables.
Run the following command in your root directory to install the dotenv package:
In your project’s root directory, create a new file and name it .env
. Here is where you will set up the environment variables for your Chainststack Polygon zkEVM endpoint and your wallet’s private key.
Save the file after you added your information. Now run the following command to load all the environment variables:
You will find a file named hardhat.config.js
in the root directory. This file is used to configure various settings for your Hardhat projects, such as the network you want to deploy your contracts on, the compilers you want to use, and the plugins you want to enable.
Delete the default code in the file and replace it with the following:
Let’s break down what each part of the file does:
require("@nomicfoundation/hardhat-toolbox");
imports the Hardhat Toolbox plugin, which provides several useful tools and utilities for Hardhat projects.require("dotenv").config();
loads environment variables from a .env
file using the dotenv
package.module.exports = { ... }
exports a JavaScript object containing the configuration for the Hardhat project.solidity: "0.8.18",
sets the Solidity compiler version to 0.8.18.networks: { ... }
defines the network configurations for the Hardhat project.defaultNetwork: { ... }
defines the default network that Hardhat will use.zkEVM_testnet: { ... }
defines the configuration for the zkEVM
network.url: ${process.env.YOUR_CHAINSTACK_ENDPOINT},
sets the RPC URL for the zkEVM network.accounts: [process.env.YOUR_PRIVATE_KEY],
sets the accounts for the zkEVM
network using the YOUR_PRIVATE_KEY
environment variable. This will allow the Hardhat project to deploy contracts and interact with the zkEVM testnet using the specified private key.In the root directory, you will find a directory named contracts
. Create a new file named SimpleVault.sol
, and paste the following code inside it:
This is a simple smart contract that allows us to deposit and withdraw ETH. All the functions are commented on for a better understanding.
In the scripts
directory inside the root of your project, you will find a file named deploy.js
. Replace its content with the following:
This is a simple deploy script that deploys the SimpleVault
smart contract to the zkEVM testnet, and returns the address of the newly deployed contract in the terminal. You can search for your contract on the Polygon zkEVM testnet explorer.
To run this script, execute the following command in the terminal:
We can interact with a deployed smart contract through Hardhat in two ways:
Let us do a bit of both.
Create a new file named interact.js
inside the scripts directory. Paste the following code inside it:
In this script, we will attach the address of the deployed smart contract to a local instance we initialize via Hardhat. We can then use the RPC URL and the private key we configured in the config file to send transactions and call functions to and from the contract.
You can see that we send 1 ETH to the contract. To execute the script, run the following command in the terminal:
Open another terminal inside the same directory and run the following command:
This will open up a Hardhat console that will allow us to interact with our smart contract via the command line. To connect the console to the deployed smart contract, run:
You can read the locked vault value from the smart contract by simply running this command in the console:
You can withdraw locked ETH from the console by running this command in the terminal:
And just like that, we used a Hardhat script to deposit ETH into a deployed smart contract, and used the Hardhat console to interact with the smart contract.
This tutorial guided you through bridging funds between the Sepolia testnet and the Polygon zkEVM testnet. We also deployed a smart contract to the zkEVM testnet using Hardhat.
TLDR:
Polygon zkEVM is the first ever EVM-compatible zero-knowledge rollup to hit the market. This means that developers can leverage existing Web3 tooling and zero-knowledge proofs to deploy smart contracts and execute transactions on the Ethereum network cheaper than ever before.
Polygon zkEVM is a layer 2 scaling solution that aims to make Ethereum transactions faster and more efficient using special math called zero-knowledge proofs to ensure transactions are valid and quickly finalized.
ZK-rollups execute smart contracts transparently by sharing zero-knowledge proofs of their validity, which allows Polygon zkEVM to work seamlessly with the Ethereum Virtual Machine. If you want to read more about ZK-EVMs and ZK-rollups, you can check out our blog article zkEVM and zk-rollups explained.
In this tutorial, we will go over how to bridge funds between Sepolia and zkEVM testnets, as well as how to deploy a smart contract to the testnet using Hardhat. Here is a brief overview of the tutorial:
.env
file to store the secrets.See Create a project.
See View node access and credentials.
Before diving into the project, make sure to top up your wallet with Sepolia ether. You can use our faucet for this.
We can easily move assets between Ethereum (L1) and Polygon zkEVM (L2) using the zkEVM bridge. The UI interface for the bridge is available at public.zkevm-test.net.
To bridge assets between L1 and L2, the user has to lock up any amount of those assets in the original network using the zkEVM bridge. An equivalent amount of wrapped tokens are then minted in the other chain.
Let us go through the process of obtaining zkEVM ETH:
Add the Polygon zkEVM to your MetaMask wallet.
You can do that by simply creating a Polygon zkEVM node with Chainstack, and adding the network to your MetaMask by clicking on the Add to MetaMask. See Chainstack console.
Make sure that your wallet is connected to the Sepolia Testnet and has sufficient Sepolia ETH.
Navigate to the zkEVM bridge interface.
Follow the instructions on the bridge to mint some ETH to the zkEVM testnet.
After submitting the transaction, it is necessary to wait until the Finalize button is activated. Without finalizing, the bridge operation won’t be complete.
Create a new directory for your project, then run the following from a terminal:
After Hardhat is installed, run:
This will launch the Hardhat CLI, which will prompt you to configure a starter project. For this tutorial, click Yes on all the prompts Hardhat offers you.
This project uses the dotenv package to safely use environment variables.
Run the following command in your root directory to install the dotenv package:
In your project’s root directory, create a new file and name it .env
. Here is where you will set up the environment variables for your Chainststack Polygon zkEVM endpoint and your wallet’s private key.
Save the file after you added your information. Now run the following command to load all the environment variables:
You will find a file named hardhat.config.js
in the root directory. This file is used to configure various settings for your Hardhat projects, such as the network you want to deploy your contracts on, the compilers you want to use, and the plugins you want to enable.
Delete the default code in the file and replace it with the following:
Let’s break down what each part of the file does:
require("@nomicfoundation/hardhat-toolbox");
imports the Hardhat Toolbox plugin, which provides several useful tools and utilities for Hardhat projects.require("dotenv").config();
loads environment variables from a .env
file using the dotenv
package.module.exports = { ... }
exports a JavaScript object containing the configuration for the Hardhat project.solidity: "0.8.18",
sets the Solidity compiler version to 0.8.18.networks: { ... }
defines the network configurations for the Hardhat project.defaultNetwork: { ... }
defines the default network that Hardhat will use.zkEVM_testnet: { ... }
defines the configuration for the zkEVM
network.url: ${process.env.YOUR_CHAINSTACK_ENDPOINT},
sets the RPC URL for the zkEVM network.accounts: [process.env.YOUR_PRIVATE_KEY],
sets the accounts for the zkEVM
network using the YOUR_PRIVATE_KEY
environment variable. This will allow the Hardhat project to deploy contracts and interact with the zkEVM testnet using the specified private key.In the root directory, you will find a directory named contracts
. Create a new file named SimpleVault.sol
, and paste the following code inside it:
This is a simple smart contract that allows us to deposit and withdraw ETH. All the functions are commented on for a better understanding.
In the scripts
directory inside the root of your project, you will find a file named deploy.js
. Replace its content with the following:
This is a simple deploy script that deploys the SimpleVault
smart contract to the zkEVM testnet, and returns the address of the newly deployed contract in the terminal. You can search for your contract on the Polygon zkEVM testnet explorer.
To run this script, execute the following command in the terminal:
We can interact with a deployed smart contract through Hardhat in two ways:
Let us do a bit of both.
Create a new file named interact.js
inside the scripts directory. Paste the following code inside it:
In this script, we will attach the address of the deployed smart contract to a local instance we initialize via Hardhat. We can then use the RPC URL and the private key we configured in the config file to send transactions and call functions to and from the contract.
You can see that we send 1 ETH to the contract. To execute the script, run the following command in the terminal:
Open another terminal inside the same directory and run the following command:
This will open up a Hardhat console that will allow us to interact with our smart contract via the command line. To connect the console to the deployed smart contract, run:
You can read the locked vault value from the smart contract by simply running this command in the console:
You can withdraw locked ETH from the console by running this command in the terminal:
And just like that, we used a Hardhat script to deposit ETH into a deployed smart contract, and used the Hardhat console to interact with the smart contract.
This tutorial guided you through bridging funds between the Sepolia testnet and the Polygon zkEVM testnet. We also deployed a smart contract to the zkEVM testnet using Hardhat.