From bb9ddb9d3a98f73743684754193828be7b206680 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Tue, 6 Jun 2023 20:56:44 -0400 Subject: [PATCH] Add confirmations to TransactionResponse (#4094). --- src.ts/providers/provider.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src.ts/providers/provider.ts b/src.ts/providers/provider.ts index 9152341897..583527dc83 100644 --- a/src.ts/providers/provider.ts +++ b/src.ts/providers/provider.ts @@ -1345,6 +1345,26 @@ export class TransactionResponse implements TransactionLike, Transaction return this.provider.getTransaction(this.hash); } + /** + * Resolve to the number of confirmations this transaction has. + */ + async confirmations(): Promise { + if (this.blockNumber == null) { + const { tx, blockNumber } = await resolveProperties({ + tx: this.getTransaction(), + blockNumber: this.provider.getBlockNumber() + }); + + // Not mined yet... + if (tx == null || tx.blockNumber == null) { return 0; } + + return blockNumber - tx.blockNumber + 1; + } + + const blockNumber = await this.provider.getBlockNumber(); + return blockNumber - this.blockNumber + 1; + } + /** * Resolves once this transaction has been mined and has * %%confirms%% blocks including it (default: ``1``) with an