@ -2,7 +2,7 @@ const ObservableStore = require('obs-store')
const log = require ( 'loglevel' )
const BN = require ( 'bn.js' )
const createId = require ( '../lib/random-id' )
const { bnToHex } = require ( '../lib/util' )
const { bnToHex , fetchWithTimeout } = require ( '../lib/util' )
const {
MAINNET _CODE ,
ROPSTEN _CODE ,
@ -14,11 +14,14 @@ const {
MAINNET ,
} = require ( './network/enums' )
const networkTypeToIdMap = {
[ ROPSTEN ] : ROPSTEN _CODE ,
[ RINKEBY ] : RINKEYBY _CODE ,
[ KOVAN ] : KOVAN _CODE ,
[ MAINNET ] : MAINNET _CODE ,
[ ROPSTEN ] : String ( ROPSTEN _CODE ) ,
[ RINKEBY ] : String ( RINKEYBY _CODE ) ,
[ KOVAN ] : String ( KOVAN _CODE ) ,
[ MAINNET ] : String ( MAINNET _CODE ) ,
}
const fetch = fetchWithTimeout ( {
timeout : 30000 ,
} )
class IncomingTransactionsController {
@ -33,6 +36,15 @@ class IncomingTransactionsController {
this . preferencesController = preferencesController
this . getCurrentNetwork = ( ) => networkController . getProviderConfig ( ) . type
this . _onLatestBlock = async ( newBlockNumberHex ) => {
const selectedAddress = this . preferencesController . getSelectedAddress ( )
const newBlockNumberDec = parseInt ( newBlockNumberHex , 16 )
await this . _update ( {
address : selectedAddress ,
newBlockNumberDec ,
} )
}
const initState = Object . assign ( {
incomingTransactions : { } ,
incomingTxLastFetchedBlocksByNetwork : {
@ -51,13 +63,6 @@ class IncomingTransactionsController {
networkType : newType ,
} )
} )
this . blockTracker . on ( 'latest' , async ( newBlockNumberHex ) => {
const address = this . preferencesController . getSelectedAddress ( )
await this . _update ( {
address ,
newBlockNumberDec : parseInt ( newBlockNumberHex , 16 ) ,
} )
} )
this . preferencesController . store . subscribe ( async ( { selectedAddress } ) => {
await this . _update ( {
address : selectedAddress ,
@ -65,6 +70,15 @@ class IncomingTransactionsController {
} )
}
start ( ) {
this . blockTracker . removeListener ( 'latest' , this . _onLatestBlock )
this . blockTracker . addListener ( 'latest' , this . _onLatestBlock )
}
stop ( ) {
this . blockTracker . removeListener ( 'latest' , this . _onLatestBlock )
}
async _update ( { address , newBlockNumberDec , networkType } = { } ) {
try {
const dataForUpdate = await this . _getDataForUpdate ( { address , newBlockNumberDec , networkType } )