WA-232 Adding token validation (deployed and real standard token)

This commit is contained in:
apanizo 2018-07-25 11:11:19 +02:00
parent 16674b79a6
commit 1ccb735494
1 changed files with 24 additions and 1 deletions

View File

@ -5,6 +5,10 @@ import TextField from '~/components/forms/TextField'
import { composeValidators, required, mustBeEthereumAddress, uniqueAddress } from '~/components/forms/validator'
import Block from '~/components/layout/Block'
import Heading from '~/components/layout/Heading'
import { promisify } from '~/utils/promisify'
import { getWeb3 } from '~/wallets/getWeb3'
import { EMPTY_DATA } from '~/wallets/ethTransactions'
import { getHumanFriendlyToken } from '~/routes/tokens/store/actions/fetchTokens'
type Props = {
addresses: string[],
@ -12,6 +16,25 @@ type Props = {
export const TOKEN_ADRESS_PARAM = 'tokenAddress'
export const token = async (tokenAddress: string) => {
const code = await promisify(cb => getWeb3().eth.getCode(tokenAddress, cb))
const isDeployed = code !== EMPTY_DATA
if (!isDeployed) {
return 'Specified address is not deployed on the current network'
}
const erc20Token = await getHumanFriendlyToken()
const instance = await erc20Token.at(tokenAddress)
const supply = await instance.totalSupply()
if (Number(supply) === 0) {
return 'Specified address is not a valid standard token'
}
return undefined
}
const FirstPage = ({ addresses }: Props) => () => (
<Block margin="md">
<Heading tag="h2" margin="lg">
@ -22,7 +45,7 @@ const FirstPage = ({ addresses }: Props) => () => (
name={TOKEN_ADRESS_PARAM}
component={TextField}
type="text"
validate={composeValidators(required, mustBeEthereumAddress, uniqueAddress(addresses))}
validate={composeValidators(required, mustBeEthereumAddress, uniqueAddress(addresses), token)}
placeholder="ERC20 Token Address*"
text="ERC20 Token Address"
/>