UNPKG

opnet

Version:

The perfect library for building Bitcoin-based applications.

65 lines (64 loc) 2.45 kB
import { fromBase64 } from '@btc-vision/bitcoin'; import { NetEvent } from '@btc-vision/transaction'; import { getP2op } from '../../cache/P2OPCache.js'; import { decodeRevertData } from '../../utils/RevertDecoder.js'; export class TransactionReceipt { receipt; receiptProofs; events; rawEvents = {}; rawRevert; revert; failed = false; gasUsed; specialGasUsed; constructor(receipt, network) { this.receipt = receipt.receipt ? fromBase64(receipt.receipt) : undefined; this.receiptProofs = receipt.receiptProofs || []; this.events = receipt.events ? this.parseEvents(receipt.events, network) : {}; this.rawRevert = receipt.revert ? fromBase64(receipt.revert) : undefined; this.revert = this.rawRevert ? decodeRevertData(this.rawRevert) : undefined; this.failed = receipt.revert !== undefined && receipt.revert !== null; this.gasUsed = BigInt(receipt.gasUsed || '0x00') || 0n; this.specialGasUsed = BigInt(receipt.specialGasUsed || '0x00') || 0n; } parseEvents(events, network) { const parsedEvents = {}; if (!Array.isArray(events)) { for (const [key, value] of Object.entries(events)) { const caP2op = getP2op(key, network); const v = value.map((event) => { return this.decodeEvent(event); }); parsedEvents[caP2op] = v; this.rawEvents[key] = v; } } else { for (const event of events) { const parsedEvent = this.decodeEvent(event); const contractAddress = event.contractAddress; const caP2op = getP2op(contractAddress, network); if (!parsedEvents[caP2op]) { parsedEvents[caP2op] = []; } parsedEvents[caP2op].push(parsedEvent); if (!this.rawEvents[contractAddress]) { this.rawEvents[contractAddress] = []; } this.rawEvents[contractAddress].push(parsedEvent); } } return parsedEvents; } decodeEvent(event) { let eventData; if (typeof event.data === 'string') { eventData = fromBase64(event.data); } else { eventData = event.data; } return new NetEvent(event.type, eventData); } }