|
|
|
@ -7,25 +7,35 @@ class Recorder { |
|
|
|
|
self._api = opts.api |
|
|
|
|
self.event = new EventManager() |
|
|
|
|
self.data = { journal: [], _pendingCreation: {} } |
|
|
|
|
opts.events.executioncontext.register('contextChanged', function () { |
|
|
|
|
self.clearAll() |
|
|
|
|
}) |
|
|
|
|
opts.events.executioncontext.register('contextChanged', () => self.clearAll()) |
|
|
|
|
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 = {} |
|
|
|
|
|
|
|
|
|
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) => { |
|
|
|
|
var { from, to, value, gas, data } = tx |
|
|
|
|
var record = { value, gas, data } |
|
|
|
|
self._api.getAccounts(function (err, accounts = []) { |
|
|
|
|
if (err) console.error(err) |
|
|
|
|
record.from = self._addressCache[from] || (self._addressCache[from] = `<account - ${getIndex(accounts, from)}>`) |
|
|
|
|
if (to) record.to = self._addressCache[to] || (self._addressCache[to] = `<account - ${getIndex(accounts, to)}>`) |
|
|
|
|
else self.data._pendingCreation[timestamp] = record |
|
|
|
|
getAddresses(addresses => { |
|
|
|
|
if (to) record.to = addresses[to] || (addresses[to] = self._addressCache[to] = `contract{${++counter}}`) |
|
|
|
|
else { |
|
|
|
|
record.src = getCurrentContractName() |
|
|
|
|
self.data._pendingCreation[timestamp] = record |
|
|
|
|
} |
|
|
|
|
record.from = addresses[from] || (addresses[from] = self._addressCache[from] = `account{${++counter}}`) |
|
|
|
|
self.append(timestamp, record) |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
@ -38,20 +48,32 @@ class Recorder { |
|
|
|
|
delete self.data._pendingCreation[timestamp] |
|
|
|
|
if (!record) return |
|
|
|
|
var to = args[2] |
|
|
|
|
self._api.getAccounts(function (err, accounts = []) { |
|
|
|
|
if (err) console.error(err) |
|
|
|
|
if (to) record.to = self._addressCache[to] || (self._addressCache[to] = `<contract - ${getIndex(accounts, to)}>`) |
|
|
|
|
getAddresses(addresses => { |
|
|
|
|
if (to) { |
|
|
|
|
delete record.src |
|
|
|
|
record.to = addresses[to] || (addresses[to] = self._addressCache[to] = `account{${++counter}}`) |
|
|
|
|
} else record.src = getCurrentContractName() |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
resolveAddress (record, accounts) { |
|
|
|
|
if (record.to && record.to[0] === '<') record.to = accounts[record.to.split('>')[0].slice(11)] |
|
|
|
|
if (record.from && record.from[0] === '<') record.from = accounts[record.from.split('>')[0].slice(11)] |
|
|
|
|
// @TODO: change copy/paste to write and read from history file
|
|
|
|
|
resolveAddress (record, addresses) { |
|
|
|
|
// var getPseudoAddress = placeholder => placeholder.split(' ')[0]//.split('-')[1].slice(1)
|
|
|
|
|
var pseudos = Object.keys(addresses).reduce((pseudos, address) => { |
|
|
|
|
// 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: replace addresses with custom ones (maybe address mapping file?)
|
|
|
|
|
return record |
|
|
|
|
} |
|
|
|
|
append (timestamp, record) { |
|
|
|
@ -61,11 +83,14 @@ class Recorder { |
|
|
|
|
getAll () { |
|
|
|
|
var self = this |
|
|
|
|
var records = [].concat(self.data.journal) |
|
|
|
|
return records.sort((A, B) => { |
|
|
|
|
var stampA = A.timestamp |
|
|
|
|
var stampB = B.timestamp |
|
|
|
|
return stampA - stampB |
|
|
|
|
}) |
|
|
|
|
return { |
|
|
|
|
addresses: self._addressCache, |
|
|
|
|
transactions: records.sort((A, B) => { |
|
|
|
|
var stampA = A.timestamp |
|
|
|
|
var stampB = B.timestamp |
|
|
|
|
return stampA - stampB |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
clearAll () { |
|
|
|
|
var self = this |
|
|
|
|