/* eslint-disable @typescript-eslint/explicit-function-return-type */ import { BigNumber } from 'bignumber.js'; import type { DeepPartial } from '../types.js'; import type { QuoteResponse } from '../validators/quote-response.js'; import { assetIdsMatch } from './assets.js'; /** * 1500000 -> 1.5 * * @param value - The value to convert to token amount * @param decimals - The number of decimals to convert to * @param base - The base of the number system to use * @returns The token amount in string format */ export const calcNormalizedTokenAmount = ( value: string | BigNumber | undefined | null, decimals: number | undefined, base: number = 10, ) => { if (value === undefined || decimals === undefined || value === null) { return; } const divisor = new BigNumber(base).pow(decimals ?? 0); return new BigNumber(value, base).div(divisor).toFixed(); }; /** * 1.5 -> 1500000 * * @param value - The amount to convert to token value * @param decimals - The number of decimals to convert to * @param base - The base of the number system to use * @returns The token value in string format */ export const calcAtomicTokenAmount = ( value: string | BigNumber | undefined | null, decimals: number | undefined, base: number = 10, ) => { if (value === undefined || decimals === undefined || value === null) { return; } const divisor = new BigNumber(base).pow(decimals); return new BigNumber(value, base).times(divisor).toFixed(); }; /** * @deprecated No longer used * @param estimatedProcessingTimeInSeconds - The estimated processing time in seconds * @returns The estimated processing time in minutes */ export const formatEtaInMinutes = ( estimatedProcessingTimeInSeconds: number, ) => { if (estimatedProcessingTimeInSeconds < 60) { return `< 1`; } return (estimatedProcessingTimeInSeconds / 60).toFixed(); }; /** * Keys that require the asset to be the same for all fees */ const AMOUNT_KEYS = [ 'amount' as const, 'normalizedAmount' as const, 'minAmount' as const, 'minAmountNormalized' as const, ]; /** * Keys that can be aggregated across all fees */ const FIAT_OR_USD_KEYS = [ 'valueInCurrency' as const, 'usd' as const, 'minAmountValueInCurrency' as const, 'minAmountUsd' as const, ]; const KEYS = [...AMOUNT_KEYS, ...FIAT_OR_USD_KEYS]; /** * Groups amounts by matching assetId and sums amount and fiat keys within each group. * * @param maybeFees - The list of fees to aggregate * @returns One dest object per unique asset, or undefined if no fees are provided */ export const sumAmountsByAssetId = ( ...maybeFees: ( | (DeepPartial | undefined | null)[] | undefined )[] ): DeepPartial[] => { const fees = maybeFees .flat() .flat() .filter( (value): value is Partial => value !== undefined && value !== null, ); if (fees.length === 0) { return []; } return fees.reduce[]>( (acc, fee) => { const feeIndex = acc.findIndex(({ asset }) => assetIdsMatch(asset?.assetId, fee.asset?.assetId), ); if (feeIndex === -1) { return [...acc, { ...fee }]; } const aggregatedFees = { ...acc[feeIndex] }; KEYS.forEach((key) => { const value = fee[key]; if (value) { aggregatedFees[key] = new BigNumber(aggregatedFees[key] ?? 0) .plus(value) .toFixed(); } }); return [ ...acc.slice(0, feeIndex), aggregatedFees, ...acc.slice(feeIndex + 1), ]; }, [], ); }; /** * Aggregates a list of amounts into a single fee object. If fees have different assets, * the returned object will only aggregate the usd and valueInCurrency values. * * @param maybeFees - The list of fees to aggregate * @returns The aggregated fee object, or null if no fees are provided */ export const sumAmounts = ( ...maybeFees: ( | (DeepPartial | undefined | null)[] | undefined )[] ): DeepPartial | undefined => { const fees = maybeFees .flat() .flat() .filter( (value): value is Partial => value !== undefined && value !== null, ); if (!fees || fees.length === 0) { return undefined; } /** * Fees and prices can be denominated in different assets, so we need to check if all fees have the same units */ const isSameAssetForAllFees = fees.reduce( (acc, fee) => acc && assetIdsMatch(fee.asset?.assetId, fees[0]?.asset?.assetId), true, ); return fees.reduce((acc, fee) => { const newAcc = { ...acc }; if (isSameAssetForAllFees && fee.asset) { newAcc.asset = fee.asset; } AMOUNT_KEYS.forEach((key) => { const value = fee[key]; if (value && isSameAssetForAllFees) { newAcc[key] = new BigNumber(acc[key] ?? 0).plus(value).toFixed(); } }); FIAT_OR_USD_KEYS.forEach((key) => { const value = fee[key]; if (value) { newAcc[key] = new BigNumber(acc[key] ?? 0).plus(value).toFixed(); } }); return newAcc; }, {}); };