Skip to content

Commit

Permalink
Allow Interface instances where InterfaceAbi are allowed (#4142).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Jun 13, 2023
1 parent 9055ef6 commit 2318005
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src.ts/contract/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down
5 changes: 5 additions & 0 deletions src.ts/contract/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 2318005

Please sign in to comment.