2018-05-28 15:34:51 +00:00
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 , { Fragment } from 'react' ;
2018-05-28 20:51:05 +00:00
import { Button , Input } 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-28 20:51:05 +00:00
const { soliditySha3 , sha3 } = web3 . utils ;
2018-05-27 20:28:07 +00:00
const InnerForm = ( {
values ,
errors ,
touched ,
handleChange ,
handleBlur ,
handleSubmit ,
isSubmitting ,
2018-05-28 17:58:51 +00:00
setFieldValue
2018-05-27 20:28:07 +00:00
} ) => (
< form onSubmit = { handleSubmit } >
< FieldGroup
id = "subDomain"
name = "subDomain"
type = "text"
label = "Sub Domain"
onChange = { handleChange }
onBlur = { handleBlur }
value = { values . subDomain }
error = { errors . subDomain }
/ >
< FieldGroup
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 = {
2018-05-29 20:51:19 +00:00
< 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 ) ; } )
} } >
Get Price
< / B u t t o n >
}
2018-05-27 20:28:07 +00:00
/ >
2018-05-28 20:51:05 +00:00
< FieldGroup
id = "price"
name = "price"
label = "Domain Price"
disabled
2018-05-30 19:00:54 +00:00
value = { values . price ? ` ${ Number ( values . price ) . toLocaleString ( ) } SNT ` : '' } / >
2018-06-07 21:55:50 +00:00
< FieldGroup
id = "statusAddress"
name = "statusAddress"
type = "text"
label = "Status messenger address domain resolves to"
onChange = { handleChange }
onBlur = { handleBlur }
value = { values . statusAddress }
error = { errors . statusAddress }
/ >
2018-05-28 15:34:51 +00:00
< FieldGroup
id = "address"
name = "address"
type = "text"
2018-06-07 21:55:50 +00:00
label = "Address domain resolves to (optional)"
2018-05-28 15:34:51 +00:00
onChange = { handleChange }
onBlur = { handleBlur }
value = { values . address }
error = { errors . address }
2018-05-28 17:58:51 +00:00
button = { < Button style = { { marginTop : '5px' } } onClick = { ( ) => setFieldValue ( 'address' , web3 . eth . defaultAccount ) } > Use My Primary Address < / B u t t o n > }
2018-05-28 15:34:51 +00:00
/ >
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)' } < / B u t t o n >
< / f o r m >
)
const RegisterSubDomain = withFormik ( {
2018-05-28 20:51:05 +00:00
mapPropsToValues : props => ( { subDomain : '' , domainName : '' , price : '' } ) ,
2018-05-28 15:34:51 +00:00
validate ( values ) {
const errors = { } ;
2018-05-28 20:51:05 +00:00
const { address , subDomain } = values ;
if ( address && ! web3 . utils . isAddress ( address ) ) errors . address = 'Not a valid address' ;
if ( ! subDomain ) errors . subDomain = 'Required' ;
2018-05-28 15:34:51 +00:00
return errors ;
2018-06-07 21:55:50 +00:00
} ,
2018-05-27 20:28:07 +00:00
handleSubmit ( values , { setSubmitting } ) {
2018-06-07 21:55:50 +00:00
const { subDomain , domainName , address , statusAddress } = values ;
2018-05-27 20:28:07 +00:00
const { methods : { register } } = ENSSubdomainRegistry ;
2018-06-07 21:55:50 +00:00
const subdomainHash = soliditySha3 ( subDomain ) ;
const domainNameHash = hash ( domainName ) ;
const resolveToAddr = address || zeroAddress ;
const toSend = register (
2018-05-31 04:59:18 +00:00
subdomainHash ,
domainNameHash ,
resolveToAddr ,
2018-05-27 20:28:07 +00:00
zeroBytes32 ,
2018-06-07 21:55:50 +00:00
zeroBytes32 ,
statusAddress || zeroBytes32
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 ( ( ) => {
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 )
export default RegisterSubDomain ;