From 67da4e6a84949f6a9d33b5c067210e8bef0efa03 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Fri, 13 Jul 2018 16:41:35 -0400 Subject: [PATCH] add function to register the domains from config --- lib/modules/ens/index.js | 43 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/lib/modules/ens/index.js b/lib/modules/ens/index.js index 79f051ac..5dbb16f9 100644 --- a/lib/modules/ens/index.js +++ b/lib/modules/ens/index.js @@ -5,7 +5,6 @@ const async = require('async'); class ENS { constructor(embark, _options) { - const self = this; this.logger = embark.logger; this.events = embark.events; this.namesConfig = embark.config.namesystemConfig; @@ -14,7 +13,11 @@ class ENS { this.addENSToEmbarkJS(); this.configureContracts(); + this.registerEvents(); + } + registerEvents() { + const self = this; self.embark.registerActionForEvent("contracts:deploy:afterAll", (cb) => { async.parallel([ function getENSRegistry(paraCb) { @@ -28,6 +31,7 @@ class ENS { }); } ], (err, results) => { + // result[0] => ENSRegistry; result[1] => FIFSRegistrar let config = { registryAbi: results[0].abiDefinition, registryAddress: results[0].deployedAddress, @@ -35,11 +39,42 @@ class ENS { registrarAddress: results[1].deployedAddress }; self.addSetProvider(config); - cb(); + + if (!self.registration || !self.registration.domains || !Object.keys(self.registration.domains).length) { + return cb(); + } + self.registerConfigDomains(config, cb); }); }); } + registerConfigDomains(config, cb) { + const self = this; + self.events.request("blockchain:defaultAccount:get", (defaultAccount) => { + self.events.request("blockchain:contract:create", + {abi: config.registrarAbi, address: config.registrarAddress}, + (registrar) => { + async.each(Object.keys(self.registration.domains), (subDomainName, eachCb) => { + const toSend = registrar.methods.register(utils.sha3(subDomainName), + defaultAccount, self.registration.domains[subDomainName]); + + toSend.estimateGas().then(gasEstimated => { + return toSend.send({gas: gasEstimated + 1000, from: defaultAccount}).then(transaction => { + if (transaction.status !== "0x1" && transaction.status !== "0x01" && transaction.status !== true) { + return eachCb('Failed to register. Check gas cost.'); + } + eachCb(null, transaction); + }).catch(err => { + eachCb('Failed to register with error: ' + (err.message || err)); + }); + }).catch(err => { + eachCb("Register would error. Is it already registered? Do you have token balance? Is Allowance set? " + (err.message || err)); + }); + }, cb); + }); + }); + } + addENSToEmbarkJS() { const self = this; // TODO: make this a shouldAdd condition @@ -52,10 +87,10 @@ class ENS { } // get namehash, import it into file - self.events.request("version:get:eth-ens-namehash", function(EnsNamehashVersion) { + self.events.request("version:get:eth-ens-namehash", function (EnsNamehashVersion) { let currentEnsNamehashVersion = require('../../../package.json').dependencies["eth-ens-namehash"]; if (EnsNamehashVersion !== currentEnsNamehashVersion) { - self.events.request("version:getPackageLocation", "eth-ens-namehash", EnsNamehashVersion, function(err, location) { + self.events.request("version:getPackageLocation", "eth-ens-namehash", EnsNamehashVersion, function (err, location) { self.embark.registerImportFile("eth-ens-namehash", fs.dappPath(location)); }); }