1- /* Ethereum Store
1+ /* Account Tracker
22 *
33 * This module is responsible for tracking any number of accounts
44 * and caching their current balances & transaction counts.
1010const async = require ( 'async' )
1111const EthQuery = require ( 'eth-query' )
1212const ObservableStore = require ( 'obs-store' )
13+ const EventEmitter = require ( 'events' ) . EventEmitter
1314function noop ( ) { }
1415
1516
16- class EthereumStore extends ObservableStore {
17+ class AccountTracker extends EventEmitter {
1718
1819 constructor ( opts = { } ) {
19- super ( {
20+ super ( )
21+
22+ const initState = {
2023 accounts : { } ,
21- transactions : { } ,
22- currentBlockNumber : '0' ,
23- currentBlockHash : '' ,
2424 currentBlockGasLimit : '' ,
25- } )
25+ }
26+ this . store = new ObservableStore ( initState )
27+
2628 this . _provider = opts . provider
2729 this . _query = new EthQuery ( this . _provider )
2830 this . _blockTracker = opts . blockTracker
@@ -37,88 +39,52 @@ class EthereumStore extends ObservableStore {
3739 //
3840
3941 addAccount ( address ) {
40- const accounts = this . getState ( ) . accounts
42+ const accounts = this . store . getState ( ) . accounts
4143 accounts [ address ] = { }
42- this . updateState ( { accounts } )
44+ this . store . updateState ( { accounts } )
4345 if ( ! this . _currentBlockNumber ) return
4446 this . _updateAccount ( address )
4547 }
4648
4749 removeAccount ( address ) {
48- const accounts = this . getState ( ) . accounts
50+ const accounts = this . store . getState ( ) . accounts
4951 delete accounts [ address ]
50- this . updateState ( { accounts } )
51- }
52-
53- addTransaction ( txHash ) {
54- const transactions = this . getState ( ) . transactions
55- transactions [ txHash ] = { }
56- this . updateState ( { transactions } )
57- if ( ! this . _currentBlockNumber ) return
58- this . _updateTransaction ( this . _currentBlockNumber , txHash , noop )
59- }
60-
61- removeTransaction ( txHash ) {
62- const transactions = this . getState ( ) . transactions
63- delete transactions [ txHash ]
64- this . updateState ( { transactions } )
52+ this . store . updateState ( { accounts } )
6553 }
6654
67-
6855 //
6956 // private
7057 //
7158
7259 _updateForBlock ( block ) {
7360 const blockNumber = '0x' + block . number . toString ( 'hex' )
7461 this . _currentBlockNumber = blockNumber
75- this . updateState ( { currentBlockNumber : parseInt ( blockNumber ) } )
76- this . updateState ( { currentBlockHash : `0x${ block . hash . toString ( 'hex' ) } ` } )
77- this . updateState ( { currentBlockGasLimit : `0x ${ block . gasLimit . toString ( 'hex' ) } ` } )
62+
63+ this . store . updateState ( { currentBlockGasLimit : `0x${ block . gasLimit . toString ( 'hex' ) } ` } )
64+
7865 async . parallel ( [
7966 this . _updateAccounts . bind ( this ) ,
80- this . _updateTransactions . bind ( this , blockNumber ) ,
8167 ] , ( err ) => {
8268 if ( err ) return console . error ( err )
83- this . emit ( 'block' , this . getState ( ) )
69+ this . emit ( 'block' , this . store . getState ( ) )
8470 } )
8571 }
8672
8773 _updateAccounts ( cb = noop ) {
88- const accounts = this . getState ( ) . accounts
74+ const accounts = this . store . getState ( ) . accounts
8975 const addresses = Object . keys ( accounts )
9076 async . each ( addresses , this . _updateAccount . bind ( this ) , cb )
9177 }
9278
9379 _updateAccount ( address , cb = noop ) {
94- const accounts = this . getState ( ) . accounts
9580 this . _getAccount ( address , ( err , result ) => {
9681 if ( err ) return cb ( err )
9782 result . address = address
83+ const accounts = this . store . getState ( ) . accounts
9884 // only populate if the entry is still present
9985 if ( accounts [ address ] ) {
10086 accounts [ address ] = result
101- this . updateState ( { accounts } )
102- }
103- cb ( null , result )
104- } )
105- }
106-
107- _updateTransactions ( block , cb = noop ) {
108- const transactions = this . getState ( ) . transactions
109- const txHashes = Object . keys ( transactions )
110- async . each ( txHashes , this . _updateTransaction . bind ( this , block ) , cb )
111- }
112-
113- _updateTransaction ( block , txHash , cb = noop ) {
114- // would use the block here to determine how many confirmations the tx has
115- const transactions = this . getState ( ) . transactions
116- this . _query . getTransaction ( txHash , ( err , result ) => {
117- if ( err ) return cb ( err )
118- // only populate if the entry is still present
119- if ( transactions [ txHash ] ) {
120- transactions [ txHash ] = result
121- this . updateState ( { transactions } )
87+ this . store . updateState ( { accounts } )
12288 }
12389 cb ( null , result )
12490 } )
@@ -135,4 +101,4 @@ class EthereumStore extends ObservableStore {
135101
136102}
137103
138- module . exports = EthereumStore
104+ module . exports = AccountTracker
0 commit comments