Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/swap/routing/exact-in.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Decimal from 'decimal.js';
import { formatUnits, type Hex } from 'viem';
import { Errors } from '../../domain/errors';
import { formatTokenBalance } from '../../domain/utils/format';
import { logger } from '../../domain/utils/logger';
import { divDecimals, mulDecimals } from '../../services/math';
import { MAYAN_MIN_USD_PER_LEG, selectMayanQuoteOutput } from '../../services/mayan';
Expand Down Expand Up @@ -221,7 +222,7 @@ const buildExactInBridge = async (input: {
} = feeSummary;
if (effectiveBridgedToDestination.lte(0)) {
throw Errors.insufficientBalance(
`Bridge fees (${totalFeeAmount.toString()}) exceed bridged COT (${bridgedCOT.toString()})`
`Bridge fees (${formatTokenBalance(totalFeeAmount.toFixed())}) exceed bridged amount (${formatTokenBalance(bridgedCOT.toFixed())})`
);
}

Expand Down
3 changes: 2 additions & 1 deletion src/swap/routing/fast-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { formatUnits, type Hex } from 'viem';
import type { ChainListType } from '../../domain';
import { ZERO_ADDRESS } from '../../domain/constants/addresses';
import { Errors } from '../../domain/errors';
import { formatTokenBalance } from '../../domain/utils/format';
import { logger } from '../../domain/utils/logger';
import { isNativeAddress } from '../../services/addresses';
import { divDecimals, mulDecimals } from '../../services/math';
Expand Down Expand Up @@ -612,7 +613,7 @@ export async function buildSameTokenBridgeRoute(
deliveredFromBridge = deliveredAmount;
if (deliveredFromBridge.lte(0)) {
throw Errors.insufficientBalance(
`Bridge fees (${totalFee.toString()}) exceed bridged amount (${bridgedToken.toString()})`
`Bridge fees (${formatTokenBalance(totalFee.toFixed())}) exceed bridged amount (${formatTokenBalance(bridgedToken.toFixed())})`
);
}
// The fast path participates in provider selection too, querying the server with the actual
Expand Down
12 changes: 9 additions & 3 deletions src/swap/routing/holdings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import Decimal from 'decimal.js';
import { type Hex, parseUnits } from 'viem';
import type { ChainListType } from '../../domain';
import { Errors } from '../../domain/errors';
import { formatTokenBalance } from '../../domain/utils/format';
import { logger } from '../../domain/utils/logger';
import { isNativeAddress } from '../../services/addresses';
import { divDecimals } from '../../services/math';
import { equalFold } from '../../services/strings';
import { filterMayanSourcesByChain } from '../algorithms/mayan-floor';
Expand Down Expand Up @@ -269,14 +271,18 @@ export function resolveExactInHoldings(
entry.chainID === source.chainId && equalFold(entry.tokenAddress, source.tokenAddress)
);
if (!balance || new Decimal(balance.amount).lte(0)) {
throw Errors.insufficientBalance('Requested source has no usable balance');
throw Errors.insufficientBalance(
`Requested source ${source.tokenAddress} on chain ${source.chainId} has no usable balance`
);
}

const availableRaw = parseUnits(balance.amount, balance.decimals);
const amountRaw = source.amountRaw ?? availableRaw;

if (amountRaw > availableRaw) {
throw Errors.insufficientBalance('Requested source amount exceeds available balance');
if (!isNativeAddress(source.tokenAddress) && amountRaw > availableRaw) {
throw Errors.insufficientBalance(
`Requested source (${balance.symbol}, ${source.chainId}) amount (${formatTokenBalance(divDecimals(amountRaw, balance.decimals).toFixed())}) exceeds available balance (${formatTokenBalance(divDecimals(availableRaw, balance.decimals).toFixed())})`
);
}

return amountRaw > 0n
Expand Down