This plugins adds an zksync-ethers object to the Hardhat Runtime Environment.
This object has the same API as zksync-ethers, with some extra Hardhat-specific functionality.
Helpers
Helpers added to zksync-ethers object:
interfaceFactoryDeps {// A mapping from the contract hash to the contract bytecode. [contractHash:string]:string;}interfaceZkSyncArtifactextendsArtifact {// List of factory dependencies of a contract. factoryDeps:FactoryDeps;// Mapping from the bytecode to the ZKsync VM assembly (used for tracing). sourceMapping:string;}interfaceFactoryOptions { wallet?:zk.Wallet;}functionproviderL2: () =>zk.Provider;functionproviderL1: () =>ethers.Provider;functiongetWallet: (privateKeyOrIndex?:string|number) =>zk.Wallet;functiongetContractFactory: (name:string, wallet?:zk.Wallet, deploymentType?:DeploymentType) =>Promise<zk.ContractFactory>;functiongetContractFactory: (abi:any[], bytecode:ethers.BytesLike,wallet?:Wallet,deploymentType?:DeploymentType) =>Promise<zk.ContractFactory>;functiongetContractFactoryFromArtifact: (artifact:ZkSyncArtifact, wallet?:zk.Wallet, deploymentType?:DeploymentType) =>Promise<zk.ContractFactory>;functiongetContractAt: (nameOrAbi:string|any[], address:string|Address, wallet?:zk.Wallet) =>Promise<zk.Contract>;functiongetContractAtFromArtifact: (artifact:ZkSyncArtifact, address:string, wallet?:zk.Wallet) =>Promise<zk.Contract>;functiongetSigner: (address:string) =>zk.Signer;functiongetSigners: () =>zk.Signer[];functiongetImpersonatedSigner: (address:string) =>Promise<zk.Signer>;functionextractFactoryDeps: (artifact:ZkSyncArtifact) =>Promise<string[]>;functionloadArtifact: (name:string) =>Promise<ZkSyncArtifact>;functiondeployContract: (artifact:ZkSyncArtifact, constructorArguments:any[], wallet?:zk.Wallet, overrides?:ethers.Overrides, additionalFactoryDeps?:ethers.BytesLike[]) =>Promise<zk.Contract>;
providerL2() - returns a zk.Provider for L2, automatically connected to the selected network.
providerL1() - returns a ethers.Provider for L1, automatically connected to the selected network.
getWallet(privateKeyOrIndex?: string | number) - returns zk.Wallet for the given private key or index. If the network is set to local and the private key is not provided, the method will return a wallet for rich accounts with the default index of 0 or the specified index. If the accounts object is set in the hardhat config and the private key is not specified, the method will return the wallet for the given account with the default index 0 or for the specified index.
getWallets() - returns all wallets of type zk.Wallet. If the network is set to local, the method will return wallets for rich accounts. If the accounts object is set in the hardhat config for the used network, the method will return the wallets for the provided accounts.
getContractFactory(name: string, wallet?: zk.Wallet, deploymentType?: DeploymentType) - returns a zk.ContractFactory for provided artifact name.
getContractFactory: (abi: any[], bytecode: ethers.BytesLike,wallet?: Wallet,deploymentType?: DeploymentType) - returns a zk.ContractFactory for provided artifact abi and bytecode.
getContractFactoryFromArtifact(artifact: ZkSyncArtifact, wallet?: zk.Wallet, deploymentType?: DeploymentType) - returns a zk.ContractFactory for provided artifact.
getContractAt(nameOrAbi: string | any[], address: string | Address, wallet?: zk.Wallet) - returns zk.Contract for provided artifact name or abi and address of deployed contract
getContractAtFromArtifact: (artifact: ZkSyncArtifact, address: string, wallet?: zk.Wallet) - returns zk.ContractFactory for provided artifact and address of deployed contract
getImpersonatedSigner(address: string) - impersonates zk.Signer from address
loadArtifact(name: string) - load ZkSyncArtifact from contract name
extractFactoryDeps(artifact: ZkSyncArtifact) - extracts factory deps from artifact
deployContract(artifact: ZkSyncArtifact, constructorArguments: any[], wallet?: zk.Wallet, overrides?: ethers.Overrides, additionalFactoryDeps?: ethers.BytesLike[]) - deploys contract, for more details check out the deployment section of the quickstart.
If wallet?: zk.Wallet is not provided and if the network is set to local, the default wallet will be the first account in the list of rich accounts. If an accounts object is set in the hardhat config for the used network, the default wallet will be taken from that object.
If deploymentType?: DeploymentType is not provided default value will be create.
Usage
Install it and access zksync-ethers through the Hardhat Runtime Environment anywhere you need it (tasks, scripts, tests, etc). For example:
exportdefaultasyncfunction (hre:HardhatRuntimeEnvironment) {console.info(chalk.yellow(`Running deploy`));//automatically connected to the selected networkconstgasPrice=awaithre.zksyncEthers.providerL2.send("eth_gasPrice", []);//getContractFactory with default wallet, deploy contract and set new greeting messageconstgreeterFactory=awaithre.zksyncEthers.getContractFactory("Greeter");constgreeter=awaitgreeterFactory.deploy("Hello, world!");console.info(chalk.green(`Greeter deployed to: ${awaitgreeter.getAddress()}`));console.info(chalk.green(`Greeter greeting set to: ${awaitgreeter.greet()}`));consttx=awaitgreeter.setGreeting("Hello, world again!");awaittx.wait();console.info(chalk.green(`Greeter greeting set to: ${awaitgreeter.greet()}`));// deploy with provided wallet using loadArtifact and deployContractconstwallet=awaithre.zksyncEthers.getWallet("0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110");console.info(chalk.green(`Wallet address: ${awaitwallet.getAddress()}`));// deposit ETH from L1 to L2 to cover costs of deploymentconstdepositHandle=awaitwallet.deposit({ to:wallet.address, token:utils.ETH_ADDRESS, amount:ethers.parseEther("0.001"), });awaitdepositHandle.wait();constartifact=awaithre.zksyncEthers.loadArtifact("Greeter");constgreets=awaithre.zksyncEthers.deployContract(artifact, ["Hello, world!"], wallet);console.info(chalk.green(`Greeter deployed to: ${awaitgreets.getAddress()}`));console.info(chalk.green(`Greeter greeting set to: ${awaitgreets.greet()}`));}