Skip to content

Commit

Permalink
Added getStatic with support for inheritance of static methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Jun 12, 2019
1 parent 7164e51 commit 5e4535e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/abi/src.ts/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { arrayify, BytesLike, concat, hexDataSlice, hexlify, hexZeroPad, isHexSt
import { id } from "@ethersproject/hash";
import { keccak256 } from "@ethersproject/keccak256"
import * as errors from "@ethersproject/errors";
import { defineReadOnly, Description } from "@ethersproject/properties";
import { defineReadOnly, Description, getStatic } from "@ethersproject/properties";

import { AbiCoder, defaultAbiCoder } from "./abi-coder";
import { ConstructorFragment, EventFragment, Fragment, FunctionFragment, JsonFragment, ParamType } from "./fragments";
Expand Down Expand Up @@ -71,7 +71,7 @@ export class Interface {
return Fragment.from(fragment);
}).filter((fragment) => (fragment != null)));

defineReadOnly(this, "_abiCoder", new.target.getAbiCoder());
defineReadOnly(this, "_abiCoder", getStatic<() => AbiCoder>(new.target, "getAbiCoder")());

defineReadOnly(this, "functions", { });
defineReadOnly(this, "errors", { });
Expand Down
10 changes: 6 additions & 4 deletions packages/contracts/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { BigNumber, BigNumberish } from "@ethersproject/bignumber";
import { BytesLike, concat, hexlify, isBytes, isHexString } from "@ethersproject/bytes";
import { Zero } from "@ethersproject/constants";
import * as errors from "@ethersproject/errors";
import { defineReadOnly, deepCopy, resolveProperties, shallowCopy } from "@ethersproject/properties";
import { defineReadOnly, deepCopy, getStatic, resolveProperties, shallowCopy } from "@ethersproject/properties";
import { UnsignedTransaction } from "@ethersproject/transactions";


Expand Down Expand Up @@ -402,6 +402,8 @@ class WildcardRunningEvent extends RunningEvent {

export type ContractInterface = string | Array<Fragment | JsonFragment | string> | Interface;

type InterfaceFunc = (contractInterface: ContractInterface) => Interface;

export class Contract {
readonly address: string;
readonly interface: Interface;
Expand Down Expand Up @@ -437,8 +439,8 @@ export class Contract {

// @TODO: Maybe still check the addressOrName looks like a valid address or name?
//address = getAddress(address);

defineReadOnly(this, "interface", new.target.getInterface(contractInterface));
console.log(getStatic(new.target, "getInterface"));
defineReadOnly(this, "interface", getStatic<InterfaceFunc>(new.target, "getInterface")(contractInterface));

if (Signer.isSigner(signerOrProvider)) {
defineReadOnly(this, "provider", signerOrProvider.provider || null);
Expand Down Expand Up @@ -850,7 +852,7 @@ export class ContractFactory {
}

defineReadOnly(this, "bytecode", bytecodeHex);
defineReadOnly(this, "interface", new.target.getInterface(contractInterface));
defineReadOnly(this, "interface", getStatic<InterfaceFunc>(new.target, "getInterface")(contractInterface));
defineReadOnly(this, "signer", signer || null);
}

Expand Down
10 changes: 10 additions & 0 deletions packages/properties/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ export function defineReadOnly(object: any, name: string, value: any): void {
});
}

// Crawl up the constructor chain to find a static method
export function getStatic<T>(ctor: any, key: string): T {
for (let i = 0; i < 32; i++) {
if (ctor[key]) { return ctor[key]; }
if (!ctor.prototype || typeof(ctor.prototype) !== "object") { break; }
ctor = Object.getPrototypeOf(ctor.prototype).constructor;
}
return null;
}

export function resolveProperties(object: any): Promise<any> {
let result: any = {};

Expand Down

0 comments on commit 5e4535e

Please sign in to comment.