Added getStatic with support for inheritance of static methods.

This commit is contained in:
Richard Moore 2019-06-11 23:42:35 -04:00
parent 7164e51131
commit 5e4535e939
No known key found for this signature in database
GPG Key ID: 525F70A6FCABC295
3 changed files with 18 additions and 6 deletions

@ -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";
@ -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", { });

@ -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";
@ -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;
@ -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);
@ -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);
}

@ -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 = {};