diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 2d3d00b..9742604 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -72,3 +72,5 @@ export const GERALD_CONTRACT_ADDRESSES = [ // Token is KSM, DOT, UNIT (depending what relaychain is connected) export const RELAY_ASSET_ID = "42259045809535163221576417993425387648"; export const USDT_ASSET_ID = "311091173110107856861649819128533077277"; + +export const EXTRINSIC_BASE_WEIGHT = 250_000_000; diff --git a/src/utils/monitoring.ts b/src/utils/monitoring.ts index 3f9ca4b..d9572cf 100644 --- a/src/utils/monitoring.ts +++ b/src/utils/monitoring.ts @@ -206,10 +206,10 @@ export const getAccountFromNimbusKey = async ( !authorMappingCache[nmbsKey] || authorMappingCache[nmbsKey].lastUpdate < Date.now() - 3600 * 1000 ) { - const mappingData = (await api.query.authorMapping.mappingWithDeposit(nmbsKey)) as Option; + const mappingData = (await api.query.authorMapping.mapping(nmbsKey)) as Option; authorMappingCache[nmbsKey] = { lastUpdate: Date.now(), - account: mappingData.isEmpty ? null : ethereumEncode(mappingData.unwrap().account.toString()), + account: mappingData.isEmpty ? null : ethereumEncode(mappingData.unwrap().toString()), }; } const { account } = authorMappingCache[nmbsKey]; @@ -284,27 +284,16 @@ export const getBlockDetails = async (api: ApiPromise, blockHash: BlockHash) => ]); const feeMultiplier = await getFeeMultiplier(api, block.header.parentHash.toString()); - const txWithEvents = mapExtrinsics( + const txWithEvents = await mapExtrinsics( + api, block.extrinsics, records, fees.map((fee) => fee.inclusionFee.unwrapOrDefault()), - feeMultiplier, - apiAt.consts.transactionPayment?.weightToFee || - ([ - { - coeffInteger: new u128( - api.registry, - api.runtimeVersion.specName.toString() == "moonbeam" ? 1_000_000 : 10_000 - ).muln(apiAt.consts.transactionPayment.operationalFeeMultiplier.toNumber() || 5), - coeffFrac: api.registry.createType("Perbill", 0), - negative: new bool(api.registry, false), - degree: new u8(api.registry, 1), - }, - ] as any) + feeMultiplier ); const blockWeight = txWithEvents.reduce((totalWeight, tx, index) => { // TODO: support weight v1/2 - return totalWeight + (tx.dispatchInfo && (tx.dispatchInfo.weight as any).refTime.toBigInt()); + return totalWeight + (tx.dispatchInfo && (tx.dispatchInfo.weight as any).toBigInt()); }, 0n); return { block, diff --git a/src/utils/types.ts b/src/utils/types.ts index d733cc2..5452474 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -9,6 +9,9 @@ import type { u128 } from "@polkadot/types"; import { BN } from "@polkadot/util"; import type { TxWithEvent } from "@polkadot/api-derive/types"; +import { ApiPromise } from "@polkadot/api"; +import { toPairsIn } from "lodash"; +import { EXTRINSIC_BASE_WEIGHT } from "./constants"; export interface ComputedFees { baseFee: bigint; @@ -20,50 +23,63 @@ export interface TxWithEventAndFee extends TxWithEvent { fees: ComputedFees; } -export function mapExtrinsics( +export const mapExtrinsics = async ( + api: ApiPromise, extrinsics: Extrinsic[], records: EventRecord[], fees: InclusionFee[], - feeMultiplier: u128, - weightToFees: any[] -): TxWithEventAndFee[] { - return extrinsics.map((extrinsic, index): TxWithEventAndFee => { - let dispatchError: DispatchError | undefined; - let dispatchInfo: DispatchInfo | undefined; + feeMultiplier: u128 +) => { + return Promise.all( + extrinsics.map(async (extrinsic, index) => { + let dispatchError: DispatchError | undefined; + let dispatchInfo: DispatchInfo | undefined; - const events = records - .filter(({ phase }) => phase.isApplyExtrinsic && phase.asApplyExtrinsic.eq(index)) - .map(({ event }) => { - if (event.section === "system") { - if (event.method === "ExtrinsicSuccess") { - dispatchInfo = event.data[0] as DispatchInfo; - } else if (event.method === "ExtrinsicFailed") { - dispatchError = event.data[0] as DispatchError; - dispatchInfo = event.data[1] as DispatchInfo; + const events = records + .filter(({ phase }) => phase.isApplyExtrinsic && phase.asApplyExtrinsic.eq(index)) + .map(({ event }) => { + if (event.section === "system") { + if (event.method === "ExtrinsicSuccess") { + dispatchInfo = event.data[0] as DispatchInfo; + } else if (event.method === "ExtrinsicFailed") { + dispatchError = event.data[0] as DispatchError; + dispatchInfo = event.data[1] as DispatchInfo; + } } - } - return event; - }); + return event; + }); - let computedFees: ComputedFees; - const feeDetails = fees[index]; + const unadjustedWeightFee = ( + (await api.call.transactionPaymentApi.queryWeightToFee(dispatchInfo.weight)) as any + ).toBigInt(); + const lengthFee = ( + (await api.call.transactionPaymentApi.queryLengthToFee(extrinsic.encodedLength)) as any + ).toBigInt(); - const frac = weightToFees[0].coeffFrac.mul((dispatchInfo.weight as any).refTime?.toBn()); - const integer = weightToFees[0].coeffInteger.mul((dispatchInfo.weight as any).refTime?.toBn()); + // TODO: should be doing this at api.at() the original block + const feeMultiplier = await api.query.transactionPayment.nextFeeMultiplier(); + const weightFee = + (unadjustedWeightFee * feeMultiplier.toBigInt()) / 1_000_000_000_000_000_000n; - const unadjustedFee = frac.add(integer); + const baseFee = ( + (await api.call.transactionPaymentApi.queryWeightToFee({ + refTime: EXTRINSIC_BASE_WEIGHT, + proofSize: 0n, + })) as any + ).toBigInt(); - const adjustedFee = BigInt( - unadjustedFee.mul(feeMultiplier.toBn()).div(new BN("1000000000000000000")).toString() - ); + const tip = extrinsic.tip.toBigInt(); - computedFees = { - baseFee: feeDetails.baseFee.toBigInt(), - lenFee: feeDetails.lenFee.toBigInt(), - weightFee: adjustedFee, - totalFees: adjustedFee + feeDetails.baseFee.toBigInt() + feeDetails.lenFee.toBigInt(), - }; - return { dispatchError, dispatchInfo, events, extrinsic, fees: computedFees }; - }); -} + const totalFees = lengthFee + weightFee + baseFee + tip; + + const computedFees: ComputedFees = { + baseFee, + lenFee: lengthFee, + weightFee, + totalFees, + }; + return { dispatchError, dispatchInfo, events, extrinsic, fees: computedFees }; + }) + ); +};