mev-inspect
Version:
A JS port of 'mev-inspect-py' optimised for ease of use.
106 lines • 3.83 kB
JavaScript
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _Chain_instances, _Chain_getBlock, _Chain_getLogs;
class Chain {
constructor(provider) {
_Chain_instances.add(this);
Object.defineProperty(this, "provider", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.provider = provider;
}
async getTransactionLogs(hash) {
const receipt = await this.getReceipt(hash);
if (!receipt) {
return [];
}
return __classPrivateFieldGet(this, _Chain_instances, "m", _Chain_getLogs).call(this, receipt);
}
async getBlockLogs(number) {
const block = await __classPrivateFieldGet(this, _Chain_instances, "m", _Chain_getBlock).call(this, number);
const hashes = block.transactions;
const logs = [];
for (const hash of hashes) {
const txLogs = await this.getTransactionLogs(hash);
logs.push(txLogs);
}
return logs.flat();
}
async getReceipt(hash) {
let receipt = undefined;
while (receipt === undefined) {
try {
receipt = await this.provider.getTransactionReceipt(hash);
}
catch (e) {
const errorCode = e.code;
if (errorCode === 'TIMEOUT') {
console.log(`Failed to fetch receipts, reason: ${errorCode}, retrying`);
}
else {
throw e;
}
}
}
return receipt;
}
async getBlockReceipts(number) {
const block = await __classPrivateFieldGet(this, _Chain_instances, "m", _Chain_getBlock).call(this, number);
const hashes = block.transactions;
const receipts = [];
for (const hash of hashes) {
const receipt = await this.getReceipt(hash);
if (!receipt) {
continue;
}
receipts.push(receipt);
}
return receipts;
}
parseReceipts(receipts) {
return receipts.map((receipt) => __classPrivateFieldGet(this, _Chain_instances, "m", _Chain_getLogs).call(this, receipt)).flat();
}
}
_Chain_instances = new WeakSet(), _Chain_getBlock = async function _Chain_getBlock(number) {
let block = null;
while (!block) {
try {
block = await this.provider.getBlock(number, true);
}
catch (e) {
const errorCode = e.code;
if (errorCode === 'TIMEOUT') {
console.log(`Failed to fetch the block, reason: ${errorCode}, retrying`);
}
else {
throw e;
}
}
}
return block;
}, _Chain_getLogs = function _Chain_getLogs(receipt) {
const { from, logs, gasUsed } = receipt;
return logs.map((log) => {
const { transactionHash, transactionIndex, index, address, topics, data, blockNumber, blockHash, } = log;
return {
blockHash,
blockNumber,
transactionFrom: from,
transactionHash,
transactionIndex,
logIndex: index,
gasUsed: parseInt(gasUsed.toString()),
address,
topics,
data,
};
});
};
export default Chain;
//# sourceMappingURL=chain.js.map