diff --git a/.gitignore b/.gitignore index 349416d020..99ccc8b448 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ npm-debug.log* babelify-src package-lock.json remix +.DS_Store diff --git a/assets/img/gasStation_50.png b/assets/img/gasStation_50.png new file mode 100644 index 0000000000..6758609385 Binary files /dev/null and b/assets/img/gasStation_50.png differ diff --git a/src/app/editor/contextView.js b/src/app/editor/contextView.js index b4a3df4485..56beacaab5 100644 --- a/src/app/editor/contextView.js +++ b/src/app/editor/contextView.js @@ -42,6 +42,15 @@ var css = csjs` float : right; margin-left : 15px; } + .gasEstimation { + margin-left: 15px; + display: flex; + align-items: center; + } + .gasStationIcon { + height: 13px; + margin-right: 5px; + } ` /* @@ -159,7 +168,21 @@ class ContextView { ${references} + ${showGasEstimation()} ` + + function showGasEstimation () { + if (node.name === 'FunctionDefinition') { + var result = self._api.contextualListener.gasEstimation(node) + var executionCost = 'Execution cost: ' + result.executionCost + ' gas' + var codeDepositCost = 'Code deposit cost: ' + result.codeDepositCost + ' gas' + var estimatedGas = result.codeDepositCost ? `${codeDepositCost}, ${executionCost}` : `${executionCost}` + return yo`
+ + ${estimatedGas} +
` + } + } } } diff --git a/src/app/editor/contextualListener.js b/src/app/editor/contextualListener.js index 914d16bcb8..4f2ea3865d 100644 --- a/src/app/editor/contextualListener.js +++ b/src/app/editor/contextualListener.js @@ -39,9 +39,6 @@ class ContextualListener { getActiveHighlights () { return [...this._activeHighlights] - // return [...this._activeHighlights].sort((a,b) => { - // return a.position.start - b.position.start - // }) } declarationOf (node) { @@ -67,6 +64,7 @@ class ContextualListener { this.currentFile = file if (compilationResult && compilationResult.data && compilationResult.data.sources[file]) { var nodes = this.sourceMappingDecoder.nodesAtPosition(null, cursorPosition, compilationResult.data.sources[file]) + this.nodes = nodes if (nodes && nodes.length && nodes[nodes.length - 1]) { this._highlightExpressions(nodes[nodes.length - 1], compilationResult) } @@ -122,6 +120,7 @@ class ContextualListener { highlights(node.id) this._highlight(node, compilationResult) } + this.results = compilationResult } _stopHighlighting () { @@ -130,6 +129,61 @@ class ContextualListener { } this._activeHighlights = [] } + + gasEstimation (node) { + this._loadContractInfos(node) + var executionCost + var codeDepositCost + if (node.name === 'FunctionDefinition') { + var visibility = node.attributes.visibility + if (!node.attributes.isConstructor) { + var fnName = node.attributes.name + var fn = fnName + this._getInputParams(node) + if (visibility === 'public' || visibility === 'external') { + executionCost = this.estimationObj.external[fn] + } else if (visibility === 'private' || visibility === 'internal') { + executionCost = this.estimationObj.internal[fn] + } + } else { + executionCost = this.creationCost + codeDepositCost = this.codeDepositCost + } + } else { + executionCost = '-' + } + return {executionCost, codeDepositCost} + } + + _loadContractInfos (node) { + for (var i in this.nodes) { + if (this.nodes[i].id === node.attributes.scope) { + var contract = this.nodes[i] + this.contract = this.results.data.contracts[this.results.source.target][contract.attributes.name] + this.estimationObj = this.contract.evm.gasEstimates + this.creationCost = this.estimationObj.creation.totalCost + this.codeDepositCost = this.estimationObj.creation.codeDepositCost + } + } + } + + _getInputParams (node) { + var params = [] + for (var i in node.children) { + if (node.children[i].name === 'ParameterList') { + var target = node.children[i] + break + } + } + if (target) { + var children = target.children + for (var j in children) { + if (children[j].name === 'VariableDeclaration') { + params.push(children[j].attributes.type) + } + } + } + return '(' + params.toString() + ')' + } } module.exports = ContextualListener