diff --git a/src/routes/safe/components/Balances/Tokens/index.jsx b/src/routes/safe/components/Balances/Tokens/index.jsx index c606d39d..2b2718a2 100644 --- a/src/routes/safe/components/Balances/Tokens/index.jsx +++ b/src/routes/safe/components/Balances/Tokens/index.jsx @@ -70,6 +70,7 @@ class Tokens extends React.Component { safeAddress={safeAddress} activeTokens={activeTokens} updateActiveTokens={updateActiveTokens} + tokens={tokens} /> )} diff --git a/src/routes/safe/components/Balances/Tokens/screens/AddCustomToken/index.jsx b/src/routes/safe/components/Balances/Tokens/screens/AddCustomToken/index.jsx index 44e9c68e..22322c1e 100644 --- a/src/routes/safe/components/Balances/Tokens/screens/AddCustomToken/index.jsx +++ b/src/routes/safe/components/Balances/Tokens/screens/AddCustomToken/index.jsx @@ -15,10 +15,10 @@ import GnoForm from '~/components/forms/GnoForm' import TextField from '~/components/forms/TextField' import Hairline from '~/components/layout/Hairline' import { composeValidators, required, mustBeEthereumAddress } from '~/components/forms/validator' -import { type TokenProps } from '~/logic/tokens/store/model/token' +import { type TokenProps, type Token } from '~/logic/tokens/store/model/token' import { setImageToPlaceholder } from '~/routes/safe/components/Balances/utils' import TokenPlaceholder from '~/routes/safe/components/Balances/assets/token_placeholder.svg' -import { addressIsTokenContract } from './validators' +import { addressIsTokenContract, doesntExistInTokenList } from './validators' import { styles } from './style' type Props = { @@ -27,6 +27,7 @@ type Props = { updateActiveTokens: Function, safeAddress: string, activeTokens: List, + tokens: List, setActiveScreen: Function, onClose: Function, } @@ -40,7 +41,7 @@ const INITIAL_FORM_STATE = { const AddCustomToken = (props: Props) => { const { - classes, setActiveScreen, onClose, addToken, updateActiveTokens, safeAddress, activeTokens, + classes, setActiveScreen, onClose, addToken, updateActiveTokens, safeAddress, activeTokens, tokens, } = props const [formValues, setFormValues] = useState(INITIAL_FORM_STATE) @@ -84,7 +85,7 @@ const AddCustomToken = (props: Props) => { name="address" component={TextField} type="text" - validate={composeValidators(required, mustBeEthereumAddress, addressIsTokenContract)} + validate={composeValidators(required, mustBeEthereumAddress, doesntExistInTokenList(tokens), addressIsTokenContract)} placeholder="Token contract address*" text="Token contract address*" className={classes.addressInput} diff --git a/src/routes/safe/components/Balances/Tokens/screens/AddCustomToken/validators.js b/src/routes/safe/components/Balances/Tokens/screens/AddCustomToken/validators.js index ba36cddf..21bde352 100644 --- a/src/routes/safe/components/Balances/Tokens/screens/AddCustomToken/validators.js +++ b/src/routes/safe/components/Balances/Tokens/screens/AddCustomToken/validators.js @@ -1,5 +1,8 @@ // @flow +import { List } from 'immutable' import { getWeb3 } from '~/logic/wallets/getWeb3' +import { type Token } from '~/logic/tokens/store/model/token' +import { sameAddress } from '~/logic/wallets/ethAddresses' export const simpleMemoize = (fn: Function) => { let lastArg @@ -22,3 +25,11 @@ export const addressIsTokenContract = simpleMemoize(async (tokenAddress: string) return 'Not a token address' } }) + +export const doesntExistInTokenList = (tokenList: List) => simpleMemoize((tokenAddress: string) => { + const tokenIndex = tokenList.findIndex(({ address }) => sameAddress(address, tokenAddress)) + + if (tokenIndex !== -1) { + return 'Token already exists in your token list' + } +})