Skip to content

Commit f8abe28

Browse files
fix: fix bugs
1 parent 1fbd025 commit f8abe28

11 files changed

Lines changed: 64 additions & 25 deletions

File tree

widget/embedded/src/components/InsufficientBalanceModal/InsufficientBalanceModal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export function InsufficientBalanceModal(props: PropTypes) {
7575
messages.length > 1 && index !== messages.length - 1;
7676

7777
return (
78-
<Fragment key={message.blockchain}>
78+
<Fragment key={message.blockchain + message.asset.symbol}>
7979
<InsufficientBalanceWarning
8080
warning={{
8181
title: warningItemReason[message.reason],

widget/embedded/src/containers/Inputs/SourceInput/MaxBalance/MaxBalance.styles.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@ export const Container = styled('div', {
3131
display: 'flex',
3232
justifyContent: 'center',
3333
alignItems: 'center',
34+
35+
'& .max-balance': {
36+
width: 'max-content',
37+
},
3438
});

widget/embedded/src/containers/Inputs/SourceInput/MaxBalance/MaxBalance.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ export function MaxBalance(props: PropTypes) {
1616
<Skeleton variant="text" size="large" width={105} />
1717
) : (
1818
<>
19-
<Typography variant="body" size="xsmall" color="neutral600">
19+
<Typography
20+
variant="body"
21+
size="xsmall"
22+
color="neutral600"
23+
className="max-balance">
2024
{t('Balance')}: {balance ?? '0.00'}
2125
</Typography>
2226
<Divider direction="horizontal" size={4} />

widget/embedded/src/hooks/useConfirmSwap/useConfirmSwap.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function useConfirmSwap() {
2020
warnings: null,
2121
});
2222

23-
const handleConfirmSwap = async () => {
23+
const handleConfirmSwap = async (callback?: () => void) => {
2424
if (!sourceWallet) {
2525
return;
2626
}
@@ -31,15 +31,15 @@ export function useConfirmSwap() {
3131
});
3232

3333
setConfirmSwapResult(result);
34-
35-
if (result.warnings?.balance?.messages) {
34+
if (result.warnings?.balance?.messages || result.error) {
3635
return;
3736
}
3837

3938
setConfirmSwapData({
4039
proceedAnyway: false,
4140
quoteData: result.quoteData,
4241
});
42+
callback?.();
4343
};
4444

4545
const clear = () => setConfirmSwapResult(null);

widget/embedded/src/hooks/useHandleSwap/useHandleSwap.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export function useHandleSwap(): ConfirmSwap {
3030
selectedQuote: initialQuote,
3131
customDestination: customDestinationFromStore,
3232
confirmSwapData,
33-
resetAlerts,
3433
} = useQuoteStore()();
3534
const { manager } = useManager();
3635
const { swapMode } = useSwapMode();
@@ -54,7 +53,10 @@ export function useHandleSwap(): ConfirmSwap {
5453
const fetch: ConfirmSwap['fetch'] = async (params) => {
5554
const selectedWallets = params.selectedWallets;
5655
const customDestination =
57-
params?.customDestination ?? customDestinationFromStore;
56+
params?.customDestination ??
57+
selectedWallets[1]?.address ??
58+
customDestinationFromStore ??
59+
destinationWallet?.address;
5860

5961
if (!fromToken || !toToken || !inputAmount) {
6062
return {
@@ -112,8 +114,6 @@ export function useHandleSwap(): ConfirmSwap {
112114
findToken,
113115
});
114116

115-
resetAlerts();
116-
117117
return {
118118
quoteData: result,
119119
error: null,

widget/embedded/src/pages/Home.tsx

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { SelectedQuote, SwapButtonState } from '../types';
22

33
import { i18n } from '@lingui/core';
4-
import { Button, Divider, styled, WarningIcon } from '@rango-dev/ui';
4+
import { Alert, Button, Divider, styled, WarningIcon } from '@rango-dev/ui';
55
import BigNumber from 'bignumber.js';
66
import React, { useEffect, useState } from 'react';
77
import { useNavigate } from 'react-router-dom';
@@ -14,6 +14,7 @@ import { SameTokensWarning } from '../components/SameTokensWarning';
1414
import { SlippageWarningsAndErrors } from '../components/SlippageWarningsAndErrors/SlippageWarningsAndErrors';
1515
import { SwapMetrics } from '../components/SwapMetrics';
1616
import { WalletAddressErrorModal } from '../components/WalletAddressErrorModal/WalletAddressErrorModal';
17+
import { getQuoteErrorMessage } from '../constants/errors';
1718
import { navigationRoutes } from '../constants/navigationRoutes';
1819
import { SLIPPAGES } from '../constants/swapSettings';
1920
import { ExpandedQuotes } from '../containers/ExpandedQuotes';
@@ -26,7 +27,7 @@ import { useSwapMode } from '../hooks/useSwapMode';
2627
import { useAppStore } from '../store/AppStore';
2728
import { useQuoteStore } from '../store/quote';
2829
import { useUiStore } from '../store/ui';
29-
import { UiEventTypes } from '../types';
30+
import { QuoteErrorType, UiEventTypes } from '../types';
3031
import { isVariantExpandable } from '../utils/configs';
3132
import { emitPreventableEvent } from '../utils/events';
3233
import { getSlippageValidation } from '../utils/settings';
@@ -59,11 +60,11 @@ export function Home() {
5960
setQuoteWarningsConfirmed,
6061
updateQuotePartialState,
6162
setConfirmSwapData,
63+
resetAlerts,
6264
} = useQuoteStore()();
6365

6466
const [isVisibleExpanded, setIsVisibleExpanded] = useState<boolean>(false);
6567
const { isLargeScreen, isExtraLargeScreen } = useScreenDetect();
66-
6768
const { fetch: fetchQuote, loading } = useSwapInput({ refetchQuote });
6869
const {
6970
config,
@@ -130,7 +131,6 @@ export function Home() {
130131

131132
const currentQuoteWarning =
132133
slippageValidation?.quoteValidation || quoteWarning;
133-
134134
const hasValidQuotes =
135135
!isExpandable || (isExpandable && quotes?.results.length);
136136
const hasWarningOrError = currentQuoteWarning || quoteError;
@@ -204,7 +204,9 @@ export function Home() {
204204
onHandleNavigation(navigationRoutes.destinationWallet),
205205
'confirm-warning': () => setShowQuoteWarningModal(true),
206206
'show-wallet-address-error': () => setShowWalletAddressError(true),
207-
'confirm-swap': handleConfirmSwap,
207+
'confirm-swap': async () => {
208+
await handleConfirmSwap(() => navigate(navigationRoutes.confirmSwap));
209+
},
208210
'select-route-wallets': () =>
209211
onHandleNavigation(navigationRoutes.routeWallets),
210212
};
@@ -218,6 +220,7 @@ export function Home() {
218220
};
219221

220222
const onConfirmBalanceWarning = () => {
223+
resetAlerts();
221224
if (confirmSwapResult?.quoteData) {
222225
setConfirmSwapData({
223226
proceedAnyway: true,
@@ -339,7 +342,7 @@ export function Home() {
339342
skipAlerts={!!slippageValidation}
340343
couldChangeSettings={true}
341344
refetchQuote={fetchQuote}
342-
showWarningModal={showQuoteWarningModal || showBalanceWarning}
345+
showWarningModal={showQuoteWarningModal}
343346
confirmationDisabled={!isActiveTab}
344347
onOpenWarningModal={() => setShowQuoteWarningModal(true)}
345348
onCloseWarningModal={() => setShowQuoteWarningModal(false)}
@@ -366,6 +369,20 @@ export function Home() {
366369
/>
367370
</>
368371
)}
372+
373+
{!!confirmSwapResult?.error &&
374+
confirmSwapResult.error.type === QuoteErrorType.REQUEST_FAILED && (
375+
<>
376+
<Divider size={8} />
377+
<Alert
378+
type="error"
379+
variant="alarm"
380+
titleAlign="left"
381+
title={getQuoteErrorMessage(confirmSwapResult.error)}
382+
/>
383+
<Divider size={8} />
384+
</>
385+
)}
369386
<SameTokensWarning />
370387
</PageContainer>
371388
</Layout>

widget/embedded/src/pages/RouteWalletsPage/RouteWalletsPage.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { SupportedWalletsPropTypes } from './RouteWalletsPage.types';
22
import type { ConfirmSwapFetchResult } from '../../hooks/useHandleSwap/useHandleSwap.types';
3-
import type { Wallet } from '../../types';
43

54
import { i18n } from '@lingui/core';
65
import { Alert, Button, Divider, Typography } from '@rango-dev/ui';
@@ -12,11 +11,13 @@ import { InsufficientBalanceModal } from '../../components/InsufficientBalanceMo
1211
import { Layout } from '../../components/Layout';
1312
import { MoreWalletsToSelect } from '../../components/MoreWalletsToSelect/MoreWalletsToSelect';
1413
import { ListContainer } from '../../components/MoreWalletsToSelect/MoreWalletsToSelect.styles';
14+
import { getQuoteErrorMessage } from '../../constants/errors';
1515
import { navigationRoutes } from '../../constants/navigationRoutes';
1616
import { useConfirmSwap } from '../../hooks/useConfirmSwap';
1717
import { useWalletList } from '../../hooks/useWalletList';
1818
import { useAppStore } from '../../store/AppStore';
1919
import { useQuoteStore } from '../../store/quote';
20+
import { QuoteErrorType, type Wallet } from '../../types';
2021

2122
import { getRouteBlockchains } from './RouteWalletsPage.helpers';
2223
import { Container, Title } from './RouteWalletsPage.styles';
@@ -132,6 +133,7 @@ export function RouteWalletsPage() {
132133
};
133134

134135
const handleConfirmWallets = () => {
136+
resetConfirmSwapState();
135137
const everyRequiredWalletsSelected = !requiredBlockchains.some(
136138
(blockchain) => !routeWallets?.[blockchain]
137139
);
@@ -141,7 +143,7 @@ export function RouteWalletsPage() {
141143
return;
142144
}
143145

144-
void handleConfirmSwap();
146+
void handleConfirmSwap(() => navigateToConfirmSwapPage());
145147
};
146148

147149
return (
@@ -170,6 +172,19 @@ export function RouteWalletsPage() {
170172
</Button>
171173
}>
172174
<Container>
175+
{!!confirmSwapResult?.error &&
176+
confirmSwapResult.error.type ===
177+
QuoteErrorType.REQUEST_FAILED && (
178+
<>
179+
<Alert
180+
type="error"
181+
variant="alarm"
182+
titleAlign="left"
183+
title={getQuoteErrorMessage(confirmSwapResult.error)}
184+
/>
185+
<Divider size={8} />
186+
</>
187+
)}
173188
{showRouteWalletsError && (
174189
<>
175190
<Alert

widget/embedded/src/utils/swap.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {
4848
TOKEN_AMOUNT_MAX_DECIMALS,
4949
TOKEN_AMOUNT_MIN_DECIMALS,
5050
} from '../constants/routing';
51+
import { getRouteBlockchains } from '../pages/RouteWalletsPage/RouteWalletsPage.helpers';
5152

5253
import { uniqueBy } from './common';
5354
import { isValidTokenAddress } from './meta';
@@ -313,7 +314,7 @@ export function getSwapButtonState(params: {
313314
};
314315
}
315316
const allChains = getQuoteChains({ quote, filter: 'all' });
316-
const requiredChains = getQuoteChains({ quote, filter: 'required' });
317+
const requiredChains = getRouteBlockchains(quote);
317318

318319
/**
319320
* For a simple on-chain swap,
@@ -334,8 +335,7 @@ export function getSwapButtonState(params: {
334335
};
335336
}
336337
}
337-
338-
if (requiredChains.length > 1) {
338+
if (requiredChains.length > 0) {
339339
return {
340340
title: swapButtonTitles().swap,
341341
action: 'select-route-wallets',
@@ -745,7 +745,7 @@ export function generateBalanceWarnings(
745745
if (!acc[item.blockchain]) {
746746
acc[item.blockchain] = [];
747747
}
748-
acc[item.blockchain].push(item);
748+
acc[item.blockchain]?.push(item);
749749
return acc;
750750
}, {});
751751
}

widget/ui/src/containers/SwapInput/SwapInput.styles.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const Container = styled('div', {
1010
},
1111
backgroundColor: '$$color',
1212
borderRadius: '$xm',
13-
padding: '$15',
13+
padding: '$8 $15',
1414
borderTopLeftRadius: '$0',
1515
borderTopRightRadius: '$0',
1616
variants: {
@@ -50,6 +50,7 @@ export const TokenValue = styled('div', {
5050
display: 'flex',
5151
flexDirection: 'row-reverse',
5252
justifyContent: 'space-between',
53+
padding: '$4 0',
5354
});
5455

5556
export const ValueTypography = styled('div', {

widget/ui/src/containers/SwapInput/SwapInput.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ export function SwapInput(props: SwapInputPropTypes) {
106106
)}
107107
</div>
108108
</div>
109-
<Divider size={4} />
110109
<TokenValue>
111110
<PriceImpact
112111
size="medium"

0 commit comments

Comments
 (0)