Merge pull request #936 from ethereum/showGasEstimation

showGasEstimation
pull/1/head
yann300 7 years ago committed by GitHub
commit 70546d72a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      .gitignore
  2. BIN
      assets/img/gasStation_50.png
  3. 23
      src/app/editor/contextView.js
  4. 60
      src/app/editor/contextualListener.js

1
.gitignore vendored

@ -7,3 +7,4 @@ npm-debug.log*
babelify-src
package-lock.json
remix
.DS_Store

Binary file not shown.

After

Width:  |  Height:  |  Size: 960 B

@ -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 {
<span class=${css.referencesnb}>${references}</span>
<i data-action='previous' class="fa fa-chevron-up ${css.jump}" aria-hidden="true" onclick=${jump}></i>
<i data-action='next' class="fa fa-chevron-down ${css.jump}" aria-hidden="true" onclick=${jump}></i>
${showGasEstimation()}
</div>`
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`<div class=${css.gasEstimation}>
<img class=${css.gasStationIcon} title='Gas estimation' src='assets/img/gasStation_50.png'>
${estimatedGas}
</div>`
}
}
}
}

@ -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

Loading…
Cancel
Save