Skip to content

Commit

Permalink
Merge pull request ethereum#598 from ethereum/resetValue
Browse files Browse the repository at this point in the history
Reset value field to 0 after each transaction
  • Loading branch information
yann300 authored Jun 20, 2017
2 parents 2c6d131 + aeda1db commit d3537b3
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 99 deletions.
167 changes: 90 additions & 77 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,20 +479,106 @@ var run = function () {
}
var renderer = new Renderer(rendererAPI, compiler.event)

// ------------------------------------------------------------
var executionContext = new ExecutionContext()

// ----------------- UniversalDApp -----------------
var udapp = new UniversalDApp(executionContext, {
removable: false,
removable_instances: true
})

udapp.event.register('debugRequested', this, function (txResult) {
startdebugging(txResult.transactionHash)
})

function swarmVerifiedPublish (content, expectedHash, cb) {
swarmgw.put(content, function (err, ret) {
if (err) {
cb(err)
} else if (ret !== expectedHash) {
cb('Hash mismatch')
} else {
cb()
}
})
}

function publishOnSwarm (contract, cb) {
// gather list of files to publish
var sources = []

sources.push({
content: contract.metadata,
hash: contract.metadataHash
})

var metadata
try {
metadata = JSON.parse(contract.metadata)
} catch (e) {
return cb(e)
}

if (metadata === undefined) {
return cb('No metadata')
}

Object.keys(metadata.sources).forEach(function (fileName) {
// find hash
var hash
try {
hash = metadata.sources[fileName].urls[0].match('bzzr://(.+)')[1]
} catch (e) {
return cb('Metadata inconsistency')
}

sources.push({
content: files.get(fileName),
hash: hash
})
})

// publish the list of sources in order, fail if any failed
async.eachSeries(sources, function (item, cb) {
swarmVerifiedPublish(item.content, item.hash, cb)
}, cb)
}

udapp.event.register('publishContract', this, function (contract) {
publishOnSwarm(contract, function (err) {
if (err) {
alert('Failed to publish metadata: ' + err)
} else {
alert('Metadata published successfully')
}
})
})

// ---------------- Righthand-panel --------------------
var rhpAPI = {
config: config,
onResize: onResize,
reAdjust: reAdjust,
renderer: renderer
warnCompilerLoading: (msg) => {
renderer.clear()
if (msg) {
renderer.error(msg, $('#output'), {type: 'warning'})
}
},
executionContextChange: (context) => {
return executionContext.executionContextChange(context)
},
executionContextProvider: () => {
return executionContext.getProvider()
}
}
var rhpEvents = {
compiler: compiler.event,
app: self.event
app: self.event,
udapp: udapp.event
}
var righthandPanel = new RighthandPanel(document.body, rhpAPI, rhpEvents, {}) // eslint-disable-line
// ------------------------------------------------------------
var executionContext = new ExecutionContext()
// ----------------- editor resize ---------------

function onResize () {
Expand Down Expand Up @@ -635,79 +721,6 @@ var run = function () {
transactionDebugger.addProvider('web3', executionContext.web3())
transactionDebugger.switchProvider(executionContext.getProvider())

// ----------------- UniversalDApp -----------------
var udapp = new UniversalDApp(executionContext, {
removable: false,
removable_instances: true
})

udapp.event.register('debugRequested', this, function (txResult) {
startdebugging(txResult.transactionHash)
})

function swarmVerifiedPublish (content, expectedHash, cb) {
swarmgw.put(content, function (err, ret) {
if (err) {
cb(err)
} else if (ret !== expectedHash) {
cb('Hash mismatch')
} else {
cb()
}
})
}

function publishOnSwarm (contract, cb) {
// gather list of files to publish
var sources = []

sources.push({
content: contract.metadata,
hash: contract.metadataHash
})

var metadata
try {
metadata = JSON.parse(contract.metadata)
} catch (e) {
return cb(e)
}

if (metadata === undefined) {
return cb('No metadata')
}

Object.keys(metadata.sources).forEach(function (fileName) {
// find hash
var hash
try {
hash = metadata.sources[fileName].urls[0].match('bzzr://(.+)')[1]
} catch (e) {
return cb('Metadata inconsistency')
}

sources.push({
content: files.get(fileName),
hash: hash
})
})

// publish the list of sources in order, fail if any failed
async.eachSeries(sources, function (item, cb) {
swarmVerifiedPublish(item.content, item.hash, cb)
}, cb)
}

udapp.event.register('publishContract', this, function (contract) {
publishOnSwarm(contract, function (err) {
if (err) {
alert('Failed to publish metadata: ' + err)
} else {
alert('Metadata published successfully')
}
})
})

// ----------------- StaticAnalysis -----------------
var staticAnalysisAPI = {
renderWarning: (label, warningContainer, type) => {
Expand Down
14 changes: 14 additions & 0 deletions src/app/contract-tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,19 @@ function contractTab (container, appAPI, appEvents, opts) {
<div id="output" class="${css.contract}"></div>
</div>
`
appEvents.udapp.register('transactionExecuted', (to, data, lookupOnly, txResult) => {
if (!lookupOnly) el.querySelector('#value').value = '0'
})

/* ---------------------------------------------------------------------------
DROPDOWN
--------------------------------------------------------------------------- */
var selectExEnv = el.querySelector('#selectExEnvOptions')
selectExEnv.addEventListener('change', function (event) {
if (!appAPI.executionContextChange(selectExEnv.options[selectExEnv.selectedIndex].value)) {
selectExEnv.value = appAPI.executionContextProvider()
}
})
selectExEnv.value = appAPI.executionContextProvider()
container.appendChild(el)
}
17 changes: 2 additions & 15 deletions src/app/execution-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ function ExecutionContext () {

this.setContext = function (context, endPointUrl) {
executionContext = context
executionContextChange(context, endPointUrl)
this.executionContextChange(context, endPointUrl)
}

function executionContextChange (context, endPointUrl) {
this.executionContextChange = function (context, endPointUrl) {
if (context === 'web3' && !confirm('Are you sure you want to connect to an ethereum node?')) {
return false
} else if (context === 'injected' && injectedProvider === undefined) {
Expand Down Expand Up @@ -131,19 +131,6 @@ function ExecutionContext () {
}
self.event.trigger('web3EndpointChanged')
}

/* ---------------------------------------------------------------------------
DROPDOWN
--------------------------------------------------------------------------- */

var selectExEnv = document.querySelector('#selectExEnvOptions')
selectExEnv.addEventListener('change', function (event) {
if (!executionContextChange(selectExEnv.options[selectExEnv.selectedIndex].value)) {
selectExEnv.value = executionContext
}
})

selectExEnv.value = executionContext
}

module.exports = ExecutionContext
7 changes: 1 addition & 6 deletions src/app/righthand-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@ function RighthandPanel (container, appAPI, events, opts) {

// ----------------- tabbed menu -----------------
var tabbedMenuAPI = {
warnCompilerLoading: function (msg) {
appAPI.renderer.clear()
if (msg) {
appAPI.renderer.error(msg, $('#output'), {type: 'warning'})
}
}
warnCompilerLoading: appAPI.warnCompilerLoading
}
// load tabbed menu component
var tabContainer // @TODO
Expand Down
3 changes: 2 additions & 1 deletion src/universal-dapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ UniversalDApp.prototype.getCallButton = function (args) {

var decoded
self.runTx({ to: args.address, data: data, useCall: lookupOnly }, function (err, txResult) {
self.event.trigger('transactionExecuted', [args.address, data, lookupOnly, txResult])
if (!txResult) {
replaceOutput($result, $('<span/>').text('callback contain no result ' + err).addClass('error'))
return
Expand Down Expand Up @@ -865,7 +866,7 @@ UniversalDApp.prototype.runTx = function (args, cb) {
// query value
function (callback) {
tx.value = 0

if (tx.useCall) return callback()
if (self.transactionContextAPI.getValue) {
self.transactionContextAPI.getValue(function (err, ret) {
if (err) {
Expand Down

0 comments on commit d3537b3

Please sign in to comment.