Skip to content

Commit

Permalink
Use currencyConfig.getTokenId instead of addCustomToken hack
Browse files Browse the repository at this point in the history
Also remove case of overriding a built-in tokenId
  • Loading branch information
Jon-edge committed Sep 23, 2024
1 parent bafaddd commit 6b7c6e9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
63 changes: 35 additions & 28 deletions src/components/scenes/EditTokenScene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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<boolean>(bridge => (
<ConfirmContinueModal bridge={bridge} body={warningMessage} title={lstrings.string_warning} warning isSkippable />
))
const isMatchingBuiltinCurrencyCode = Object.values(builtinTokens).find(builtInToken => builtInToken.currencyCode === currencyCode) != null
const approveAdd = !isMatchingBuiltinCurrencyCode
? true
: await Airship.show<boolean>(bridge => (
<ConfirmContinueModal
bridge={bridge}
body={sprintf(lstrings.warning_token_code_override_2s, currencyCode, config.supportEmail)}
title={lstrings.string_warning}
warning
isSkippable
/>
))

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()
}
})

Expand Down
2 changes: 0 additions & 2 deletions src/locales/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`,

Expand Down
1 change: 0 additions & 1 deletion src/locales/strings/enUS.json
Original file line number Diff line number Diff line change
Expand Up @@ -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! ",
Expand Down

0 comments on commit 6b7c6e9

Please sign in to comment.