ethers-io / ethers.js

Complete Ethereum library and wallet implementation in JavaScript.
https://ethers.org/
MIT License
7.96k stars 1.85k forks source link

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

Open gabrielstoica opened 9 months ago

gabrielstoica commented 9 months ago

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 commented 9 months ago

Up @ricmoo

ricmoo commented 9 months ago

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.

gabrielstoica commented 9 months ago

Thanks, @ricmoo - looking forward to it!

Toomaie commented 5 months ago

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

FabijanC commented 4 months ago

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