Skip to content

Commit

Permalink
Merge pull request #28 from multiversx/development
Browse files Browse the repository at this point in the history
Improve RouteError Handling
  • Loading branch information
EmanuelMiron authored Oct 8, 2024
2 parents d95dc52 + 1f31321 commit 46ab682
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 43 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [[1.8.3](https://github.com/multiversx/mx-sdk-dapp-swap/pull/28)] - 2024-10-08

- [Improve RouteError Handling](https://github.com/multiversx/mx-sdk-dapp-swap/pull/27)

## [[1.8.2](https://github.com/multiversx/mx-sdk-dapp-swap/pull/26)] - 2024-09-12

- [Fixes EGLD <-> WEGLD missing transactions](https://github.com/multiversx/mx-sdk-dapp-swap/pull/26)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiversx/sdk-dapp-swap",
"version": "1.8.2",
"version": "1.8.3",
"description": "A library to hold the main logic for swapping between tokens on the MultiversX blockchain",
"author": "MultiversX",
"license": "GPL-3.0-or-later",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const SwapAuthorizationProvider = ({
defaultOptions: {
watchQuery: {
fetchPolicy: 'no-cache',
errorPolicy: 'ignore'
errorPolicy: 'all'
},
query: {
fetchPolicy: 'no-cache',
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/useQueryWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ export const useQueryWrapper = <TData>({
});

const startPollingCallback = useCallback(() => {
if (isPageVisible && isPollingEnabled) {
if (isPageVisible && isPollingEnabled && !error) {
startPolling(POLLING_INTERVAL);
}
}, [isPageVisible, isPollingEnabled, startPolling]);
}, [isPageVisible, isPollingEnabled, startPolling, error]);

// mount and unmount
useEffect(() => {
Expand All @@ -61,7 +61,7 @@ export const useQueryWrapper = <TData>({
}, [refetchTrigger, isRefetchEnabled, refetch, startPollingCallback]);

const isLoading = data == null && loading;
const isError = Boolean(error && data == null);
const isError = Boolean(error || data == null);
const isRefetching = loading;

return {
Expand Down
69 changes: 33 additions & 36 deletions src/hooks/useSwapRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,35 @@ export const useSwapRoute = ({
[variables, wrappedEgld]
);

const query = useMemo(() => {
if (swapActionType === SwapActionTypesEnum.wrap) return wrapEgldQuery;
if (swapActionType === SwapActionTypesEnum.unwrap) return unwrapEgldQuery;

return isAuthenticated ? swapQuery : swapWithoutTransactionsQuery;
}, [isAuthenticated, swapActionType]);

const skip = useMemo(() => {
if (!variables) return true;

const { amountIn, amountOut } = variables;
const hasAmount = Boolean(amountIn ?? amountOut);

if (!hasAmount) return true;

return false;
}, [variables]);

const { data, error, refetch, isRefetching, isLoading, isError } =
useQueryWrapper<SwapRouteQueryResponseType | WrappingQueryResponseType>({
query,
queryOptions: {
skip,
client,
variables
},
isPollingEnabled
});

const handleOnCompleted = (
data?: SwapRouteQueryResponseType | WrappingQueryResponseType
) => {
Expand Down Expand Up @@ -123,47 +152,15 @@ export const useSwapRoute = ({
setSwapRouteError(undefined);
break;
default:
const { swap, errors } = data as SwapRouteQueryResponseType;

const error = errors
? translateSwapError(errors[0].message)
: undefined;
const { swap } = data as SwapRouteQueryResponseType;

setSwapRoute(swap);
setSwapRouteError(error);

const translatedError = translateSwapError(error?.message);
setSwapRouteError(translatedError);
}
};

const query = useMemo(() => {
if (swapActionType === SwapActionTypesEnum.wrap) return wrapEgldQuery;
if (swapActionType === SwapActionTypesEnum.unwrap) return unwrapEgldQuery;

return isAuthenticated ? swapQuery : swapWithoutTransactionsQuery;
}, [isAuthenticated, swapActionType]);

const skip = useMemo(() => {
if (!variables) return true;

const { amountIn, amountOut } = variables;
const hasAmount = Boolean(amountIn ?? amountOut);

if (!hasAmount) return true;

return false;
}, [variables]);

const { data, refetch, isRefetching, isLoading, isError } = useQueryWrapper<
SwapRouteQueryResponseType | WrappingQueryResponseType
>({
query,
queryOptions: {
skip,
client,
variables
},
isPollingEnabled
});

const getSwapRoute = ({
amountIn,
amountOut,
Expand Down
1 change: 0 additions & 1 deletion src/queries/swap/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,4 @@ export const swapWithoutTransactionsQuery = gql`

export interface SwapRouteQueryResponseType {
swap: SwapRouteType;
errors?: GraphqlErrorsResponseType[];
}
4 changes: 3 additions & 1 deletion src/utils/translateSwapError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ const errorMap: Record<string, string> = {
'Smart Swap not possible because you would lose more than 5%.'
};

export const translateSwapError = (serviceError: string) => {
export const translateSwapError = (serviceError?: string) => {
if (!serviceError) return;

const defaultTranslation = 'No swap route found.';
const foundErrorKey = Object.keys(errorMap).find((key) =>
serviceError.includes(key)
Expand Down

0 comments on commit 46ab682

Please sign in to comment.