Web3JS

Smart Contracts

Deploy smart contracts to the ZKsync Era network

The Web3.js plugin for ZKsync provides a ContractFactory class that can be used to deploy smart contracts to the ZKsync Era network. The ContractFactory class can be used with all four deployment types supported by the ZKsync Era network:

  • create: Deploy a regular smart contract with a non-deterministic address. (default deployment type)
  • create2: Deploy a regular smart contract with a deterministic address.
  • createAccount: Deploy a smart contract for a smart account with a non-deterministic address.
  • create2Account: Deploy a smart contract for a smart account with a deterministic address.
Learn about using smart accounts with the Web3.js plugin for ZKsync!

Create a contract factory

The following code sample demonstrates creating a new ContractFactory and using it to deploy a simple smart contract (keep reading to learn about more deployment options):

import { Bytes, Contract, ContractAbi, Web3 } from "web3";
import {
  ContractFactory,
  types,
  Web3ZKsyncL2,
  ZKsyncPlugin,
  ZKsyncWallet,
} from "web3-plugin-zksync";

async function main() {
  const web3: Web3 = new Web3(/* optional L1 provider */);
  web3.registerPlugin(
    new ZKsyncPlugin(
      Web3ZKsyncL2.initWithDefaultProvider(types.Network.Sepolia),
    ),
  );
  const zksync: ZKsyncPlugin = web3.ZKsync;

  const PRIVATE_KEY: string = "<PRIVATE_KEY>";
  const wallet: ZKsyncWallet = new zksync.Wallet(PRIVATE_KEY);

  // replace with actual values
  const contractAbi: ContractAbi = [];
  const constractByteCode: Bytes = "";

  // create a ContractFactory that uses the default create deployment type
  const contractFactory: ContractFactory<ContractAbi> = new ContractFactory(
    contractAbi,
    constractByteCode,
    wallet,
  );

  // or specify the deployment type
  // const contractFactory: ContractFactory<ContractAbi> = new ContractFactory(
  //   contractAbi,
  //   constractByteCode,
  //   wallet,
  //   "createAccount",
  // );

  const contract: Contract<ContractAbi> = await contractFactory.deploy();
  console.log("Contract methods:", contract.methods);
}

main()
  .then(() => console.log("✅ Script executed successfully"))
  .catch((error) => console.error(`❌ Error executing script: ${error}`));

Deploy a smart contract

Some smart contracts require constructor parameters to be supplied when they are deployed. The following code snippet demonstrates deploying a smart contract with constructor parameters:

// deploy a smart contract with an array of constructor parameters
const contract: Contract<ContractAbi> = await contractFactory.deploy([
  arg1,
  arg2,
]);

Smart contracts that are deployed using a ContractFactory that was created with the create2 or create2Account deployment type must specify a "salt" value when they are deployed. The following code snippets demonstrates deploying a smart contract with a provided salt value:

// deploy a smart contract with a salt value
const contract: Contract<ContractAbi> = await contractFactory.deploy(
  [
    /* empty array or constructor parameters */
  ],
  {
    customData: { salt: "salt" },
  },
);

Interact with a smart contract

The return type of the ContractFactory.deploy method is the Web3.js Contract class. The smart contract's methods can be accessed with the Contract.methods property, as demonstrated by the following code snippet:

const returnValue = await contract.methods
  .contractMethod(/* method parameters, if any */)
  .call();

Made with ❤️ by the ZKsync Community