Deploy your dApp
This guide shows you how to deploy and interact with a smart contract on PlayChain. This requires some preexisting knowledge of how to develop EVM smart contracts in general.
Following the below instructions will allow you to deploy and interact with a smart contract on PlayChain in less than 5 minutes. It will help you get familiar with the zkSync development toolbox which can be used to develop PlayFi projects.
This is what we're going to do:
Fund your wallet with PlayChain testnet ETH.
Use zksync-cli to scaffold a new project.
Build a smart contract that stores a greeting message and deploy it to PlayChain Albireo testnet.
Run a script to retrieve and update the greeting message using zksync-ethers.
Prerequisites
Make sure your machine satisfies the system requirements.
Download and install Node.js.
Use the
yarn
ornpm
package manager. We recommend usingyarn
. To installyarn
, follow the Yarn installation guide.Learn how to get your private key from your Metamask wallet as we'll use it to deploy a contract.
Some testnet ETH (guide)
Create the project
Run the following command in your terminal to create a new project using zkSync CLI.
It will give you options for different types of projects but for this tutorial choose the the following:
The private key of your wallet will be included in the .env
file of the project and won't be pushed to GitHub.
The project structure is pretty straight forward:
hardhat.config.ts
contains the general configuration for Hardhat and the zkSync plugins, which are already imported and setup./contracts
contains smart contracts.zksync-cli
provides common examples like an ERC20, an NFT, and the Greeter contract that we'll use later on./deploy
contains the deployment scripts.
Add a new network entry in the hardhat.config.ts file:
Set the defaultNetwork to this network:
For this tutorial we'll focus on the /contracts/Greeter.sol
contract:
As you can see, it a simple Solidity contract with two methods to read a message, greet()
, and modify it, setGreeting()
.
Takeaway
PlayFi is EVM compatible. You can write smart contracts with Solidity or Vyper and use existing popular libraries like OpenZeppelin.
Compile the contract
Smart contracts deployed to PlayFi must be compiled using our custom compilers:
zksolc
for Solidity contracts.zkvyper
for Vyper contracts.
As this is a Solidity project, it already has the hardhat-zksync-solc
plugin installed and configured so there's nothing you need to setup. To compile the contracts in the project, run the following command:
You'll get the following output:
The compiled artifacts will be located in the /artifacts-zk
folder. These artifacts are similar to the ones generated by the Solidity compiler. For example, the ABI of the Greeter contract will be located in /artifacts-zk/contracts/Greeter.sol/Greeter.json
.
Takeaway
Smart contracts deployed to PlayFi must be compiled using zksolc
or zkvyper
as they generate a custom bytecode compatible with PlayFi's ZKEVM.
The configuration for the zksolc
compiler is located in the zksolc
section of the hardhat.config.ts
file. You can find more info about the compiler settings in the hardhat-zksync-solc plugin and the compiler section of the ZK Stack documentation.
Deploy and verify
The project also contains a script to deploy and verify the contract in /deploy/deploy.ts
. Under the hood, this script uses hardhat-zksync-deploy
and hardhat-zksync-verify
for deployment and contract verification.
To execute it, just run:
You'll get the following output:
🥳 Congratulations! You just deployed a smart contract to PlayFi Albireo testnet. You can find it in the PlayFi Albireo explorer by searching the contract address.
In addition, the deployment script verified the contract automatically so you can see the source code in the contract tab of the block explorer.
Interact with the contract
The project also comes with a script to interact with the contract in /deploy/interact.ts
. Add the address of the Greeter contract you just deployed in the CONTRACT_ADDRESS
variable inside the /deploy/interact.ts
file:
As you can see, we're simply using ethers
to interact with our contract. PlayFi is EVM compatible so you can use existing tools and libraries like Hardhat
, ethers
, web3.js
, and users can use their existing wallets like Metamask, Rabby or Zerion.
Takeaway
Existing libraries like ethers
and web3.js
can be used to interact with smart contracts deployed on zkSync.
To execute the /deploy/interact.ts
script, run:
You'll get the following output:
Congratulations! You've retrieved and updated the message on the Greeter
contract. You can see the transaction in the PlayFi Albireo explorer by searching the transaction hash.
Takeaways
PlayFi is built on the ZKStack and is therefore EVM compatible. This means you can write smart contracts in Solidity or Vyper, use Hardhat, libraries like Ethers and Web3.js, or wallets like Metamask and Rabby.
zkSync CLI provides a quick way to scaffold different types of projects thanks to its multiple templates.
Contracts deployed to PlayFi are compiled using
zksolc
orzkvyper
as they generate a special bytecode for PlayFi's ZKEVM.
Last updated