From 2318005dfd996c8a7c51603d0264ceabe9bb6141 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Tue, 13 Jun 2023 16:22:40 -0400 Subject: [PATCH] Allow Interface instances where InterfaceAbi are allowed (#4142). --- src.ts/contract/contract.ts | 14 +++++++++++--- src.ts/contract/factory.ts | 5 +++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src.ts/contract/contract.ts b/src.ts/contract/contract.ts index a23545c6a8..4157121631 100644 --- a/src.ts/contract/contract.ts +++ b/src.ts/contract/contract.ts @@ -768,6 +768,14 @@ export class BaseContract implements Addressable, EventEmitterable(abi: InterfaceAbi): new (target: string, runner?: null | ContractRunner) => BaseContract & Omit { + static buildClass(abi: Interface | InterfaceAbi): new (target: string, runner?: null | ContractRunner) => BaseContract & Omit { 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(target: string, abi: InterfaceAbi, runner?: null | ContractRunner): BaseContract & Omit { + static from(target: string, abi: Interface | InterfaceAbi, runner?: null | ContractRunner): BaseContract & Omit { 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 { +function _ContractBase(): new (target: string, abi: Interface | InterfaceAbi, runner?: null | ContractRunner) => BaseContract & Omit { return BaseContract as any; } diff --git a/src.ts/contract/factory.ts b/src.ts/contract/factory.ts index 16709ac09c..ee957bb0a5 100644 --- a/src.ts/contract/factory.ts +++ b/src.ts/contract/factory.ts @@ -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 = Array, I = BaseContract }); } + attach(target: string | Addressable): BaseContract & Omit { + return new (BaseContract)(target, this.interface, this.runner); + } + /** * Resolves to the transaction to deploy the contract, passing %%args%% * into the constructor.