2022-09-27 10:45:27 +03:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2022-11-30 23:44:23 +03: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 10:45:27 +03:00
|
|
|
const index_js_1 = require("../address/index.js");
|
2022-09-30 05:57:27 +03: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 10:45:27 +03:00
|
|
|
const BN_0 = BigInt(0);
|
|
|
|
function allowNull(format, nullValue) {
|
|
|
|
return (function (value) {
|
|
|
|
if (value == null) {
|
|
|
|
return nullValue;
|
|
|
|
}
|
|
|
|
return format(value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
exports.allowNull = allowNull;
|
|
|
|
function arrayOf(format) {
|
|
|
|
return ((array) => {
|
|
|
|
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 10:57:02 +03:00
|
|
|
(0, index_js_4.assert)(false, `invalid value for value.${key} (${message})`, "BAD_DATA", { value });
|
2022-09-27 10:45:27 +03: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 10:57:02 +03:00
|
|
|
(0, index_js_4.assertArgument)(false, `invalid boolean; ${JSON.stringify(value)}`, "value", value);
|
2022-09-27 10:45:27 +03:00
|
|
|
}
|
|
|
|
exports.formatBoolean = formatBoolean;
|
|
|
|
function formatData(value) {
|
2022-11-09 10:57:02 +03:00
|
|
|
(0, index_js_4.assertArgument)((0, index_js_4.isHexString)(value, true), "invalid data", "value", value);
|
2022-09-27 10:45:27 +03:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
exports.formatData = formatData;
|
|
|
|
function formatHash(value) {
|
2022-11-09 10:57:02 +03:00
|
|
|
(0, index_js_4.assertArgument)((0, index_js_4.isHexString)(value, 32), "invalid hash", "value", value);
|
2022-09-27 10:45:27 +03:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
exports.formatHash = formatHash;
|
|
|
|
function formatUint256(value) {
|
2022-09-30 05:57:27 +03:00
|
|
|
if (!(0, index_js_4.isHexString)(value)) {
|
2022-09-27 10:45:27 +03:00
|
|
|
throw new Error("invalid uint256");
|
|
|
|
}
|
2022-09-30 05:57:27 +03:00
|
|
|
return (0, index_js_4.zeroPadValue)(value, 32);
|
2022-09-27 10:45:27 +03:00
|
|
|
}
|
|
|
|
exports.formatUint256 = formatUint256;
|
2022-11-30 23:44:23 +03:00
|
|
|
const _formatLog = object({
|
2022-09-27 10:45:27 +03:00
|
|
|
address: index_js_1.getAddress,
|
|
|
|
blockHash: formatHash,
|
2022-09-30 05:57:27 +03:00
|
|
|
blockNumber: index_js_4.getNumber,
|
2022-09-27 10:45:27 +03:00
|
|
|
data: formatData,
|
2022-09-30 05:57:27 +03:00
|
|
|
index: index_js_4.getNumber,
|
2022-09-27 10:45:27 +03:00
|
|
|
removed: formatBoolean,
|
|
|
|
topics: arrayOf(formatHash),
|
|
|
|
transactionHash: formatHash,
|
2022-09-30 05:57:27 +03:00
|
|
|
transactionIndex: index_js_4.getNumber,
|
2022-09-27 10:45:27 +03:00
|
|
|
}, {
|
|
|
|
index: ["logIndex"]
|
|
|
|
});
|
2022-11-30 23:44:23 +03:00
|
|
|
function formatLog(value) {
|
|
|
|
return _formatLog(value);
|
|
|
|
}
|
|
|
|
exports.formatLog = formatLog;
|
|
|
|
const _formatBlock = object({
|
|
|
|
hash: allowNull(formatHash),
|
|
|
|
parentHash: formatHash,
|
|
|
|
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,
|
|
|
|
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 10:45:27 +03:00
|
|
|
});
|
2022-11-30 23:44:23 +03:00
|
|
|
return result;
|
2022-09-27 10:45:27 +03:00
|
|
|
}
|
2022-11-30 23:44:23 +03:00
|
|
|
exports.formatBlock = formatBlock;
|
|
|
|
const _formatReceiptLog = object({
|
2022-09-30 05:57:27 +03:00
|
|
|
transactionIndex: index_js_4.getNumber,
|
|
|
|
blockNumber: index_js_4.getNumber,
|
2022-09-27 10:45:27 +03:00
|
|
|
transactionHash: formatHash,
|
|
|
|
address: index_js_1.getAddress,
|
|
|
|
topics: arrayOf(formatHash),
|
|
|
|
data: formatData,
|
2022-09-30 05:57:27 +03:00
|
|
|
index: index_js_4.getNumber,
|
2022-09-27 10:45:27 +03:00
|
|
|
blockHash: formatHash,
|
2022-09-30 05:57:27 +03:00
|
|
|
}, {
|
|
|
|
index: ["logIndex"]
|
2022-09-27 10:45:27 +03:00
|
|
|
});
|
2022-11-30 23:44:23 +03:00
|
|
|
function formatReceiptLog(value) {
|
|
|
|
return _formatReceiptLog(value);
|
|
|
|
}
|
|
|
|
exports.formatReceiptLog = formatReceiptLog;
|
|
|
|
const _formatTransactionReceipt = object({
|
2022-09-27 10:45:27 +03: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-30 05:57:27 +03:00
|
|
|
index: index_js_4.getNumber,
|
|
|
|
root: allowNull(index_js_4.hexlify),
|
|
|
|
gasUsed: index_js_4.getBigInt,
|
2022-09-27 10:45:27 +03:00
|
|
|
logsBloom: allowNull(formatData),
|
|
|
|
blockHash: formatHash,
|
2022-09-30 05:57:27 +03:00
|
|
|
hash: formatHash,
|
2022-11-30 23:44:23 +03:00
|
|
|
logs: arrayOf(formatReceiptLog),
|
2022-09-30 05:57:27 +03:00
|
|
|
blockNumber: index_js_4.getNumber,
|
2022-11-09 10:57:02 +03:00
|
|
|
//confirmations: allowNull(getNumber, null),
|
2022-09-30 05:57:27 +03:00
|
|
|
cumulativeGasUsed: index_js_4.getBigInt,
|
|
|
|
effectiveGasPrice: allowNull(index_js_4.getBigInt),
|
|
|
|
status: allowNull(index_js_4.getNumber),
|
|
|
|
type: index_js_4.getNumber
|
2022-09-27 10:45:27 +03:00
|
|
|
}, {
|
2022-09-30 05:57:27 +03:00
|
|
|
effectiveGasPrice: ["gasPrice"],
|
|
|
|
hash: ["transactionHash"],
|
|
|
|
index: ["transactionIndex"],
|
2022-09-27 10:45:27 +03:00
|
|
|
});
|
2022-11-30 23:44:23 +03:00
|
|
|
function formatTransactionReceipt(value) {
|
|
|
|
return _formatTransactionReceipt(value);
|
|
|
|
}
|
|
|
|
exports.formatTransactionReceipt = formatTransactionReceipt;
|
2022-09-27 10:45:27 +03: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-30 05:57:27 +03:00
|
|
|
if (value.to && (0, index_js_4.getBigInt)(value.to) === BN_0) {
|
2022-09-27 10:45:27 +03:00
|
|
|
value.to = "0x0000000000000000000000000000000000000000";
|
|
|
|
}
|
|
|
|
const result = object({
|
|
|
|
hash: formatHash,
|
|
|
|
type: (value) => {
|
|
|
|
if (value === "0x" || value == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
2022-09-30 05:57:27 +03:00
|
|
|
return (0, index_js_4.getNumber)(value);
|
2022-09-27 10:45:27 +03:00
|
|
|
},
|
2022-09-30 05:57:27 +03:00
|
|
|
accessList: allowNull(index_js_3.accessListify, null),
|
2022-09-27 10:45:27 +03:00
|
|
|
blockHash: allowNull(formatHash, null),
|
2022-09-30 05:57:27 +03:00
|
|
|
blockNumber: allowNull(index_js_4.getNumber, null),
|
|
|
|
transactionIndex: allowNull(index_js_4.getNumber, null),
|
2022-11-09 10:57:02 +03:00
|
|
|
//confirmations: allowNull(getNumber, null),
|
2022-09-27 10:45:27 +03:00
|
|
|
from: index_js_1.getAddress,
|
|
|
|
// either (gasPrice) or (maxPriorityFeePerGas + maxFeePerGas) must be set
|
2022-09-30 05:57:27 +03:00
|
|
|
gasPrice: allowNull(index_js_4.getBigInt),
|
|
|
|
maxPriorityFeePerGas: allowNull(index_js_4.getBigInt),
|
|
|
|
maxFeePerGas: allowNull(index_js_4.getBigInt),
|
|
|
|
gasLimit: index_js_4.getBigInt,
|
2022-09-27 10:45:27 +03:00
|
|
|
to: allowNull(index_js_1.getAddress, null),
|
2022-09-30 05:57:27 +03:00
|
|
|
value: index_js_4.getBigInt,
|
|
|
|
nonce: index_js_4.getNumber,
|
2022-09-27 10:45:27 +03:00
|
|
|
data: formatData,
|
|
|
|
creates: allowNull(index_js_1.getAddress, null),
|
2022-09-30 05:57:27 +03:00
|
|
|
chainId: allowNull(index_js_4.getBigInt, null)
|
2022-09-27 10:45:27 +03:00
|
|
|
}, {
|
|
|
|
data: ["input"],
|
|
|
|
gasLimit: ["gas"]
|
|
|
|
})(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-30 05:57:27 +03:00
|
|
|
result.accessList = [];
|
|
|
|
}
|
|
|
|
// Compute the signature
|
2022-11-09 10:57:02 +03:00
|
|
|
if (value.signature) {
|
|
|
|
result.signature = index_js_2.Signature.from(value.signature);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.signature = index_js_2.Signature.from(value);
|
|
|
|
}
|
2022-09-30 05:57:27 +03: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 10:45:27 +03: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-30 05:57:27 +03:00
|
|
|
if (result.blockHash && (0, index_js_4.getBigInt)(result.blockHash) === BN_0) {
|
2022-09-27 10:45:27 +03:00
|
|
|
result.blockHash = null;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
exports.formatTransactionResponse = formatTransactionResponse;
|
|
|
|
//# sourceMappingURL=format.js.map
|