aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts
diff options
context:
space:
mode:
authorDan Finlay <dan@danfinlay.com>2017-01-05 05:56:21 +0800
committerDan Finlay <dan@danfinlay.com>2017-01-05 05:56:21 +0800
commit08351f801abd1bfd2a8c7071c6a3b0bd40570a4e (patch)
treeb0664925160b8467b647342f58f2937e8d0b7bc9 /app/scripts
parentc22da8450cf5014fb2bf3c96e0df88c70aa39465 (diff)
downloadtangerine-wallet-browser-08351f801abd1bfd2a8c7071c6a3b0bd40570a4e.tar
tangerine-wallet-browser-08351f801abd1bfd2a8c7071c6a3b0bd40570a4e.tar.gz
tangerine-wallet-browser-08351f801abd1bfd2a8c7071c6a3b0bd40570a4e.tar.bz2
tangerine-wallet-browser-08351f801abd1bfd2a8c7071c6a3b0bd40570a4e.tar.lz
tangerine-wallet-browser-08351f801abd1bfd2a8c7071c6a3b0bd40570a4e.tar.xz
tangerine-wallet-browser-08351f801abd1bfd2a8c7071c6a3b0bd40570a4e.tar.zst
tangerine-wallet-browser-08351f801abd1bfd2a8c7071c6a3b0bd40570a4e.zip
Move eth-store@1.1.0 into local repo.
Diffstat (limited to 'app/scripts')
-rw-r--r--app/scripts/lib/eth-store.js132
1 files changed, 132 insertions, 0 deletions
diff --git a/app/scripts/lib/eth-store.js b/app/scripts/lib/eth-store.js
new file mode 100644
index 000000000..aeb5667e5
--- /dev/null
+++ b/app/scripts/lib/eth-store.js
@@ -0,0 +1,132 @@
+const EventEmitter = require('events').EventEmitter
+const inherits = require('util').inherits
+const async = require('async')
+const clone = require('clone')
+const EthQuery = require('eth-query')
+
+module.exports = EthereumStore
+
+
+inherits(EthereumStore, EventEmitter)
+function EthereumStore(engine) {
+ const self = this
+ EventEmitter.call(self)
+ self._currentState = {
+ accounts: {},
+ transactions: {},
+ }
+ self._query = new EthQuery(engine)
+
+ engine.on('block', self._updateForBlock.bind(self))
+}
+
+//
+// public
+//
+
+EthereumStore.prototype.getState = function(){
+ const self = this
+ return clone(self._currentState)
+}
+
+EthereumStore.prototype.addAccount = function(address){
+ const self = this
+ self._currentState.accounts[address] = {}
+ self._didUpdate()
+ if (!self.currentBlockNumber) return
+ self._updateAccountForBlock(self.currentBlockNumber, address, noop)
+}
+
+EthereumStore.prototype.removeAccount = function(address){
+ const self = this
+ delete self._currentState.accounts[address]
+ self._didUpdate()
+}
+
+EthereumStore.prototype.addTransaction = function(txHash){
+ const self = this
+ self._currentState.transactions[txHash] = {}
+ self._didUpdate()
+ if (!self.currentBlockNumber) return
+ self._updateTransaction(self.currentBlockNumber, txHash, noop)
+}
+
+EthereumStore.prototype.removeTransaction = function(address){
+ const self = this
+ delete self._currentState.transactions[address]
+ self._didUpdate()
+}
+
+
+//
+// private
+//
+
+EthereumStore.prototype._didUpdate = function() {
+ const self = this
+ var state = self.getState()
+ self.emit('update', state)
+}
+
+EthereumStore.prototype._updateForBlock = function(block) {
+ const self = this
+ var blockNumber = '0x'+block.number.toString('hex')
+ self.currentBlockNumber = blockNumber
+ async.parallel([
+ self._updateAccountsForBlock.bind(self, blockNumber),
+ self._updateTransactions.bind(self, blockNumber),
+ ], function(err){
+ if (err) return console.error(err)
+ self.emit('block', self.getState())
+ })
+}
+
+EthereumStore.prototype._updateAccountsForBlock = function(block, cb) {
+ const self = this
+ var accountsState = self._currentState.accounts
+ var addresses = Object.keys(accountsState)
+ async.each(addresses, self._updateAccountForBlock.bind(self, block), cb)
+}
+
+EthereumStore.prototype._updateAccountForBlock = function(block, address, cb) {
+ const self = this
+ var accountsState = self._currentState.accounts
+ self._query.getAccount(address, block, function(err, result){
+ if (err) return cb(err)
+ result.address = address
+ // only populate if the entry is still present
+ if (accountsState[address]) {
+ accountsState[address] = result
+ self._didUpdate()
+ }
+ cb(null, result)
+ })
+}
+
+EthereumStore.prototype._updateTransactions = function(block, cb) {
+ const self = this
+ var transactionsState = self._currentState.transactions
+ var txHashes = Object.keys(transactionsState)
+ async.each(txHashes, self._updateTransaction.bind(self, block), cb)
+}
+
+EthereumStore.prototype._updateTransaction = function(block, txHash, cb) {
+ const self = this
+ // would use the block here to determine how many confirmations the tx has
+ var transactionsState = self._currentState.transactions
+ self._query.getTransaction(txHash, function(err, result){
+ if (err) return cb(err)
+ // only populate if the entry is still present
+ if (transactionsState[txHash]) {
+ transactionsState[txHash] = result
+ self._didUpdate()
+ }
+ cb(null, result)
+ })
+}
+
+function valuesFor(obj){
+ return Object.keys(obj).map(function(key){ return obj[key] })
+}
+
+function noop(){}