add secureSend to get estimate gas and interval the transactions

This commit is contained in:
Jonathan Rainville 2018-08-13 15:01:34 -04:00 committed by Iuri Matias
parent 083c936351
commit d6b814dc3e
4 changed files with 73 additions and 12 deletions

View File

@ -1,4 +1,4 @@
/*global EmbarkJS, web3, registerSubDomain, namehash*/
/*global EmbarkJS, web3, registerSubDomain, namehash, secureSend*/
let __embarkENS = {};

View File

@ -147,6 +147,7 @@ class ENS {
registerConfigDomains(config, cb) {
const self = this;
const register = require('./register');
const secureSend = require('../../utils/secureSend');
self.events.request("blockchain:defaultAccount:get", (defaultAccount) => {
async.parallel([
function createRegistryContract(paraCb) {
@ -180,7 +181,7 @@ class ENS {
const address = self.registration.subdomains[subDomainName];
const reverseNode = utils.soliditySha3(address.toLowerCase().substr(2) + reverseAddrSuffix);
register(ens, registrar, resolver, defaultAccount, subDomainName, self.registration.rootDomain,
reverseNode, address, self.logger, eachCb);
reverseNode, address, self.logger, secureSend, eachCb);
}, cb);
});
@ -201,6 +202,7 @@ class ENS {
});
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 += "\nEmbarkJS.Names.registerProvider('ens', __embarkENS);";

View File

@ -1,16 +1,14 @@
/*global web3*/
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 node = namehash.hash(`${subdomain}.${rootDomain}`);
const toSend = registrar.methods.register(subnode, defaultAccount);
let transaction;
toSend.estimateGas()
// Register domain
.then(gasEstimated => {
return toSend.send({gas: gasEstimated + 1000, from: defaultAccount});
})
secureSend(web3, toSend, {from: defaultAccount})
// Set resolver for the node
.then(transac => {
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.');
}
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
.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
.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
.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 => {
callback(null, transaction);

61
lib/utils/secureSend.js Normal file
View File

@ -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;
}