Skip to content

Commit

Permalink
feat: display same source and destination token warning on widget
Browse files Browse the repository at this point in the history
  • Loading branch information
mikasackermn committed Nov 13, 2024
1 parent 1ec9b81 commit 346c93e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 18 deletions.
18 changes: 12 additions & 6 deletions widget/embedded/src/components/NoResult/NoResult.helpers.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import type { Info } from './NoResult.types';
import type {
NoResultError,
QuoteRequestFailed,
SameTokensWarning,
} from '../../types';

import { i18n } from '@lingui/core';

import { errorMessages } from '../../constants/errors';
import {
type NoResultError,
QuoteErrorType,
type QuoteRequestFailed,
} from '../../types';
import { QuoteErrorType, QuoteWarningType } from '../../types';

const SMALL_NO_ROUTE__ICON_SIZE = 24;
const LARGE_NO_ROUTE_ICON_SIZE = 60;

export function makeInfo(
error: NoResultError | QuoteRequestFailed | null,
error: NoResultError | QuoteRequestFailed | SameTokensWarning | null,
disabledLiquiditySources: string[],
toggleAllLiquiditySources: (shouldReset: boolean) => void,
refetchQuote: () => void
Expand All @@ -30,6 +31,11 @@ export function makeInfo(
},
description: '',
};
} else if (error?.type === QuoteWarningType.SAME_TOKENS) {
return {
alert: null,
description: '',
};
} else if (disabledLiquiditySources.length) {
return {
alert: {
Expand Down
8 changes: 6 additions & 2 deletions widget/embedded/src/components/NoResult/NoResult.types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import type { NoResultError, QuoteRequestFailed } from '../../types';
import type {
NoResultError,
QuoteRequestFailed,
SameTokensWarning,
} from '../../types';

export interface PropTypes {
fetch: () => void;
error: NoResultError | QuoteRequestFailed | null;
error: NoResultError | QuoteRequestFailed | SameTokensWarning | null;
size?: 'small' | 'large';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ export function makeAlerts(
alertInfo.title = i18n.t('Caution, your slippage is high.');
alertInfo.action = 'change-settings';
}
if (warning.type === QuoteWarningType.SAME_TOKENS) {
alertInfo.title = i18n.t(
'You cannot use the same token for From and To.'
);
}

return alertInfo;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,18 @@ export function QuoteWarningsAndErrors(props: PropTypes) {
onClose: onCloseWarningModal,
onConfirm: onConfirmWarningModal,
};
const isNoResultError = error?.type === QuoteErrorType.NO_RESULT;
const isRequestFailedError = error?.type === QuoteErrorType.REQUEST_FAILED;
const isSameTokensWarning = warning?.type === QuoteWarningType.SAME_TOKENS;

const showNoResultMessage =
error?.type === QuoteErrorType.NO_RESULT ||
error?.type === QuoteErrorType.REQUEST_FAILED;
isNoResultError || isRequestFailedError || isSameTokensWarning;

const displayedError = isSameTokensWarning
? warning
: isNoResultError || isRequestFailedError
? error
: null;

const alertInfo = makeAlerts(
warning,
Expand All @@ -53,8 +61,9 @@ export function QuoteWarningsAndErrors(props: PropTypes) {

return (
<>
{showNoResultMessage && <NoResult error={error} fetch={refetchQuote} />}

{showNoResultMessage && (
<NoResult error={displayedError} fetch={refetchQuote} />
)}
{showAlerts && (
<Alerts>
<Alert
Expand Down
13 changes: 8 additions & 5 deletions widget/embedded/src/hooks/useSwapInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useCallback, useEffect, useRef, useState } from 'react';
import { useAppStore } from '../store/AppStore';
import { useQuoteStore } from '../store/quote';
import { useWalletsStore } from '../store/wallets';
import { QuoteErrorType } from '../types';
import { QuoteErrorType, QuoteWarningType } from '../types';
import { debounce } from '../utils/common';
import { isPositiveNumber } from '../utils/numbers';
import {
Expand Down Expand Up @@ -86,9 +86,7 @@ export function useSwapInput({
const userSlippage = customSlippage ?? slippage;
const tokensValueInvalid = !fromToken || !toToken;
const shouldSkipRequest =
tokensValueInvalid ||
areTokensEqual(fromToken, toToken) ||
!isPositiveNumber(inputAmount);
tokensValueInvalid || !isPositiveNumber(inputAmount);

const resetState = (loading: boolean) => {
setLoading(loading);
Expand All @@ -107,7 +105,12 @@ export function useSwapInput({
affiliatePercent,
affiliateWallets,
} = params;

if (areTokensEqual(fromToken, toToken)) {
updateQuotePartialState('warning', {
type: QuoteWarningType.SAME_TOKENS,
});
return;
}
if (!loading) {
resetState(true);
}
Expand Down
8 changes: 7 additions & 1 deletion widget/embedded/src/types/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export enum QuoteWarningType {
UNKNOWN_PRICE,
INSUFFICIENT_SLIPPAGE,
HIGH_SLIPPAGE,
SAME_TOKENS,
}

export enum QuoteUpdateType {
Expand Down Expand Up @@ -100,11 +101,16 @@ export type UnknownPriceWarning = {
type: QuoteWarningType.UNKNOWN_PRICE;
};

export type SameTokensWarning = {
type: QuoteWarningType.SAME_TOKENS;
};

export type QuoteWarning =
| HighValueLossWarning
| InsufficientSlippageWarning
| HighSlippageWarning
| UnknownPriceWarning;
| UnknownPriceWarning
| SameTokensWarning;

export type ConfirmSwapWarnings = {
quote: QuoteWarning | null;
Expand Down

0 comments on commit 346c93e

Please sign in to comment.