add secureSend to get estimate gas and interval the transactions
This commit is contained in:
parent
083c936351
commit
d6b814dc3e
|
@ -1,4 +1,4 @@
|
||||||
/*global EmbarkJS, web3, registerSubDomain, namehash*/
|
/*global EmbarkJS, web3, registerSubDomain, namehash, secureSend*/
|
||||||
|
|
||||||
let __embarkENS = {};
|
let __embarkENS = {};
|
||||||
|
|
||||||
|
|
|
@ -147,6 +147,7 @@ class ENS {
|
||||||
registerConfigDomains(config, cb) {
|
registerConfigDomains(config, cb) {
|
||||||
const self = this;
|
const self = this;
|
||||||
const register = require('./register');
|
const register = require('./register');
|
||||||
|
const secureSend = require('../../utils/secureSend');
|
||||||
self.events.request("blockchain:defaultAccount:get", (defaultAccount) => {
|
self.events.request("blockchain:defaultAccount:get", (defaultAccount) => {
|
||||||
async.parallel([
|
async.parallel([
|
||||||
function createRegistryContract(paraCb) {
|
function createRegistryContract(paraCb) {
|
||||||
|
@ -180,7 +181,7 @@ class ENS {
|
||||||
const address = self.registration.subdomains[subDomainName];
|
const address = self.registration.subdomains[subDomainName];
|
||||||
const reverseNode = utils.soliditySha3(address.toLowerCase().substr(2) + reverseAddrSuffix);
|
const reverseNode = utils.soliditySha3(address.toLowerCase().substr(2) + reverseAddrSuffix);
|
||||||
register(ens, registrar, resolver, defaultAccount, subDomainName, self.registration.rootDomain,
|
register(ens, registrar, resolver, defaultAccount, subDomainName, self.registration.rootDomain,
|
||||||
reverseNode, address, self.logger, eachCb);
|
reverseNode, address, self.logger, secureSend, eachCb);
|
||||||
}, cb);
|
}, cb);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@ -201,6 +202,7 @@ class ENS {
|
||||||
});
|
});
|
||||||
|
|
||||||
let code = fs.readFileSync(utils.joinPath(__dirname, 'register.js')).toString();
|
let code = fs.readFileSync(utils.joinPath(__dirname, 'register.js')).toString();
|
||||||
|
code += "\n" + fs.readFileSync(utils.joinPath(__dirname, '../../utils/secureSend.js')).toString();
|
||||||
code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString();
|
code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString();
|
||||||
code += "\nEmbarkJS.Names.registerProvider('ens', __embarkENS);";
|
code += "\nEmbarkJS.Names.registerProvider('ens', __embarkENS);";
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,14 @@
|
||||||
|
/*global web3*/
|
||||||
const namehash = require('eth-ens-namehash');
|
const namehash = require('eth-ens-namehash');
|
||||||
|
|
||||||
function registerSubDomain(ens, registrar, resolver, defaultAccount, subdomain, rootDomain, reverseNode, address, logger, callback) {
|
function registerSubDomain(ens, registrar, resolver, defaultAccount, subdomain, rootDomain, reverseNode, address, logger, secureSend, callback) {
|
||||||
const subnode = namehash.hash(subdomain);
|
const subnode = namehash.hash(subdomain);
|
||||||
const node = namehash.hash(`${subdomain}.${rootDomain}`);
|
const node = namehash.hash(`${subdomain}.${rootDomain}`);
|
||||||
const toSend = registrar.methods.register(subnode, defaultAccount);
|
const toSend = registrar.methods.register(subnode, defaultAccount);
|
||||||
let transaction;
|
let transaction;
|
||||||
|
|
||||||
toSend.estimateGas()
|
|
||||||
// Register domain
|
secureSend(web3, toSend, {from: defaultAccount})
|
||||||
.then(gasEstimated => {
|
|
||||||
return toSend.send({gas: gasEstimated + 1000, from: defaultAccount});
|
|
||||||
})
|
|
||||||
// Set resolver for the node
|
// Set resolver for the node
|
||||||
.then(transac => {
|
.then(transac => {
|
||||||
if (transac.status !== "0x1" && transac.status !== "0x01" && transac.status !== true) {
|
if (transac.status !== "0x1" && transac.status !== "0x01" && transac.status !== true) {
|
||||||
|
@ -18,19 +16,19 @@ function registerSubDomain(ens, registrar, resolver, defaultAccount, subdomain,
|
||||||
return callback('Failed to register. Check gas cost.');
|
return callback('Failed to register. Check gas cost.');
|
||||||
}
|
}
|
||||||
transaction = transac;
|
transaction = transac;
|
||||||
return ens.methods.setResolver(node, resolver.options.address).send({from: defaultAccount});
|
return secureSend(web3, ens.methods.setResolver(node, resolver.options.address), {from: defaultAccount});
|
||||||
})
|
})
|
||||||
// Set address for node
|
// Set address for node
|
||||||
.then(_result => {
|
.then(_result => {
|
||||||
return resolver.methods.setAddr(node, address).send({from: defaultAccount});
|
return secureSend(web3, resolver.methods.setAddr(node, address), {from: defaultAccount});
|
||||||
})
|
})
|
||||||
// Set resolver for the reverse node
|
// Set resolver for the reverse node
|
||||||
.then(_result => {
|
.then(_result => {
|
||||||
return ens.methods.setResolver(reverseNode, resolver.options.address).send({from: defaultAccount});
|
return secureSend(web3, ens.methods.setResolver(reverseNode, resolver.options.address), {from: defaultAccount});
|
||||||
})
|
})
|
||||||
// Set name for reverse node
|
// Set name for reverse node
|
||||||
.then(_result => {
|
.then(_result => {
|
||||||
return resolver.methods.setName(reverseNode, subdomain + '.embark.eth').send({from: defaultAccount});
|
return secureSend(web3, resolver.methods.setName(reverseNode, subdomain + '.embark.eth'), {from: defaultAccount});
|
||||||
})
|
})
|
||||||
.then(_result => {
|
.then(_result => {
|
||||||
callback(null, transaction);
|
callback(null, transaction);
|
||||||
|
|
|
@ -0,0 +1,61 @@
|
||||||
|
|
||||||
|
function secureSend(web3, toSend, params, cb) {
|
||||||
|
cb = cb || function(){};
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
let hash;
|
||||||
|
let calledBacked = false;
|
||||||
|
|
||||||
|
function callback(err, receipt) {
|
||||||
|
if (calledBacked) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (interval) {
|
||||||
|
clearInterval(interval);
|
||||||
|
}
|
||||||
|
calledBacked = true;
|
||||||
|
cb(err, receipt);
|
||||||
|
if (err) {
|
||||||
|
return reject(err);
|
||||||
|
}
|
||||||
|
resolve(receipt);
|
||||||
|
}
|
||||||
|
|
||||||
|
// This interval is there to compensate for the event that sometimes doesn't get triggered when using WebSocket
|
||||||
|
// FIXME The issue somehow only happens when the blockchain node is started in the same terminal
|
||||||
|
const interval = setInterval(() => {
|
||||||
|
if (!hash) {
|
||||||
|
return; // Wait until we receive the hash
|
||||||
|
}
|
||||||
|
web3.eth.getTransactionReceipt(hash, (err, receipt) => {
|
||||||
|
if (!err && !receipt) {
|
||||||
|
return; // Transaction is not yet complete
|
||||||
|
}
|
||||||
|
callback(err, receipt);
|
||||||
|
});
|
||||||
|
}, 100);
|
||||||
|
|
||||||
|
toSend.estimateGas()
|
||||||
|
.then(gasEstimated => {
|
||||||
|
params.gas = gasEstimated + 1000;
|
||||||
|
return toSend.send(params, function(err, transactionHash) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
hash = transactionHash;
|
||||||
|
}).on('receipt', function(receipt) {
|
||||||
|
if (receipt.contractAddress) {
|
||||||
|
callback(null, receipt);
|
||||||
|
}
|
||||||
|
}).then(function(_contract) {
|
||||||
|
if (!hash) {
|
||||||
|
return; // Somehow we didn't get the receipt yet... Interval will catch it
|
||||||
|
}
|
||||||
|
web3.eth.getTransactionReceipt(hash, callback);
|
||||||
|
}).catch(callback);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof module !== 'undefined' && module.exports) {
|
||||||
|
module.exports = secureSend;
|
||||||
|
}
|
Loading…
Reference in New Issue