ethers.js/lib.commonjs/providers/format.js

287 lines
9.8 KiB
JavaScript
Raw Permalink Normal View History

2022-09-27 03:45:27 -04:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
2022-11-30 15:44:23 -05:00
exports.formatTransactionResponse = exports.formatTransactionReceipt = exports.formatReceiptLog = exports.formatBlock = exports.formatLog = exports.formatUint256 = exports.formatHash = exports.formatData = exports.formatBoolean = exports.object = exports.arrayOf = exports.allowNull = void 0;
/**
* @_ignore
*/
2022-09-27 03:45:27 -04:00
const index_js_1 = require("../address/index.js");
2022-09-29 22:57:27 -04:00
const index_js_2 = require("../crypto/index.js");
const index_js_3 = require("../transaction/index.js");
const index_js_4 = require("../utils/index.js");
2022-09-27 03:45:27 -04:00
const BN_0 = BigInt(0);
function allowNull(format, nullValue) {
return (function (value) {
if (value == null) {
return nullValue;
}
return format(value);
});
}
exports.allowNull = allowNull;
2024-02-03 00:19:51 -05:00
function arrayOf(format, allowNull) {
2022-09-27 03:45:27 -04:00
return ((array) => {
2024-02-03 00:19:51 -05:00
if (allowNull && array == null) {
return null;
}
2022-09-27 03:45:27 -04:00
if (!Array.isArray(array)) {
throw new Error("not an array");
}
return array.map((i) => format(i));
});
}
exports.arrayOf = arrayOf;
// 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`.
function object(format, altNames) {
return ((value) => {
const result = {};
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;
}
}
}
try {
const nv = format[key](value[srcKey]);
if (nv !== undefined) {
result[key] = nv;
}
}
catch (error) {
const message = (error instanceof Error) ? error.message : "not-an-error";
2022-11-09 02:57:02 -05:00
(0, index_js_4.assert)(false, `invalid value for value.${key} (${message})`, "BAD_DATA", { value });
2022-09-27 03:45:27 -04:00
}
}
return result;
});
}
exports.object = object;
function formatBoolean(value) {
switch (value) {
case true:
case "true":
return true;
case false:
case "false":
return false;
}
2022-11-09 02:57:02 -05:00
(0, index_js_4.assertArgument)(false, `invalid boolean; ${JSON.stringify(value)}`, "value", value);
2022-09-27 03:45:27 -04:00
}
exports.formatBoolean = formatBoolean;
function formatData(value) {
2022-11-09 02:57:02 -05:00
(0, index_js_4.assertArgument)((0, index_js_4.isHexString)(value, true), "invalid data", "value", value);
2022-09-27 03:45:27 -04:00
return value;
}
exports.formatData = formatData;
function formatHash(value) {
2022-11-09 02:57:02 -05:00
(0, index_js_4.assertArgument)((0, index_js_4.isHexString)(value, 32), "invalid hash", "value", value);
2022-09-27 03:45:27 -04:00
return value;
}
exports.formatHash = formatHash;
function formatUint256(value) {
2022-09-29 22:57:27 -04:00
if (!(0, index_js_4.isHexString)(value)) {
2022-09-27 03:45:27 -04:00
throw new Error("invalid uint256");
}
2022-09-29 22:57:27 -04:00
return (0, index_js_4.zeroPadValue)(value, 32);
2022-09-27 03:45:27 -04:00
}
exports.formatUint256 = formatUint256;
2022-11-30 15:44:23 -05:00
const _formatLog = object({
2022-09-27 03:45:27 -04:00
address: index_js_1.getAddress,
blockHash: formatHash,
2022-09-29 22:57:27 -04:00
blockNumber: index_js_4.getNumber,
2022-09-27 03:45:27 -04:00
data: formatData,
2022-09-29 22:57:27 -04:00
index: index_js_4.getNumber,
2023-04-19 17:30:37 +09:00
removed: allowNull(formatBoolean, false),
2022-09-27 03:45:27 -04:00
topics: arrayOf(formatHash),
transactionHash: formatHash,
2022-09-29 22:57:27 -04:00
transactionIndex: index_js_4.getNumber,
2022-09-27 03:45:27 -04:00
}, {
index: ["logIndex"]
});
2022-11-30 15:44:23 -05:00
function formatLog(value) {
return _formatLog(value);
}
exports.formatLog = formatLog;
const _formatBlock = object({
hash: allowNull(formatHash),
parentHash: formatHash,
2024-02-05 19:59:21 -05:00
parentBeaconBlockRoot: allowNull(formatHash, null),
2022-11-30 15:44:23 -05:00
number: index_js_4.getNumber,
timestamp: index_js_4.getNumber,
nonce: allowNull(formatData),
difficulty: index_js_4.getBigInt,
gasLimit: index_js_4.getBigInt,
gasUsed: index_js_4.getBigInt,
2024-02-05 19:59:21 -05:00
stateRoot: allowNull(formatHash, null),
receiptsRoot: allowNull(formatHash, null),
2024-02-03 00:19:51 -05:00
blobGasUsed: allowNull(index_js_4.getBigInt, null),
excessBlobGas: allowNull(index_js_4.getBigInt, null),
2022-11-30 15:44:23 -05:00
miner: allowNull(index_js_1.getAddress),
extraData: formatData,
baseFeePerGas: allowNull(index_js_4.getBigInt)
});
function formatBlock(value) {
const result = _formatBlock(value);
result.transactions = value.transactions.map((tx) => {
if (typeof (tx) === "string") {
return tx;
}
return formatTransactionResponse(tx);
2022-09-27 03:45:27 -04:00
});
2022-11-30 15:44:23 -05:00
return result;
2022-09-27 03:45:27 -04:00
}
2022-11-30 15:44:23 -05:00
exports.formatBlock = formatBlock;
const _formatReceiptLog = object({
2022-09-29 22:57:27 -04:00
transactionIndex: index_js_4.getNumber,
blockNumber: index_js_4.getNumber,
2022-09-27 03:45:27 -04:00
transactionHash: formatHash,
address: index_js_1.getAddress,
topics: arrayOf(formatHash),
data: formatData,
2022-09-29 22:57:27 -04:00
index: index_js_4.getNumber,
2022-09-27 03:45:27 -04:00
blockHash: formatHash,
2022-09-29 22:57:27 -04:00
}, {
index: ["logIndex"]
2022-09-27 03:45:27 -04:00
});
2022-11-30 15:44:23 -05:00
function formatReceiptLog(value) {
return _formatReceiptLog(value);
}
exports.formatReceiptLog = formatReceiptLog;
const _formatTransactionReceipt = object({
2022-09-27 03:45:27 -04:00
to: allowNull(index_js_1.getAddress, null),
from: allowNull(index_js_1.getAddress, null),
contractAddress: allowNull(index_js_1.getAddress, null),
// should be allowNull(hash), but broken-EIP-658 support is handled in receipt
2022-09-29 22:57:27 -04:00
index: index_js_4.getNumber,
root: allowNull(index_js_4.hexlify),
gasUsed: index_js_4.getBigInt,
2024-02-03 00:19:51 -05:00
blobGasUsed: allowNull(index_js_4.getBigInt, null),
2022-09-27 03:45:27 -04:00
logsBloom: allowNull(formatData),
blockHash: formatHash,
2022-09-29 22:57:27 -04:00
hash: formatHash,
2022-11-30 15:44:23 -05:00
logs: arrayOf(formatReceiptLog),
2022-09-29 22:57:27 -04:00
blockNumber: index_js_4.getNumber,
2022-11-09 02:57:02 -05:00
//confirmations: allowNull(getNumber, null),
2022-09-29 22:57:27 -04:00
cumulativeGasUsed: index_js_4.getBigInt,
effectiveGasPrice: allowNull(index_js_4.getBigInt),
2024-02-03 00:19:51 -05:00
blobGasPrice: allowNull(index_js_4.getBigInt, null),
2022-09-29 22:57:27 -04:00
status: allowNull(index_js_4.getNumber),
2023-02-12 22:14:26 -05:00
type: allowNull(index_js_4.getNumber, 0)
2022-09-27 03:45:27 -04:00
}, {
2022-09-29 22:57:27 -04:00
effectiveGasPrice: ["gasPrice"],
hash: ["transactionHash"],
index: ["transactionIndex"],
2022-09-27 03:45:27 -04:00
});
2022-11-30 15:44:23 -05:00
function formatTransactionReceipt(value) {
return _formatTransactionReceipt(value);
}
exports.formatTransactionReceipt = formatTransactionReceipt;
2022-09-27 03:45:27 -04:00
function formatTransactionResponse(value) {
// Some clients (TestRPC) do strange things like return 0x0 for the
// 0 address; correct this to be a real address
2022-09-29 22:57:27 -04:00
if (value.to && (0, index_js_4.getBigInt)(value.to) === BN_0) {
2022-09-27 03:45:27 -04:00
value.to = "0x0000000000000000000000000000000000000000";
}
const result = object({
hash: formatHash,
2024-02-14 13:14:24 -05:00
// Some nodes do not return this, usually test nodes (like Ganache)
index: allowNull(index_js_4.getNumber, undefined),
2022-09-27 03:45:27 -04:00
type: (value) => {
if (value === "0x" || value == null) {
return 0;
}
2022-09-29 22:57:27 -04:00
return (0, index_js_4.getNumber)(value);
2022-09-27 03:45:27 -04:00
},
2022-09-29 22:57:27 -04:00
accessList: allowNull(index_js_3.accessListify, null),
2024-02-03 00:19:51 -05:00
blobVersionedHashes: allowNull(arrayOf(formatHash, true), null),
2022-09-27 03:45:27 -04:00
blockHash: allowNull(formatHash, null),
2022-09-29 22:57:27 -04:00
blockNumber: allowNull(index_js_4.getNumber, null),
transactionIndex: allowNull(index_js_4.getNumber, null),
2022-09-27 03:45:27 -04:00
from: index_js_1.getAddress,
// either (gasPrice) or (maxPriorityFeePerGas + maxFeePerGas) must be set
2022-09-29 22:57:27 -04:00
gasPrice: allowNull(index_js_4.getBigInt),
maxPriorityFeePerGas: allowNull(index_js_4.getBigInt),
maxFeePerGas: allowNull(index_js_4.getBigInt),
2024-02-03 00:19:51 -05:00
maxFeePerBlobGas: allowNull(index_js_4.getBigInt, null),
2022-09-29 22:57:27 -04:00
gasLimit: index_js_4.getBigInt,
2022-09-27 03:45:27 -04:00
to: allowNull(index_js_1.getAddress, null),
2022-09-29 22:57:27 -04:00
value: index_js_4.getBigInt,
nonce: index_js_4.getNumber,
2022-09-27 03:45:27 -04:00
data: formatData,
creates: allowNull(index_js_1.getAddress, null),
2022-09-29 22:57:27 -04:00
chainId: allowNull(index_js_4.getBigInt, null)
2022-09-27 03:45:27 -04:00
}, {
data: ["input"],
2024-02-14 13:14:24 -05:00
gasLimit: ["gas"],
index: ["transactionIndex"]
2022-09-27 03:45:27 -04:00
})(value);
// If to and creates are empty, populate the creates from the value
if (result.to == null && result.creates == null) {
result.creates = (0, index_js_1.getCreateAddress)(result);
}
// @TODO: Check fee data
// Add an access list to supported transaction types
if ((value.type === 1 || value.type === 2) && value.accessList == null) {
2022-09-29 22:57:27 -04:00
result.accessList = [];
}
// Compute the signature
2022-11-09 02:57:02 -05:00
if (value.signature) {
result.signature = index_js_2.Signature.from(value.signature);
}
else {
result.signature = index_js_2.Signature.from(value);
}
2022-09-29 22:57:27 -04:00
// Some backends omit ChainId on legacy transactions, but we can compute it
if (result.chainId == null) {
const chainId = result.signature.legacyChainId;
if (chainId != null) {
result.chainId = chainId;
}
2022-09-27 03:45:27 -04:00
}
// @TODO: check chainID
/*
if (value.chainId != null) {
let chainId = value.chainId;
if (isHexString(chainId)) {
chainId = BigNumber.from(chainId).toNumber();
}
result.chainId = chainId;
} else {
let chainId = value.networkId;
// geth-etc returns chainId
if (chainId == null && result.v == null) {
chainId = value.chainId;
}
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;
}
*/
// 0x0000... should actually be null
2022-09-29 22:57:27 -04:00
if (result.blockHash && (0, index_js_4.getBigInt)(result.blockHash) === BN_0) {
2022-09-27 03:45:27 -04:00
result.blockHash = null;
}
return result;
}
exports.formatTransactionResponse = formatTransactionResponse;
//# sourceMappingURL=format.js.map