matter-labs/hardhat-zksync

`hre.ethers.ContractFactory` and `hre.zksyncEthers.ContractFactory`ย don't work with zksync nodes

Closed this issue ยท 4 comments

๐Ÿ› Bug Report for hardaht-zksync plugins

๐Ÿ’ฅ Plugin name

I guess it's hardhat-zksync-ethers

๐Ÿ“ Description

I needed to deploy a contract from the bytecode in tests, and I tried using:

  1. hre.ethers.ContractFactory (I thought there was an interop)
  2. hre.zksyncEthers.ContractFactory
  3. signer.sendTransaction({ data: deploymentBytecode }) (I thought there was an interop)

All approaches failed with an error:

 (error={ "code": 3, "message": "Failed to serialize transaction: toAddressIsNull" }

Importing the ContractFactory directly from zksync-ethers worked.

๐Ÿ”„ Reproduction Steps

So, for me, the main bug here is that `hre.zksyncEthers.ContractFactory is not working as expected, all others are nice-to-haves. Therefore I'm only describing reproduction steps for that issue.

  1. In any hardhat project, create a helper function
export const getContractFactory = (
    hre: HardhatRuntimeEnvironment,
    abi: Interface | InterfaceAbi,
    bytecode: string,
    contractRunner?: ContractRunner,
) => {
    if (hre.network.zksync) {
        return new hre.zksyncEthers.ContractFactory(abi, bytecode, contractRunner);
    }

    return new hre.ethers.ContractFactory(abi, bytecode, contractRunner);
};
  1. Attempt to deploy any contract with the function like so:
const contractAddress = await getContractFactory(hre, abi, bytecode, signer)
                    .deploy()
                    .then((c) => c?.getAddress());
  1. Observe the error

๐Ÿค” Expected Behavior

The contract is deployed and no error is thrown

๐Ÿ˜ฏ Current Behavior

 (error={ "code": 3, "message": "Failed to serialize transaction: toAddressIsNull" }

๐Ÿ–ฅ๏ธ Environment

  • Node version: v20.10.0
  • Operating System & Version: macOS Sequoia 15.0 arm64
  • Other relevant environment details: All the latest hardhat/zksync-related package versions at the moment of writing the issue.

Hello @mmv08 ,

Which versions of zksync-ethers and hardhat-zksync-ethers are you using? Could you provide us with the GitHub repository so we can try it ourselves?

Hello @mmv08 ,

Which versions of zksync-ethers and hardhat-zksync-ethers are you using? Could you provide us with the GitHub repository so we can try it ourselves?

zksync-ethers@6.11.2
@matterlabs/hardhat-zksync-ethers@1.2.0

For the github repo you can try safe-global/safe-smart-account@7975f52 (notice the commit hash)

The function for getting the contract factory is in test/utils/contracts.ts and you can run the zk test suite with npm run test:zk

Hey @mmv08, please use the latest release of @matterlabs/hardhat-zksync-ethers, version 1.2.1.

Also, in your code, you dont have to use if statement to check weather network is zksync or not.
You can just use hre.ethers and it will execute proper functionality based on current network.

thnaks, I've just tested it and it works