Add ethereum contract address validator
This commit is contained in:
parent
6372674fa6
commit
36b817d34f
|
@ -7,6 +7,7 @@ import {
|
|||
composeValidators,
|
||||
required,
|
||||
mustBeEthereumAddress,
|
||||
mustBeEthereumContractAddress,
|
||||
} from '~/components/forms/validator'
|
||||
import { getAddressFromENS } from '~/logic/wallets/getWeb3'
|
||||
|
||||
|
@ -19,6 +20,7 @@ type Props = {
|
|||
testId?: string,
|
||||
validators?: Function[],
|
||||
inputAdornment?: React.Element,
|
||||
mustBeContract?: boolean,
|
||||
}
|
||||
|
||||
const isValidEnsName = (name) => /^([\w-]+\.)+(eth|test|xyz|luxe)$/.test(name)
|
||||
|
@ -35,6 +37,7 @@ const AddressInput = ({
|
|||
testId,
|
||||
inputAdornment,
|
||||
validators = [],
|
||||
mustBeContract,
|
||||
}: Props): React.Element<*> => (
|
||||
<>
|
||||
<Field
|
||||
|
@ -44,6 +47,7 @@ const AddressInput = ({
|
|||
validate={composeValidators(
|
||||
required,
|
||||
mustBeEthereumAddress,
|
||||
mustBeContract && mustBeEthereumContractAddress,
|
||||
...validators,
|
||||
)}
|
||||
inputAdornment={inputAdornment}
|
||||
|
|
|
@ -66,6 +66,14 @@ export const mustBeEthereumAddress = simpleMemoize((address: Field) => {
|
|||
return isAddress ? undefined : 'Address should be a valid Ethereum address or ENS name'
|
||||
})
|
||||
|
||||
export const mustBeEthereumContractAddress = simpleMemoize(async (address: Field) => {
|
||||
const contractCode: string = await getWeb3().eth.getCode(address)
|
||||
|
||||
return (!contractCode || contractCode.replace('0x', '').replace(/0/g, '') === '')
|
||||
? 'Address should be a valid Ethereum contract address or ENS name'
|
||||
: undefined
|
||||
})
|
||||
|
||||
export const minMaxLength = (minLen: string | number, maxLen: string | number) => (value: string) => (value.length >= +minLen && value.length <= +maxLen ? undefined : `Should be ${minLen} to ${maxLen} symbols`)
|
||||
|
||||
export const ADDRESS_REPEATED_ERROR = 'Address already introduced'
|
||||
|
|
|
@ -99,6 +99,7 @@ const SendCustomTx = ({
|
|||
text="Recipient*"
|
||||
className={classes.addressInput}
|
||||
fieldMutator={mutators.setRecipient}
|
||||
mustBeContract
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
|
|
Loading…
Reference in New Issue