Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(swap): validate total liquidity for non dca quotes #1123

Closed
wants to merge 2 commits into from
Closed
Changes from all 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
30 changes: 19 additions & 11 deletions packages/swap/src/routes/v2/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,29 +234,37 @@ export const generateQuotes = async ({
const boostedQuote = getFulfilledResult(boostedQuoteResult, null);
const dcaBoostedQuote = getFulfilledResult(dcaBoostedQuoteResult, null) as DCABoostQuote | null;

if (dcaQuoteParams && dcaQuote) {
// The received quotes are for a single chunk, we need to extrapolate them to the full amount
adjustDcaQuote({ dcaQuoteParams, dcaQuote, originalDepositAmount: amount });
if (dcaBoostedQuote) {
adjustDcaQuote({ dcaQuoteParams, dcaQuote: dcaBoostedQuote, originalDepositAmount: amount });
}

// Check liquidity for DCA
const validateLiquidity = async (egressAmount: string, intermediateAmount?: string) => {
if (srcAsset === 'Usdc' || destAsset === 'Usdc') {
const totalLiquidity = await getTotalLiquidity(srcAsset, destAsset);
if (totalLiquidity < BigInt(dcaQuote.egressAmount)) {
if (totalLiquidity < BigInt(egressAmount)) {
throw ServiceError.badRequest(`Insufficient liquidity for the requested amount`);
}
} else {
const totalLiquidityLeg1 = await getTotalLiquidity(srcAsset, 'Usdc');
const totalLiquidityLeg2 = await getTotalLiquidity('Usdc', destAsset);
if (
totalLiquidityLeg1 < BigInt(dcaQuote.intermediateAmount!) ||
totalLiquidityLeg2 < BigInt(dcaQuote.egressAmount)
totalLiquidityLeg1 < BigInt(intermediateAmount!) ||
totalLiquidityLeg2 < BigInt(egressAmount)
) {
throw ServiceError.badRequest(`Insufficient liquidity for the requested amount`);
}
}
};

if (dcaQuoteParams && dcaQuote) {
// The received quotes are for a single chunk, we need to extrapolate them to the full amount
adjustDcaQuote({ dcaQuoteParams, dcaQuote, originalDepositAmount: amount });
if (dcaBoostedQuote) {
adjustDcaQuote({ dcaQuoteParams, dcaQuote: dcaBoostedQuote, originalDepositAmount: amount });
}

// Check liquidity for DCA
await validateLiquidity(dcaQuote.egressAmount, dcaQuote.intermediateAmount);
} else if (quote) {
await validateLiquidity(quote.egressAmount, quote.intermediateAmount);
} else {
throw ServiceError.internalError('could not generate quotes');
Comment on lines +264 to +267
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we should always do this for all of the quotes, no?

Suggested change
} else if (quote) {
await validateLiquidity(quote.egressAmount, quote.intermediateAmount);
} else {
throw ServiceError.internalError('could not generate quotes');
}
if (quote) {
await validateLiquidity(quote.egressAmount, quote.intermediateAmount);
}

}

if (quote && boostedQuote && estimatedBoostFeeBps && maxBoostFeeBps) {
Expand Down
Loading