ethers.js/lib.commonjs/providers/formatter.js

445 lines
15 KiB
JavaScript
Raw Normal View History

2022-09-05 23:57:11 +03:00
"use strict";
// Belongs to Networks; requires abstract-provider
// provider requires abstract-provider and network
/**
* Formatter
*
* This is responsibile for converting much of the various
* loose network values into a concrete ethers-ready value.
*
* For example, converting addresses to checksum addresses,
* validating a hash is 32 bytes, and so on.
*
* By sub-classing this class and providing it in a custom
* Network object this allows exotic (non-Ethereum) networks
* to be fairly simple to adapt to ethers.
*/
2022-09-27 10:45:27 +03:00
/*
import { getAddress, getCreateAddress } from "../address/index.js";
import {
dataLength, dataSlice, getBigInt, getNumber, isHexString, toQuantity,
throwArgumentError, throwError
} from "../utils/index.js";
import { Signature } from "../crypto/signature.js";
import { accessListify } from "../transaction/index.js";
import { Block, Log, TransactionReceipt, TransactionResponse } from "./provider.js";
import type { AccessList } from "../transaction/index.js";
import type { PerformActionTransaction } from "./abstract-provider.js";
import type { Filter, Provider } from "./provider.js";
2022-09-05 23:57:11 +03:00
const BN_MAX_UINT256 = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
2022-09-27 10:45:27 +03:00
export type FormatFunc = (value: any) => any;
2022-09-05 23:57:11 +03:00
//export type AccessListSet = { address: string, storageKeys: Array<string> };
//export type AccessList = Array<AccessListSet>;
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
//export type AccessListish = AccessList |
// Array<[ string, Array<string> ]> |
// Record<string, Array<string>>;
2022-09-27 10:45:27 +03:00
function stringify(value: any): string {
if (typeof(value) !== "string") { throw new Error("invalid string"); }
2022-09-05 23:57:11 +03:00
return value;
}
2022-09-27 10:45:27 +03:00
export class Formatter {
#format: {
address: FormatFunc,
bigNumber: FormatFunc,
blockTag: FormatFunc,
data: FormatFunc,
filter: FormatFunc,
hash: FormatFunc,
number: FormatFunc,
topics: FormatFunc,
transactionRequest: FormatFunc,
transactionResponse: FormatFunc,
uint256: FormatFunc,
};
#baseBlock: FormatFunc;
2022-09-05 23:57:11 +03:00
constructor() {
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 number = this.number.bind(this);
const uint256 = this.uint256.bind(this);
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
const topics = this.arrayOf(hash);
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
this.#format = {
address,
bigNumber,
blockTag,
data,
hash,
number,
uint256,
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
topics,
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
filter: this.object({
fromBlock: this.allowNull(blockTag, undefined),
toBlock: this.allowNull(blockTag, undefined),
blockHash: this.allowNull(hash, undefined),
address: this.allowNull(address, undefined),
topics: this.allowNull(topics, undefined)
}),
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
transactionRequest: this.object({
from: this.allowNull(address),
type: this.allowNull(number),
to: this.allowNull(address),
nonce: this.allowNull(number),
gasLimit: this.allowNull(uint256),
gasPrice: this.allowNull(uint256),
maxFeePerGas: this.allowNull(uint256),
maxPriorityFeePerGas: this.allowNull(uint256),
data: this.allowNull(data),
value: this.allowNull(uint256),
}),
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
transactionResponse: this.object({
hash: hash,
index: number,
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
type: this.allowNull(number, 0),
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
// These can be null for pending blocks
blockHash: this.allowNull(hash),
blockNumber: this.allowNull(number),
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
// For Legacy transactions, this comes from the v
chainId: this.allowNull(number),
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
from: address,
to: this.address,
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
gasLimit: bigNumber,
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
gasPrice: this.allowNull(bigNumber),
maxFeePerGas: this.allowNull(bigNumber),
maxPriorityFeePerGas: this.allowNull(bigNumber),
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
value: bigNumber,
data: data,
nonce: number,
r: hash,
s: hash,
v: number,
accessList: this.allowNull(this.accessList)
}, {
2022-09-27 10:45:27 +03:00
index: [ "transactionIndex" ]
2022-09-05 23:57:11 +03:00
}),
};
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
this.#baseBlock = this.object({
number: number,
hash: this.allowNull(hash, null),
timestamp: number,
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
parentHash: hash,
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
nonce: this.allowNull(stringify, "0x0000000000000000"),
difficulty: bigNumber,
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
gasLimit: bigNumber,
gasUsed: bigNumber,
miner: this.allowNull(address, "0x0000000000000000000000000000000000000000"),
extraData: stringify,
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
baseFeePerGas: this.allowNull(bigNumber),
});
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
}
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
// An address
2022-09-27 10:45:27 +03:00
address(value: any): string {
return getAddress(value);
2022-09-05 23:57:11 +03:00
}
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
// An address from a call result; may be zero-padded
2022-09-27 10:45:27 +03:00
callAddress(value: any): string {
if (dataLength(value) !== 32 || dataSlice(value, 0, 12) !== "0x000000000000000000000000") {
throwArgumentError("invalid call address", "value", value);
2022-09-05 23:57:11 +03:00
}
2022-09-27 10:45:27 +03:00
return this.address(dataSlice(value, 12));
2022-09-05 23:57:11 +03:00
}
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
// An address from a transaction (e.g. { from: string, nonce: number })
2022-09-27 10:45:27 +03:00
contractAddress(value: any): string {
return getCreateAddress({
2022-09-05 23:57:11 +03:00
from: this.address(value.from),
2022-09-27 10:45:27 +03:00
nonce: getNumber(value.nonce, "value.nonce")
2022-09-05 23:57:11 +03:00
});
}
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
// Block Tag
2022-09-27 10:45:27 +03:00
blockTag(value?: any): string {
if (value == null) { return "latest"; }
2022-09-05 23:57:11 +03:00
switch (value) {
case "earliest":
return "0x0";
2022-09-27 10:45:27 +03:00
case "latest": case "pending": case "safe": case "finalized":
2022-09-05 23:57:11 +03:00
return value;
}
2022-09-27 10:45:27 +03:00
if (typeof(value) === "number" || (isHexString(value) && dataLength(value) < 32)) {
return toQuantity(value);
2022-09-05 23:57:11 +03:00
}
2022-09-27 10:45:27 +03:00
return throwArgumentError("invalid blockTag", "value", value);
2022-09-05 23:57:11 +03:00
}
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
// Block objects
2022-09-27 10:45:27 +03:00
block(value: any, provider?: Provider): Block<string> {
2022-09-05 23:57:11 +03:00
const params = this.#baseBlock(value);
2022-09-27 10:45:27 +03:00
params.transactions = value.transactions.map((t: any) => this.hash(t));
return new Block(params, provider);
2022-09-05 23:57:11 +03:00
}
2022-09-27 10:45:27 +03:00
blockWithTransactions(value: any, provider?: Provider): Block<TransactionResponse> {
2022-09-05 23:57:11 +03:00
throw new Error();
}
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
// Transactions
2022-09-27 10:45:27 +03:00
transactionRequest(value: any, provider?: Provider): PerformActionTransaction {
2022-09-05 23:57:11 +03:00
return this.#format.transactionRequest(value);
}
2022-09-27 10:45:27 +03:00
transactionResponse(value: any, provider?: Provider): TransactionResponse {
value = Object.assign({ }, value);
2022-09-05 23:57:11 +03:00
// @TODO: Use the remap feature
2022-09-27 10:45:27 +03:00
if (value.data == null && value.input != null) { value.data = value.input; }
if (value.gasLimit == null && value.gas) { value.gasLimit = value.gas; }
2022-09-05 23:57:11 +03:00
value = this.#format.transactionResponse(value);
2022-09-27 10:45:27 +03:00
const sig = Signature.from({ r: value.r, s: value.s, v: value.v });
2022-09-05 23:57:11 +03:00
value.signature = sig;
2022-09-27 10:45:27 +03:00
if (value.chainId == null) { value.chainId = sig.legacyChainId; }
return new TransactionResponse(value, provider);
2022-09-05 23:57:11 +03:00
}
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
// Receipts
2022-09-27 10:45:27 +03:00
log(value: any, provider?: Provider): Log {
2022-09-05 23:57:11 +03:00
const log = this.object({
address: this.address,
blockHash: this.hash,
blockNumber: this.number,
data: this.data,
index: this.number,
removed: this.boolean,
topics: this.topics,
transactionHash: this.hash,
transactionIndex: this.number,
}, {
2022-09-27 10:45:27 +03:00
index: [ "logIndex" ]
2022-09-05 23:57:11 +03:00
})(value);
2022-09-27 10:45:27 +03:00
return new Log(log, provider);
2022-09-05 23:57:11 +03:00
}
2022-09-27 10:45:27 +03:00
receipt(value: any, provider?: Provider): TransactionReceipt {
2022-09-05 23:57:11 +03:00
const receipt = this.object({
blockHash: this.hash,
blockNumber: this.number,
contractAddress: this.allowNull(this.address),
cumulativeGasUsed: this.bigNumber,
from: this.address,
gasUsed: this.bigNumber,
2022-09-27 10:45:27 +03:00
logs: this.arrayOf((v: any) => (this.log(v, provider))),
2022-09-05 23:57:11 +03:00
logsBloom: this.data,
root: this.allowNull(this.data),
status: this.allowNull(this.number),
to: this.address,
gasPrice: this.allowNull(this.bigNumber),
hash: this.hash,
index: this.number,
type: this.allowNull(this.number, 0),
}, {
2022-09-27 10:45:27 +03:00
hash: [ "transactionHash" ],
gasPrice: [ "effectiveGasPrice" ],
index: [ "transactionIndex" ]
2022-09-05 23:57:11 +03:00
})(value);
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
// RSK incorrectly implemented EIP-658, so we munge things a bit here for it
if (receipt.root != null) {
if (receipt.root.length <= 4) {
// Could be 0x00, 0x0, 0x01 or 0x1
const value = parseInt(receipt.root);
if (value === 0 || value === 1) {
// Make sure if both are specified, they match
if (receipt.status != null && receipt.status !== value) {
2022-09-27 10:45:27 +03:00
return throwError("alt-root-status/status mismatch", "BAD_DATA", {
2022-09-05 23:57:11 +03:00
value: { root: receipt.root, status: receipt.status }
});
}
receipt.status = value;
delete receipt.root;
2022-09-27 10:45:27 +03:00
} else {
return throwError("invalid alt-root-status", "BAD_DATA", {
2022-09-05 23:57:11 +03:00
value: receipt.root
});
}
2022-09-27 10:45:27 +03:00
} else if (!isHexString(receipt.root, 32)) {
2022-09-05 23:57:11 +03:00
// Must be a valid bytes32
2022-09-27 10:45:27 +03:00
return throwError("invalid receipt root hash", "BAD_DATA", {
2022-09-05 23:57:11 +03:00
value: receipt.root
});
}
}
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
//receipt.byzantium = (receipt.root == null);
2022-09-27 10:45:27 +03:00
return new TransactionReceipt(receipt, provider);
2022-09-05 23:57:11 +03:00
}
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
// Fitlers
2022-09-27 10:45:27 +03:00
topics(value: any): Array<string> {
2022-09-05 23:57:11 +03:00
return this.#format.topics(value);
}
2022-09-27 10:45:27 +03:00
filter(value: any): Filter {
2022-09-05 23:57:11 +03:00
return this.#format.filter(value);
}
2022-09-27 10:45:27 +03:00
filterLog(value: any): any {
2022-09-05 23:57:11 +03:00
console.log("ME", value);
return null;
}
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
// Converts a serialized transaction to a TransactionResponse
2022-09-27 10:45:27 +03:00
transaction(value: any): TransactionResponse {
2022-09-05 23:57:11 +03:00
throw new Error();
}
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
// Useful utility formatters functions, which if need be use the
// methods within the formatter to ensure internal compatibility
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
// Access List; converts an AccessListish to an AccessList
2022-09-27 10:45:27 +03:00
accessList(value: any): AccessList {
return accessListify(value);
2022-09-05 23:57:11 +03:00
}
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
// Converts falsish values to a specific value, otherwise use the formatter. Calls preserve `this`.
2022-09-27 10:45:27 +03:00
allowFalsish(format: FormatFunc, ifFalse: any): FormatFunc {
return ((value: any) => {
if (!value) { return ifFalse; }
2022-09-05 23:57:11 +03:00
return format.call(this, value);
});
}
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
// Allows null, optionally replacing it with a default value. Calls preserve `this`.
2022-09-27 10:45:27 +03:00
allowNull(format: FormatFunc, ifNull?: any): FormatFunc {
return ((value: any) => {
if (value == null) { return ifNull; }
2022-09-05 23:57:11 +03:00
return format.call(this, value);
});
}
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
// Requires an Array satisfying the formatter. Calls preserves `this`.
2022-09-27 10:45:27 +03:00
arrayOf(format: FormatFunc): FormatFunc {
return ((array: any) => {
if (!Array.isArray(array)) { throw new Error("not an array"); }
2022-09-05 23:57:11 +03:00
return array.map((i) => format.call(this, i));
});
}
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
// Requires a value which is a value BigNumber
2022-09-27 10:45:27 +03:00
bigNumber(value: any): bigint {
return getBigInt(value, "value");
2022-09-05 23:57:11 +03:00
}
2022-09-27 10:45:27 +03:00
uint256(value: any): bigint {
2022-09-05 23:57:11 +03:00
const result = this.bigNumber(value);
if (result < 0 || result > BN_MAX_UINT256) {
2022-09-27 10:45:27 +03:00
throwArgumentError("invalid uint256", "value", value);
2022-09-05 23:57:11 +03:00
}
return result;
}
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
// Requires a value which is a value boolean or string equivalent
2022-09-27 10:45:27 +03:00
boolean(value: any): boolean {
2022-09-05 23:57:11 +03:00
switch (value) {
2022-09-27 10:45:27 +03:00
case true: case "true":
2022-09-05 23:57:11 +03:00
return true;
2022-09-27 10:45:27 +03:00
case false: case "false":
2022-09-05 23:57:11 +03:00
return false;
}
2022-09-27 10:45:27 +03:00
return throwArgumentError(`invalid boolean; ${ JSON.stringify(value) }`, "value", value);
2022-09-05 23:57:11 +03:00
}
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
// Requires a value which is a valid hexstring. If dataOrLength is true,
// the length must be even (i.e. a datahexstring) or if it is a number,
// specifies teh number of bytes value must represent
2022-09-27 10:45:27 +03:00
_hexstring(dataOrLength?: boolean | number): FormatFunc {
if (dataOrLength == null) { dataOrLength = false; }
return (function(value: any) {
if (isHexString(value, dataOrLength)) {
2022-09-05 23:57:11 +03:00
return value.toLowerCase();
}
throw new Error("bad hexstring");
});
}
2022-09-27 10:45:27 +03:00
data(value: string): string {
if (dataLength(value) == null) {
throwArgumentError("", "value", value);
2022-09-05 23:57:11 +03:00
}
return value;
}
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
// Requires a network-native hash
2022-09-27 10:45:27 +03:00
hash(value: any): string {
if (dataLength(value) !== 32) {
throwArgumentError("", "value", value);
2022-09-05 23:57:11 +03:00
}
return this.#format.data(value);
}
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
// Requires a valid number, within the IEEE 754 safe range
2022-09-27 10:45:27 +03:00
number(value: any): number {
return getNumber(value);
2022-09-05 23:57:11 +03:00
}
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
// Requires an object which matches a fleet of other formatters
// Any FormatFunc may return `undefined` to have the value omitted
// from the result object. Calls preserve `this`.
2022-09-27 10:45:27 +03:00
object(format: Record<string, FormatFunc>, altNames?: Record<string, Array<string>>): FormatFunc {
return ((value: any) => {
const result: any = { };
2022-09-05 23:57:11 +03:00
for (const key in format) {
let srcKey = key;
if (altNames && key in altNames && !(srcKey in value)) {
for (const altKey of altNames[key]) {
if (altKey in value) {
srcKey = altKey;
break;
}
}
}
2022-09-27 10:45:27 +03:00
2022-09-05 23:57:11 +03:00
try {
const nv = format[key].call(this, value[srcKey]);
2022-09-27 10:45:27 +03:00
if (nv !== undefined) { result[key] = nv; }
} catch (error) {
const message = (error instanceof Error) ? error.message: "not-an-error";
throwError(`invalid value for value.${ key } (${ message })`, "BAD_DATA", { value })
2022-09-05 23:57:11 +03:00
}
}
return result;
});
}
}
2022-09-27 10:45:27 +03:00
*/
2022-09-05 23:57:11 +03:00
//# sourceMappingURL=formatter.js.map