Skip to content

Commit 948f22c

Browse files
fix: fix repay with collateral (#770)
* fix: repay with collateral * fix: repay with collateral * fix: remove nused variable * fix: remove unused import * bump: utils version * feat: set default repay with collateral slippage to 0.5% * fix: exclude pools Co-authored-by: Andrew Schmidt <andrew@aave.com>
1 parent 0491ec3 commit 948f22c

File tree

3 files changed

+21
-26
lines changed

3 files changed

+21
-26
lines changed

src/components/transactions/Repay/CollateralRepayActions.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { useWeb3Context } from 'src/libs/hooks/useWeb3Context';
1010
import { TxActionsWrapper } from '../TxActionsWrapper';
1111
import { OptimalRate } from 'paraswap-core';
1212
import { getRepayCallData } from 'src/hooks/useSwap';
13+
import { normalize } from '@aave/math-utils';
1314

1415
export interface RepayActionProps extends BoxProps {
1516
rateMode: InterestRate;
@@ -24,6 +25,7 @@ export interface RepayActionProps extends BoxProps {
2425
repayAllDebt: boolean;
2526
useFlashLoan: boolean;
2627
blocked: boolean;
28+
maxSlippage: number;
2729
}
2830

2931
export const CollateralRepayActions = ({
@@ -39,6 +41,7 @@ export const CollateralRepayActions = ({
3941
repayAllDebt,
4042
useFlashLoan,
4143
blocked,
44+
maxSlippage,
4245
...props
4346
}: RepayActionProps) => {
4447
const { lendingPool } = useTxBuilderContext();
@@ -48,22 +51,22 @@ export const CollateralRepayActions = ({
4851
const { approval, action, requiresApproval, loadingTxns, approvalTxState, mainTxState } =
4952
useTransactionHandler({
5053
handleGetTxns: async () => {
51-
const { swapCallData, augustus } = await getRepayCallData({
54+
const { swapCallData, augustus, srcAmountWithSlippage } = await getRepayCallData({
5255
srcToken: fromAssetData.underlyingAsset,
5356
srcDecimals: fromAssetData.decimals,
5457
destToken: poolReserve.underlyingAsset,
5558
destDecimals: poolReserve.decimals,
5659
user: currentAccount,
5760
route: priceRoute as OptimalRate,
5861
chainId: currentNetworkConfig.underlyingChainId || chainId,
59-
repayWithAmount,
62+
maxSlippage,
6063
});
6164
return lendingPool.paraswapRepayWithCollateral({
6265
user: currentAccount,
6366
fromAsset: fromAssetData.underlyingAsset,
6467
fromAToken: fromAssetData.aTokenAddress,
6568
assetToRepay: poolReserve.underlyingAsset,
66-
repayWithAmount,
69+
repayWithAmount: normalize(srcAmountWithSlippage, fromAssetData.decimals),
6770
repayAmount,
6871
repayAllDebt,
6972
rateMode,

src/components/transactions/Repay/CollateralRepayModalContent.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export function CollateralRepayModalContent({
6868
) as ComputedUserReserveData;
6969

7070
const [_amount, setAmount] = useState('');
71-
const [maxSlippage, setMaxSlippage] = useState('0.1');
71+
const [maxSlippage, setMaxSlippage] = useState('0.5');
7272

7373
const amountRef = useRef<string>('');
7474

@@ -90,7 +90,6 @@ export function CollateralRepayModalContent({
9090
swapOut: { ...poolReserve, amount: amountRef.current },
9191
max: isMaxSelected,
9292
skip: mainTxState.loading,
93-
maxSlippage,
9493
});
9594

9695
// Calculations to get the max repayable debt depending on the balance and value of the
@@ -267,6 +266,7 @@ export function CollateralRepayModalContent({
267266
rateMode={debtType}
268267
priceRoute={priceRoute}
269268
blocked={blockingError !== undefined}
269+
maxSlippage={Number(maxSlippage)}
270270
/>
271271
</>
272272
);

src/hooks/useSwap.ts

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { useCallback, useEffect, useState } from 'react';
1010
import { ComputedReserveData } from './app-data-provider/useAppDataProvider';
1111
import { ChainId } from '@aave/contract-helpers';
1212
import { BigNumberZeroDecimal, normalize, normalizeBN, valueToBigNumber } from '@aave/math-utils';
13-
import BigNumber from 'bignumber.js';
1413

1514
const ParaSwap = (chainId: number) => {
1615
const fetcher = constructFetchFetcher(fetch); // alternatively constructFetchFetcher
@@ -45,23 +44,13 @@ type UseSwapProps = {
4544
userId?: string;
4645
chainId: ChainId;
4746
skip?: boolean;
48-
maxSlippage?: string;
4947
};
5048

5149
const MESSAGE_MAP = {
5250
ESTIMATED_LOSS_GREATER_THAN_MAX_IMPACT: 'Price impact to high',
5351
};
5452

55-
export const useSwap = ({
56-
swapIn,
57-
swapOut,
58-
variant,
59-
userId,
60-
max,
61-
chainId,
62-
skip,
63-
maxSlippage,
64-
}: UseSwapProps) => {
53+
export const useSwap = ({ swapIn, swapOut, variant, userId, max, chainId, skip }: UseSwapProps) => {
6554
const paraSwap = getParaswap(chainId);
6655
const [loading, setLoading] = useState(false);
6756
const [error, setError] = useState('');
@@ -76,11 +65,6 @@ export const useSwap = ({
7665
if (variant === 'exactIn' && max && swapIn.supplyAPY !== '0') {
7766
_amount = _amount.plus(_amount.multipliedBy(swapIn.supplyAPY).dividedBy(360 * 24));
7867
}
79-
if (variant === 'exactOut') {
80-
_amount = new BigNumber(_amount).multipliedBy(
81-
new BigNumber(100).plus(maxSlippage || 0).dividedBy(100)
82-
);
83-
}
8468
if (variant === 'exactOut' && max) {
8569
// variableBorrowAPY in most cases should be higher than stableRate so while this is slightly inaccurate it should be enough
8670
_amount = _amount.plus(_amount.multipliedBy(swapIn.variableBorrowAPY).dividedBy(360 * 24));
@@ -103,9 +87,12 @@ export const useSwap = ({
10387
side: variant === 'exactIn' ? SwapSide.SELL : SwapSide.BUY,
10488
options: {
10589
partner: 'aave',
90+
excludeDEXS:
91+
'ParaSwapPool,ParaSwapPool2,ParaSwapPool3,ParaSwapPool4,ParaSwapPool5,ParaSwapPool6,ParaSwapPool7,ParaSwapPool8,ParaSwapPool9,ParaSwapPool10',
10692
...(max
10793
? {
108-
excludeDEXS: 'Balancer',
94+
excludeDEXS:
95+
'Balancer,ParaSwapPool,ParaSwapPool2,ParaSwapPool3,ParaSwapPool4,ParaSwapPool5,ParaSwapPool6,ParaSwapPool7,ParaSwapPool8,ParaSwapPool9,ParaSwapPool10',
10996
excludeContractMethods: [excludedMethod],
11097
}
11198
: {}),
@@ -131,7 +118,6 @@ export const useSwap = ({
131118
variant,
132119
max,
133120
chainId,
134-
maxSlippage,
135121
]);
136122

137123
// updates the route on input change
@@ -199,7 +185,7 @@ type GetSwapAndRepayCallDataProps = {
199185
route: OptimalRate;
200186
max?: boolean;
201187
chainId: ChainId;
202-
repayWithAmount: string;
188+
maxSlippage: number;
203189
};
204190

205191
export const getSwapCallData = async ({
@@ -251,15 +237,20 @@ export const getRepayCallData = async ({
251237
user,
252238
route,
253239
chainId,
240+
maxSlippage,
254241
}: GetSwapAndRepayCallDataProps) => {
255242
const paraSwap = getParaswap(chainId);
243+
const srcAmountWithSlippage = new BigNumberZeroDecimal(route.srcAmount)
244+
.multipliedBy(100 + maxSlippage)
245+
.dividedBy(100)
246+
.toFixed(0);
256247

257248
try {
258249
const params = await paraSwap.buildTx(
259250
{
260251
srcToken,
261252
destToken,
262-
srcAmount: route.srcAmount,
253+
srcAmount: srcAmountWithSlippage,
263254
destAmount: route.destAmount,
264255
priceRoute: route,
265256
userAddress: user,
@@ -273,6 +264,7 @@ export const getRepayCallData = async ({
273264
return {
274265
swapCallData: (params as TransactionParams).data,
275266
augustus: (params as TransactionParams).to,
267+
srcAmountWithSlippage,
276268
};
277269
} catch (e) {
278270
console.log(e);

0 commit comments

Comments
 (0)