From 0b0f08ee6e8cbab9a5d674d8dff13eb26f19a337 Mon Sep 17 00:00:00 2001 From: Christopher Howard Date: Fri, 13 Sep 2024 14:10:37 -0400 Subject: [PATCH] fix: prevent underflow error when parsing native asset fee and price (#1690) Co-authored-by: Daniel Sinclair <4412473+DanielSinclair@users.noreply.github.com> --- src/core/utils/transactions.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/core/utils/transactions.ts b/src/core/utils/transactions.ts index 79a52b4350..820eebf77a 100644 --- a/src/core/utils/transactions.ts +++ b/src/core/utils/transactions.ts @@ -222,7 +222,10 @@ const parseFees = ( const feeValue = FixedNumber.from( formatUnits(BigInt(fee.value) + rollupFee, nativeAssetDecimals), ); - const feePrice = FixedNumber.fromString(fee.price.toString()); + const feePrice = FixedNumber.fromString( + fee.price.toFixed(nativeAssetDecimals).toString(), + nativeAssetDecimals, + ); return { fee: feeValue.toString(), @@ -278,19 +281,23 @@ export function parseTransaction({ const description = getDescription(asset, type, meta); const nativeAsset = changes.find((change) => change?.asset.isNativeAsset); - const nativeAssetPrice = FixedNumber.fromString( - nativeAsset?.price?.toString() || '0', - ); const value = FixedNumber.fromValue( BigNumber.from(nativeAsset?.value || 0), nativeAsset?.asset.decimals || 0, ); - const valueInNative = value.mulUnsafe(nativeAssetPrice).toString(); - const nativeAssetDecimals = 18; // we only support networks with 18 decimals native assets rn, backend will change when we support more + const nativeAssetPrice = FixedNumber.fromString( + typeof nativeAsset?.price === 'number' + ? nativeAsset.price.toFixed(nativeAssetDecimals).toString() + : '0', + nativeAssetDecimals, + ); + + const valueInNative = value.mulUnsafe(nativeAssetPrice).toString(); + const { feeInNative, ...fee } = parseFees(tx.fee, nativeAssetDecimals); const native = {