Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: check existing tokens before auto-create #1385

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/navigation/wallet/screens/AccountDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ import KeySvg from '../../../../assets/img/key.svg';
import ReceiveAddress from '../components/ReceiveAddress';
import {IsEVMChain} from '../../../store/wallet/utils/currency';
import {LogActions} from '../../../store/log/';
import uniqBy from 'lodash.uniqby';

export type AccountDetailsScreenParamList = {
selectedAccountAddress: string;
Expand Down Expand Up @@ -341,8 +342,11 @@ const AccountDetails: React.FC<AccountDetailsScreenProps> = ({route}) => {
);

const key = keys[keyId];
const keyFullWalletObjs = key.wallets.filter(
w => w.receiveAddress === selectedAccountAddress,
const keyFullWalletObjs = uniqBy(
key.wallets.filter(w => w.receiveAddress === selectedAccountAddress),
wallet => {
return wallet.id;
},
);
let pendingTxps: AccountProposalsProps = {};
keyFullWalletObjs.forEach(x => {
Expand Down
88 changes: 57 additions & 31 deletions src/store/wallet/effects/create/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
failedAddWallet,
successAddWallet,
successCreateKey,
successUpdateKey,
} from '../../wallet.actions';
import API from 'bitcore-wallet-client/ts_build';
import {Key, KeyMethods, KeyOptions, Token, Wallet} from '../../wallet.models';
Expand Down Expand Up @@ -45,6 +46,7 @@ import {MoralisErc20TokenBalanceByWalletData} from '../../../moralis/moralis.typ
import {getERC20TokenBalanceByWallet} from '../../../moralis/moralis.effects';
import {getTokenContractInfo, startUpdateWalletStatus} from '../status/status';
import {addCustomTokenOption} from '../currencies/currencies';
import {uniq} from 'lodash';

export interface CreateOptions {
network?: Network;
Expand Down Expand Up @@ -905,41 +907,65 @@ export const detectAndCreateTokensForEachEvmWallet =
}

for (const [index, tokenToAdd] of filteredTokens.entries()) {
try {
const newTokenWallet: AddWalletData = {
key,
associatedWallet: w,
currency: {
chain: w.chain,
currencyAbbreviation: tokenToAdd.symbol.toLowerCase(),
isToken: true,
tokenAddress: tokenToAdd.token_address,
decimals: tokenToAdd.decimals,
},
options: {
network: Network.mainnet,
...(account !== undefined && {
account,
customAccount,
}),
},
};
const newWallet = await dispatch(addWallet(newTokenWallet));
if (newWallet) {
await dispatch(
startUpdateWalletStatus({
key,
wallet: newWallet,
force: true,
}),
);
}
} catch (err) {
const existingTokenWallet = key.wallets.filter(wallet => {
return (
wallet.id ===
`${w.id}-${cloneDeep(tokenToAdd.token_address).toLowerCase()}`
);
});
if (existingTokenWallet[0]) {
// workaround for cases where the token was already created but for some reason was not included in the list of tokens in the associated wallet
dispatch(
LogActions.debug(
`Error[${index}] adding Token: ${tokenToAdd?.symbol} (${tokenToAdd.token_address}). Continue anyway...`,
`Token ${tokenToAdd.symbol} (${tokenToAdd.token_address}) already created for this wallet. Adding to tokens list in the associated wallet`,
),
);

(w.tokens || []).push(existingTokenWallet[0].id);
w.tokens = uniq(w.tokens);

await dispatch(
successUpdateKey({
key,
}),
);
} else {
try {
const newTokenWallet: AddWalletData = {
key,
associatedWallet: w,
currency: {
chain: w.chain,
currencyAbbreviation: tokenToAdd.symbol.toLowerCase(),
isToken: true,
tokenAddress: tokenToAdd.token_address,
decimals: tokenToAdd.decimals,
},
options: {
network: Network.mainnet,
...(account !== undefined && {
account,
customAccount,
}),
},
};
const newWallet = await dispatch(addWallet(newTokenWallet));
if (newWallet) {
await dispatch(
startUpdateWalletStatus({
key,
wallet: newWallet,
force: true,
}),
);
}
} catch (err) {
dispatch(
LogActions.debug(
`Error[${index}] adding Token: ${tokenToAdd?.symbol} (${tokenToAdd.token_address}). Continue anyway...`,
),
);
}
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/store/wallet/utils/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ import {
AssetsByChainData,
AssetsByChainListProps,
} from '../../../navigation/wallet/screens/AccountDetails';
import {DeviceEventEmitter} from 'react-native';
import {DeviceEmitterEvents} from '../../../constants/device-emitter-events';
import uniqBy from 'lodash.uniqby';

export const mapAbbreviationAndName =
(
Expand Down Expand Up @@ -1065,7 +1064,10 @@ export const buildAccountList = (
currencyDisplay: 'symbol',
});

const wallets = opts?.filterByCustomWallets || key.wallets;
const wallets = uniqBy(
opts?.filterByCustomWallets || key.wallets,
wallet => wallet.id,
);

wallets.forEach(wallet => {
if (opts?.filterByHideWallet && wallet.hideWallet) {
Expand Down