use secureSend for deploy

This commit is contained in:
Jonathan Rainville 2018-08-13 16:23:45 -04:00 committed by Iuri Matias
parent d6b814dc3e
commit c211430fbd
3 changed files with 13 additions and 64 deletions

View File

@ -3,6 +3,7 @@ const async = require('async');
const Provider = require('./provider.js'); const Provider = require('./provider.js');
const utils = require('../../utils/utils'); const utils = require('../../utils/utils');
const constants = require('../../constants'); const constants = require('../../constants');
const secureSend = require('../../utils/secureSend');
const WEB3_READY = 'web3Ready'; const WEB3_READY = 'web3Ready';
@ -230,65 +231,10 @@ class BlockchainConnector {
}).catch(cb); }).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) { deployContractFromObject(deployContractObject, params, cb) {
this.secureSend(deployContractObject, { secureSend(this.web3, deployContractObject, {
from: params.from, gas: params.gas, gasPrice: params.gasPrice from: params.from, gas: params.gas, gasPrice: params.gasPrice
}, cb); }, true, cb);
} }
determineDefaultAccount(cb) { determineDefaultAccount(cb) {

View File

@ -8,7 +8,7 @@ function registerSubDomain(ens, registrar, resolver, defaultAccount, subdomain,
let transaction; let transaction;
secureSend(web3, toSend, {from: defaultAccount}) secureSend(web3, toSend, {from: defaultAccount}, false)
// 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) {
@ -16,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 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 // Set address for node
.then(_result => { .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 // Set resolver for the reverse node
.then(_result => { .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 // Set name for reverse node
.then(_result => { .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 => { .then(_result => {
callback(null, transaction); callback(null, transaction);

View File

@ -1,5 +1,5 @@
function secureSend(web3, toSend, params, cb) { function secureSend(web3, toSend, params, isContractDeploy, cb) {
cb = cb || function(){}; cb = cb || function(){};
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let hash; let hash;
@ -9,6 +9,9 @@ function secureSend(web3, toSend, params, cb) {
if (calledBacked) { if (calledBacked) {
return; return;
} }
if (!err && (isContractDeploy && !receipt.contractAddress)) {
return; // Not deployed yet. Need to wait
}
if (interval) { if (interval) {
clearInterval(interval); clearInterval(interval);
} }
@ -43,7 +46,7 @@ function secureSend(web3, toSend, params, cb) {
} }
hash = transactionHash; hash = transactionHash;
}).on('receipt', function(receipt) { }).on('receipt', function(receipt) {
if (receipt.contractAddress) { if (!isContractDeploy || receipt.contractAddress) {
callback(null, receipt); callback(null, receipt);
} }
}).then(function(_contract) { }).then(function(_contract) {