From b617322ee841ea7cf0dae07f3f1997d9752712c4 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Fri, 9 Sep 2022 18:33:36 -0400 Subject: [PATCH] Removed superfluous types for refining type properties. --- src.ts/abi/fragments.ts | 42 +++++++++------------------ src.ts/providers/abstract-provider.ts | 17 ++++++----- src.ts/providers/provider.ts | 32 +++++--------------- src.ts/transaction/transaction.ts | 29 ++++++------------ src.ts/utils/fetch.ts | 14 ++------- 5 files changed, 42 insertions(+), 92 deletions(-) diff --git a/src.ts/abi/fragments.ts b/src.ts/abi/fragments.ts index 426649a5a..96c0e88cc 100644 --- a/src.ts/abi/fragments.ts +++ b/src.ts/abi/fragments.ts @@ -428,18 +428,6 @@ function verifyBasicType(type: string): string { // Make the Fragment constructors effectively private const _guard = { }; -export interface ArrayParamType { //extends ParamType { - readonly arrayLength: number; - readonly arrayChildren: ParamType; -} - -export interface TupleParamType extends ParamType { - readonly components: ReadonlyArray; -} - -export interface IndexableParamType extends ParamType { - readonly indexed: boolean; -} export type FragmentWalkFunc = (type: string, value: any) => any; export type FragmentWalkAsyncFunc = (type: string, value: any) => any | Promise; @@ -545,15 +533,15 @@ export class ParamType { return value && (value.baseType === "array") } - isArray(): this is (ParamType & ArrayParamType) { + isArray(): this is (ParamType & { arrayLength: number, arrayChildren: ParamType }) { return (this.baseType === "array") } - isTuple(): this is TupleParamType { + isTuple(): this is (ParamType & { components: ReadonlyArray }) { return (this.baseType === "tuple"); } - isIndexable(): this is IndexableParamType { + isIndexable(): this is (ParamType & { indexed: boolean }) { return (this.indexed != null); } @@ -563,7 +551,8 @@ export class ParamType { if (this.arrayLength !== -1 && value.length !== this.arrayLength) { throw new Error("array is wrong length"); } - return value.map((v) => ((this).arrayChildren.walk(v, process))); + const _this = this; + return value.map((v) => (_this.arrayChildren.walk(v, process))); } if (this.isTuple()) { @@ -571,7 +560,8 @@ export class ParamType { if (value.length !== this.components.length) { throw new Error("array is wrong length"); } - return value.map((v, i) => ((this).components[i].walk(v, process))); + const _this = this; + return value.map((v, i) => (_this.components[i].walk(v, process))); } return process(this.type, value); @@ -742,13 +732,7 @@ export class ParamType { } } -export enum FragmentType { - "constructor" = "constructor", - "error" = "error", - "event" = "event", - "function" = "function", - "struct" = "struct", -}; +export type FragmentType = "constructor" | "error" | "event" | "function" | "struct"; export abstract class Fragment { readonly type!: FragmentType; @@ -858,7 +842,7 @@ function joinParams(format: FormatType, params: ReadonlyArray): strin export class ErrorFragment extends NamedFragment { constructor(guard: any, name: string, inputs: ReadonlyArray) { - super(guard, FragmentType.error, name, inputs); + super(guard, "error", name, inputs); } format(format: FormatType = "sighash"): string { @@ -894,7 +878,7 @@ export class EventFragment extends NamedFragment { readonly anonymous!: boolean; constructor(guard: any, name: string, inputs: ReadonlyArray, anonymous: boolean) { - super(guard, FragmentType.event, name, inputs); + super(guard, "event", name, inputs); defineProperties(this, { anonymous }); } @@ -977,7 +961,7 @@ export class ConstructorFragment extends Fragment { const gas = consumeGas(tokens); consumeEoi(tokens); - return new ConstructorFragment(_guard, FragmentType.constructor, inputs, payable, gas); + return new ConstructorFragment(_guard, "constructor", inputs, payable, gas); } } @@ -990,7 +974,7 @@ export class FunctionFragment extends NamedFragment { readonly gas!: null | bigint; constructor(guard: any, name: string, stateMutability: string, inputs: ReadonlyArray, outputs: ReadonlyArray, gas: null | bigint) { - super(guard, FragmentType.function, name, inputs); + super(guard, "function", name, inputs); outputs = Object.freeze(outputs.slice()); const constant = (stateMutability === "view" || stateMutability === "pure"); const payable = (stateMutability === "payable"); @@ -1068,7 +1052,7 @@ export class StructFragment extends NamedFragment { const inputs = consumeParams(tokens); consumeEoi(tokens); - return new StructFragment(_guard, FragmentType.struct, name, inputs); + return new StructFragment(_guard, "struct", name, inputs); } } diff --git a/src.ts/providers/abstract-provider.ts b/src.ts/providers/abstract-provider.ts index 78f628ebe..3bf64607b 100644 --- a/src.ts/providers/abstract-provider.ts +++ b/src.ts/providers/abstract-provider.ts @@ -597,7 +597,7 @@ export class AbstractProvider implements Provider { return network.formatter.transactionRequest(request); } - async estimateGas(_tx: TransactionRequest) { + async estimateGas(_tx: TransactionRequest): Promise { const transaction = await this._getTransaction(_tx); return getBigInt(await this.#perform({ method: "estimateGas", transaction @@ -665,7 +665,7 @@ export class AbstractProvider implements Provider { } } - async call(_tx: CallRequest) { + async call(_tx: CallRequest): Promise { const [ tx, blockTag ] = await Promise.all([ this._getTransaction(_tx), this._getBlockTag(_tx.blockTag) ]); @@ -684,25 +684,25 @@ export class AbstractProvider implements Provider { return await this.#perform(Object.assign(request, { address, blockTag })); } - async getBalance(address: AddressLike, blockTag?: BlockTag) { + async getBalance(address: AddressLike, blockTag?: BlockTag): Promise { return getBigInt(await this.#getAccountValue({ method: "getBalance" }, address, blockTag), "%response"); } - async getTransactionCount(address: AddressLike, blockTag?: BlockTag) { + async getTransactionCount(address: AddressLike, blockTag?: BlockTag): Promise { return getNumber(await this.#getAccountValue({ method: "getTransactionCount" }, address, blockTag), "%response"); } - async getCode(address: AddressLike, blockTag?: BlockTag) { + async getCode(address: AddressLike, blockTag?: BlockTag): Promise { return hexlify(await this.#getAccountValue({ method: "getCode" }, address, blockTag)); } - async getStorageAt(address: AddressLike, _position: BigNumberish, blockTag?: BlockTag) { + async getStorageAt(address: AddressLike, _position: BigNumberish, blockTag?: BlockTag): Promise { const position = getBigInt(_position, "position"); return hexlify(await this.#getAccountValue({ method: "getStorageAt", position }, address, blockTag)); } // Write - async broadcastTransaction(signedTx: string) { + async broadcastTransaction(signedTx: string): Promise { throw new Error(); return { }; } @@ -869,6 +869,7 @@ export class AbstractProvider implements Provider { } async resolveName(name: string): Promise{ + if (isHexString(name, 20)) { return name; } //if (typeof(name) === "string") { const resolver = await this.getResolver(name); if (resolver) { return await resolver.getAddress(); } @@ -1230,7 +1231,7 @@ function bytesPad(value: Uint8Array): Uint8Array { const empty = new Uint8Array([ ]); // ABI Encodes a series of (bytes, bytes, ...) -function encodeBytes(datas: Array) { +function encodeBytes(datas: Array): string { const result: Array = [ ]; let byteCount = 0; diff --git a/src.ts/providers/provider.ts b/src.ts/providers/provider.ts index b419f7ab9..4cda9219d 100644 --- a/src.ts/providers/provider.ts +++ b/src.ts/providers/provider.ts @@ -206,10 +206,6 @@ export interface MinedBlock ext readonly miner: string; } -export interface LondonBlock extends Block { - readonly baseFeePerGas: bigint; -} - export class Block implements BlockParams, Iterable { readonly provider!: Provider; @@ -320,7 +316,9 @@ export class Block implements BlockParam } isMined(): this is MinedBlock { return !!this.hash; } - isLondon(): this is LondonBlock { return !!this.baseFeePerGas; } + isLondon(): this is (Block & { baseFeePerGas: bigint }) { + return !!this.baseFeePerGas; + } orphanedEvent(): OrphanFilter { if (!this.isMined()) { throw new Error(""); } @@ -449,6 +447,7 @@ export interface TransactionReceiptParams { root: null | string; } +/* export interface LegacyTransactionReceipt { byzantium: false; status: null; @@ -460,6 +459,7 @@ export interface ByzantiumTransactionReceipt { status: number; root: null; } +*/ export class TransactionReceipt implements TransactionReceiptParams, Iterable { readonly provider!: Provider; @@ -631,23 +631,7 @@ export interface MinedTransactionResponse extends TransactionResponse { date: Date; } -export interface LegacyTransactionResponse extends TransactionResponse { - accessList: null; - maxFeePerGas: null; - maxPriorityFeePerGas: null; -} -export interface BerlinTransactionResponse extends TransactionResponse { - accessList: AccessList; - maxFeePerGas: null; - maxPriorityFeePerGas: null; -} - -export interface LondonTransactionResponse extends TransactionResponse { - accessList: AccessList; - maxFeePerGas: bigint; - maxPriorityFeePerGas: bigint; -} export class TransactionResponse implements TransactionLike, TransactionResponseParams { readonly provider: Provider; @@ -760,15 +744,15 @@ export class TransactionResponse implements TransactionLike, Transaction return (this.blockHash != null); } - isLegacy(): this is LegacyTransactionResponse { + isLegacy(): this is (TransactionResponse & { accessList: null, maxFeePerGas: null, maxPriorityFeePerGas: null }) { return (this.type === 0) } - isBerlin(): this is BerlinTransactionResponse { + isBerlin(): this is (TransactionResponse & { accessList: AccessList, maxFeePerGas: null, maxPriorityFeePerGas: null }) { return (this.type === 1); } - isLondon(): this is LondonTransactionResponse { + isLondon(): this is (TransactionResponse & { accessList: AccessList, maxFeePerGas: bigint, maxPriorityFeePerGas: bigint }){ return (this.type === 2); } diff --git a/src.ts/transaction/transaction.ts b/src.ts/transaction/transaction.ts index 99856884e..23a7da868 100644 --- a/src.ts/transaction/transaction.ts +++ b/src.ts/transaction/transaction.ts @@ -328,23 +328,6 @@ export interface SignedTransaction extends Transaction { signature: Signature; } -export interface LegacyTransaction extends Transaction { - type: 0; - gasPrice: bigint; -} - -export interface BerlinTransaction extends Transaction { - type: 1; - gasPrice: bigint; - accessList: AccessList; -} - -export interface LondonTransaction extends Transaction { - type: 2; - maxFeePerGas: bigint; - maxPriorityFeePerGas: bigint; - accessList: AccessList; -} export class Transaction implements Freezable, TransactionLike { #props: { @@ -595,9 +578,15 @@ export class Transaction implements Freezable, TransactionLike { /** * Returns true if the request has a body. */ - hasBody(): this is FetchRequestWithBody { + hasBody(): this is (FetchRequest & { body: Uint8Array }) { return (this.#body != null); } @@ -617,10 +613,6 @@ export class FetchRequest implements Iterable<[ key: string, value: string ]> { } -export interface FetchResponseWithBody extends FetchResponse { - body: Readonly; -} - interface ThrottleError extends Error { stall: number; throttle: true; @@ -771,7 +763,7 @@ export class FetchResponse implements Iterable<[ key: string, value: string ]> { /** * Returns true of the response has a body. */ - hasBody(): this is FetchResponseWithBody { + hasBody(): this is (FetchResponse & { body: Uint8Array }) { return (this.#body != null); }