From 288324918b7445fce8be6c4606c41058c66cdf69 Mon Sep 17 00:00:00 2001 From: Tarik Gul <47201679+TarikGul@users.noreply.github.com> Date: Thu, 21 Dec 2023 12:30:58 -0500 Subject: [PATCH] fix: transactionPaidFee event optimization (#1367) --- src/services/blocks/BlocksService.ts | 33 ++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/services/blocks/BlocksService.ts b/src/services/blocks/BlocksService.ts index 12ce7c13c..e06b23314 100644 --- a/src/services/blocks/BlocksService.ts +++ b/src/services/blocks/BlocksService.ts @@ -74,6 +74,11 @@ interface FetchBlockOptions { noFees: boolean; } +interface ExtrinsicSuccessOrFailedOverride { + weight: Weight; + class: DispatchInfo; +} + /** * Event methods that we check for. */ @@ -233,6 +238,30 @@ export class BlocksService extends AbstractService { continue; } + const transactionPaidFeeEvent = xtEvents.find( + ({ method }) => isFrameMethod(method) && method.method === Event.transactionPaidFee, + ); + const extrinsicSuccess = xtEvents.find(({ method }) => isFrameMethod(method) && method.method === Event.success); + const extrinsicFailed = xtEvents.find(({ method }) => isFrameMethod(method) && method.method === Event.failure); + + const eventFailureOrSuccess = extrinsicSuccess || extrinsicFailed; + if (transactionPaidFeeEvent && eventFailureOrSuccess) { + let availableData: ExtrinsicSuccessOrFailedOverride; + if (extrinsicSuccess) { + availableData = eventFailureOrSuccess.data[0] as unknown as ExtrinsicSuccessOrFailedOverride; + } else { + availableData = eventFailureOrSuccess.data[1] as unknown as ExtrinsicSuccessOrFailedOverride; + } + + extrinsics[idx].info = { + weight: availableData.weight, + class: availableData.class, + partialFee: transactionPaidFeeEvent.data[1].toString(), + kind: 'fromEvent', + }; + continue; + } + /** * Grab the initial partialFee, and information required for calculating a partialFee * if queryFeeDetails is available in the runtime. @@ -244,10 +273,6 @@ export class BlocksService extends AbstractService { } = await this.fetchQueryInfo(block.extrinsics[idx], previousBlockHash); const versionedWeight = (weight as Weight).refTime ? (weight as Weight).refTime.unwrap() : (weight as WeightV1); - const transactionPaidFeeEvent = xtEvents.find( - ({ method }) => isFrameMethod(method) && method.method === Event.transactionPaidFee, - ); - let finalPartialFee = partialFee.toString(), dispatchFeeType = 'preDispatch'; if (transactionPaidFeeEvent) {