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' ;
2018-06-11 17:18:59 +00:00
import React from 'react' ;
import { Button } from 'react-bootstrap' ;
2018-05-27 20:28:07 +00:00
import { withFormik } from 'formik' ;
2018-06-11 17:18:59 +00:00
import { hash } from 'eth-ens-namehash' ;
import { zeroAddress , zeroBytes32 } from './utils' ;
import FieldGroup from '../standard/FieldGroup' ;
2018-05-27 20:28:07 +00:00
2018-06-11 17:18:59 +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 ,
2018-06-11 17:18:59 +00:00
setFieldValue ,
2018-06-11 19:55:42 +00:00
subDomain ,
domainName ,
domainPrice ,
2018-05-27 20:28:07 +00:00
} ) => (
< form onSubmit = { handleSubmit } >
2018-06-11 19:55:42 +00:00
{ ! 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 }
2018-06-11 19:55:42 +00:00
/ > }
{ ! 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 = {
2018-06-11 17:18:59 +00:00
< Button
style = { { marginTop : '5px' } }
onClick = { ( ) => {
2018-05-28 20:51:05 +00:00
ENSSubdomainRegistry . methods . getPrice ( hash ( values . domainName ) )
. call ( )
2018-06-11 17:18:59 +00:00
. then ( ( res ) => { setFieldValue ( 'price' , res ) ; } ) ;
} }
>
2018-05-28 20:51:05 +00:00
Get Price
< / B u t t o n >
}
2018-06-11 19:55:42 +00:00
/ > }
{ ! domainPrice && < FieldGroup
2018-05-28 20:51:05 +00:00
id = "price"
name = "price"
label = "Domain Price"
disabled
2018-06-11 19:55:42 +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-06-11 19:55:42 +00:00
wide = "true"
2018-06-07 21:55:50 +00:00
/ >
2018-05-28 15:34:51 +00:00
< FieldGroup
id = "address"
name = "address"
type = "text"
2018-06-11 17:18:59 +00:00
label = "Ethereum 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-06-11 19:55:42 +00:00
validate ( values , props ) {
2018-05-28 15:34:51 +00:00
const errors = { } ;
2018-06-11 19:55:42 +00:00
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' ;
2018-05-28 15:34:51 +00:00
return errors ;
2018-06-07 21:55:50 +00:00
} ,
2018-06-11 19:55:42 +00:00
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 ;
2018-06-07 21:55:50 +00:00
const subdomainHash = soliditySha3 ( subDomain ) ;
const domainNameHash = hash ( domainName ) ;
const resolveToAddr = address || zeroAddress ;
2018-06-11 19:55:42 +00:00
const resolveToStatusAddr = statusAddress || zeroBytes32 ;
2018-06-07 21:55:50 +00:00
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 ,
2018-06-11 19:55:42 +00:00
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 ( ( ) => {
2018-06-11 19:55:42 +00:00
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
}
2018-06-11 17:18:59 +00:00
} ) ( InnerForm ) ;
2018-05-27 20:28:07 +00:00
export default RegisterSubDomain ;