|
|
@ -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 = [] |
|
|
|