Skip to content

Commit

Permalink
fix: prevent underflow error when parsing native asset fee and price (#…
Browse files Browse the repository at this point in the history
…1690)

Co-authored-by: Daniel Sinclair <4412473+DanielSinclair@users.noreply.github.com>
  • Loading branch information
derHowie and DanielSinclair authored Sep 13, 2024
1 parent fbb5694 commit 0b0f08e
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/core/utils/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 0b0f08e

Please sign in to comment.