ens-usernames/app/components/ens/registerSubDomain.js

143 lines
4.5 KiB
JavaScript
Raw Normal View History

import web3 from "Embark/web3"
2018-05-27 20:28:07 +00:00
import ENSSubdomainRegistry from 'Embark/contracts/ENSSubdomainRegistry';
import ENSRegistry from 'Embark/contracts/ENSRegistry';
import React from 'react';
import { Button } from 'react-bootstrap';
2018-05-27 20:28:07 +00:00
import { withFormik } from 'formik';
import { hash } from 'eth-ens-namehash';
import { zeroAddress, zeroBytes32 } from './utils';
import FieldGroup from '../standard/FieldGroup';
2018-05-27 20:28:07 +00:00
const { soliditySha3 } = web3.utils;
2018-05-28 20:51:05 +00:00
2018-05-27 20:28:07 +00:00
const InnerForm = ({
values,
errors,
touched,
handleChange,
handleBlur,
handleSubmit,
isSubmitting,
setFieldValue,
subDomain,
domainName,
domainPrice,
2018-05-27 20:28:07 +00:00
}) => (
<form onSubmit={handleSubmit}>
{!subDomain && <FieldGroup
2018-05-27 20:28:07 +00:00
id="subDomain"
name="subDomain"
type="text"
label="Sub Domain"
onChange={handleChange}
onBlur={handleBlur}
value={values.subDomain}
error={errors.subDomain}
/>}
{!domainName && <FieldGroup
2018-05-27 20:28:07 +00:00
id="domainName"
name="domainName"
type="text"
label="Domain Name"
onChange={handleChange}
onBlur={handleBlur}
value={values.domainName}
2018-05-28 20:51:05 +00:00
button={
<Button
style={{ marginTop: '5px' }}
onClick={() => {
2018-05-28 20:51:05 +00:00
ENSSubdomainRegistry.methods.getPrice(hash(values.domainName))
.call()
.then((res) => { setFieldValue('price', res); });
}}
>
2018-05-28 20:51:05 +00:00
Get Price
</Button>
}
/>}
{!domainPrice && <FieldGroup
2018-05-28 20:51:05 +00:00
id="price"
name="price"
label="Domain Price"
disabled
value={values.price ? `${Number(values.price).toLocaleString()} SNT` : ''} />}
<FieldGroup
id="statusAddress"
name="statusAddress"
type="text"
label="Status messenger address domain resolves to"
onChange={handleChange}
onBlur={handleBlur}
value={values.statusAddress}
error={errors.statusAddress}
wide="true"
/>
<FieldGroup
id="address"
name="address"
type="text"
label="Ethereum address domain resolves to (optional)"
onChange={handleChange}
onBlur={handleBlur}
value={values.address}
error={errors.address}
button={<Button style={{ marginTop: '5px' }} onClick={() => setFieldValue('address', web3.eth.defaultAccount)}>Use My Primary Address</Button>}
/>
2018-05-27 20:28:07 +00:00
<Button bsStyle="primary" type="submit" disabled={isSubmitting || !!Object.keys(errors).length}>{!isSubmitting ? 'Submit' : 'Submitting to the Blockchain - (this may take awhile)'}</Button>
</form>
)
const RegisterSubDomain = withFormik({
2018-05-28 20:51:05 +00:00
mapPropsToValues: props => ({ subDomain: '', domainName: '', price: '' }),
validate(values, props) {
const errors = {};
const { address } = values;
const { subDomain } = props || values;
2018-05-28 20:51:05 +00:00
if (address && !web3.utils.isAddress(address)) errors.address = 'Not a valid address';
if (!subDomain) errors.subDomain = 'Required';
return errors;
},
handleSubmit(values, { setSubmitting, props }) {
const { address, statusAddress } = values;
const { subDomain, domainName, registeredCallbackFn } = props || values;
2018-05-27 20:28:07 +00:00
const { methods: { register } } = ENSSubdomainRegistry;
const subdomainHash = soliditySha3(subDomain);
const domainNameHash = hash(domainName);
const resolveToAddr = address || zeroAddress;
const resolveToStatusAddr = statusAddress || zeroBytes32;
const toSend = register(
2018-05-31 04:59:18 +00:00
subdomainHash,
domainNameHash,
resolveToAddr,
2018-05-27 20:28:07 +00:00
zeroBytes32,
zeroBytes32,
resolveToStatusAddr,
2018-05-31 04:59:18 +00:00
);
toSend.estimateGas().then(gasEstimated => {
console.log("Register would work. :D Gas estimated: "+gasEstimated)
console.log("Trying: register(\""+subdomainHash+"\",\""+domainNameHash+"\",\""+resolveToAddr+"\",\""+zeroBytes32+"\",\""+zeroBytes32+"\")")
toSend.send({gas: gasEstimated+1000}).then(txId => {
if(txId.status == "0x1" || txId.status == "0x01"){
console.log("Register send success. :)")
} else {
console.log("Register send errored. :( Out of gas? ")
}
console.dir(txId)
}).catch(err => {
console.log("Register send errored. :( Out of gas?")
console.dir(err)
}).finally(() => {
registeredCallbackFn(resolveToAddr, resolveToStatusAddr);
2018-05-27 20:28:07 +00:00
setSubmitting(false);
});
2018-05-31 04:59:18 +00:00
}).catch(err => {
console.log("Register would error. :/ Already Registered? Have Token Balance? Is Allowance set?")
console.dir(err)
setSubmitting(false);
});
2018-05-27 20:28:07 +00:00
}
})(InnerForm);
2018-05-27 20:28:07 +00:00
export default RegisterSubDomain;