Hello ZKsync!
Welcome to ZKsync 101 for deploying smart contracts on ZKsync! In this series, we'll walk you through the process of creating and deploying a simple smart contract that creates a crowdfunding campaign for Zeek. In this section you will learn how to:
Craft a smart contract to fund Zeek's latest adventure.
Deploy the contract onto your local in-memory anvil-zksync
node.
Interact with the contract with ZKsync CLI
Let's dive in and start your developer journey on ZKsync!
Compile the CrowdfundingCampaign.sol
contract
To compile and deploy to ZKsync you can use either EVM or EraVM:
Compile the CrowdfundingCampaign.sol contract
This guide introduces a crowdfunding campaign contract aimed at supporting Zeek's creative ventures.
Let's start by reviewing the starter contract CrowdfundingCampaign.sol
in the
contracts/1-hello-zksync/CrowdfundingCampaign.sol
directory.
The CrowdfundingCampaign
contract is designed for a simple crowdfunding campaign.
This contract features:
- A constructor to initialize the campaign's funding target.
- The
contribute
method to log funds, triggeringContributionReceived
andGoalReached
events. - The
withdrawFunds
method, allowing the owner to collect accumulated funds post-goal achievement. - The
getTotalFundsRaised
method to return the total amount of funds that's been raised. - The
getFundingGoal
method to return the goal for the campaign to reach.
npm run compile
Upon successful compilation, you'll receive output detailing the typings and Solidity files compiled.
Generating typings for: 20 artifacts in dir: typechain-types for target: ethers-v6
Successfully generated 78 typings!
Compiled 20 Solidity files successfully
The compiled artifacts will be located in the /artifacts
folder.
Run a local node
For this section, we will deploy to a local Hardhat node. Run the command below to start a persistent in-memory node:
npx hardhat node
Configuring a Hardhat Wallet
Since we are using a local Hardhat in-memory node for development, we can use one of the rich wallets for transactions and deployments.
Copy one of the private keys logged after starting the node to the .env
file, using the .env.example
file as an example.
Deploy the contract
The deployment script is located at
/deploy/1-hello-zksync/deploy.ts
.
Key Components:
- contractArtifactName: Identifies the
CrowdfundingCampaign
contract for deployment. - constructorArguments: Sets initialization parameters for the contract. In this case,
the fundraising goal, converted from ether to wei to match Solidity's
uint256
type.
Execute the deployment command from package.json
.
npm run deploy:hello-zksync
Upon successful deployment, you'll receive output showing the contract name and address.
Deploying CrowdfundingCampaign contract to localhost
CrowdfundingCampaign deployed to 0x5FbDB2315678afecb367f032d93F642f64180aa3
Interact with the contract
Now that our contract is deployed to our local in-memory node, let's interact with it!
We initially set up the crowdfunding campaign with an amount of .02 ETH
for the goal.
Interact with contract
Let's try interacting with the deployed contract.
In the deploy/1-hello-zksync/interact.ts
script, use your deployed contract address for the CONTRACT_ADDRESS
variable.
Then, run the script:
npm run interact:hello-zksync
Congratulations! You've deployed a crowdfunding contract and learned how to interact with the deployed contract using Hardhat!
Takeaways
- EVM Compatibility: ZKsync is EVM compatible and you can write smart contracts in Solidity or Vyper.
- Custom Compilation: Contracts deployed to ZKsync are compiled using
zksolc
orzkvyper
as they generate a special bytecode for ZKsync's ZKEVM. - Development Tools: in-memory
anvil-zksync
node is a quick and easy local node environment to deploy to, saving costs on deployment while developing. - ZKsync CLI: A powerful command line tool to interact with contracts easily.
Next steps
Having successfully deployed your first contract on ZKsync, you're well on your way to becoming a proficient ZKsync developer. To expand your expertise:
- Explore Contract Factories: Enhance your project by building a contract factory
for the
CrowdfundingCampaign
contract in the next guide. This will allow you to efficiently manage multiple crowdfunding campaigns, each with their own unique parameters. - Dive Deeper into ZKsync Features: Investigate advanced ZKsync features such as account abstraction, and paymasters.
- Join the Community: Engage with the ZKsync developer community through forums, Discord channels, Dev Discussions, or GitHub repositories. Share your experiences, ask questions, and collaborate on projects.