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 { List } from 'immutable'
import { OnChange } from 'react-final-form-listeners'
import { FormSpy } from 'react-final-form'
import { withStyles } from '@material-ui/core/styles'
import Block from '~/components/layout/Block'
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 { type TokenProps } from '~/logic/tokens/store/model/token'
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'
type Props = {
@ -70,7 +71,7 @@ const AddCustomToken = (props: Props) => {
validate={composeValidators(
required,
mustBeEthereumAddress,
checkTokenExistenceAndSetFields(setFormValue),
addressIsTokenContract,
)}
placeholder="Token contract address*"
text="Token contract address*"

View File

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