Skip to content

Commit

Permalink
optimize: swap UI in send confirm OK-34238 (#6316)
Browse files Browse the repository at this point in the history
* feat: swap action block

* chore: update locale

* feat: swap tip

* feat: lint

* fix: lint
  • Loading branch information
weatherstar authored Dec 6, 2024
1 parent 12164f5 commit d8729c7
Show file tree
Hide file tree
Showing 32 changed files with 489 additions and 176 deletions.
3 changes: 3 additions & 0 deletions packages/kit-bg/src/vaults/base/VaultBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,7 @@ export abstract class VaultBase extends VaultBaseChainOnly {
amount: swapInfo.sender.amount,
isNFT: false,
isNative: swapSendToken.isNative,
networkId: swapInfo.sender.accountInfo.networkId,
},
{
from: '',
Expand All @@ -970,6 +971,7 @@ export abstract class VaultBase extends VaultBaseChainOnly {
amount: swapInfo.receiver.amount,
isNFT: false,
isNative: swapReceiveToken.isNative,
networkId: swapInfo.receiver.accountInfo.networkId,
},
];

Expand All @@ -990,6 +992,7 @@ export abstract class VaultBase extends VaultBaseChainOnly {
amount: feeInfo.amount,
isNFT: false,
isNative: feeInfo.token.isNative,
networkId: swapInfo.sender.accountInfo.networkId,
});
}
});
Expand Down
2 changes: 2 additions & 0 deletions packages/kit-bg/src/vaults/impls/btc/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ export default class VaultBtc extends VaultBase {
amount: swapInfo.sender.amount,
isNFT: false,
isNative: swapSendToken.isNative,
networkId: swapInfo.sender.accountInfo.networkId,
},
{
from: '',
Expand All @@ -262,6 +263,7 @@ export default class VaultBtc extends VaultBase {
amount: swapInfo.receiver.amount,
isNFT: false,
isNative: swapReceiveToken.isNative,
networkId: swapInfo.receiver.accountInfo.networkId,
},
],
isInternalSwap: true,
Expand Down
2 changes: 2 additions & 0 deletions packages/kit-bg/src/vaults/impls/kaspa/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ export default class Vault extends VaultBase {
amount: swapInfo.sender.amount,
isNFT: false,
isNative: swapSendToken.isNative,
networkId: swapInfo.sender.accountInfo.networkId,
},
{
from: '',
Expand All @@ -219,6 +220,7 @@ export default class Vault extends VaultBase {
amount: swapInfo.receiver.amount,
isNFT: false,
isNative: swapReceiveToken.isNative,
networkId: swapInfo.receiver.accountInfo.networkId,
},
],
isInternalSwap: true,
Expand Down
261 changes: 261 additions & 0 deletions packages/kit/src/components/TxAction/TxActionSwapInfo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
import { useMemo } from 'react';

import BigNumber from 'bignumber.js';
import { isNil } from 'lodash';
import { useIntl } from 'react-intl';

import type { ISizableTextProps } from '@onekeyhq/components';
import {
Divider,
IconButton,
Image,
NumberSizeableText,
SizableText,
Stack,
XStack,
} from '@onekeyhq/components';
import { ETranslations } from '@onekeyhq/shared/src/locale';
import {
swapServiceFeeDefault,
swapSlippageDecimal,
} from '@onekeyhq/shared/types/swap/SwapProvider.constants';
import type { ISwapTxInfo } from '@onekeyhq/shared/types/swap/types';

import { useAccountData } from '../../hooks/useAccountData';
import {
InfoItem,
InfoItemGroup,
} from '../../views/AssetDetails/pages/HistoryDetails/components/TxDetailsInfoItem';
import { SwapServiceFeeOverview } from '../../views/Swap/components/SwapServiceFeeOverview';
import { NetworkAvatar } from '../NetworkAvatar';

interface IProps {
swapInfo: ISwapTxInfo;
}

const textStyle: ISizableTextProps = {
size: '$bodyMd',
};

const FROM_TOKEN_RATE_BASE_NUMBER = 1;

function TxActionSwapInfo(props: IProps) {
const { swapInfo } = props;
const { sender, receiver, swapBuildResData, swapRequiredApproves } = swapInfo;
const {
info: provider,
instantRate,
slippage,
unSupportSlippage,
fee,
} = swapBuildResData.result;

const { network: senderNetwork } = useAccountData({
networkId: sender.accountInfo.networkId,
});

const { network: receiverNetwork } = useAccountData({
networkId: receiver.accountInfo.networkId,
});

const displaySlippage = useMemo(
() =>
new BigNumber(slippage ?? 0)
.decimalPlaces(swapSlippageDecimal, BigNumber.ROUND_DOWN)
.toFixed(),
[slippage],
);

const tokenRate = useMemo(() => {
if (isNil(instantRate)) {
return null;
}

const senderTokenSymbol = sender.token.symbol;
const receiverTokenSymbol = receiver.token.symbol;

return (
<XStack alignItems="center" gap="$1">
<NumberSizeableText
formatter="balance"
formatterOptions={{
tokenSymbol: senderTokenSymbol,
}}
{...textStyle}
>
{FROM_TOKEN_RATE_BASE_NUMBER}
</NumberSizeableText>
<SizableText {...textStyle}>=</SizableText>
<NumberSizeableText
formatter="balance"
formatterOptions={{
tokenSymbol: receiverTokenSymbol,
}}
{...textStyle}
>
{instantRate}
</NumberSizeableText>
</XStack>
);
}, [instantRate, receiver.token.symbol, sender.token.symbol]);

const serviceFee = useMemo(() => {
if (!fee || isNil(fee.percentageFee)) {
return null;
}

if (new BigNumber(fee.percentageFee).gte(swapServiceFeeDefault)) {
return (
<XStack alignItems="center" gap="$1">
<SizableText {...textStyle}>{fee.percentageFee}%</SizableText>
<SwapServiceFeeOverview onekeyFee={fee.percentageFee} />
</XStack>
);
}

return (
<XStack alignItems="center" gap="$1">
<SizableText color="$textSuccess" {...textStyle}>
{fee.percentageFee}%
</SizableText>
<SizableText textDecorationLine="line-through" {...textStyle}>
{swapServiceFeeDefault}%
</SizableText>
<SwapServiceFeeOverview onekeyFee={fee.percentageFee} />
</XStack>
);
}, [fee]);

const intl = useIntl();

if (!swapInfo) {
return null;
}

return (
<Stack>
<InfoItemGroup testID="swap-tx-action">
<InfoItem
label={intl.formatMessage({
id: ETranslations.swap_history_detail_provider,
})}
renderContent={
<XStack alignItems="center" gap="$2">
<Image
borderRadius="$1"
w="$5"
h="$5"
source={{ uri: provider.providerLogo }}
/>
<SizableText {...textStyle}>{provider.providerName}</SizableText>
</XStack>
}
compactAll
/>
{tokenRate ? (
<InfoItem
label={intl.formatMessage({
id: ETranslations.swap_history_detail_rate,
})}
renderContent={tokenRate}
compactAll
/>
) : null}

{unSupportSlippage ? null : (
<InfoItem
label={intl.formatMessage({
id: ETranslations.swap_page_provider_slippage_tolerance,
})}
renderContent={
<SizableText {...textStyle}>{displaySlippage}%</SizableText>
}
compactAll
/>
)}
{serviceFee ? (
<InfoItem
label={intl.formatMessage({
id: ETranslations.swap_history_detail_service_fee,
})}
renderContent={serviceFee}
compactAll
/>
) : null}
{sender.accountInfo.networkId !== receiver.accountInfo.networkId ? (
<InfoItem
compactAll
label={intl.formatMessage({ id: ETranslations.network__network })}
renderContent={
<XStack alignItems="center" gap="$2">
<XStack alignItems="center">
<NetworkAvatar
networkId={sender.accountInfo.networkId}
size="$5"
/>

<Stack
p="$0.5"
m="$-0.5"
ml="$-1"
borderRadius="$full"
bg="$bgApp"
>
<NetworkAvatar
networkId={receiver.accountInfo.networkId}
size="$5"
/>
</Stack>
</XStack>
<SizableText size="$bodyMd" color="$textSubdued">
{senderNetwork?.name}{receiverNetwork?.name}
</SizableText>
</XStack>
}
/>
) : (
<InfoItem
compactAll
label={intl.formatMessage({ id: ETranslations.network__network })}
renderContent={
<XStack alignItems="center" gap="$2">
<NetworkAvatar
networkId={sender.accountInfo.networkId}
size="$5"
/>
<SizableText {...textStyle}>{senderNetwork?.name}</SizableText>
</XStack>
}
/>
)}
</InfoItemGroup>
<Divider mx="$5" />
<Stack p="$5">
<SizableText size="$bodySm" color="$textSubdued">
{swapRequiredApproves
? intl.formatMessage(
{
id: ETranslations.transaction_confirm_batch_swap_tip,
},
{
token: sender.token.symbol,
chain: senderNetwork?.name,
provider: provider.providerName,
},
)
: intl.formatMessage(
{
id: ETranslations.transaction_confirm_single_swap_tip,
},
{
chain: senderNetwork?.name,
provider: provider.providerName,
},
)}
</SizableText>
</Stack>
</Stack>
);
}

export { TxActionSwapInfo };
Loading

0 comments on commit d8729c7

Please sign in to comment.