From c211430fbddc328551c76e8dfe899bc54285463d Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Mon, 13 Aug 2018 16:23:45 -0400 Subject: [PATCH] use secureSend for deploy --- lib/modules/blockchain_connector/index.js | 60 ++--------------------- lib/modules/ens/register.js | 10 ++-- lib/utils/secureSend.js | 7 ++- 3 files changed, 13 insertions(+), 64 deletions(-) diff --git a/lib/modules/blockchain_connector/index.js b/lib/modules/blockchain_connector/index.js index 9a4fc869..b2b6f245 100644 --- a/lib/modules/blockchain_connector/index.js +++ b/lib/modules/blockchain_connector/index.js @@ -3,6 +3,7 @@ const async = require('async'); const Provider = require('./provider.js'); const utils = require('../../utils/utils'); const constants = require('../../constants'); +const secureSend = require('../../utils/secureSend'); const WEB3_READY = 'web3Ready'; @@ -230,65 +231,10 @@ class BlockchainConnector { }).catch(cb); } - secureSend(toSend, params, cb) { - const self = this; - let hash; - let calledBacked = false; - - function callback(err, receipt) { - if (calledBacked) { - return; - } - if (!err && !receipt.contractAddress) { - return; // Not deployed yet. Need to wait - } - if (interval) { - clearInterval(interval); - } - calledBacked = true; - cb(err, 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 - } - self.web3.eth.getTransactionReceipt(hash, (err, receipt) => { - if (!err && !receipt) { - return; // Transaction is not yet complete - } - callback(err, receipt); - }); - }, 500); - - toSend.estimateGas() - .then(gasEstimated => { - params.gas = gasEstimated + 1000; - params.from = params.from || self.defaultAccount(); - return toSend.send(params, function(err, transactionHash) { - if (err) { - return callback(err); - } - hash = transactionHash; - }).on('receipt', function(receipt) { - if (receipt.contractAddress !== undefined) { - callback(null, receipt); - } - }).then(function(_contract) { - if (!hash) { - return; // Somehow we didn't get the receipt yet... Interval will catch it - } - self.web3.eth.getTransactionReceipt(hash, callback); - }).catch(callback); - }); - } - deployContractFromObject(deployContractObject, params, cb) { - this.secureSend(deployContractObject, { + secureSend(this.web3, deployContractObject, { from: params.from, gas: params.gas, gasPrice: params.gasPrice - }, cb); + }, true, cb); } determineDefaultAccount(cb) { diff --git a/lib/modules/ens/register.js b/lib/modules/ens/register.js index 6c6158f5..761eff9c 100644 --- a/lib/modules/ens/register.js +++ b/lib/modules/ens/register.js @@ -8,7 +8,7 @@ function registerSubDomain(ens, registrar, resolver, defaultAccount, subdomain, let transaction; - secureSend(web3, toSend, {from: defaultAccount}) + secureSend(web3, toSend, {from: defaultAccount}, false) // Set resolver for the node .then(transac => { if (transac.status !== "0x1" && transac.status !== "0x01" && transac.status !== true) { @@ -16,19 +16,19 @@ function registerSubDomain(ens, registrar, resolver, defaultAccount, subdomain, return callback('Failed to register. Check gas cost.'); } transaction = transac; - return secureSend(web3, ens.methods.setResolver(node, resolver.options.address), {from: defaultAccount}); + return secureSend(web3, ens.methods.setResolver(node, resolver.options.address), {from: defaultAccount}, false); }) // Set address for node .then(_result => { - return secureSend(web3, resolver.methods.setAddr(node, address), {from: defaultAccount}); + return secureSend(web3, resolver.methods.setAddr(node, address), {from: defaultAccount}, false); }) // Set resolver for the reverse node .then(_result => { - return secureSend(web3, ens.methods.setResolver(reverseNode, resolver.options.address), {from: defaultAccount}); + return secureSend(web3, ens.methods.setResolver(reverseNode, resolver.options.address), {from: defaultAccount}, false); }) // Set name for reverse node .then(_result => { - return secureSend(web3, resolver.methods.setName(reverseNode, subdomain + '.embark.eth'), {from: defaultAccount}); + return secureSend(web3, resolver.methods.setName(reverseNode, subdomain + '.embark.eth'), {from: defaultAccount}, false); }) .then(_result => { callback(null, transaction); diff --git a/lib/utils/secureSend.js b/lib/utils/secureSend.js index 0723a41a..4fa468b4 100644 --- a/lib/utils/secureSend.js +++ b/lib/utils/secureSend.js @@ -1,5 +1,5 @@ -function secureSend(web3, toSend, params, cb) { +function secureSend(web3, toSend, params, isContractDeploy, cb) { cb = cb || function(){}; return new Promise((resolve, reject) => { let hash; @@ -9,6 +9,9 @@ function secureSend(web3, toSend, params, cb) { if (calledBacked) { return; } + if (!err && (isContractDeploy && !receipt.contractAddress)) { + return; // Not deployed yet. Need to wait + } if (interval) { clearInterval(interval); } @@ -43,7 +46,7 @@ function secureSend(web3, toSend, params, cb) { } hash = transactionHash; }).on('receipt', function(receipt) { - if (receipt.contractAddress) { + if (!isContractDeploy || receipt.contractAddress) { callback(null, receipt); } }).then(function(_contract) {