diff --git a/CHANGELOG.md b/CHANGELOG.md index e8294670c08..9c28aed7b3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ - added: Buy conversion tracking for Moonpay - added: Error tracking for failure to report conversions to referral server - fixed: Clear previous swap errors when new amounts are entered or swap assets are changed in `SwapCreateScene` +- fixed: Cases where it was possible to create duplicate custom tokens +- fixed: Message about overriding a built-in token contract, which is not possible to do ## 4.14.0 diff --git a/src/components/scenes/EditTokenScene.tsx b/src/components/scenes/EditTokenScene.tsx index 9327908b980..2ed6fe0e986 100644 --- a/src/components/scenes/EditTokenScene.tsx +++ b/src/components/scenes/EditTokenScene.tsx @@ -26,7 +26,10 @@ export interface EditTokenParams { displayName?: string multiplier?: string networkLocation?: JsonObject - tokenId?: EdgeTokenId // Acts like "add token" if this is missing + + /** If exists, means they are editing an existing custom token. + * If missing, then creating/adding a new token */ + tokenId?: EdgeTokenId walletId: string } @@ -138,45 +141,49 @@ function EditTokenSceneComponent(props: Props) { await wallet.currencyConfig.changeCustomToken(tokenId, customTokenInput) navigation.goBack() } else { + // Creating a new token const { currencyConfig } = wallet const { builtinTokens } = currencyConfig - // Check if custom token input conflicts with built-in tokens. - // There's currently no mechanism to obtain a new custom token's tokenId - // for proper comparison against built-in tokens besides physically adding - // the new custom token first. - const newTokenId = await currencyConfig.addCustomToken(customTokenInput) - - const matchingContractToken = - Object.keys(builtinTokens).find(builtinTokenId => builtinTokenId === newTokenId) == null ? undefined : builtinTokens[newTokenId] - const isMatchingCurrencyCode = Object.values(builtinTokens).find(builtInToken => builtInToken.currencyCode === currencyCode) != null + const newTokenId = await currencyConfig.getTokenId(customTokenInput) - if (matchingContractToken != null && isMatchingCurrencyCode) { - await showMessage(sprintf(lstrings.warning_token_exists_1s, currencyCode)) + // Check if custom token input conflicts with built-in tokens. + const matchingBuiltinTokenId = Object.keys(builtinTokens).find(builtinTokenId => builtinTokenId === newTokenId) + if (matchingBuiltinTokenId != null) { + await showMessage(sprintf(lstrings.warning_token_exists_1s, builtinTokens[matchingBuiltinTokenId].currencyCode)) return } - const warningMessage = - isMatchingCurrencyCode && matchingContractToken == null - ? sprintf(lstrings.warning_token_code_override_2s, currencyCode, config.supportEmail) - : matchingContractToken != null && !isMatchingCurrencyCode - ? sprintf(lstrings.warning_token_contract_override_3s, currencyCode, matchingContractToken.currencyCode, config.supportEmail) - : undefined - - const approveAdd = - warningMessage == null - ? true - : await Airship.show(bridge => ( - - )) + const isMatchingBuiltinCurrencyCode = Object.values(builtinTokens).find(builtInToken => builtInToken.currencyCode === currencyCode) != null + const approveAdd = !isMatchingBuiltinCurrencyCode + ? true + : await Airship.show(bridge => ( + + )) if (approveAdd) { + // Check if custom token input conflicts with custom tokens. + if (currencyConfig.customTokens[newTokenId] != null) { + // Always override changes to custom tokens + // TODO: Fine for if they are on this scene intentionally modifying a + // custom token, but maybe warn about this override if they are trying + // to add a new custom token with the same contract address as an + // existing custom token + await currencyConfig.changeCustomToken(newTokenId, customTokenInput) + } else { + await currencyConfig.addCustomToken(customTokenInput) + } + await wallet.changeEnabledTokenIds([...wallet.enabledTokenIds, newTokenId]) logActivity(`Add Custom Token: ${account.username} -- ${getWalletName(wallet)} -- ${wallet.type} -- ${newTokenId} -- ${currencyCode} -- ${decimals}`) - navigation.goBack() - } else { - await currencyConfig.removeCustomToken(newTokenId) } + navigation.goBack() } }) diff --git a/src/locales/en_US.ts b/src/locales/en_US.ts index 4366f0a01ad..ab60b65dde3 100644 --- a/src/locales/en_US.ts +++ b/src/locales/en_US.ts @@ -124,8 +124,6 @@ const strings = { warning_token_code_override_2s: 'The entered contract address differs from the contract address of built-in token %1$s. Please proceed with caution and verify the contract is legitimate as use of this token can result in loss of funds. If you have questions about this feature or contract please contact %2$s.', - warning_token_contract_override_3s: - 'The entered token %1$s exists as a built-in token %2$s with the same contract address. Please proceed with caution and verify the contract is legitimate as use of this token can result in loss of funds. If you have questions about this feature please contact %3$s.', warning_token_exists_1s: 'The entered token already exists as a built-in token %1$s', warning_uk_risk: `Don't invest unless you're prepared to lose all the money you invest. This is a high-risk investment and you should not expect to be protected if something goes wrong. Take 2 min to learn more.`, diff --git a/src/locales/strings/enUS.json b/src/locales/strings/enUS.json index fc9c8c1389b..5f847884f72 100644 --- a/src/locales/strings/enUS.json +++ b/src/locales/strings/enUS.json @@ -79,7 +79,6 @@ "warning_scam_message_no_1s": "Great, if you ever have any questions, please reach out to our support team at %1$s.", "warning_scam_message_yes_1s": "Please proceed with caution! Assistance with account creation has the potential for fraud. Users should never share passwords or private keys. Social media and chat platforms have been involved in attacks. Do not send cryptocurrency to strangers. If you believe you’re being taken advantage of, please contact our support team at %1$s.", "warning_token_code_override_2s": "The entered contract address differs from the contract address of built-in token %1$s. Please proceed with caution and verify the contract is legitimate as use of this token can result in loss of funds. If you have questions about this feature or contract please contact %2$s.", - "warning_token_contract_override_3s": "The entered token %1$s exists as a built-in token %2$s with the same contract address. Please proceed with caution and verify the contract is legitimate as use of this token can result in loss of funds. If you have questions about this feature please contact %3$s.", "warning_token_exists_1s": "The entered token already exists as a built-in token %1$s", "warning_uk_risk": "Don't invest unless you're prepared to lose all the money you invest. This is a high-risk investment and you should not expect to be protected if something goes wrong. Take 2 min to learn more.", "alert_dropdown_alert": "Alert! ",