ethers.js icon indicating copy to clipboard operation
ethers.js copied to clipboard

How to specify the typed interface when deploying a contract with new ContractFactory?

Open gabrielstoica opened this issue 1 year ago • 5 comments

Ethers Version

6.10.0

Search Terms

abi, type

Describe the Problem

Having the following code snippet, when trying to deploy a custom Box contract, I'm getting the following type error: Type 'BaseContract & Omit<ContractInterface, keyof BaseContract>' is missing the following properties from type 'Box': UPGRADE_INTERFACE_VERSION, foo, initialize, owner, and 5 more.

How should one specify the contract type when deploying using new ContractFactory() so all the Box-specific methods should be included and therefore suggested? (e.g. box.owner(), box.version())

Code Snippet

import { Box, Box__factory } from "typechain-types";
import { upgrades } from "hardhat";
import { expect } from "chai";

describe("Box", function () {
  let box: Box;

  before(async function () {
    const boxFactory = new ContractFactory(Box__factory.abi, Box__factory.bytecode);
    box = await upgrades.deployProxy(boxFactory, [], { // error Type 'BaseContract & Omit<ContractInterface, keyof BaseContract>' is missing the following properties
      kind: "uups",
      initializer: "initialize",
    });
  });

  it("should set the correct owner", async function () {
    expect(await box.owner()).to.eq(signers.deployer.address); // box is casted 
  });

  it("should retrieve the current version", async function () {
    expect(await box.version()).to.eq("1.0.0");
  });
});

Contract ABI

No response

Errors

Type 'BaseContract & Omit<ContractInterface, keyof BaseContract>' is missing the following properties from type 'Box': UPGRADE_INTERFACE_VERSION, foo, initialize, owner, and 5 more.

Environment

Hardhat

Environment (Other)

No response

gabrielstoica avatar Jan 29 '24 09:01 gabrielstoica

Up @ricmoo

gabrielstoica avatar Feb 05 '24 06:02 gabrielstoica

I’ll get some docs up soon on typing in Ethers.

I’m also working on a version of Contracts based on AbiType that can automatically generate types from the ABI.

ricmoo avatar Feb 05 '24 07:02 ricmoo

Thanks, @ricmoo - looking forward to it!

gabrielstoica avatar Feb 05 '24 07:02 gabrielstoica

Any update on this one, or can you provide an example of how we should address this @ricmoo ? Thanks!

Toomaie avatar May 19 '24 08:05 Toomaie

Also looking forward to seeing some examples. Especially since downgrading to 5.7.2 reports using a highly vulnerable version of the ws dependency.

FabijanC avatar Jun 24 '24 16:06 FabijanC