UPDATE save/run transaction from copy2clipboard to files

pull/3094/head
serapath 7 years ago committed by yann300
parent 23963cfba1
commit ee09cb822d
  1. 39
      src/app/tabs/run-tab.js
  2. 71
      src/recorder.js
  3. 6
      src/universal-dapp.js

@ -195,18 +195,8 @@ function runTab (container, appAPI, appEvents, opts) {
` `
container.appendChild(el) container.appendChild(el)
function addInstance (result) { function addInstance (sourcename) {
// { var contract = appAPI.getContract(sourcename)
// "result": {
// "gasUsed": "5318",
// "vm": { "exception": 1, "selfdestruct": {} },
// "bloom": { "bitvector": { "type": "Buffer", "data": [0, /* ... */ 0, 0] } },
// "amountSpent": "5318"
// },
// "transactionHash": "0x84f68f96944a47b27af4b4ed1986637aa1bc05fd7a6f5cb1d6a53f68058276d8"
// }
var contractNames = document.querySelector(`.${css.contractNames.classNames[0]}`)
var contract = appAPI.getContract(contractNames.children[contractNames.selectedIndex].innerHTML)
var address = self._view.atAddressButtonInput.value var address = self._view.atAddressButtonInput.value
var instance = udapp.renderInstance(contract.object, address, self._view.selectContractNames.value) var instance = udapp.renderInstance(contract.object, address, self._view.selectContractNames.value)
instanceContainer.appendChild(instance) instanceContainer.appendChild(instance)
@ -322,22 +312,27 @@ function makeRecorder (self, appAPI, appEvents) {
var json = appAPI.filesProviders['browser'].get(filename) var json = appAPI.filesProviders['browser'].get(filename)
if (!json) return modalDialogCustom.alert('Could not find file with transactions, please try again') if (!json) return modalDialogCustom.alert('Could not find file with transactions, please try again')
try { try {
var txArray = JSON.parse(json) var obj = JSON.parse(json)
var txArray = obj.transactions || []
var addresses = obj.addresses || {}
} catch (e) { } catch (e) {
modalDialogCustom.alert('Invalid JSON, please try again') modalDialogCustom.alert('Invalid JSON, please try again')
} }
if (txArray.length) { if (txArray.length) {
txArray.forEach(tx => { txArray.forEach(tx => {
udapp.getAccounts((err, accounts = []) => { var record = recorder.resolveAddress(tx.record, addresses)
if (err) console.error(err) udapp.rerunTx(record, function (err, result) {
tx.record = recorder.resolveAddress(tx.record, accounts) // {
udapp.rerunTx(tx.record, function (err, result) { // "result": {
// "gasUsed": "5318",
// "vm": { "exception": 1, "selfdestruct": {} },
// "bloom": { "bitvector": { "type": "Buffer", "data": [0, /* ... */ 0, 0] } },
// "amountSpent": "5318"
// },
// "transactionHash": "0x84f68f96944a47b27af4b4ed1986637aa1bc05fd7a6f5cb1d6a53f68058276d8"
// }
if (err) console.error(err) if (err) console.error(err)
else { else if (record.src) self.addInstance(record.src)
// at each callback call, if the transaction succeed and if this is a creation transaction, we should call
self.addInstance(result)
}
})
}) })
}) })
} }

@ -7,25 +7,35 @@ class Recorder {
self._api = opts.api self._api = opts.api
self.event = new EventManager() self.event = new EventManager()
self.data = { journal: [], _pendingCreation: {} } self.data = { journal: [], _pendingCreation: {} }
opts.events.executioncontext.register('contextChanged', function () { opts.events.executioncontext.register('contextChanged', () => self.clearAll())
self.clearAll()
})
var counter = 0 var counter = 0
function getIndex (accounts, address) {
var index
accounts.forEach((addr, idx) => { if (address === addr) index = idx })
if (!index) index = (++counter)
return index
}
self._addressCache = {} self._addressCache = {}
function getAddresses (cb) {
self._api.getAccounts(function (err, accounts = []) {
if (err) console.error(err)
var addresses = accounts.reduce((addr, account) => {
if (!addr[account]) addr[account] = `account{${++counter}}`
return addr
}, self._addressCache)
cb(addresses)
})
}
function getCurrentContractName () {
var contractNames = document.querySelector(`[class^="contractNames"]`)
var contractName = contractNames.children[contractNames.selectedIndex].innerHTML
return contractName
}
opts.events.udapp.register('initiatingTransaction', (timestamp, tx) => { opts.events.udapp.register('initiatingTransaction', (timestamp, tx) => {
var { from, to, value, gas, data } = tx var { from, to, value, gas, data } = tx
var record = { value, gas, data } var record = { value, gas, data }
self._api.getAccounts(function (err, accounts = []) { getAddresses(addresses => {
if (err) console.error(err) if (to) record.to = addresses[to] || (addresses[to] = self._addressCache[to] = `contract{${++counter}}`)
record.from = self._addressCache[from] || (self._addressCache[from] = `<account - ${getIndex(accounts, from)}>`) else {
if (to) record.to = self._addressCache[to] || (self._addressCache[to] = `<account - ${getIndex(accounts, to)}>`) record.src = getCurrentContractName()
else self.data._pendingCreation[timestamp] = record self.data._pendingCreation[timestamp] = record
}
record.from = addresses[from] || (addresses[from] = self._addressCache[from] = `account{${++counter}}`)
self.append(timestamp, record) self.append(timestamp, record)
}) })
}) })
@ -38,20 +48,32 @@ class Recorder {
delete self.data._pendingCreation[timestamp] delete self.data._pendingCreation[timestamp]
if (!record) return if (!record) return
var to = args[2] var to = args[2]
self._api.getAccounts(function (err, accounts = []) { getAddresses(addresses => {
if (err) console.error(err) if (to) {
if (to) record.to = self._addressCache[to] || (self._addressCache[to] = `<contract - ${getIndex(accounts, to)}>`) delete record.src
record.to = addresses[to] || (addresses[to] = self._addressCache[to] = `account{${++counter}}`)
} else record.src = getCurrentContractName()
}) })
}) })
} }
resolveAddress (record, accounts) { resolveAddress (record, addresses) {
if (record.to && record.to[0] === '<') record.to = accounts[record.to.split('>')[0].slice(11)] // var getPseudoAddress = placeholder => placeholder.split(' ')[0]//.split('-')[1].slice(1)
if (record.from && record.from[0] === '<') record.from = accounts[record.from.split('>')[0].slice(11)] var pseudos = Object.keys(addresses).reduce((pseudos, address) => {
// @TODO: change copy/paste to write and read from history file // var p = addresses[address]//getPseudoAddress()//.split('>')[0].split('-')[1].slice(1)
pseudos[addresses[address]] = address
return pseudos
}, {})
if (record.to && record.to[0] !== '0') record.to = pseudos[record.to]
if (record.from && record.from[0] !== '0') record.from = pseudos[record.from]
// @TODO: fix load transactions and execute !
// @TODO: add 'clean' button to clear all recorded transactions
// @TODO: prefix path with `browser/` or `localhost/` if user provides
// @TODO: offer users by default a "save path" prefixed with the currently open file in the editor
// @TODO: offer users by default a "load path" prefixed with the currently open file in the editor (show first one that comes)
// @TODO: writing browser test // @TODO: writing browser test
// @TODO: replace addresses with custom ones (maybe address mapping file?)
return record return record
} }
append (timestamp, record) { append (timestamp, record) {
@ -61,12 +83,15 @@ class Recorder {
getAll () { getAll () {
var self = this var self = this
var records = [].concat(self.data.journal) var records = [].concat(self.data.journal)
return records.sort((A, B) => { return {
addresses: self._addressCache,
transactions: records.sort((A, B) => {
var stampA = A.timestamp var stampA = A.timestamp
var stampB = B.timestamp var stampB = B.timestamp
return stampA - stampB return stampA - stampB
}) })
} }
}
clearAll () { clearAll () {
var self = this var self = this
self.data.journal = [] self.data.journal = []

@ -469,12 +469,10 @@ function execute (pipeline, env, callback) {
UniversalDApp.prototype.rerunTx = function (args, cb) { UniversalDApp.prototype.rerunTx = function (args, cb) {
var self = this var self = this
self.getAccounts(function (err, accounts = []) { var tx = { to: args.to, from: args.from, data: args.data, useCall: args.useCall }
if (err) console.error(err)
var pipeline = [queryGasLimit, runTransaction] var pipeline = [queryGasLimit, runTransaction]
var env = { self, args, tx: { to: args.to, from: args.from, data: args.data, useCall: args.useCall } } var env = { self, args, tx }
execute(pipeline, env, cb) execute(pipeline, env, cb)
})
} }
UniversalDApp.prototype.runTx = function (args, cb) { UniversalDApp.prototype.runTx = function (args, cb) {

Loading…
Cancel
Save