Skip to content

Commit

Permalink
Moved away from isNamedInstance which breaks after Browserify name ma…
Browse files Browse the repository at this point in the history
…ngling.
  • Loading branch information
ricmoo committed Jun 11, 2019
1 parent e6f6383 commit 257d67c
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 217 deletions.
46 changes: 40 additions & 6 deletions packages/abi/src.ts/fragments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { BigNumber } from "@ethersproject/bignumber";
import * as errors from "@ethersproject/errors";
import { defineReadOnly, isNamedInstance } from "@ethersproject/properties";
import { defineReadOnly } from "@ethersproject/properties";


export interface JsonFragmentType {
Expand Down Expand Up @@ -238,6 +238,8 @@ export class ParamType {
readonly arrayLength: number;
readonly arrayChildren: ParamType;

readonly _isParamType: boolean;

constructor(constructorGuard: any, params: any) {
if (constructorGuard !== _constructorGuard) { throw new Error("use fromString"); }
populate(this, params);
Expand All @@ -259,6 +261,10 @@ export class ParamType {
baseType: ((this.components != null) ? "tuple": this.type)
});
}

this._isParamType = true;

Object.freeze(this);
}

// Format the parameter fragment
Expand Down Expand Up @@ -298,7 +304,7 @@ export class ParamType {
}

static fromObject(value: JsonFragmentType | ParamType): ParamType {
if (isNamedInstance<ParamType>(ParamType, value)) { return value; }
if (ParamType.isParamType(value)) { return value; }

return new ParamType(_constructorGuard, {
name: (value.name || null),
Expand All @@ -320,6 +326,10 @@ export class ParamType {

return ParamTypify(parseParamType(value, !!allowIndexed));
}

static isParamType(value: any): value is ParamType {
return !!(value != null && value._isParamType);
}
};

function parseParams(value: string, allowIndex: boolean): Array<ParamType> {
Expand All @@ -332,9 +342,15 @@ export abstract class Fragment {
readonly name: string;
readonly inputs: Array<ParamType>;

readonly _isFragment: boolean;

constructor(constructorGuard: any, params: any) {
if (constructorGuard !== _constructorGuard) { throw new Error("use a static from method"); }
populate(this, params);

this._isFragment = true;

Object.freeze(this);
}

// @TOOD: move logic to sub-classes; make this abstract
Expand Down Expand Up @@ -370,14 +386,17 @@ export abstract class Fragment {
}

static from(value: Fragment | JsonFragment | string): Fragment {
if (Fragment.isFragment(value)) { return value; }

if (typeof(value) === "string") {
return Fragment.fromString(value);
}

return Fragment.fromObject(value);
}

static fromObject(value: Fragment | JsonFragment): Fragment {
if (isNamedInstance<Fragment>(Fragment, value)) { return value; }
if (Fragment.isFragment(value)) { return value; }

if (value.type === "function") {
return FunctionFragment.fromObject(value);
Expand Down Expand Up @@ -412,6 +431,10 @@ export abstract class Fragment {

throw new Error("unknown fragment");
}

static isFragment(value: any): value is Fragment {
return !!(value && value._isFragment);
}
}

export class EventFragment extends Fragment {
Expand All @@ -425,7 +448,7 @@ export class EventFragment extends Fragment {
}

static fromObject(value: JsonFragment | EventFragment): EventFragment {
if (isNamedInstance<EventFragment>(EventFragment, value)) { return value; }
if (EventFragment.isEventFragment(value)) { return value; }

if (value.type !== "event") { throw new Error("invalid event object - " + value.type); }

Expand Down Expand Up @@ -462,6 +485,10 @@ export class EventFragment extends Fragment {
type: "event"
});
}

static isEventFragment(value: any): value is EventFragment {
return (value && value._isFragment && value.type === "event");
}
}

function parseGas(value: string, params: any): string {
Expand Down Expand Up @@ -528,7 +555,7 @@ export class ConstructorFragment extends Fragment {
}

static fromObject(value: ConstructorFragment | JsonFragment): ConstructorFragment {
if (isNamedInstance<ConstructorFragment>(ConstructorFragment, value)) { return value; }
if (ConstructorFragment.isConstructorFragment(value)) { return value; }

if (value.type !== "constructor") { throw new Error("invalid constructor object - " + value.type); }

Expand Down Expand Up @@ -557,6 +584,9 @@ export class ConstructorFragment extends Fragment {
return ConstructorFragment.fromObject(params);
}

static isConstructorFragment(value: any): value is ConstructorFragment {
return (value && value._isFragment && value.type === "constructor");
}
}

export class FunctionFragment extends ConstructorFragment {
Expand All @@ -571,7 +601,7 @@ export class FunctionFragment extends ConstructorFragment {
}

static fromObject(value: FunctionFragment | JsonFragment): FunctionFragment {
if (isNamedInstance<FunctionFragment>(FunctionFragment, value)) { return value; }
if (FunctionFragment.isFunctionFragment(value)) { return value; }

if (value.type !== "function") { throw new Error("invalid function object - " + value.type); }

Expand Down Expand Up @@ -619,6 +649,10 @@ export class FunctionFragment extends ConstructorFragment {

return FunctionFragment.fromObject(params);
}

static isFunctionFragment(value: any): value is FunctionFragment {
return (value && value._isFragment && value.type === "function");
}
}

//export class ErrorFragment extends Fragment {
Expand Down
21 changes: 15 additions & 6 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, isNamedInstance } from "@ethersproject/properties";
import { defineReadOnly, Description } from "@ethersproject/properties";

import { AbiCoder, defaultAbiCoder } from "./abi-coder";
import { ConstructorFragment, EventFragment, Fragment, FunctionFragment, JsonFragment, ParamType } from "./fragments";
Expand All @@ -31,6 +31,10 @@ export class TransactionDescription extends Description {

export class Indexed extends Description {
readonly hash: string;

static isIndexed(value: any): value is Indexed {
return !!(value && value._isIndexed);
}
}

export class Result {
Expand All @@ -51,6 +55,8 @@ export class Interface {

readonly _abiCoder: AbiCoder;

static _isInterface: boolean;

constructor(fragments: string | Array<Fragment | JsonFragment | string>) {
errors.checkNew(new.target, Interface);

Expand All @@ -62,9 +68,6 @@ export class Interface {
}

defineReadOnly(this, "fragments", abi.map((fragment) => {
if (isNamedInstance<Fragment>(Fragment, fragment)) {
return fragment
}
return Fragment.from(fragment);
}).filter((fragment) => (fragment != null)));

Expand Down Expand Up @@ -122,6 +125,8 @@ export class Interface {
if (!this.deploy) {
defineReadOnly(this, "deploy", ConstructorFragment.from( { type: "constructor" } ));
}

defineReadOnly(this, "_isInterface", true);
}

static getAbiCoder(): AbiCoder {
Expand Down Expand Up @@ -325,10 +330,10 @@ export class Interface {
eventFragment.inputs.forEach((param, index) => {
if (param.indexed) {
if (resultIndexed == null) {
result[index] = new Indexed({ hash: null });
result[index] = new Indexed({ _isIndexed: true, hash: null });

} else if (dynamic[index]) {
result[index] = new Indexed({ hash: resultIndexed[indexedIndex++] });
result[index] = new Indexed({ _isIndexed: true, hash: resultIndexed[indexedIndex++] });

} else {
result[index] = resultIndexed[indexedIndex++];
Expand Down Expand Up @@ -387,6 +392,10 @@ export class Interface {
return new Interface(value);
}
*/

static isInterface(value: any): value is Interface {
return !!(value && value._isInterface);
}
}

function getFragment(hash: string, calcFunc: (f: Fragment) => string, items: { [ sig: string ]: Fragment } ) {
Expand Down
46 changes: 35 additions & 11 deletions packages/abstract-provider/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BytesLike, isHexString } from "@ethersproject/bytes";
import * as errors from "@ethersproject/errors";
import { checkAbstract } from "@ethersproject/errors";
import { Network } from "@ethersproject/networks";
import { defineReadOnly } from "@ethersproject/properties";
import { Description, defineReadOnly } from "@ethersproject/properties";
import { Transaction } from "@ethersproject/transactions";
import { OnceBlockable } from "@ethersproject/web";

Expand Down Expand Up @@ -128,11 +128,13 @@ export interface FilterByBlockHash extends EventFilter {
// call(transaction: TransactionRequest): Promise<TransactionResponse>;
//};

export class ForkEvent {
export abstract class ForkEvent extends Description {
readonly expiry: number;

constructor(expiry?: number) {
defineReadOnly(this, "expiry", expiry || 0);
readonly _isForkEvent: boolean;

static isForkEvent(value: any): value is ForkEvent {
return !!(value && value._isForkEvent);
}
}

Expand All @@ -143,8 +145,13 @@ export class BlockForkEvent extends ForkEvent {
if (!isHexString(blockhash, 32)) {
errors.throwArgumentError("invalid blockhash", "blockhash", blockhash);
}
super(expiry);
defineReadOnly(this, "blockhash", blockhash);

super({
_isForkEvent: true,
_isBlockForkEvent: true,
expiry: (expiry || 0),
blockHash: blockhash
});
}
}

Expand All @@ -155,8 +162,13 @@ export class TransactionForkEvent extends ForkEvent {
if (!isHexString(hash, 32)) {
errors.throwArgumentError("invalid transaction hash", "hash", hash);
}
super(expiry);
defineReadOnly(this, "hash", hash);

super({
_isForkEvent: true,
_isTransactionForkEvent: true,
expiry: (expiry || 0),
hash: hash
});
}
}

Expand All @@ -171,9 +183,14 @@ export class TransactionOrderForkEvent extends ForkEvent {
if (!isHexString(afterHash, 32)) {
errors.throwArgumentError("invalid transaction hash", "afterHash", afterHash);
}
super(expiry);
defineReadOnly(this, "beforeHash", beforeHash);
defineReadOnly(this, "afterHash", afterHash);

super({
_isForkEvent: true,
_isTransactionOrderForkEvent: true,
expiry: (expiry || 0),
beforeHash: beforeHash,
afterHash: afterHash
});
}
}

Expand Down Expand Up @@ -239,8 +256,15 @@ export abstract class Provider implements OnceBlockable {
// @TODO: This *could* be implemented here, but would pull in events...
abstract waitForTransaction(transactionHash: string, timeout?: number): Promise<TransactionReceipt>;

readonly _isProvider: boolean;

constructor() {
checkAbstract(new.target, Provider);
defineReadOnly(this, "_isProvider", true);
}

static isProvider(value: any): value is Provider {
return !!(value && value._isProvider);
}

/*
Expand Down
7 changes: 7 additions & 0 deletions packages/abstract-signer/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ export abstract class Signer {
// This MAY throw if changing providers is not supported.
abstract connect(provider: Provider): Signer;

readonly _isSigner: boolean;


///////////////////
// Sub-classes MUST call super
constructor() {
errors.checkAbstract(new.target, Signer);
defineReadOnly(this, "_isSigner", true);
}


Expand Down Expand Up @@ -179,6 +182,10 @@ export abstract class Signer {
operation: (operation || "_checkProvider") });
}
}

static isSigner(value: any): value is Signer {
return !!(value && value._isSigner);
}
}

export class VoidSigner extends Signer {
Expand Down
Loading

0 comments on commit 257d67c

Please sign in to comment.