Skip to content

Commit

Permalink
fix:swap to token balance use fromAccount & open receiveAddress auto …
Browse files Browse the repository at this point in the history
…manualSelectQuote ok-33944 ok-33948 (#6224)
  • Loading branch information
ezailWang authored Nov 19, 2024
1 parent d91e2b8 commit 670b65a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
24 changes: 21 additions & 3 deletions packages/kit/src/states/jotai/contexts/swap/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1250,9 +1250,27 @@ class ContentJotaiActionsSwap extends ContextJotaiActionsBase {
type === ESwapDirectionType.FROM
? get(swapSelectFromTokenAtom())
: get(swapSelectToTokenAtom());
const accountAddress = swapAddressInfo.address;
const accountNetworkId = swapAddressInfo.networkId;
const accountId = swapAddressInfo.accountInfo?.account?.id;
let accountAddress: string | undefined;
let accountNetworkId: string | undefined;
let accountId: string | undefined;
if (type === ESwapDirectionType.TO) {
// fetch to Token balance use FromAccount id
const toAccountInfos =
await backgroundApiProxy.serviceStaking.getEarnAccount({
accountId: swapAddressInfo.accountInfo?.account?.id ?? '',
networkId: token?.networkId ?? '',
indexedAccountId: swapAddressInfo.accountInfo?.indexedAccount?.id,
});
if (toAccountInfos) {
accountAddress = toAccountInfos.accountAddress;
accountNetworkId = toAccountInfos.networkId;
accountId = toAccountInfos.accountId;
}
} else {
accountAddress = swapAddressInfo.address;
accountNetworkId = swapAddressInfo.networkId;
accountId = swapAddressInfo.accountInfo?.account?.id;
}
let balanceDisplay;
if (
token &&
Expand Down
3 changes: 3 additions & 0 deletions packages/kit/src/states/jotai/contexts/swap/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ export const {
return manualSelectQuoteResult;
}
if (list?.length > 0) {
if (!manualSelectQuoteProviders?.unSupportReceiveAddressDifferent) {
return list.find((item) => !item.unSupportReceiveAddressDifferent);
}
return list[0];
}
return undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ import {
useSwapAddressInfo,
useSwapRecipientAddressInfo,
} from '../../hooks/useSwapAccount';
import {
useSwapActionState,
useSwapQuoteLoading,
} from '../../hooks/useSwapState';
import { useSwapActionState } from '../../hooks/useSwapState';

interface ISwapActionsStateProps {
onBuildTx: () => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import { AccountSelectorProviderMirror } from '@onekeyhq/kit/src/components/Acco
import type { IAddressInputValue } from '@onekeyhq/kit/src/components/AddressInput';
import { AddressInput } from '@onekeyhq/kit/src/components/AddressInput';
import useAppNavigation from '@onekeyhq/kit/src/hooks/useAppNavigation';
import { useSwapToAnotherAccountAddressAtom } from '@onekeyhq/kit/src/states/jotai/contexts/swap';
import {
useSwapManualSelectQuoteProvidersAtom,
useSwapQuoteCurrentSelectAtom,
useSwapToAnotherAccountAddressAtom,
} from '@onekeyhq/kit/src/states/jotai/contexts/swap';
import { useSettingsAtom } from '@onekeyhq/kit-bg/src/states/jotai/atoms';
import { ETranslations } from '@onekeyhq/shared/src/locale';
import type {
Expand Down Expand Up @@ -52,6 +56,8 @@ const SwapToAnotherAddressPage = () => {

const [, setSettings] = useSettingsAtom();
const [, setSwapToAddress] = useSwapToAnotherAccountAddressAtom();
const [selectedQuote] = useSwapQuoteCurrentSelectAtom();
const [, setSwapManualSelectQuote] = useSwapManualSelectQuoteProvidersAtom();
const intl = useIntl();
const form = useForm({
defaultValues: {
Expand Down Expand Up @@ -91,9 +97,17 @@ const SwapToAnotherAddressPage = () => {
networkId: activeAccount?.network?.id,
accountInfo: activeAccount,
}));
setSwapManualSelectQuote(selectedQuote);
navigation.pop();
},
[activeAccount, navigation, setSettings, setSwapToAddress],
[
activeAccount,
navigation,
selectedQuote,
setSettings,
setSwapManualSelectQuote,
setSwapToAddress,
],
);

const handleOnCancel = useCallback(() => {
Expand Down

0 comments on commit 670b65a

Please sign in to comment.