2019-05-14 18:25:46 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import { Block, TransactionReceipt, TransactionResponse } from "@ethersproject/abstract-provider";
|
|
|
|
import { getAddress, getContractAddress } from "@ethersproject/address";
|
|
|
|
import { BigNumber } from "@ethersproject/bignumber";
|
|
|
|
import { hexDataLength, hexDataSlice, hexValue, hexZeroPad, isHexString } from "@ethersproject/bytes";
|
|
|
|
import { AddressZero } from "@ethersproject/constants";
|
|
|
|
import { shallowCopy } from "@ethersproject/properties";
|
2021-03-26 16:16:56 -04:00
|
|
|
import { AccessList, accessListify, parse as parseTransaction } from "@ethersproject/transactions";
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-08-01 18:04:06 -04:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
export type FormatFunc = (value: any) => any;
|
|
|
|
|
|
|
|
export type FormatFuncs = { [ key: string ]: FormatFunc };
|
|
|
|
|
|
|
|
export type Formats = {
|
|
|
|
transaction: FormatFuncs,
|
|
|
|
transactionRequest: FormatFuncs,
|
|
|
|
receipt: FormatFuncs,
|
|
|
|
receiptLog: FormatFuncs,
|
|
|
|
block: FormatFuncs,
|
|
|
|
blockWithTransactions: FormatFuncs,
|
|
|
|
filter: FormatFuncs,
|
|
|
|
filterLog: FormatFuncs,
|
|
|
|
};
|
|
|
|
|
|
|
|
export class Formatter {
|
|
|
|
readonly formats: Formats;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.formats = this.getDefaultFormats();
|
|
|
|
}
|
|
|
|
|
|
|
|
getDefaultFormats(): Formats {
|
2020-01-21 18:37:16 -05:00
|
|
|
const formats: Formats = <Formats>({ });
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-01-21 18:37:16 -05:00
|
|
|
const address = this.address.bind(this);
|
|
|
|
const bigNumber = this.bigNumber.bind(this);
|
|
|
|
const blockTag = this.blockTag.bind(this);
|
|
|
|
const data = this.data.bind(this);
|
|
|
|
const hash = this.hash.bind(this);
|
|
|
|
const hex = this.hex.bind(this);
|
|
|
|
const number = this.number.bind(this);
|
2021-06-23 23:39:57 -04:00
|
|
|
const type = this.type.bind(this);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-01-21 18:37:16 -05:00
|
|
|
const strictData = (v: any) => { return this.data(v, true); };
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
formats.transaction = {
|
|
|
|
hash: hash,
|
|
|
|
|
2021-06-23 23:39:57 -04:00
|
|
|
type: type,
|
2021-03-26 16:16:56 -04:00
|
|
|
accessList: Formatter.allowNull(this.accessList.bind(this), null),
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
blockHash: Formatter.allowNull(hash, null),
|
|
|
|
blockNumber: Formatter.allowNull(number, null),
|
|
|
|
transactionIndex: Formatter.allowNull(number, null),
|
|
|
|
|
|
|
|
confirmations: Formatter.allowNull(number, null),
|
|
|
|
|
|
|
|
from: address,
|
|
|
|
|
2021-05-30 17:47:04 -04:00
|
|
|
// either (gasPrice) or (maxPriorityFeePerGas + maxFeePerGas)
|
|
|
|
// must be set
|
|
|
|
gasPrice: Formatter.allowNull(bigNumber),
|
|
|
|
maxPriorityFeePerGas: Formatter.allowNull(bigNumber),
|
|
|
|
maxFeePerGas: Formatter.allowNull(bigNumber),
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
gasLimit: bigNumber,
|
|
|
|
to: Formatter.allowNull(address, null),
|
|
|
|
value: bigNumber,
|
|
|
|
nonce: number,
|
|
|
|
data: data,
|
|
|
|
|
|
|
|
r: Formatter.allowNull(this.uint256),
|
|
|
|
s: Formatter.allowNull(this.uint256),
|
|
|
|
v: Formatter.allowNull(number),
|
|
|
|
|
|
|
|
creates: Formatter.allowNull(address, null),
|
|
|
|
|
|
|
|
raw: Formatter.allowNull(data),
|
|
|
|
};
|
|
|
|
|
|
|
|
formats.transactionRequest = {
|
|
|
|
from: Formatter.allowNull(address),
|
|
|
|
nonce: Formatter.allowNull(number),
|
|
|
|
gasLimit: Formatter.allowNull(bigNumber),
|
|
|
|
gasPrice: Formatter.allowNull(bigNumber),
|
2021-05-30 17:47:04 -04:00
|
|
|
maxPriorityFeePerGas: Formatter.allowNull(bigNumber),
|
|
|
|
maxFeePerGas: Formatter.allowNull(bigNumber),
|
2019-05-14 18:25:46 -04:00
|
|
|
to: Formatter.allowNull(address),
|
|
|
|
value: Formatter.allowNull(bigNumber),
|
|
|
|
data: Formatter.allowNull(strictData),
|
2021-03-26 16:16:56 -04:00
|
|
|
type: Formatter.allowNull(number),
|
|
|
|
accessList: Formatter.allowNull(this.accessList.bind(this), null),
|
2019-05-14 18:25:46 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
formats.receiptLog = {
|
|
|
|
transactionIndex: number,
|
|
|
|
blockNumber: number,
|
|
|
|
transactionHash: hash,
|
|
|
|
address: address,
|
|
|
|
topics: Formatter.arrayOf(hash),
|
|
|
|
data: data,
|
|
|
|
logIndex: number,
|
|
|
|
blockHash: hash,
|
|
|
|
};
|
|
|
|
|
|
|
|
formats.receipt = {
|
2019-10-19 20:33:57 +09:00
|
|
|
to: Formatter.allowNull(this.address, null),
|
|
|
|
from: Formatter.allowNull(this.address, null),
|
2019-05-14 18:25:46 -04:00
|
|
|
contractAddress: Formatter.allowNull(address, null),
|
|
|
|
transactionIndex: number,
|
2021-02-01 15:50:27 -05:00
|
|
|
// should be allowNull(hash), but broken-EIP-658 support is handled in receipt
|
|
|
|
root: Formatter.allowNull(hex),
|
2019-05-14 18:25:46 -04:00
|
|
|
gasUsed: bigNumber,
|
|
|
|
logsBloom: Formatter.allowNull(data),// @TODO: should this be data?
|
|
|
|
blockHash: hash,
|
|
|
|
transactionHash: hash,
|
|
|
|
logs: Formatter.arrayOf(this.receiptLog.bind(this)),
|
|
|
|
blockNumber: number,
|
|
|
|
confirmations: Formatter.allowNull(number, null),
|
|
|
|
cumulativeGasUsed: bigNumber,
|
2021-06-25 22:57:05 -04:00
|
|
|
effectiveGasPrice: Formatter.allowNull(bigNumber),
|
2021-06-23 23:39:57 -04:00
|
|
|
status: Formatter.allowNull(number),
|
|
|
|
type: type
|
2019-05-14 18:25:46 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
formats.block = {
|
2022-03-08 17:16:32 -05:00
|
|
|
hash: Formatter.allowNull(hash),
|
2019-05-14 18:25:46 -04:00
|
|
|
parentHash: hash,
|
|
|
|
number: number,
|
|
|
|
|
|
|
|
timestamp: number,
|
|
|
|
nonce: Formatter.allowNull(hex),
|
|
|
|
difficulty: this.difficulty.bind(this),
|
|
|
|
|
|
|
|
gasLimit: bigNumber,
|
|
|
|
gasUsed: bigNumber,
|
|
|
|
|
2022-03-08 17:16:32 -05:00
|
|
|
miner: Formatter.allowNull(address),
|
2019-05-14 18:25:46 -04:00
|
|
|
extraData: data,
|
|
|
|
|
|
|
|
transactions: Formatter.allowNull(Formatter.arrayOf(hash)),
|
2021-05-30 17:47:04 -04:00
|
|
|
|
2021-06-24 00:46:53 -04:00
|
|
|
baseFeePerGas: Formatter.allowNull(bigNumber)
|
2019-05-14 18:25:46 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
formats.blockWithTransactions = shallowCopy(formats.block);
|
|
|
|
formats.blockWithTransactions.transactions = Formatter.allowNull(Formatter.arrayOf(this.transactionResponse.bind(this)));
|
|
|
|
|
|
|
|
formats.filter = {
|
|
|
|
fromBlock: Formatter.allowNull(blockTag, undefined),
|
|
|
|
toBlock: Formatter.allowNull(blockTag, undefined),
|
|
|
|
blockHash: Formatter.allowNull(hash, undefined),
|
|
|
|
address: Formatter.allowNull(address, undefined),
|
|
|
|
topics: Formatter.allowNull(this.topics.bind(this), undefined),
|
|
|
|
};
|
|
|
|
|
|
|
|
formats.filterLog = {
|
|
|
|
blockNumber: Formatter.allowNull(number),
|
|
|
|
blockHash: Formatter.allowNull(hash),
|
|
|
|
transactionIndex: number,
|
|
|
|
|
|
|
|
removed: Formatter.allowNull(this.boolean.bind(this)),
|
|
|
|
|
|
|
|
address: address,
|
|
|
|
data: Formatter.allowFalsish(data, "0x"),
|
|
|
|
|
|
|
|
topics: Formatter.arrayOf(hash),
|
|
|
|
|
|
|
|
transactionHash: hash,
|
|
|
|
logIndex: number,
|
|
|
|
};
|
|
|
|
|
|
|
|
return formats;
|
|
|
|
}
|
|
|
|
|
2021-03-26 16:16:56 -04:00
|
|
|
accessList(accessList: Array<any>): AccessList {
|
|
|
|
return accessListify(accessList || []);
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
// Requires a BigNumberish that is within the IEEE754 safe integer range; returns a number
|
|
|
|
// Strict! Used on input.
|
|
|
|
number(number: any): number {
|
2020-10-22 21:03:51 -04:00
|
|
|
if (number === "0x") { return 0; }
|
2019-05-14 18:25:46 -04:00
|
|
|
return BigNumber.from(number).toNumber();
|
|
|
|
}
|
|
|
|
|
2021-06-23 23:39:57 -04:00
|
|
|
type(number: any): number {
|
|
|
|
if (number === "0x" || number == null) { return 0; }
|
|
|
|
return BigNumber.from(number).toNumber();
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
// Strict! Used on input.
|
|
|
|
bigNumber(value: any): BigNumber {
|
|
|
|
return BigNumber.from(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Requires a boolean, "true" or "false"; returns a boolean
|
|
|
|
boolean(value: any): boolean {
|
|
|
|
if (typeof(value) === "boolean") { return value; }
|
|
|
|
if (typeof(value) === "string") {
|
|
|
|
value = value.toLowerCase();
|
|
|
|
if (value === "true") { return true; }
|
|
|
|
if (value === "false") { return false; }
|
|
|
|
}
|
2020-04-22 02:42:25 -04:00
|
|
|
throw new Error("invalid boolean - " + value);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hex(value: any, strict?: boolean): string {
|
|
|
|
if (typeof(value) === "string") {
|
|
|
|
if (!strict && value.substring(0, 2) !== "0x") { value = "0x" + value; }
|
|
|
|
if (isHexString(value)) {
|
|
|
|
return value.toLowerCase();
|
|
|
|
}
|
|
|
|
}
|
2019-08-01 18:04:06 -04:00
|
|
|
return logger.throwArgumentError("invalid hash", "value", value);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
data(value: any, strict?: boolean): string {
|
2020-01-21 18:37:16 -05:00
|
|
|
const result = this.hex(value, strict);
|
2019-05-14 18:25:46 -04:00
|
|
|
if ((result.length % 2) !== 0) {
|
|
|
|
throw new Error("invalid data; odd-length - " + value);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Requires an address
|
|
|
|
// Strict! Used on input.
|
|
|
|
address(value: any): string {
|
|
|
|
return getAddress(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
callAddress(value: any): string {
|
|
|
|
if (!isHexString(value, 32)) { return null; }
|
2020-01-21 18:37:16 -05:00
|
|
|
const address = getAddress(hexDataSlice(value, 12));
|
2019-05-14 18:25:46 -04:00
|
|
|
return (address === AddressZero) ? null: address;
|
|
|
|
}
|
|
|
|
|
|
|
|
contractAddress(value: any): string {
|
|
|
|
return getContractAddress(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strict! Used on input.
|
|
|
|
blockTag(blockTag: any): string {
|
|
|
|
if (blockTag == null) { return "latest"; }
|
|
|
|
|
2021-10-04 11:46:24 -04:00
|
|
|
if (blockTag === "earliest") { return "0x0"; }
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2022-08-11 15:03:02 -04:00
|
|
|
switch (blockTag) {
|
|
|
|
case "earliest": return "0x0";
|
|
|
|
case "latest": case "pending": case "safe": case "finalized":
|
|
|
|
return blockTag;
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(blockTag) === "number" || isHexString(blockTag)) {
|
|
|
|
return hexValue(<number | string>blockTag);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error("invalid blockTag");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Requires a hash, optionally requires 0x prefix; returns prefixed lowercase hash.
|
|
|
|
hash(value: any, strict?: boolean): string {
|
2020-01-21 18:37:16 -05:00
|
|
|
const result = this.hex(value, strict);
|
2019-05-14 18:25:46 -04:00
|
|
|
if (hexDataLength(result) !== 32) {
|
2019-08-01 18:04:06 -04:00
|
|
|
return logger.throwArgumentError("invalid hash", "value", value);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the difficulty as a number, or if too large (i.e. PoA network) null
|
|
|
|
difficulty(value: any): number {
|
2020-01-21 18:37:16 -05:00
|
|
|
if (value == null) { return null; }
|
|
|
|
|
|
|
|
const v = BigNumber.from(value);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
try {
|
|
|
|
return v.toNumber();
|
|
|
|
} catch (error) { }
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint256(value: any): string {
|
|
|
|
if (!isHexString(value)) {
|
|
|
|
throw new Error("invalid uint256");
|
|
|
|
}
|
|
|
|
return hexZeroPad(value, 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
_block(value: any, format: any): Block {
|
|
|
|
if (value.author != null && value.miner == null) {
|
|
|
|
value.miner = value.author;
|
|
|
|
}
|
2021-10-04 12:09:44 -04:00
|
|
|
// The difficulty may need to come from _difficulty in recursed blocks
|
|
|
|
const difficulty = (value._difficulty != null) ? value._difficulty: value.difficulty;
|
|
|
|
const result = Formatter.check(format, value);
|
|
|
|
result._difficulty = ((difficulty == null) ? null: BigNumber.from(difficulty));
|
|
|
|
return result;
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
block(value: any): Block {
|
|
|
|
return this._block(value, this.formats.block);
|
|
|
|
}
|
|
|
|
|
|
|
|
blockWithTransactions(value: any): Block {
|
|
|
|
return this._block(value, this.formats.blockWithTransactions);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strict! Used on input.
|
|
|
|
transactionRequest(value: any): any {
|
|
|
|
return Formatter.check(this.formats.transactionRequest, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
transactionResponse(transaction: any): TransactionResponse {
|
|
|
|
|
|
|
|
// Rename gas to gasLimit
|
|
|
|
if (transaction.gas != null && transaction.gasLimit == null) {
|
|
|
|
transaction.gasLimit = transaction.gas;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some clients (TestRPC) do strange things like return 0x0 for the
|
|
|
|
// 0 address; correct this to be a real address
|
|
|
|
if (transaction.to && BigNumber.from(transaction.to).isZero()) {
|
|
|
|
transaction.to = "0x0000000000000000000000000000000000000000";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename input to data
|
|
|
|
if (transaction.input != null && transaction.data == null) {
|
|
|
|
transaction.data = transaction.input;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If to and creates are empty, populate the creates from the transaction
|
|
|
|
if (transaction.to == null && transaction.creates == null) {
|
|
|
|
transaction.creates = this.contractAddress(transaction);
|
|
|
|
}
|
|
|
|
|
2021-08-18 02:59:47 -03:00
|
|
|
if ((transaction.type === 1 || transaction.type === 2)&& transaction.accessList == null) {
|
2021-03-26 16:16:56 -04:00
|
|
|
transaction.accessList = [ ];
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-01-21 18:37:16 -05:00
|
|
|
const result: TransactionResponse = Formatter.check(this.formats.transaction, transaction);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-01-11 03:34:59 -05:00
|
|
|
if (transaction.chainId != null) {
|
|
|
|
let chainId = transaction.chainId;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-01-11 03:34:59 -05:00
|
|
|
if (isHexString(chainId)) {
|
|
|
|
chainId = BigNumber.from(chainId).toNumber();
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-01-11 03:34:59 -05:00
|
|
|
result.chainId = chainId;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-01-11 03:34:59 -05:00
|
|
|
} else {
|
|
|
|
let chainId = transaction.networkId;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-01-11 03:34:59 -05:00
|
|
|
// geth-etc returns chainId
|
|
|
|
if (chainId == null && result.v == null) {
|
|
|
|
chainId = transaction.chainId;
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-01-11 03:34:59 -05:00
|
|
|
if (isHexString(chainId)) {
|
|
|
|
chainId = BigNumber.from(chainId).toNumber();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(chainId) !== "number" && result.v != null) {
|
|
|
|
chainId = (result.v - 35) / 2;
|
|
|
|
if (chainId < 0) { chainId = 0; }
|
|
|
|
chainId = parseInt(chainId);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(chainId) !== "number") { chainId = 0; }
|
|
|
|
|
|
|
|
result.chainId = chainId;
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
// 0x0000... should actually be null
|
|
|
|
if (result.blockHash && result.blockHash.replace(/0/g, "") === "x") {
|
|
|
|
result.blockHash = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction(value: any): any {
|
|
|
|
return parseTransaction(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
receiptLog(value: any): any {
|
|
|
|
return Formatter.check(this.formats.receiptLog, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
receipt(value: any): TransactionReceipt {
|
2020-01-21 18:37:16 -05:00
|
|
|
const result: TransactionReceipt = Formatter.check(this.formats.receipt, value);
|
2020-04-03 20:45:23 -04:00
|
|
|
|
2021-02-01 15:50:27 -05:00
|
|
|
// RSK incorrectly implemented EIP-658, so we munge things a bit here for it
|
|
|
|
if (result.root != null) {
|
|
|
|
if (result.root.length <= 4) {
|
|
|
|
// Could be 0x00, 0x0, 0x01 or 0x1
|
|
|
|
const value = BigNumber.from(result.root).toNumber();
|
|
|
|
if (value === 0 || value === 1) {
|
|
|
|
// Make sure if both are specified, they match
|
|
|
|
if (result.status != null && (result.status !== value)) {
|
|
|
|
logger.throwArgumentError("alt-root-status/status mismatch", "value", { root: result.root, status: result.status });
|
|
|
|
}
|
|
|
|
result.status = value;
|
|
|
|
delete result.root;
|
|
|
|
} else {
|
|
|
|
logger.throwArgumentError("invalid alt-root-status", "value.root", result.root);
|
|
|
|
}
|
|
|
|
} else if (result.root.length !== 66) {
|
|
|
|
// Must be a valid bytes32
|
|
|
|
logger.throwArgumentError("invalid root hash", "value.root", result.root);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.status != null) {
|
2019-05-14 18:25:46 -04:00
|
|
|
result.byzantium = true;
|
|
|
|
}
|
2020-04-03 20:45:23 -04:00
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
topics(value: any): any {
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
return value.map((v) => this.topics(v));
|
|
|
|
|
|
|
|
} else if (value != null) {
|
|
|
|
return this.hash(value, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
filter(value: any): any {
|
|
|
|
return Formatter.check(this.formats.filter, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
filterLog(value: any): any {
|
|
|
|
return Formatter.check(this.formats.filterLog, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static check(format: { [ name: string ]: FormatFunc }, object: any): any {
|
2020-01-21 18:37:16 -05:00
|
|
|
const result: any = {};
|
|
|
|
for (const key in format) {
|
2019-05-14 18:25:46 -04:00
|
|
|
try {
|
2020-01-21 18:37:16 -05:00
|
|
|
const value = format[key](object[key]);
|
2019-05-14 18:25:46 -04:00
|
|
|
if (value !== undefined) { result[key] = value; }
|
|
|
|
} catch (error) {
|
|
|
|
error.checkKey = key;
|
|
|
|
error.checkValue = object[key];
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if value is null-ish, nullValue is returned
|
|
|
|
static allowNull(format: FormatFunc, nullValue?: any): FormatFunc {
|
|
|
|
return (function(value: any) {
|
|
|
|
if (value == null) { return nullValue; }
|
|
|
|
return format(value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// If value is false-ish, replaceValue is returned
|
|
|
|
static allowFalsish(format: FormatFunc, replaceValue: any): FormatFunc {
|
|
|
|
return (function(value: any) {
|
|
|
|
if (!value) { return replaceValue; }
|
|
|
|
return format(value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Requires an Array satisfying check
|
|
|
|
static arrayOf(format: FormatFunc): FormatFunc {
|
|
|
|
return (function(array: any): Array<any> {
|
|
|
|
if (!Array.isArray(array)) { throw new Error("not an array"); }
|
|
|
|
|
2020-01-21 18:37:16 -05:00
|
|
|
const result: any = [];
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
array.forEach(function(value) {
|
|
|
|
result.push(format(value));
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 17:28:18 -04:00
|
|
|
export interface CommunityResourcable {
|
|
|
|
isCommunityResource(): boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isCommunityResourcable(value: any): value is CommunityResourcable {
|
|
|
|
return (value && typeof(value.isCommunityResource) === "function");
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isCommunityResource(value: any): boolean {
|
|
|
|
return (isCommunityResourcable(value) && value.isCommunityResource());
|
|
|
|
}
|
|
|
|
|
2020-07-14 02:26:45 -04:00
|
|
|
// Show the throttle message only once
|
|
|
|
let throttleMessage = false;
|
|
|
|
export function showThrottleMessage() {
|
|
|
|
if (throttleMessage) { return; }
|
|
|
|
throttleMessage = true;
|
|
|
|
|
|
|
|
console.log("========= NOTICE =========")
|
|
|
|
console.log("Request-Rate Exceeded (this message will not be repeated)");
|
|
|
|
console.log("");
|
|
|
|
console.log("The default API keys for each service are provided as a highly-throttled,");
|
|
|
|
console.log("community resource for low-traffic projects and early prototyping.");
|
|
|
|
console.log("");
|
|
|
|
console.log("While your application will continue to function, we highly recommended");
|
|
|
|
console.log("signing up for your own API keys to improve performance, increase your");
|
|
|
|
console.log("request rate/limit and enable other perks, such as metrics and advanced APIs.");
|
|
|
|
console.log("");
|
|
|
|
console.log("For more details: https:/\/docs.ethers.io/api-keys/");
|
|
|
|
console.log("==========================");
|
|
|
|
}
|
|
|
|
|