import ENSSubdomainRegistry from 'Embark/contracts/ENSSubdomainRegistry'; import web3 from 'web3'; import ENSRegistry from 'Embark/contracts/ENSRegistry'; import React from 'react'; import { Button } from 'react-bootstrap'; import FieldGroup from '../standard/FieldGroup'; import { withFormik } from 'formik'; import { hash } from 'eth-ens-namehash'; import { debounce } from 'lodash/fp'; const { methods: { owner } } = ENSRegistry; const delay = debounce(500); const getDomain = (hashedDomain, domains) => domains(hashedDomain).call(); const registryIsOwner = address => address == ENSSubdomainRegistry._address; const fetchOwner = domainName => owner(hash(domainName)).call(); const debounceFetchOwner = delay(fetchOwner); const getAndIsOwner = async domainName => { const address = await debounceFetchOwner(domainName); return registryIsOwner(address); } const fetchDomain = delay(getDomain); const setPrice = (domainFn, hashedDomain, price) => domainFn(hashedDomain, price || 0).send(); const InnerForm = ({ values, errors, touched, handleChange, handleBlur, handleSubmit, isSubmitting, }) => (
) const AddDomain = withFormik({ mapPropsToValues: props => ({ domainName: '', domainPrice: '' }), async validate(values) { const { domainName } = values; const errors = {}; if (!domainName) errors.domainName = 'Required'; if (domainName && !await getAndIsOwner(domainName)) errors.domainName = 'This domain is not owned by registry'; if (Object.keys(errors).length) throw errors; }, async handleSubmit(values, { setSubmitting }) { const { domainName, domainPrice } = values; const { methods: { domains, setDomainPrice, updateDomainPrice } } = ENSSubdomainRegistry; const hashedDomain = hash(domainName); const { state } = await getDomain(hashedDomain, domains); console.log( 'Inputs for setPrice', Number(state) ? 'updateDomainPrice' : 'setDomainPrice', hashedDomain, web3.utils.toWei(domainPrice.toString(), 'ether'), ); setPrice( Number(state) ? updateDomainPrice : setDomainPrice, hashedDomain, web3.utils.toWei(domainPrice.toString(), 'ether'), ) .then(res => { setSubmitting(false); console.log(res); }) .catch(err => { setSubmitting(false); console.log(err); }) } })(InnerForm); export default AddDomain;