diff --git a/src/app.js b/src/app.js index adfc825cab..2ce9980ffd 100644 --- a/src/app.js +++ b/src/app.js @@ -475,7 +475,8 @@ var run = function () { callback(null, executionContext.web3().fromWei(balance, 'ether')) } }) - } + }, + currentblockGasLimit: () => { return executionContext.currentblockGasLimit() } } var renderer = new Renderer(rendererAPI, compiler.event) diff --git a/src/app/execution-context.js b/src/app/execution-context.js index ccba55f739..f14e91924d 100644 --- a/src/app/execution-context.js +++ b/src/app/execution-context.js @@ -123,6 +123,23 @@ function ExecutionContext () { } } + this.currentblockGasLimit = function () { + return this.blockGasLimit + } + + this.blockGasLimitDefault = 4300000 + this.blockGasLimit = this.blockGasLimitDefault + setInterval(() => { + web3.eth.getBlock('latest', (err, block) => { + if (!err) { + // we can't use the blockGasLimit cause the next blocks could have a lower limit : https://github.com/ethereum/remix/issues/506 + this.blockGasLimit = (block && block.gasLimit) ? Math.floor(block.gasLimit - (5 * block.gasLimit) / 1024) : this.blockGasLimitDefault + } else { + this.blockGasLimit = this.blockGasLimitDefault + } + }) + }, 15000) + function setProviderFromEndpoint (endpoint) { if (endpoint === 'ipc') { web3.setProvider(new web3.providers.IpcProvider()) diff --git a/src/app/renderer.js b/src/app/renderer.js index c97106950c..db39e6ae5e 100644 --- a/src/app/renderer.js +++ b/src/app/renderer.js @@ -111,6 +111,7 @@ Renderer.prototype.error = function (message, container, options) { } Renderer.prototype.contracts = function (data, source) { + var self = this var retrieveMetadataHash = function (bytecode) { var match = /a165627a7a72305820([0-9a-f]{64})0029$/.exec(bytecode) if (match) { @@ -210,7 +211,7 @@ Renderer.prototype.contracts = function (data, source) { code += '\n {' + '\n from: web3.eth.accounts[0], ' + "\n data: '0x" + bytecode + "', " + - "\n gas: '4700000'" + + "\n gas: '" + self.appAPI.currentblockGasLimit() + "'" + '\n }, function (e, contract){' + '\n console.log(e, contract);' + "\n if (typeof contract.address !== 'undefined') {" + @@ -313,7 +314,6 @@ Renderer.prototype.contracts = function (data, source) { return $('
').append(button).append(details) } - var self = this var renderOutputModifier = function (contractName, $contractOutput) { var contract = data.contracts[contractName] var ctrSource = self.appAPI.currentCompiledSourceCode() diff --git a/src/app/txRunner.js b/src/app/txRunner.js index 5c48b87f91..03b48b708f 100644 --- a/src/app/txRunner.js +++ b/src/app/txRunner.js @@ -69,30 +69,23 @@ TxRunner.prototype.execute = function () { if (err) { return callback(err, gasEstimation) } - self.web3.eth.getBlock('latest', function (err, block) { + var blockGasLimit = self.executionContext.currentblockGasLimit() + // NOTE: estimateGas very likely will return a large limit if execution of the code failed + // we want to be able to run the code in order to debug and find the cause for the failure + if (gasEstimation > gasLimit) { + return callback('Gas required exceeds limit: ' + gasLimit) + } + if (gasEstimation > blockGasLimit) { + return callback('Gas required exceeds block gas limit: ' + gasLimit) + } + tx.gas = gasEstimation + var sendTransaction = self.personalMode ? self.web3.personal.sendTransaction : self.web3.eth.sendTransaction + sendTransaction(tx, function (err, resp) { if (err) { - return callback(err) - } else { - // NOTE: estimateGas very likely will return a large limit if execution of the code failed - // we want to be able to run the code in order to debug and find the cause for the failure - // we can't use the blockGasLimit cause the next blocks could have a lower limit : https://github.com/ethereum/remix/issues/506 - var blockGasLimit = Math.floor(block.gasLimit - (5 * block.gasLimit) / 1024) - if (gasEstimation > gasLimit) { - return callback('Gas required exceeds limit: ' + gasLimit) - } - if (gasEstimation > blockGasLimit) { - return callback('Gas required exceeds block gas limit: ' + gasLimit) - } - tx.gas = gasEstimation - var sendTransaction = self.personalMode ? self.web3.personal.sendTransaction : self.web3.eth.sendTransaction - sendTransaction(tx, function (err, resp) { - if (err) { - return callback(err, resp) - } - - tryTillResponse(self.web3, resp, callback) - }) + return callback(err, resp) } + + tryTillResponse(self.web3, resp, callback) }) }) }