Skip to content

Commit

Permalink
Fix/swap some bugs fix (#5721)
Browse files Browse the repository at this point in the history
  • Loading branch information
ezailWang authored Sep 5, 2024
1 parent 6b26fc0 commit 6378ec6
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 14 deletions.
1 change: 1 addition & 0 deletions packages/kit-bg/src/services/ServiceSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ export default class ServiceSwap extends ServiceBase {
}

@backgroundMethod()
@toastIfError()
async fetchQuotesEvents({
fromToken,
toToken,
Expand Down
17 changes: 13 additions & 4 deletions packages/kit/src/components/TokenListItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,24 @@ export function TokenListItem({
return (
<ListItem
userSelect="none"
{...(disabled && {
opacity: 0.5,
})}
{...(isSearch && {
$md: {
alignItems: 'flex-start',
},
})}
{...rest}
>
<Token tokenImageUri={tokenImageSrc} networkImageUri={networkImageSrc} />
<Token
{...(disabled && {
opacity: 0.5,
})}
tokenImageUri={tokenImageSrc}
networkImageUri={networkImageSrc}
/>
<ListItem.Text
{...(disabled && {
opacity: 0.5,
})}
flex={1}
primary={tokenSymbol}
primaryMatch={titleMatchStr}
Expand Down Expand Up @@ -79,6 +85,9 @@ export function TokenListItem({
}
/>
<ListItem.Text
{...(disabled && {
opacity: 0.5,
})}
align="right"
primary={
<NumberSizeableText
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ const SwapPopularTokenGroup = ({
selectedToken,
tokens,
}: ISwapPopularTokenGroupProps) => (
<XStack pt="$1" pb="$3" gap="$2" flexWrap="wrap">
<XStack pt="$1" pb="$3" gap="$1.5" flexWrap="wrap">
{tokens.map((token) => (
<Button
key={token.contractAddress}
variant="secondary"
borderRadius="$4"
px="$1.5"
size="small"
borderColor="$borderSubdued"
flexDirection="row"
justifyContent="center"
alignItems="center"
backgroundColor="$background"
gap="$0.5"
disabled={
!!equalTokenNoCaseSensitive({
token1: selectedToken,
Expand Down
2 changes: 0 additions & 2 deletions packages/kit/src/views/Swap/hooks/useSwapQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,6 @@ export function useSwapQuote() {
if (isFocus) {
appEventBus.off(EAppEventBusNames.SwapQuoteEvent, quoteEventHandler);
appEventBus.on(EAppEventBusNames.SwapQuoteEvent, quoteEventHandler);
} else {
appEventBus.off(EAppEventBusNames.SwapQuoteEvent, quoteEventHandler);
}
}
setTimeout(() => {
Expand Down
11 changes: 8 additions & 3 deletions packages/kit/src/views/Swap/pages/components/SwapQuoteResult.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { memo, useCallback, useRef, useState } from 'react';

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

import type { IDialogInstance } from '@onekeyhq/components';
Expand Down Expand Up @@ -94,8 +94,13 @@ const SwapQuoteResult = ({
slippageOnSave,
setSwapSlippageDialogOpening,
]);

if (!fromToken || !toToken || !fromTokenAmount) {
const fromTokenAmountBN = new BigNumber(fromTokenAmount ?? 0);
if (
!fromToken ||
!toToken ||
fromTokenAmountBN.isZero() ||
fromTokenAmountBN.isNaN()
) {
return null;
}
return (
Expand Down
13 changes: 10 additions & 3 deletions packages/kit/src/views/Swap/pages/modal/SwapTokenSelectModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';

import { useRoute } from '@react-navigation/core';
import BigNumber from 'bignumber.js';
Expand Down Expand Up @@ -68,6 +68,7 @@ import { useSwapTokenList } from '../../hooks/useSwapTokens';
import { SwapProviderMirror } from '../SwapProviderMirror';

import type { RouteProp } from '@react-navigation/core';
import type { FlatList } from 'react-native';

const SwapTokenSelectPage = () => {
const navigation =
Expand Down Expand Up @@ -140,6 +141,7 @@ const SwapTokenSelectPage = () => {
const [currentSelectNetwork, setCurrentSelectNetwork] = useState<
ISwapNetwork | undefined
>(syncDefaultNetworkSelect);
const listViewRef = useRef<FlatList>(null);

useEffect(() => {
const accountNet =
Expand Down Expand Up @@ -233,6 +235,10 @@ const SwapTokenSelectPage = () => {
networkId: network.networkId,
});
}
listViewRef.current?.scrollToOffset({
offset: 0,
animated: true,
});
},
[type, updateSelectedAccountNetwork],
);
Expand Down Expand Up @@ -434,7 +440,7 @@ const SwapTokenSelectPage = () => {
/>
<Page.Body>
<XStack px="$5" pb="$2">
<SizableText size="$headingSm" color="$textSubdued" pr="$2">
<SizableText size="$bodyMd" color="$textSubdued" pr="$2">
{`${intl.formatMessage({
id: ETranslations.token_selector_network,
})}`}
Expand Down Expand Up @@ -472,14 +478,15 @@ const SwapTokenSelectPage = () => {
<Divider mt="$2" />
<YStack flex={1}>
<ListView
ref={listViewRef}
data={currentTokens}
renderItem={renderItem}
estimatedItemSize={60}
ListHeaderComponent={
currentNetworkPopularTokens.length > 0 &&
!searchKeywordDebounce ? (
<YStack px="$5" pt="$3" gap="$2">
<SizableText size="$headingSm" color="$textSubdued" pr="$2">
<SizableText size="$bodyMd" color="$textSubdued" pr="$2">
{`${intl.formatMessage({
id: ETranslations.swap_token_selector_popular_token,
})}`}
Expand Down
36 changes: 36 additions & 0 deletions packages/shared/types/swap/SwapProvider.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,42 @@ export const swapDefaultSetTokens: Record<
'isNative': false,
},
},
'sui--mainnet': {
fromToken: {
'networkId': 'sui--mainnet',
'contractAddress': '0x2::sui::SUI',
'name': 'Sui',
'symbol': 'SUI',
'decimals': 9,
'logoURI':
'https://uni-test.onekey-asset.com/server-service-onchain/sui--mainnet/tokens/0x2::sui::SUI.png',
'isNative': true,
},
},
'ton--mainnet': {
fromToken: {
'networkId': 'ton--mainnet',
'contractAddress': '',
'name': 'Toncoin',
'symbol': 'TON',
'decimals': 9,
'logoURI':
'https://uni-test.onekey-asset.com/server-service-onchain/ton--mainnet/tokens/native.png',
'isNative': true,
},
},
'aptos--1': {
fromToken: {
'networkId': 'aptos--1',
'contractAddress': '0x1::aptos_coin::AptosCoin',
'name': 'Aptos Coin',
'symbol': 'APT',
'decimals': 8,
'logoURI':
'https://uni-test.onekey-asset.com/server-service-onchain/aptos--1/tokens/0x1::aptos_coin::AptosCoin.png',
'isNative': true,
},
},
};

export const swapPopularTokens: Record<string, ISwapToken[]> = {
Expand Down

0 comments on commit 6378ec6

Please sign in to comment.