change validation for token address field

This commit is contained in:
Mikhail Mikheev 2019-04-24 12:48:34 +04:00
parent b46ba47516
commit 30228345d3
2 changed files with 10 additions and 19 deletions

View File

@ -2,6 +2,7 @@
import React, { useState } from 'react' import React, { useState } from 'react'
import { List } from 'immutable' import { List } from 'immutable'
import { OnChange } from 'react-final-form-listeners' import { OnChange } from 'react-final-form-listeners'
import { FormSpy } from 'react-final-form'
import { withStyles } from '@material-ui/core/styles' import { withStyles } from '@material-ui/core/styles'
import Block from '~/components/layout/Block' import Block from '~/components/layout/Block'
import Paragraph from '~/components/layout/Paragraph' import Paragraph from '~/components/layout/Paragraph'
@ -17,7 +18,7 @@ import Hairline from '~/components/layout/Hairline'
import { composeValidators, required, mustBeEthereumAddress } from '~/components/forms/validator' import { composeValidators, required, mustBeEthereumAddress } from '~/components/forms/validator'
import { type TokenProps } from '~/logic/tokens/store/model/token' import { type TokenProps } from '~/logic/tokens/store/model/token'
import TokenPlaceholder from '~/routes/safe/components/Balances/assets/token_placeholder.svg' import TokenPlaceholder from '~/routes/safe/components/Balances/assets/token_placeholder.svg'
import { checkTokenExistenceAndSetFields, INITIAL_FORM_STATE } from './validators' import { addressIsTokenContract, INITIAL_FORM_STATE } from './validators'
import { styles } from './style' import { styles } from './style'
type Props = { type Props = {
@ -70,7 +71,7 @@ const AddCustomToken = (props: Props) => {
validate={composeValidators( validate={composeValidators(
required, required,
mustBeEthereumAddress, mustBeEthereumAddress,
checkTokenExistenceAndSetFields(setFormValue), addressIsTokenContract,
)} )}
placeholder="Token contract address*" placeholder="Token contract address*"
text="Token contract address*" text="Token contract address*"

View File

@ -1,5 +1,5 @@
// @flow // @flow
import { fetchToken } from '~/logic/tokens/api' import { getWeb3 } from '~/logic/wallets/getWeb3'
export const INITIAL_FORM_STATE = { export const INITIAL_FORM_STATE = {
address: '', address: '',
@ -20,22 +20,12 @@ export const simpleMemoize = (fn: Function) => {
} }
} }
export const checkTokenExistenceAndSetFields = (updateForm: ?Function) => simpleMemoize(async (tokenAddress: string, anotherArgument, andAnotherone) => { // eslint-disable-next-line
const relayToken = await fetchToken(tokenAddress) export const addressIsTokenContract = simpleMemoize(async (tokenAddress: string) => {
const web3 = getWeb3()
const call = await web3.eth.call({ to: tokenAddress, data: web3.utils.sha3('totalSupply()') })
if (!relayToken.data.count) { if (call === '0x') {
return "Couldn't find the token" return 'Not a token address'
}
if (updateForm) {
const {
address, symbol, decimals, logoUri,
} = relayToken.data.results[0]
updateForm({
address,
symbol,
decimals: String(decimals),
logoUri,
})
} }
}) })