Skip to content
9 changes: 6 additions & 3 deletions src/components/transactions/Repay/CollateralRepayActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useWeb3Context } from 'src/libs/hooks/useWeb3Context';
import { TxActionsWrapper } from '../TxActionsWrapper';
import { OptimalRate } from 'paraswap-core';
import { getRepayCallData } from 'src/hooks/useSwap';
import { normalize } from '@aave/math-utils';

export interface RepayActionProps extends BoxProps {
rateMode: InterestRate;
Expand All @@ -24,6 +25,7 @@ export interface RepayActionProps extends BoxProps {
repayAllDebt: boolean;
useFlashLoan: boolean;
blocked: boolean;
maxSlippage: number;
}

export const CollateralRepayActions = ({
Expand All @@ -39,6 +41,7 @@ export const CollateralRepayActions = ({
repayAllDebt,
useFlashLoan,
blocked,
maxSlippage,
...props
}: RepayActionProps) => {
const { lendingPool } = useTxBuilderContext();
Expand All @@ -48,22 +51,22 @@ export const CollateralRepayActions = ({
const { approval, action, requiresApproval, loadingTxns, approvalTxState, mainTxState } =
useTransactionHandler({
handleGetTxns: async () => {
const { swapCallData, augustus } = await getRepayCallData({
const { swapCallData, augustus, srcAmountWithSlippage } = await getRepayCallData({
srcToken: fromAssetData.underlyingAsset,
srcDecimals: fromAssetData.decimals,
destToken: poolReserve.underlyingAsset,
destDecimals: poolReserve.decimals,
user: currentAccount,
route: priceRoute as OptimalRate,
chainId: currentNetworkConfig.underlyingChainId || chainId,
repayWithAmount,
maxSlippage,
});
return lendingPool.paraswapRepayWithCollateral({
user: currentAccount,
fromAsset: fromAssetData.underlyingAsset,
fromAToken: fromAssetData.aTokenAddress,
assetToRepay: poolReserve.underlyingAsset,
repayWithAmount,
repayWithAmount: normalize(srcAmountWithSlippage, fromAssetData.decimals),
repayAmount,
repayAllDebt,
rateMode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function CollateralRepayModalContent({
) as ComputedUserReserveData;

const [_amount, setAmount] = useState('');
const [maxSlippage, setMaxSlippage] = useState('0.1');
const [maxSlippage, setMaxSlippage] = useState('0.5');

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

Expand All @@ -90,7 +90,6 @@ export function CollateralRepayModalContent({
swapOut: { ...poolReserve, amount: amountRef.current },
max: isMaxSelected,
skip: mainTxState.loading,
maxSlippage,
});

// Calculations to get the max repayable debt depending on the balance and value of the
Expand Down Expand Up @@ -267,6 +266,7 @@ export function CollateralRepayModalContent({
rateMode={debtType}
priceRoute={priceRoute}
blocked={blockingError !== undefined}
maxSlippage={Number(maxSlippage)}
/>
</>
);
Expand Down
34 changes: 13 additions & 21 deletions src/hooks/useSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { useCallback, useEffect, useState } from 'react';
import { ComputedReserveData } from './app-data-provider/useAppDataProvider';
import { ChainId } from '@aave/contract-helpers';
import { BigNumberZeroDecimal, normalize, normalizeBN, valueToBigNumber } from '@aave/math-utils';
import BigNumber from 'bignumber.js';

const ParaSwap = (chainId: number) => {
const fetcher = constructFetchFetcher(fetch); // alternatively constructFetchFetcher
Expand Down Expand Up @@ -45,23 +44,13 @@ type UseSwapProps = {
userId?: string;
chainId: ChainId;
skip?: boolean;
maxSlippage?: string;
};

const MESSAGE_MAP = {
ESTIMATED_LOSS_GREATER_THAN_MAX_IMPACT: 'Price impact to high',
};

export const useSwap = ({
swapIn,
swapOut,
variant,
userId,
max,
chainId,
skip,
maxSlippage,
}: UseSwapProps) => {
export const useSwap = ({ swapIn, swapOut, variant, userId, max, chainId, skip }: UseSwapProps) => {
const paraSwap = getParaswap(chainId);
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
Expand All @@ -76,11 +65,6 @@ export const useSwap = ({
if (variant === 'exactIn' && max && swapIn.supplyAPY !== '0') {
_amount = _amount.plus(_amount.multipliedBy(swapIn.supplyAPY).dividedBy(360 * 24));
}
if (variant === 'exactOut') {
_amount = new BigNumber(_amount).multipliedBy(
new BigNumber(100).plus(maxSlippage || 0).dividedBy(100)
);
}
if (variant === 'exactOut' && max) {
// variableBorrowAPY in most cases should be higher than stableRate so while this is slightly inaccurate it should be enough
_amount = _amount.plus(_amount.multipliedBy(swapIn.variableBorrowAPY).dividedBy(360 * 24));
Expand All @@ -103,9 +87,12 @@ export const useSwap = ({
side: variant === 'exactIn' ? SwapSide.SELL : SwapSide.BUY,
options: {
partner: 'aave',
excludeDEXS:
'ParaSwapPool,ParaSwapPool2,ParaSwapPool3,ParaSwapPool4,ParaSwapPool5,ParaSwapPool6,ParaSwapPool7,ParaSwapPool8,ParaSwapPool9,ParaSwapPool10',
...(max
? {
excludeDEXS: 'Balancer',
excludeDEXS:
'Balancer,ParaSwapPool,ParaSwapPool2,ParaSwapPool3,ParaSwapPool4,ParaSwapPool5,ParaSwapPool6,ParaSwapPool7,ParaSwapPool8,ParaSwapPool9,ParaSwapPool10',
excludeContractMethods: [excludedMethod],
}
: {}),
Expand All @@ -131,7 +118,6 @@ export const useSwap = ({
variant,
max,
chainId,
maxSlippage,
]);

// updates the route on input change
Expand Down Expand Up @@ -199,7 +185,7 @@ type GetSwapAndRepayCallDataProps = {
route: OptimalRate;
max?: boolean;
chainId: ChainId;
repayWithAmount: string;
maxSlippage: number;
};

export const getSwapCallData = async ({
Expand Down Expand Up @@ -251,15 +237,20 @@ export const getRepayCallData = async ({
user,
route,
chainId,
maxSlippage,
}: GetSwapAndRepayCallDataProps) => {
const paraSwap = getParaswap(chainId);
const srcAmountWithSlippage = new BigNumberZeroDecimal(route.srcAmount)
.multipliedBy(100 + maxSlippage)
.dividedBy(100)
.toFixed(0);

try {
const params = await paraSwap.buildTx(
{
srcToken,
destToken,
srcAmount: route.srcAmount,
srcAmount: srcAmountWithSlippage,
destAmount: route.destAmount,
priceRoute: route,
userAddress: user,
Expand All @@ -273,6 +264,7 @@ export const getRepayCallData = async ({
return {
swapCallData: (params as TransactionParams).data,
augustus: (params as TransactionParams).to,
srcAmountWithSlippage,
};
} catch (e) {
console.log(e);
Expand Down