Hello ZKsync!
Learn to deploy smart contracts efficiently in the Validium environment.
Welcome to Validium 101 for deploying smart contracts on Validium! 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 node.
Interact with the contract with ZKsync CLI
Let's dive in and start your developer journey on Validium!
This series requires some initial setup of tools to enhance your development experience building for Validium. Make sure to go through the setup provided in the initial Getting started section.
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.
Run the compile script from your project in the terminal:
npm
Upon successful compilation, you'll receive output detailing the zksolc
and solc
versions used during compiling and the number of Solidity files compiled.
The compiled artifacts will be located in the /artifacts-zk
folder.
Smart contracts deployed to ZKsync must be compiled using our custom compiler. zksolc
is the compiler used for Solidity. This is why they're placed in the artifacts-zk
folder!
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
Upon successful deployment, you'll receive output detailing the deployment process, including the contract address, source, and encoded constructor arguments:
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.
We can confirm the amount by calling the getFundingGoal
method using ZKsync CLI.
In the terminal, run the following in your project directory, replacing the contract address with your contract's address:
The CLI will prompt you with a list of available methods to select from. Navigate with the arrow keys and press Enter on the method getFundingGoal()
.
The
--chain
option defines the network we want to use, which is our local in-memory node.The
--contract
is the address of the contract you just deployed.The
--abi
provides the ABI to decode the contract and provide the list of methods to select from. Without the ABI provided, you will have to manually type out the method name.
You will get a response with the amount that we passed in to the constructor on deploy:
Let's fund this crowdfunding campaign and make Zeek happy!
We will write to the contract using ZKsync CLI, which requires a private key. In this demonstration we will use the second wallet provided in the list of rich wallets.
In the terminal, run the following command with your deployed contract's address:
In the prompt, press Enter on the contribute() payable
method. The CLI will then output the transaction information upon success.
We can read the transaction data of the contribution with the following:
Our crowdfund has reached its funding goal! Let's withdraw the funds for the owner:
The CLI will prompt for the method to call, we will call withdrawFunds()
:
Congratulations! You've deployed a crowdfunding contract and learned how to interact with the deployed contract using ZKsync CLI!
Takeaways
EVM Compatibility: Validium is EVM compatible and you can write smart contracts in Solidity or Vyper.
Custom Compilation: Contracts deployed to Validium are compiled using
zksolc
orzkvyper
as they generate a special bytecode for Validium's ZKEVM.Development Tools: In memory 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 Validium, you're well on your way to becoming a proficient Validium 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 Validium 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.
Last updated