Skip to content

Commit ed39bac

Browse files
committed
add slippage tolerance logic to quote param
1 parent 16a5492 commit ed39bac

File tree

5 files changed

+34
-14
lines changed

5 files changed

+34
-14
lines changed

src/components/transactions/Swap/details/RepayWithCollateralDetails.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,12 @@ export const RepayWithCollateralDetails = ({
5959
const displayAmountAfterRepayInUsd = debtAmountAfterRepay.multipliedBy(
6060
state.sourceReserve.reserve.priceInUSD
6161
);
62-
const collateralAmountAfterRepay = tokenToRepayWithBalance
62+
const rawCollateralAmountAfterRepay = tokenToRepayWithBalance
6363
? valueToBigNumber(tokenToRepayWithBalance).minus(state.sellAmountFormatted ?? '0')
6464
: valueToBigNumber('0');
65+
const collateralAmountAfterRepay = rawCollateralAmountAfterRepay.isNegative()
66+
? valueToBigNumber('0')
67+
: rawCollateralAmountAfterRepay;
6568
const collateralAmountAfterRepayUSD = collateralAmountAfterRepay.multipliedBy(
6669
state.destinationReserve.reserve.priceInUSD
6770
);

src/components/transactions/Swap/helpers/cow/orders.helpers.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
QuoteAndPost,
1212
SellTokenSource,
1313
SigningScheme,
14+
SlippageToleranceRequest,
1415
SupportedChainId,
1516
WRAPPED_NATIVE_CURRENCIES,
1617
} from '@cowprotocol/cow-sdk';
@@ -494,6 +495,32 @@ export const getPermitHook = async ({
494495
};
495496
};
496497

498+
// This function is used to get the slippage suggestion for a token pair on the respective chain based on the pair volatility.
499+
export const getSlippageSuggestion = async (request: SlippageToleranceRequest) => {
500+
const { sellToken, buyToken } = request;
501+
502+
try {
503+
if (request.chainId && sellToken && buyToken) {
504+
const chainSlug = request.chainId; // e.g., 42161 for Arbitrum
505+
const sell = sellToken.toLowerCase();
506+
const buy = buyToken.toLowerCase();
507+
const url = `https://bff.cow.fi/${chainSlug}/markets/${sell}-${buy}/slippageTolerance`;
508+
509+
const res = await fetch(url);
510+
511+
if (res.ok) {
512+
const result = await res.json();
513+
// The endpoint returns { slippageBps: number }
514+
// This is expected by the CoW SDK within the Slippage logic.
515+
return result;
516+
}
517+
}
518+
} catch (e) {
519+
console.error('Error fetching slippage suggestion:', e);
520+
return undefined;
521+
}
522+
};
523+
497524
export const addOrderTypeToAppData = (
498525
orderType: OrderType,
499526
appData?: AppDataParams

src/components/transactions/Swap/helpers/cow/rates.helpers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { isNativeToken } from '../../helpers/cow';
1313
import { CowProtocolRatesType, ProviderRatesParams, SwapProvider } from '../../types';
1414
import { getAppDataForQuote } from './adapters.helpers';
1515
import { getCowTradingSdkByChainIdAndAppCode } from './env.helpers';
16-
import { priceQualityToUse } from './orders.helpers';
16+
import { getSlippageSuggestion, priceQualityToUse } from './orders.helpers';
1717

1818
export const getTokenUsdPrice = async (
1919
chainId: number,
@@ -119,6 +119,7 @@ export async function getCowProtocolSellRates({
119119
destToken,
120120
destDecimals,
121121
}),
122+
getSlippageSuggestion,
122123
}
123124
)
124125
.catch((cowError) => {

src/components/transactions/Swap/hooks/useSwapQuote.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -392,22 +392,11 @@ const useMultiProviderSwapQuoteQuery = ({
392392
state.user
393393
),
394394
enabled: (() => {
395-
console.log('state.side', state.side);
396-
console.log('state.debouncedInputAmount', state.debouncedInputAmount);
397-
console.log('state.debouncedOutputAmount', state.debouncedOutputAmount);
398-
399-
console.log(
400-
'limit',
401-
(state.orderType === OrderType.LIMIT && !state.swapRate) ||
402-
state.orderType === OrderType.MARKET
403-
);
404-
405395
// Allow fetch when user has entered a positive amount, even if normalization rounded to '0'
406396
const hasPositiveUserAmount =
407397
state.side === 'sell'
408398
? Number(state.debouncedInputAmount || '0') > 0
409399
: Number(state.debouncedOutputAmount || '0') > 0;
410-
console.log('hasPositiveUserAmount', hasPositiveUserAmount);
411400

412401
// Basic pre-blockers to avoid provider requests
413402
const isSameTokenPair =

src/components/transactions/Swap/modals/request/RepayWithCollateralModalContent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export const RepayWithCollateralModalContent = ({
8989
interestMode,
9090
resultScreenTokensFromTitle: 'Repay',
9191
resultScreenTokensToTitle: 'With',
92-
resultScreenTitleItems: 'repay',
92+
resultScreenTitleItems: ' and repaid',
9393
customReceivedTitle: 'Repaid',
9494

9595
// Note: Repay With Collateral order is inverted

0 commit comments

Comments
 (0)