Skip to content

Commit 4cf6366

Browse files
authored
feat: add Add to wallet button, show USD amounts and fix small-number rounding (#2631)
1 parent 700658a commit 4cf6366

File tree

3 files changed

+144
-28
lines changed

3 files changed

+144
-28
lines changed

src/components/transactions/Switch/BaseSwitchModalContent.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,17 @@ export const BaseSwitchModalContent = ({
715715
.multipliedBy(1 - safeSlippage)
716716
.decimalPlaces(switchRates.destDecimals, BigNumber.ROUND_UP)
717717
.toString()}
718+
amountUsd={Number(switchRates.srcUSD)}
719+
outAmountUSD={Number(
720+
switchRates.provider === 'cowprotocol' ? switchRates.destSpotInUsd : switchRates.destUSD
721+
)}
722+
addToken={{
723+
address: selectedOutputToken.aToken ?? selectedOutputToken.address,
724+
symbol: selectedOutputToken.symbol,
725+
decimals: selectedOutputToken.decimals,
726+
aToken: selectedOutputToken.aToken ? true : false,
727+
}}
728+
modalType={modalType}
718729
/>
719730
);
720731
}

src/components/transactions/Switch/SwitchTxSuccessView.tsx

Lines changed: 131 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { normalize } from '@aave/math-utils';
22
import { Trans } from '@lingui/macro';
3-
import { Box, CircularProgress, Divider, Typography } from '@mui/material';
3+
import { Box, Button, CircularProgress, Divider, Typography } from '@mui/material';
44
import { BigNumber } from 'ethers';
55
import { useEffect, useMemo, useRef, useState } from 'react';
6+
import { WalletIcon } from 'src/components/icons/WalletIcon';
67
import { DarkTooltip } from 'src/components/infoTooltips/DarkTooltip';
78
import { FormattedNumber } from 'src/components/primitives/FormattedNumber';
8-
import { ExternalTokenIcon } from 'src/components/primitives/TokenIcon';
9+
import { Base64Token, ExternalTokenIcon, TokenIcon } from 'src/components/primitives/TokenIcon';
910
import { TextWithTooltip, TextWithTooltipProps } from 'src/components/TextWithTooltip';
1011
import { useCowOrderToast } from 'src/hooks/useCowOrderToast';
12+
import { ModalType } from 'src/hooks/useModal';
13+
import { useWeb3Context } from 'src/libs/hooks/useWeb3Context';
14+
import { ERC20TokenType } from 'src/libs/web3-data-provider/Web3Provider';
1115
import { networkConfigs } from 'src/ui-config/networksConfig';
1216
import { parseUnits } from 'viem';
1317

@@ -38,6 +42,10 @@ export type SwitchTxSuccessViewProps = {
3842
chainId: number;
3943
destDecimals: number;
4044
srcDecimals: number;
45+
amountUsd: number;
46+
outAmountUSD: number;
47+
addToken?: ERC20TokenType;
48+
modalType: ModalType;
4149
};
4250

4351
export const SwitchWithSurplusTooltip = ({
@@ -83,8 +91,14 @@ export const SwitchTxSuccessView = ({
8391
chainId,
8492
destDecimals,
8593
srcDecimals,
94+
amountUsd,
95+
outAmountUSD,
96+
addToken,
97+
modalType,
8698
}: SwitchTxSuccessViewProps) => {
8799
const { trackOrder, setHasActiveOrders } = useCowOrderToast();
100+
const { addERC20Token } = useWeb3Context();
101+
const [base64, setBase64] = useState<string>('');
88102

89103
// Do polling each 10 seconds until the order get's filled
90104
const [orderStatus, setOrderStatus] = useState<'succeed' | 'failed' | 'open'>('open');
@@ -196,6 +210,9 @@ export const SwitchTxSuccessView = ({
196210
) : undefined;
197211
}, [provider, txHashOrOrderId]);
198212

213+
const shouldShowATokenCta = modalType === ModalType.CollateralSwap && addToken?.aToken;
214+
const watchedTokenSymbol = shouldShowATokenCta ? '' : addToken?.symbol ?? '';
215+
199216
return (
200217
<View
201218
txHash={txHashOrOrderId}
@@ -227,7 +244,6 @@ export const SwitchTxSuccessView = ({
227244
border: '1px solid',
228245
borderColor: 'divider',
229246
p: 3,
230-
mb: 4,
231247
width: '80%',
232248
}}
233249
>
@@ -257,22 +273,40 @@ export const SwitchTxSuccessView = ({
257273
enterTouchDelay={100}
258274
leaveTouchDelay={500}
259275
>
260-
<Box>
261-
<Typography fontWeight={600}>
262-
{Number(inAmount).toLocaleString(undefined, {
263-
minimumFractionDigits: 2,
264-
maximumFractionDigits: Number(inAmount) < 0.01 ? 4 : 2,
265-
})}{' '}
266-
</Typography>
267-
</Box>
276+
<>
277+
<Box>
278+
<Typography fontWeight={600}>
279+
<FormattedNumber
280+
value={inAmount}
281+
variant="main14"
282+
visibleDecimals={4}
283+
compact={false}
284+
/>
285+
</Typography>
286+
</Box>
287+
</>
268288
</DarkTooltip>
269-
<Typography fontWeight={600} sx={{ color: 'text.secondary' }}>
270-
{symbol}
271-
</Typography>
289+
290+
<Typography fontWeight={600}>{symbol}</Typography>
272291
</Box>
273292
</Box>
293+
{amountUsd && amountUsd > 0 && (
294+
<Box display="flex" justifyContent="flex-end" mb={'12px'}>
295+
<Typography>
296+
<FormattedNumber
297+
value={amountUsd}
298+
symbol="USD"
299+
symbolsColor="text.muted"
300+
variant="caption"
301+
color="text.muted"
302+
visibleDecimals={2}
303+
compact={false}
304+
/>
305+
</Typography>
306+
</Box>
307+
)}
274308
<Divider sx={{ my: 1 }} />
275-
<Box display="flex" justifyContent="space-between" alignItems="center" mb={0.5}>
309+
<Box display="flex" justifyContent="space-between" alignItems="center" mb={1} mt={'12px'}>
276310
<Typography color="text.secondary">
277311
{provider == 'cowprotocol' && (orderStatus == 'open' || orderStatus == 'failed')
278312
? 'Receive'
@@ -297,31 +331,100 @@ export const SwitchTxSuccessView = ({
297331
enterTouchDelay={100}
298332
leaveTouchDelay={500}
299333
>
300-
<Box>
301-
<Typography fontWeight={600}>
302-
{Number(outFinalAmount).toLocaleString(undefined, {
303-
minimumFractionDigits: 2,
304-
maximumFractionDigits: Number(outFinalAmount) < 0.01 ? 4 : 2,
305-
})}
306-
</Typography>
307-
</Box>
334+
<>
335+
<Box>
336+
<Typography fontWeight={600}>
337+
<FormattedNumber
338+
value={outFinalAmount}
339+
variant="main14"
340+
visibleDecimals={4}
341+
compact={false}
342+
/>
343+
</Typography>
344+
</Box>
345+
</>
308346
</DarkTooltip>
309-
<Typography fontWeight={600} sx={{ color: 'text.secondary' }}>
310-
{outSymbol}
311-
</Typography>
347+
348+
<Typography fontWeight={600}>{outSymbol}</Typography>
312349
</Box>
313350
</Box>
351+
{outAmountUSD && outAmountUSD > 0 && (
352+
<Box display="flex" justifyContent="flex-end">
353+
<Typography>
354+
<FormattedNumber
355+
value={outAmountUSD}
356+
symbol="USD"
357+
symbolsColor="text.muted"
358+
variant="caption"
359+
color="text.muted"
360+
visibleDecimals={2}
361+
compact={false}
362+
/>
363+
</Typography>
364+
</Box>
365+
)}
314366
{surplusDisplay && (
315367
<Typography
316368
variant="helperText"
317369
fontWeight={500}
318-
sx={{ float: 'right', color: 'text.secondary' }}
319-
mt={0.5}
370+
sx={{ float: 'right', color: 'text.secondary', mt: '4px' }}
320371
>
321372
{surplusDisplay}
322373
</Typography>
323374
)}
324375
</Box>
376+
{shouldShowATokenCta && (
377+
<Box
378+
sx={(theme) => ({
379+
border: theme.palette.mode === 'dark' ? `1px solid ${theme.palette.divider}` : 'none',
380+
background: theme.palette.mode === 'dark' ? 'none' : '#F7F7F9',
381+
borderRadius: 2,
382+
display: 'flex',
383+
flexDirection: 'column',
384+
alignItems: 'center',
385+
justifyContent: 'center',
386+
mt: 4,
387+
388+
width: '80%',
389+
})}
390+
>
391+
<TokenIcon
392+
symbol={addToken.symbol}
393+
aToken={addToken.aToken || false}
394+
sx={{ fontSize: '32px', mt: '12px', mb: '8px' }}
395+
/>
396+
<Typography variant="description" color="text.primary" sx={{ mx: '24px' }}>
397+
<Trans>
398+
Add {addToken.aToken ? 'aToken ' : 'token '} to wallet to track your balance.
399+
</Trans>
400+
</Typography>
401+
<Button
402+
onClick={() => {
403+
addERC20Token({
404+
address: addToken.address,
405+
decimals: addToken.decimals,
406+
symbol: watchedTokenSymbol,
407+
image: !/_/.test(addToken.symbol) ? base64 : undefined,
408+
});
409+
}}
410+
variant="outlined"
411+
size="medium"
412+
sx={{ mt: '8px', mb: '12px' }}
413+
>
414+
{addToken.symbol && !/_/.test(addToken.symbol) && (
415+
<Base64Token
416+
symbol={addToken.symbol}
417+
onImageGenerated={setBase64}
418+
aToken={addToken.aToken}
419+
/>
420+
)}
421+
<WalletIcon sx={{ width: '20px', height: '20px', mr: '4px' }} />
422+
<Typography variant="buttonM" ml="4px">
423+
<Trans>Add to wallet</Trans>
424+
</Typography>
425+
</Button>
426+
</Box>
427+
)}
325428
</View>
326429
);
327430
};

src/locales/en/messages.po

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ msgid "All proposals"
639639
msgstr "All proposals"
640640

641641
#: src/components/transactions/FlowCommons/Success.tsx
642+
#: src/components/transactions/Switch/SwitchTxSuccessView.tsx
642643
msgid "Add to wallet"
643644
msgstr "Add to wallet"
644645

@@ -2193,6 +2194,7 @@ msgid "Interest rate rebalance conditions were not met"
21932194
msgstr "Interest rate rebalance conditions were not met"
21942195

21952196
#: src/components/transactions/FlowCommons/Success.tsx
2197+
#: src/components/transactions/Switch/SwitchTxSuccessView.tsx
21962198
msgid "Add {0} to wallet to track your balance."
21972199
msgstr "Add {0} to wallet to track your balance."
21982200

0 commit comments

Comments
 (0)