2

in my solidity codebase I am using an interface and these are the types generated by TypeChain:

import { BaseContract, BigNumber, BigNumberish, BytesLike, CallOverrides, PopulatedTransaction, Signer, utils } from "ethers";
import { FunctionFragment, Result } from "@ethersproject/abi";
import { Listener, Provider } from "@ethersproject/providers";
import { TypedEventFilter, TypedEvent, TypedListener, OnEvent } from "./common";

export interface IHintHelpersInterface extends utils.Interface {
    contractName: "IHintHelpers";
    functions: {
        "getApproxHint(uint256,uint256,uint256)": FunctionFragment;
    };
    encodeFunctionData(functionFragment: "getApproxHint", values: [BigNumberish, BigNumberish, BigNumberish]): string;
    decodeFunctionResult(functionFragment: "getApproxHint", data: BytesLike): Result;
    events: {};
}

export interface IHintHelpers extends BaseContract {
    contractName: "IHintHelpers";
    connect(signerOrProvider: Signer | Provider | string): this;
    attach(addressOrName: string): this;
    deployed(): Promise<this>;
    interface: IHintHelpersInterface;
    queryFilter<TEvent extends TypedEvent>(event: TypedEventFilter<TEvent>, fromBlockOrBlockhash?: string | number | undefined, toBlock?: string | number | undefined): Promise<Array<TEvent>>;
    listeners<TEvent extends TypedEvent>(eventFilter?: TypedEventFilter<TEvent>): Array<TypedListener<TEvent>>;
    listeners(eventName?: string): Array<Listener>;
    removeAllListeners<TEvent extends TypedEvent>(eventFilter: TypedEventFilter<TEvent>): this;
    removeAllListeners(eventName?: string): this;
    off: OnEvent<this>;
    on: OnEvent<this>;
    once: OnEvent<this>;
    removeListener: OnEvent<this>;
    functions: {
        getApproxHint(_CR: BigNumberish, _numTrials: BigNumberish, _inputRandomSeed: BigNumberish, overrides?: CallOverrides): Promise<[
            string,
            BigNumber,
            BigNumber
        ] & {
            hintAddress: string;
            diff: BigNumber;
            latestRandomSeed: BigNumber;
        }>;
    };
    getApproxHint(_CR: BigNumberish, _numTrials: BigNumberish, _inputRandomSeed: BigNumberish, overrides?: CallOverrides): Promise<[
        string,
        BigNumber,
        BigNumber
    ] & {
        hintAddress: string;
        diff: BigNumber;
        latestRandomSeed: BigNumber;
    }>;
    callStatic: {
        getApproxHint(_CR: BigNumberish, _numTrials: BigNumberish, _inputRandomSeed: BigNumberish, overrides?: CallOverrides): Promise<[
            string,
            BigNumber,
            BigNumber
        ] & {
            hintAddress: string;
            diff: BigNumber;
            latestRandomSeed: BigNumber;
        }>;
    };
    filters: {};
    estimateGas: {
        getApproxHint(_CR: BigNumberish, _numTrials: BigNumberish, _inputRandomSeed: BigNumberish, overrides?: CallOverrides): Promise<BigNumber>;
    };
    populateTransaction: {
        getApproxHint(_CR: BigNumberish, _numTrials: BigNumberish, _inputRandomSeed: BigNumberish, overrides?: CallOverrides): Promise<PopulatedTransaction>;
    };
}
//# sourceMappingURL=IHintHelpers.d.ts.map

How would I use this to instantiate a contract using Typescript and ethers? Do I simply have to create a class implementing this interface?

Thank you

2 Answers 2

7

There are two ways you can go about this:

Method 1: (not using generated factory)

import { IHintHelpers } from '../typechain-types'; 

const contract = new ethers.Contract(address, abi, signer) as IHintHelpers;

Method 2: (using the generated factory in typechain)

import { IHintHelpers__factory } from '../typechain-types'; 

const contract = IHintHelpers__factory.connect(address, signer);
1
  • Method 2 worked great. Thank you very much
    – Jan Beneš
    May 26, 2022 at 0:02
0

Define the type as both BaseContract and the typechain type:

import { Contract, BaseContract } from 'ethers'
import { IHintHelpers } from '../typechain-types'; 

const contract = new Contract(address, abi, signer) as BaseContract as IHintHelpers;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.