Skip to content

Commit

Permalink
[Issue-1826] Fix bug for Transaction actions
Browse files Browse the repository at this point in the history
  • Loading branch information
dominhquang committed Nov 19, 2024
1 parent bc6fdfc commit 0a1821c
Show file tree
Hide file tree
Showing 24 changed files with 563 additions and 359 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
YieldPoolType,
YieldPositionInfo,
} from '@subwallet/extension-base/types';
import { isSameAddress } from '@subwallet/extension-base/utils';
import BigN from 'bignumber.js';
import { Button, Icon, Typography } from 'components/design-system-ui';
import MetaInfo from 'components/MetaInfo';
Expand All @@ -25,6 +24,7 @@ import i18n from 'utils/i18n/i18n';
import { findAccountByAddress, toShort } from 'utils/index';
import createStyles from './styles';
import { AccountProxyAvatar } from 'components/design-system-ui/avatar/account-proxy-avatar';
import { isSameAddress } from 'utils/account/account';

type Props = {
compound: YieldPositionInfo;
Expand Down
2 changes: 1 addition & 1 deletion src/components/common/StakingValidatorItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { FontMedium } from 'styles/sharedStyles';
import { CheckCircle, DotsThree, Medal } from 'phosphor-react-native';
import { useSubWalletTheme } from 'hooks/useSubWalletTheme';
import StakingValidatorItemStyle from './style';
import { ValidatorDataType } from 'hooks/screen/Staking/useGetValidatorList';
import { isEthereumAddress } from '@polkadot/util-crypto';
import { toShort } from 'utils/index';
import { getValidatorKey } from 'utils/transaction/stake';
import i18n from 'utils/i18n/i18n';
import { ValidatorDataType } from 'types/earning';

interface Props {
validatorInfo: ValidatorDataType;
Expand Down
3 changes: 3 additions & 0 deletions src/constants/localStorage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const SELECTED_MNEMONIC_TYPE = 'account.selected-mnemonic-type';
export const SEED_PREVENT_MODAL = 'seed.prevent-modal';
export const CONFIRM_TERM_SEED_PHRASE = 'seed-phrase.term-and-condition';
export const EARNING_WARNING_ANNOUNCEMENT = 'announcement.earning-position';

export const TRANSFER_TRANSACTION = 'transaction.transfer';
export const NFT_TRANSACTION = 'transaction.nft';
Expand All @@ -11,4 +12,6 @@ export const UN_STAKE_TRANSACTION = 'transaction.un-stake';
export const CANCEL_UN_STAKE_TRANSACTION = 'transaction.cancel-un-stake';
export const WITHDRAW_TRANSACTION = 'transaction.withdraw';
export const CLAIM_REWARD_TRANSACTION = 'transaction.claim-reward';
export const IMPORT_NFT = 'import.nft';
export const IMPORT_TOKEN = 'import.token';
export const CLAIM_AVAIL_BRIDGE_TRANSACTION = 'transaction.claim-avail-bridge';
2 changes: 1 addition & 1 deletion src/hooks/account/usePreCheckAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

import { ExtrinsicType } from '@subwallet/extension-base/background/KoniTypes';
import { AccountJson } from '@subwallet/extension-base/background/types';
import { useCallback } from 'react';

import { VoidFunction } from 'types/index';
Expand All @@ -13,6 +12,7 @@ import { ALL_STAKING_ACTIONS } from 'constants/transaction';
import { useToast } from 'react-native-toast-notifications';
import { isEthereumAddress } from '@polkadot/util-crypto';
import { getDevMode } from 'utils/storage';
import { AccountJson } from '@subwallet/extension-base/types';

//todo: i18n
//todo: solve error
Expand Down
136 changes: 0 additions & 136 deletions src/hooks/common/useGetChainSlugsByCurrentAccount.ts

This file was deleted.

53 changes: 53 additions & 0 deletions src/hooks/earning/useGetYieldPositionForSpecificAccount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2019-2022 @polkadot/extension-koni-ui authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { YieldPositionInfo } from '@subwallet/extension-base/types';
import BigN from 'bignumber.js';
import { useMemo } from 'react';
import { useSelector } from 'react-redux';
import { RootState } from 'stores/index';
import { useGetChainSlugsByAccount } from 'hooks/useGetChainSlugsByAccount';
import { isSameAddress } from 'utils/account/account';

const useGetYieldPositionForSpecificAccount = (address?: string): YieldPositionInfo[] => {
const poolInfoMap = useSelector((state: RootState) => state.earning.poolInfoMap);
const yieldPositions = useSelector((state: RootState) => state.earning.yieldPositions);
const isAllAccount = useSelector((state: RootState) => state.accountState.isAllAccount);
const currentAccountProxy = useSelector((state: RootState) => state.accountState.currentAccountProxy);
const chainsByAccountType = useGetChainSlugsByAccount();

return useMemo(() => {
const infoSpecificList: YieldPositionInfo[] = [];

const checkAddress = (item: YieldPositionInfo) => {
if (isAllAccount) {
if (address) {
return isSameAddress(address, item.address);
}

return true;
} else {
return currentAccountProxy?.accounts.some(({ address: _address }) => {
const compareAddress = address ? isSameAddress(address, _address) : true;

return compareAddress && isSameAddress(_address, item.address);
});
}
};

for (const info of yieldPositions) {
if (chainsByAccountType.includes(info.chain) && poolInfoMap[info.slug]) {
const isValid = checkAddress(info);
const haveStake = new BigN(info.totalStake).gt(0);

if (isValid && haveStake) {
infoSpecificList.push(info);
}
}
}

return infoSpecificList;
}, [address, chainsByAccountType, currentAccountProxy?.accounts, isAllAccount, poolInfoMap, yieldPositions]);
};

export default useGetYieldPositionForSpecificAccount;
23 changes: 10 additions & 13 deletions src/hooks/earning/useGroupYieldPosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,28 @@ import {
YieldPoolType,
YieldPositionInfo,
} from '@subwallet/extension-base/types';
import { isAccountAll } from '@subwallet/extension-base/utils';
import BigN from 'bignumber.js';
import { useGetChainSlugs } from 'hooks/screen/Home/useGetChainSlugs';
import { useMemo } from 'react';
import { useSelector } from 'react-redux';
import { RootState } from 'stores/index';
import { isSameAddress } from 'utils/account/account';
import { useGetChainSlugsByAccount } from 'hooks/useGetChainSlugsByAccount';

const useGroupYieldPosition = (selectedAddress?: string): YieldPositionInfo[] => {
const { poolInfoMap, yieldPositions } = useSelector((state: RootState) => state.earning);
const { currentAccount } = useSelector((state: RootState) => state.accountState);
const chainsByAccountType = useGetChainSlugs();
const useGroupYieldPosition = (): YieldPositionInfo[] => {
const poolInfoMap = useSelector((state: RootState) => state.earning.poolInfoMap);
const yieldPositions = useSelector((state: RootState) => state.earning.yieldPositions);
const { currentAccountProxy, isAllAccount } = useSelector((state: RootState) => state.accountState);
const chainsByAccountType = useGetChainSlugsByAccount();

return useMemo(() => {
const raw: Record<string, YieldPositionInfo[]> = {};
const result: YieldPositionInfo[] = [];

const address = selectedAddress ? selectedAddress : currentAccount?.address || '';
const isAll = isAccountAll(address);

const checkAddress = (item: YieldPositionInfo) => {
if (isAll) {
if (isAllAccount) {
return true;
} else {
return isSameAddress(address, item.address);
return currentAccountProxy?.accounts.some(({ address }) => isSameAddress(address, item.address));
}
};

Expand All @@ -62,7 +59,7 @@ const useGroupYieldPosition = (selectedAddress?: string): YieldPositionInfo[] =>
continue;
}

if (isAll) {
if (isAllAccount) {
const base: AbstractYieldPositionInfo = {
slug: slug,
chain: positionInfo.chain,
Expand Down Expand Up @@ -120,7 +117,7 @@ const useGroupYieldPosition = (selectedAddress?: string): YieldPositionInfo[] =>
}

return result;
}, [chainsByAccountType, currentAccount?.address, poolInfoMap, selectedAddress, yieldPositions]);
}, [currentAccountProxy?.accounts, isAllAccount, yieldPositions, chainsByAccountType, poolInfoMap]);
};

export default useGroupYieldPosition;
4 changes: 2 additions & 2 deletions src/hooks/earning/useYieldGroupInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { BN_TEN } from 'utils/number';
import { _STAKING_CHAIN_GROUP } from '@subwallet/extension-base/services/earning-service/constants';
import { _getAssetOriginChain } from '@subwallet/extension-base/services/chain-service/utils';
import { BN_ZERO } from '@subwallet/extension-base/utils';
import useGetChainSlugsByCurrentAccount from 'hooks/common/useGetChainSlugsByCurrentAccount';
import { useGetChainSlugsByAccount } from 'hooks/useGetChainSlugsByAccount';

const isRelatedToRelayChain = (
group: string,
Expand Down Expand Up @@ -57,7 +57,7 @@ const useYieldGroupInfo = (): YieldGroupInfo[] => {
const { poolInfoMap } = useSelector((state: RootState) => state.earning);
const { assetRegistry, multiChainAssetMap } = useSelector((state: RootState) => state.assetRegistry);
const { chainInfoMap } = useSelector((state: RootState) => state.chainStore);
const chainsByAccountType = useGetChainSlugsByCurrentAccount();
const chainsByAccountType = useGetChainSlugsByAccount();
const { tokenGroupMap } = useTokenGroup(chainsByAccountType);
const { tokenBalanceMap, tokenGroupBalanceMap } = useAccountBalance(tokenGroupMap, true);
const { priceMap } = useSelector((state: RootState) => state.price);
Expand Down
Loading

0 comments on commit 0a1821c

Please sign in to comment.