Allow Interface instances where InterfaceAbi are allowed (#4142).

This commit is contained in:
Richard Moore 2023-06-13 16:22:40 -04:00
parent 9055ef6c69
commit 2318005dfd
2 changed files with 16 additions and 3 deletions

@ -768,6 +768,14 @@ export class BaseContract implements Addressable, EventEmitterable<ContractEvent
return new BaseContract(this.target, this.interface, runner);
}
/**
* Return a new Contract instance with the same ABI and runner, but
* a different %%target%%.
*/
attach(target: string | Addressable): BaseContract {
return new BaseContract(target, this.interface, this.runner);
}
/**
* Return the resolved address of this Contract.
*/
@ -1022,7 +1030,7 @@ export class BaseContract implements Addressable, EventEmitterable<ContractEvent
/**
* Create a new Class for the %%abi%%.
*/
static buildClass<T = ContractInterface>(abi: InterfaceAbi): new (target: string, runner?: null | ContractRunner) => BaseContract & Omit<T, keyof BaseContract> {
static buildClass<T = ContractInterface>(abi: Interface | InterfaceAbi): new (target: string, runner?: null | ContractRunner) => BaseContract & Omit<T, keyof BaseContract> {
class CustomContract extends BaseContract {
constructor(address: string, runner: null | ContractRunner = null) {
super(address, abi, runner);
@ -1034,14 +1042,14 @@ export class BaseContract implements Addressable, EventEmitterable<ContractEvent
/**
* Create a new BaseContract with a specified Interface.
*/
static from<T = ContractInterface>(target: string, abi: InterfaceAbi, runner?: null | ContractRunner): BaseContract & Omit<T, keyof BaseContract> {
static from<T = ContractInterface>(target: string, abi: Interface | InterfaceAbi, runner?: null | ContractRunner): BaseContract & Omit<T, keyof BaseContract> {
if (runner == null) { runner = null; }
const contract = new this(target, abi, runner );
return contract as any;
}
}
function _ContractBase(): new (target: string, abi: InterfaceAbi, runner?: null | ContractRunner) => BaseContract & Omit<ContractInterface, keyof BaseContract> {
function _ContractBase(): new (target: string, abi: Interface | InterfaceAbi, runner?: null | ContractRunner) => BaseContract & Omit<ContractInterface, keyof BaseContract> {
return BaseContract as any;
}

@ -9,6 +9,7 @@ import {
import { BaseContract, copyOverrides, resolveArgs } from "./contract.js";
import type { InterfaceAbi } from "../abi/index.js";
import type { Addressable } from "../address/index.js";
import type { ContractRunner } from "../providers/index.js";
import type { BytesLike } from "../utils/index.js";
@ -65,6 +66,10 @@ export class ContractFactory<A extends Array<any> = Array<any>, I = BaseContract
});
}
attach(target: string | Addressable): BaseContract & Omit<I, keyof BaseContract> {
return new (<any>BaseContract)(target, this.interface, this.runner);
}
/**
* Resolves to the transaction to deploy the contract, passing %%args%%
* into the constructor.