Merge pull request #985 from gnosis/fix/#983-support-ens-for-contract-interaction

(Fix) [Contract Interaction] Support ENS for contract address
This commit is contained in:
Mikhail Mikheev 2020-06-09 18:11:28 +04:00 committed by GitHub
commit 8f9cd58d4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 8 deletions

View File

@ -5,8 +5,7 @@ import { OnChange } from 'react-final-form-listeners'
import TextField from 'src/components/forms/TextField' import TextField from 'src/components/forms/TextField'
import { composeValidators, mustBeEthereumAddress, required } from 'src/components/forms/validator' import { composeValidators, mustBeEthereumAddress, required } from 'src/components/forms/validator'
import { getAddressFromENS } from 'src/logic/wallets/getWeb3' import { getAddressFromENS } from 'src/logic/wallets/getWeb3'
import { isValidEnsName } from 'src/logic/wallets/ethAddresses'
const isValidEnsName = (name) => /^([\w-]+\.)+(eth|test|xyz|luxe)$/.test(name)
// an idea for second field was taken from here // an idea for second field was taken from here
// https://github.com/final-form/react-final-form-listeners/blob/master/src/OnBlur.js // https://github.com/final-form/react-final-form-listeners/blob/master/src/OnBlur.js

View File

@ -43,3 +43,5 @@ export const isUserOwner = (safe, userAccount) => {
} }
export const isUserOwnerOnAnySafe = (safes, userAccount) => safes.some((safe) => isUserOwner(safe, userAccount)) export const isUserOwnerOnAnySafe = (safes, userAccount) => safes.some((safe) => isUserOwner(safe, userAccount))
export const isValidEnsName = (name) => /^([\w-]+\.)+(eth|test|xyz|luxe)$/.test(name)

View File

@ -12,6 +12,7 @@ import Identicon from 'src/components/Identicon'
import { mustBeEthereumAddress, mustBeEthereumContractAddress } from 'src/components/forms/validator' import { mustBeEthereumAddress, mustBeEthereumContractAddress } from 'src/components/forms/validator'
import { getAddressBookListSelector } from 'src/logic/addressBook/store/selectors' import { getAddressBookListSelector } from 'src/logic/addressBook/store/selectors'
import { getAddressFromENS } from 'src/logic/wallets/getWeb3' import { getAddressFromENS } from 'src/logic/wallets/getWeb3'
import { isValidEnsName } from 'src/logic/wallets/ethAddresses'
const textFieldLabelStyle = makeStyles(() => ({ const textFieldLabelStyle = makeStyles(() => ({
root: { root: {
@ -38,8 +39,6 @@ const filterAddressBookWithContractAddresses = async (addressBook) => {
return addressBook.filter((adbkEntry, index) => abFlags[index]) return addressBook.filter((adbkEntry, index) => abFlags[index])
} }
const isValidEnsName = (name) => /^([\w-]+\.)+(eth|test|xyz|luxe)$/.test(name)
const AddressBookInput = ({ const AddressBookInput = ({
classes, classes,
fieldMutator, fieldMutator,

View File

@ -18,7 +18,7 @@ import Header from './Header'
import MethodsDropdown from './MethodsDropdown' import MethodsDropdown from './MethodsDropdown'
import RenderInputParams from './RenderInputParams' import RenderInputParams from './RenderInputParams'
import RenderOutputParams from './RenderOutputParams' import RenderOutputParams from './RenderOutputParams'
import { abiExtractor, createTxObject, formMutators, handleSubmitError, isReadMethod } from './utils' import { abiExtractor, createTxObject, formMutators, handleSubmitError, isReadMethod, ensResolver } from './utils'
const useStyles = makeStyles(styles) const useStyles = makeStyles(styles)
@ -73,7 +73,7 @@ const ContractInteraction = ({ contractAddress, initialValues, onClose, onNext }
<Header onClose={onClose} subTitle="1 of 2" title="Contract Interaction" /> <Header onClose={onClose} subTitle="1 of 2" title="Contract Interaction" />
<Hairline /> <Hairline />
<GnoForm <GnoForm
decorators={[abiExtractor]} decorators={[abiExtractor, ensResolver]}
formMutators={formMutators} formMutators={formMutators}
initialValues={initialValues} initialValues={initialValues}
onSubmit={handleSubmit} onSubmit={handleSubmit}

View File

@ -6,8 +6,9 @@ import { mustBeEthereumAddress, mustBeEthereumContractAddress } from 'src/compon
import { getNetwork } from 'src/config' import { getNetwork } from 'src/config'
import { getConfiguredSource } from 'src/logic/contractInteraction/sources' import { getConfiguredSource } from 'src/logic/contractInteraction/sources'
import { AbiItemExtended } from 'src/logic/contractInteraction/sources/ABIService' import { AbiItemExtended } from 'src/logic/contractInteraction/sources/ABIService'
import { getWeb3 } from 'src/logic/wallets/getWeb3' import { getAddressFromENS, getWeb3 } from 'src/logic/wallets/getWeb3'
import { TransactionReviewType } from '../Review' import { TransactionReviewType } from '../Review'
import { isValidEnsName } from 'src/logic/wallets/ethAddresses'
export const NO_CONTRACT = 'no contract' export const NO_CONTRACT = 'no contract'
@ -20,7 +21,7 @@ export const abiExtractor = createDecorator({
mustBeEthereumAddress(contractAddress) || mustBeEthereumAddress(contractAddress) ||
(await mustBeEthereumContractAddress(contractAddress)) (await mustBeEthereumContractAddress(contractAddress))
) { ) {
return NO_CONTRACT return
} }
const network = getNetwork() const network = getNetwork()
const source = getConfiguredSource() const source = getConfiguredSource()
@ -29,6 +30,26 @@ export const abiExtractor = createDecorator({
}, },
}) })
export const ensResolver = createDecorator({
field: 'contractAddress',
updates: {
contractAddress: async (contractAddress) => {
try {
const resolvedAddress = isValidEnsName(contractAddress) && (await getAddressFromENS(contractAddress))
if (resolvedAddress) {
return resolvedAddress
}
} catch (e) {
console.error(e.message)
return contractAddress
}
return contractAddress
},
},
})
export const formMutators = { export const formMutators = {
setMax: (args, state, utils) => { setMax: (args, state, utils) => {
utils.changeValue(state, 'value', () => args[0]) utils.changeValue(state, 'value', () => args[0])