opnet
Version:
The perfect library for building Bitcoin-based applications.
53 lines (52 loc) • 2.03 kB
JavaScript
import { TransactionInput } from './metadata/TransactionInput.js';
import { TransactionOutput } from './metadata/TransactionOutput.js';
import { TransactionReceipt } from './metadata/TransactionReceipt.js';
export class TransactionBase extends TransactionReceipt {
id;
hash;
index;
burnedBitcoin;
priorityFee;
maxGasSat;
inputs;
outputs;
OPNetType;
gasUsed;
specialGasUsed;
pow;
blockNumber;
constructor(transaction, network) {
super({
receipt: transaction.receipt,
receiptProofs: transaction.receiptProofs,
events: transaction.events,
revert: transaction.revert,
gasUsed: transaction.gasUsed,
specialGasUsed: transaction.specialGasUsed,
}, network);
this.id = transaction.id;
this.hash = transaction.hash;
this.index = transaction.index;
if (transaction.blockNumber)
this.blockNumber = BigInt(transaction.blockNumber);
this.burnedBitcoin = BigInt(transaction.burnedBitcoin) || 0n;
this.priorityFee = BigInt(transaction.priorityFee) || 0n;
this.inputs = transaction.inputs.map((input) => new TransactionInput(input));
this.outputs = transaction.outputs.map((output) => new TransactionOutput(output));
this.OPNetType = transaction.OPNetType;
this.gasUsed = BigInt(transaction.gasUsed || '0x00') || 0n;
this.specialGasUsed = BigInt(transaction.specialGasUsed || '0x00') || 0n;
if (transaction.pow) {
this.pow = this.decodeProofOfWorkChallenge(transaction.pow);
}
this.maxGasSat = this.burnedBitcoin + (this.pow?.reward || 0n) - this.priorityFee;
}
decodeProofOfWorkChallenge(challenge) {
return {
preimage: Buffer.from(challenge.preimage, 'base64'),
reward: BigInt(challenge.reward) || 0n,
difficulty: BigInt(challenge.difficulty || '0'),
version: challenge.version || 0,
};
}
}