From 083c936351ae5d553124b3ef8daa2231d913177a Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Mon, 13 Aug 2018 13:04:42 -0400 Subject: [PATCH] create separate function for secure send --- lib/modules/blockchain_connector/index.js | 45 ++++++++++++++--------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/lib/modules/blockchain_connector/index.js b/lib/modules/blockchain_connector/index.js index 378a8574c..9a4fc8695 100644 --- a/lib/modules/blockchain_connector/index.js +++ b/lib/modules/blockchain_connector/index.js @@ -230,7 +230,7 @@ class BlockchainConnector { }).catch(cb); } - deployContractFromObject(deployContractObject, params, cb) { + secureSend(toSend, params, cb) { const self = this; let hash; let calledBacked = false; @@ -263,28 +263,37 @@ class BlockchainConnector { }); }, 500); - deployContractObject.send({ + 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, { from: params.from, gas: params.gas, gasPrice: params.gasPrice - }, 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); + }, cb); } determineDefaultAccount(cb) { const self = this; - self.getAccounts(function (err, accounts) { + self.getAccounts(function(err, accounts) { if (err) { self.logger.error(err); return cb(new Error(err));