ContractFactory
The ContractFactory
in ZKsync is a utility that facilitates the deployment of smart contracts. It handles contract
creation and initialization with various deployment methods.
Basic deployment methods
create
Deploy a contract using the basic create
method.
Example
import { ContractFactory, Provider, Wallet } from "zksync-ethers";
import { ethers } from "ethers";
const provider = Provider.getDefaultProvider();
const wallet = new Wallet("<PRIVATE_KEY>", provider);
const abi = [ /* contract ABI */ ];
const bytecode = "0x..."; // contract bytecode
const factory = new ContractFactory(abi, bytecode, wallet);
async function deployContract() {
const contract = await factory.deploy();
await contract.deployed();
console.log("Contract deployed at address:", contract.address);
}
deployContract();
create2
Deploy a contract using the create2
method, which allows you to specify a salt for deterministic deployment.
Example
const salt = ethers.utils.id("some unique salt");
async function deployContractCreate2() {
const contract = await factory.getDeployTransaction(salt);
await contract.deployed();
console.log("Contract deployed at address:", contract.address);
}
deployContractCreate2();
Account deployment methods
createAccount
Deploy a contract to create an account using the createAccount
method.
Example
const accountAbi = [ /* account contract ABI */ ];
const accountBytecode = "0x..."; // account contract bytecode
const accountFactory = new ContractFactory(accountAbi, accountBytecode, wallet);
async function createAccount() {
const account = await accountFactory.deploy();
await account.deployed();
console.log("Account contract deployed at address:", account.address);
}
createAccount();
create2Account
Deploy a contract to create an account using the create2Account
method with a specified salt for deterministic deployment.
Example
const salt = ethers.utils.id("another unique salt");
async function createAccountWithCreate2() {
const account = await accountFactory.getDeployTransaction(salt);
await account.deployed();
console.log("Account contract deployed at address:", account.address);
}
createAccountWithCreate2();
Advanced deployment with constructor arguments
If your contract or account requires constructor arguments, you can pass them during deployment.
Example for create
with constructor arguments
async function deployContractWithArgs(arg1, arg2) {
const contract = await factory.deploy(arg1, arg2);
await contract.deployed();
console.log("Contract with args deployed at address:", contract.address);
}
deployContractWithArgs("Argument1", 123);
Example for create2
with Constructor Arguments
async function deployContractCreate2WithArgs(arg1, arg2) {
const contract = await factory.getDeployTransaction(salt, arg1, arg2);
await contract.deployed();
console.log("Contract with args deployed at address:", contract.address);
}
deployContractCreate2WithArgs("Argument1", 123);
Deployment using a deployed library
If your contract depends on a library, you must link the library before deploying the contract.
Example
const libraryAbi = [ /* library ABI */ ];
const libraryBytecode = "0x..."; // library bytecode
const libraryFactory = new ContractFactory(libraryAbi, libraryBytecode, wallet);
async function deployContractWithLibrary() {
const library = await libraryFactory.deploy();
await library.deployed();
console.log("Library deployed at address:", library.address);
const linkedBytecode = bytecode.replace(/__LibraryName__/g, library.address.replace("0x", ""));
const factory = new ContractFactory(abi, linkedBytecode, wallet);
const contract = await factory.deploy();
await contract.deployed();
console.log("Contract with library deployed at address:", contract.address);
}
deployContractWithLibrary();
Deployment and interaction
After deploying a contract, you can interact with it using the contract instance.
Example
async function deployAndInteract() {
const contract = await factory.deploy();
await contract.deployed();
console.log("Contract deployed at address:", contract.address);
const response = await contract.someFunction("someParameter");
console.log("Function response:", response);
}
deployAndInteract();