Contract Factory

Learn how to deploy and manage multiple smart contracts on ZKsync using a contract factory.

This second ZKsync 101 installment advances from your introductory exploration of smart contract deployment to dive into the utility of contract factories. Through this guide, you'll learn how to streamline the deployment of multiple crowdfunding campaigns using a single contract factory, leveraging the foundational CrowdfundingCampaign contract in the first guide.

Advance your ZKsync development journey with contract factories.

Construct a contract factory to create multiple crowdfunding campaigns.

Seamlessly deploy your contract factory on ZKsync Era using Hardhat.

Let's explore the efficiency and scalability that contract factories bring.

What is a contract factory?

A contract factory is a design pattern that allows for the creation of multiple contract instances from a single "factory" contract. It's essentially a contract that creates other contracts, streamlining and organizing the deployment of numerous similar contracts efficiently.


Setup the project

Make sure to go through the setup provided in the initial Getting started section. You will have downloaded the 101 project through ZKsync CLI create and started up a local in memory node for development.

If you haven't started up your local in memory node or you're not sure, run the following:

zksync-cli dev restart

Compile the contracts

This section will focus on compiling and deploying the CrowdfundingFactory.sol contract that is provided under the /contracts/2-contract-factory directory.

The CrowdfundingFactory.solcontract will be used to deploy multiple instances of the CrowdfundingCampaign.sol contract from the previous guide. This contract factory approach streamlines the deployment of crowdfunding campaigns, making it efficient to launch and manage multiple campaigns.

The CrowdfundingFactory contract automates the creation and oversight of CrowdfundingCampaign contracts, each with their own distinct funding goals. The factory contract features:

  • Campaign Creation: Utilizes the createCampaign method to initiate a new CrowdfundingCampaign contract. This function takes a fundingGoal as an argument, deploys a new campaign contract with this goal, and tracks the created campaign in the campaigns array.
  • Campaign Tracking: The getCampaigns method offers a view into all the campaigns created by the factory, allowing for easy access and management of multiple crowdfunding initiatives.

Run the compile script from your project in the terminal:

npm
npm run compile

Deploy CrowdfundingCampaigns via the CrowdfundingFactory

This section outlines the steps to deploy the CrowdfundingCampaign contract using our new CrowdfundingFactory.

The deployment script is located at /deploy/2-contract-factory/deploy.ts.

The deploy script:

  • deploys a single CrowdfundingFactory.
  • saves an instance of the deployed factory to factoryContract. This gives us access to the factory's functionalities.
  • The createCampaign method is called on this instance to create and deploy a new crowdfunding campaign contract.

Run the deployment command.

npm
npm run deploy:crowdfunding-factory

Upon successful deployment, you'll receive output detailing the deployment process, including the contract address, source, and encoded constructor arguments:

Starting deployment process of "CrowdfundingFactory"...
Estimated deployment cost: 0.0002500236 ETH

"CrowdfundingFactory" was successfully deployed:
 - Contract address: <0xFACTORY_CONTRACT_ADDRESS>
 - Contract source: contracts/CrowdfundFactory.sol:CrowdfundingFactory
 - Encoded constructor arguments: 0x

 Deployment and campaign creation complete!

Create another campaign with ZKsync CLI

We've got one crowdfunding campaign created from the deployment script, let's create another through ZKsync CLI!

Run the following command:

zksync-cli contract write \
--chain in-memory-node \
--contract <0xFACTORY_CONTRACT_ADDRESS> \
--pk 0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110

The CLI will prompt you with a few questions. We want to call the method createCampaign(uint256 fundingGoal) and provide a funding goal in wei:

? Enter method to call createCampaign(uint256 fundingGoal)
? Provide method arguments:
? [1/1] fundingGoal (uint256) 200000000000000000

Now that we've created a second crowdfunding campaign, let's check the list of campaigns!

Run the following command from within your project directory.

zksync-cli contract read \
--chain in-memory-node \
--contract <0xFACTORY_CONTRACT_ADDRESS> \
--abi artifacts-zk/contracts/2-contract-factory/CrowdfundingFactory.sol/CrowdfundingFactory.json
We pass the ABI in to help decode the output. Without the ABI, the CLI will return the raw response.

The CLI will prompt with a list of the available methods from the contract. Navigate with the arrow keys and press Enter on getCampaigns() view returns (address[]).

The ClI returns the method's response in raw format along with the decoded format because we passed in the ABI.

You should see two addresses in the decoded method response, the first created from the deploy script and the second for the campaign you created with ZKsync CLI!

 Method response (raw): 0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000020000000000000000000000009bf2281f53847387e1d8b5645cfcfbea576c4397000000000000000000000000ca55c6bbb6b122058ed742c33859f3621bc8030c
 Decoded method response: 0x9bf2281F53847387e1d8b5645cFCfbea576c4397,0xca55c6BBB6B122058Ed742C33859F3621bc8030c

Takeaways

  • Contract Factories: Utilizing contract factories significantly streamlines the deployment process, allowing for the creation of multiple instances of a contract, like the CrowdfundingCampaign, with varied parameters.
  • Scalability and Management: Contract factories offer a scalable solution to manage numerous contract instances, enhancing project organization and efficiency.
  • Event-Driven Insights: The CampaignCreated event in the factory contract provides a transparent mechanism to track each crowdfunding campaign's deployment, useful for off-chain monitoring and interaction.

Next steps

With the contract factory in your ZKsync development arsenal, you're set to elevate your smart contract projects. Here's how you can further your journey:

  • Contract Testing: Progress to the next guide focused on testing your contracts. Ensuring the reliability and security of your CrowdfundingCampaign through comprehensive tests is critical.
  • Advanced ZKsync Integrations: Explore deeper into ZKsync's ecosystem by implementing features like account abstraction and paymasters to enhance user experience and contract flexibility.
  • Community Engagement and Contribution: Join the vibrant ZKsync developer community. Participate in forums, Discord, or GitHub discussions. Sharing insights, asking queries, and contributing can enrich the ecosystem and your understanding of ZKsync.

Made with ❤️ by the ZKsync Community