Skip to content

Commit

Permalink
fix: inexplicit small amount message when repay (#1679)
Browse files Browse the repository at this point in the history
* fix: inexplicit small amount message when repay

* chore: merged with main branch

* chore: changed updated small amount message
  • Loading branch information
mishannn authored Aug 8, 2023
1 parent 004c1cf commit f17f766
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
23 changes: 22 additions & 1 deletion src/hooks/paraswap/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,34 @@ export const getParaswap = (chainId: ChainId) => {
throw new Error('chain not supported');
};

export const MESSAGE_MAP: { [key: string]: string } = {
const MESSAGE_MAP: { [key: string]: string } = {
ESTIMATED_LOSS_GREATER_THAN_MAX_IMPACT:
'Price impact too high. Please try a different amount or asset pair.',
// not sure why this error-code is not upper-cased
'No routes found with enough liquidity': 'No routes found with enough liquidity.',
};

const MESSAGE_REGEX_MAP: Array<{ regex: RegExp; message: string }> = [
{
regex: /^Amount \d+ is too small to proceed$/,
message: 'Amount is too small. Please try larger amount.',
},
];

/**
* Converts Paraswap error message to message for displaying in interface
* @param message Paraswap error message
* @returns Message for displaying in interface
*/
export function convertParaswapErrorMessage(message: string): string {
if (message in MESSAGE_MAP) {
return MESSAGE_MAP[message];
}

const newMessage = MESSAGE_REGEX_MAP.find((mapping) => mapping.regex.test(message))?.message;
return newMessage || 'There was an issue fetching data from Paraswap';
}

/**
* Uses the Paraswap SDK to build the transaction parameters for a 'Sell', or 'Exact In' swap.
* @param {OptimalRate} route
Expand Down
11 changes: 5 additions & 6 deletions src/hooks/paraswap/useCollateralRepaySwap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { OptimalRate } from '@paraswap/sdk';
import { useCallback, useEffect, useMemo, useState } from 'react';

import {
convertParaswapErrorMessage,
fetchExactInRate,
fetchExactInTxParams,
fetchExactOutRate,
fetchExactOutTxParams,
MESSAGE_MAP,
SwapData,
SwapTransactionParams,
SwapVariant,
Expand Down Expand Up @@ -84,10 +84,9 @@ export const useCollateralRepaySwap = ({
return fetchExactInRate(swapInData, swapOutData, chainId, userAddress);
}, [chainId, swapInData, swapOutData, userAddress]);

const exactOutRate = useCallback(
() => fetchExactOutRate(swapInData, swapOutData, chainId, userAddress, max),
[chainId, max, swapInData, swapOutData, userAddress]
);
const exactOutRate = useCallback(() => {
return fetchExactOutRate(swapInData, swapOutData, chainId, userAddress, max);
}, [chainId, max, swapInData, swapOutData, userAddress]);

useEffect(() => {
if (skip) return;
Expand Down Expand Up @@ -141,7 +140,7 @@ export const useCollateralRepaySwap = ({
setOutputAmountUSD(route.destUSD);
} catch (e) {
console.error(e);
const message = MESSAGE_MAP[e.message] || 'There was an issue fetching data from Paraswap';
const message = convertParaswapErrorMessage(e.message);
setError(message);
} finally {
setLoading(false);
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/paraswap/useCollateralSwap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { OptimalRate } from '@paraswap/sdk';
import { useCallback, useEffect, useMemo, useState } from 'react';

import {
convertParaswapErrorMessage,
fetchExactInRate,
fetchExactInTxParams,
MESSAGE_MAP,
SwapData,
SwapTransactionParams,
UseSwapProps,
Expand Down Expand Up @@ -113,7 +113,7 @@ export const useCollateralSwap = ({
setOutputAmountUSD(route.destUSD);
} catch (e) {
console.error(e);
const message = MESSAGE_MAP[e.message] || 'There was an issue fetching data from Paraswap';
const message = convertParaswapErrorMessage(e.message);
setError(message);
} finally {
setLoading(false);
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/paraswap/useDebtSwitch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { OptimalRate } from '@paraswap/sdk';
import { useCallback, useEffect, useMemo, useState } from 'react';

import {
convertParaswapErrorMessage,
fetchExactOutRate,
fetchExactOutTxParams,
MESSAGE_MAP,
SwapData,
SwapTransactionParams,
UseSwapProps,
Expand Down Expand Up @@ -116,7 +116,7 @@ export const useDebtSwitch = ({
setOutputAmountUSD(route.destUSD);
} catch (e) {
console.error(e);
const message = MESSAGE_MAP[e.message] || 'There was an issue fetching data from Paraswap';
const message = convertParaswapErrorMessage(e.message);
setError(message);
} finally {
setLoading(false);
Expand Down

1 comment on commit f17f766

@github-actions
Copy link

Choose a reason for hiding this comment

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

This commit was deployed on ipfs

Please sign in to comment.