Added sync methods for Block with prefetched transactions.

This commit is contained in:
Richard Moore 2023-01-26 23:35:25 -05:00
parent 8c3d677371
commit 360835e301

@ -382,6 +382,25 @@ export class Block implements BlockParams, Iterable<string> {
}); });
} }
/**
* Returns the complete transactions for blocks which
* prefetched them, by passing ``true`` to %%prefetchTxs%%
* into [[provider_getBlock]].
*/
get transactionResponses(): Array<TransactionResponse> {
const txs = this.#transactions.slice();
// Doesn't matter...
if (txs.length === 0) { return [ ]; }
// Make sure we prefetched the transactions
assert(typeof(txs[0]) === "object", "transactions were not prefetched with block request", "UNSUPPORTED_OPERATION", {
operation: "transactionResponses()"
});
return <Array<TransactionResponse>>txs;
}
/** /**
* Returns a JSON-friendly value. * Returns a JSON-friendly value.
*/ */
@ -462,6 +481,20 @@ export class Block implements BlockParams, Iterable<string> {
} }
} }
getTransactionResponse(indexOrHash: number | string): TransactionResponse {
const txs = this.transactionResponses;
if (typeof(indexOrHash) === "number") {
return txs[indexOrHash];
}
indexOrHash = indexOrHash.toLowerCase();
for (const tx of txs) {
if (tx.hash === indexOrHash) { return tx; }
}
throw new Error("no such tx");
}
/** /**
* Has this block been mined. * Has this block been mined.
* *
@ -797,6 +830,7 @@ export interface MinedTransactionResponse extends TransactionResponse {
date: Date; date: Date;
} }
/*
export type ReplacementDetectionSetup = { export type ReplacementDetectionSetup = {
to: string; to: string;
from: string; from: string;
@ -805,7 +839,7 @@ export type ReplacementDetectionSetup = {
nonce: number; nonce: number;
block: number; block: number;
}; };
*/
export class TransactionResponse implements TransactionLike<string>, TransactionResponseParams { export class TransactionResponse implements TransactionLike<string>, TransactionResponseParams {
readonly provider: Provider; readonly provider: Provider;