Skip to content

Commit

Permalink
Update incoming txs when address changes
Browse files Browse the repository at this point in the history
  • Loading branch information
whymarrh committed Aug 16, 2019
1 parent 076041d commit a7eba59
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 38 deletions.
28 changes: 20 additions & 8 deletions app/scripts/controllers/incoming-transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class IncomingTransactionsController {
const {
blockTracker,
networkController,
getSelectedAddress,
preferencesController,
} = opts
this.blockTracker = blockTracker
this.networkController = networkController
this.getSelectedAddress = getSelectedAddress
this.preferencesController = preferencesController
this.getCurrentNetwork = () => networkController.getProviderConfig().type

const initState = Object.assign({
Expand All @@ -45,29 +45,41 @@ class IncomingTransactionsController {
this.store = new ObservableStore(initState)

this.networkController.on('networkDidChange', async (newType) => {
await this._update({ networkType: newType })
const address = this.preferencesController.getSelectedAddress()
await this._update({
address,
networkType: newType,
})
})
this.blockTracker.on('latest', async (newBlockNumberHex) => {
await this._update({ newBlockNumberDec: parseInt(newBlockNumberHex, 16) })
const address = this.preferencesController.getSelectedAddress()
await this._update({
address,
newBlockNumberDec: parseInt(newBlockNumberHex, 16),
})
})
this.preferencesController.store.subscribe(async ({ selectedAddress }) => {
await this._update({
address: selectedAddress,
})
})
}

async _update ({ newBlockNumberDec, networkType } = {}) {
async _update ({ address, newBlockNumberDec, networkType } = {}) {
try {
const dataForUpdate = await this._getDataForUpdate({ newBlockNumberDec, networkType })
const dataForUpdate = await this._getDataForUpdate({ address, newBlockNumberDec, networkType })
await this._updateStateWithNewTxData(dataForUpdate)
} catch (err) {
log.error(err)
}
}

async _getDataForUpdate ({ newBlockNumberDec, networkType } = {}) {
async _getDataForUpdate ({ address, newBlockNumberDec, networkType } = {}) {
const {
incomingTransactions: currentIncomingTxs,
incomingTxLastFetchedBlocksByNetwork: currentBlocksByNetwork,
} = this.store.getState()

const address = this.getSelectedAddress()
const network = networkType || this.getCurrentNetwork()
const lastFetchBlockByCurrentNetwork = currentBlocksByNetwork[network]
let blockToFetchFrom = lastFetchBlockByCurrentNetwork || newBlockNumberDec
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ module.exports = class MetamaskController extends EventEmitter {
this.incomingTransactionsController = new IncomingTransactionsController({
blockTracker: this.blockTracker,
networkController: this.networkController,
getSelectedAddress: this.preferencesController.getSelectedAddress.bind(this.preferencesController),
preferencesController: this.preferencesController,
initState: initState.IncomingTransactionsController,
})

Expand Down
75 changes: 46 additions & 29 deletions test/unit/app/controllers/incoming-transactions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,26 @@ describe('IncomingTransactionsController', () => {
on: sinon.spy(),
}

const MOCK_PREFERENCES_CONTROLLER = {
getSelectedAddress: sinon.stub().returns('0x0101'),
store: {
subscribe: sinon.spy(),
},
}

describe('constructor', () => {
it('should set up correct store, listeners and properties in the constructor', () => {
const incomingTransactionsController = new IncomingTransactionsController({
blockTracker: MOCK_BLOCKTRACKER,
networkController: MOCK_NETWORK_CONTROLLER,
getSelectedAddress: 'fakeGetSelectedAddress',
preferencesController: MOCK_PREFERENCES_CONTROLLER,
initState: {},
})
sinon.spy(incomingTransactionsController, '_update')

assert.deepEqual(incomingTransactionsController.blockTracker, MOCK_BLOCKTRACKER)
assert.deepEqual(incomingTransactionsController.networkController, MOCK_NETWORK_CONTROLLER)
assert.equal(incomingTransactionsController.getSelectedAddress, 'fakeGetSelectedAddress')
assert.equal(incomingTransactionsController.preferencesController, MOCK_PREFERENCES_CONTROLLER)
assert.equal(incomingTransactionsController.getCurrentNetwork(), 'FAKE_NETWORK')

assert.deepEqual(incomingTransactionsController.store.getState(), EMPTY_INIT_STATE)
Expand All @@ -82,7 +89,10 @@ describe('IncomingTransactionsController', () => {
assert.equal(incomingTransactionsController._update.callCount, 0)
networkControllerListenerCallback('testNetworkType')
assert.equal(incomingTransactionsController._update.callCount, 1)
assert.deepEqual(incomingTransactionsController._update.getCall(0).args[0], { networkType: 'testNetworkType' })
assert.deepEqual(incomingTransactionsController._update.getCall(0).args[0], {
address: '0x0101',
networkType: 'testNetworkType',
})

incomingTransactionsController._update.resetHistory()

Expand All @@ -92,14 +102,17 @@ describe('IncomingTransactionsController', () => {
assert.equal(incomingTransactionsController._update.callCount, 0)
blockTrackerListenerCallback('0xabc')
assert.equal(incomingTransactionsController._update.callCount, 1)
assert.deepEqual(incomingTransactionsController._update.getCall(0).args[0], { newBlockNumberDec: 2748 })
assert.deepEqual(incomingTransactionsController._update.getCall(0).args[0], {
address: '0x0101',
newBlockNumberDec: 2748,
})
})

it('should set the store to a provided initial state', () => {
const incomingTransactionsController = new IncomingTransactionsController({
blockTracker: MOCK_BLOCKTRACKER,
networkController: MOCK_NETWORK_CONTROLLER,
getSelectedAddress: 'fakeGetSelectedAddress',
preferencesController: MOCK_PREFERENCES_CONTROLLER,
initState: NON_EMPTY_INIT_STATE,
})

Expand All @@ -112,12 +125,12 @@ describe('IncomingTransactionsController', () => {
const incomingTransactionsController = new IncomingTransactionsController({
blockTracker: MOCK_BLOCKTRACKER,
networkController: MOCK_NETWORK_CONTROLLER,
getSelectedAddress: () => 'fakeAddress',
preferencesController: MOCK_PREFERENCES_CONTROLLER,
initState: NON_EMPTY_INIT_STATE,
})
incomingTransactionsController._fetchAll = sinon.stub().returns({})

await incomingTransactionsController._getDataForUpdate({ newBlockNumberDec: 999 })
await incomingTransactionsController._getDataForUpdate({ address: 'fakeAddress', newBlockNumberDec: 999 })

assert(incomingTransactionsController._fetchAll.calledOnce)

Expand All @@ -130,12 +143,12 @@ describe('IncomingTransactionsController', () => {
const incomingTransactionsController = new IncomingTransactionsController({
blockTracker: MOCK_BLOCKTRACKER,
networkController: MOCK_NETWORK_CONTROLLER,
getSelectedAddress: () => 'fakeAddress',
preferencesController: MOCK_PREFERENCES_CONTROLLER,
initState: NON_EMPTY_INIT_STATE_WITH_FAKE_NETWORK_STATE,
})
incomingTransactionsController._fetchAll = sinon.stub().returns({})

await incomingTransactionsController._getDataForUpdate({ newBlockNumberDec: 999 })
await incomingTransactionsController._getDataForUpdate({ address: 'fakeAddress', newBlockNumberDec: 999 })

assert(incomingTransactionsController._fetchAll.calledOnce)

Expand All @@ -148,12 +161,15 @@ describe('IncomingTransactionsController', () => {
const incomingTransactionsController = new IncomingTransactionsController({
blockTracker: MOCK_BLOCKTRACKER,
networkController: MOCK_NETWORK_CONTROLLER,
getSelectedAddress: () => 'fakeAddress',
preferencesController: MOCK_PREFERENCES_CONTROLLER,
initState: NON_EMPTY_INIT_STATE_WITH_FAKE_NETWORK_STATE,
})
incomingTransactionsController._fetchAll = sinon.stub().returns({})

await incomingTransactionsController._getDataForUpdate({ networkType: 'NEW_FAKE_NETWORK' })
await incomingTransactionsController._getDataForUpdate({
address: 'fakeAddress',
networkType: 'NEW_FAKE_NETWORK',
})

assert(incomingTransactionsController._fetchAll.calledOnce)

Expand All @@ -166,12 +182,12 @@ describe('IncomingTransactionsController', () => {
const incomingTransactionsController = new IncomingTransactionsController({
blockTracker: MOCK_BLOCKTRACKER,
networkController: MOCK_NETWORK_CONTROLLER,
getSelectedAddress: () => 'fakeAddress',
preferencesController: MOCK_PREFERENCES_CONTROLLER,
initState: NON_EMPTY_INIT_STATE_WITH_FAKE_NETWORK_STATE,
})
incomingTransactionsController._fetchAll = sinon.stub().returns({})

await incomingTransactionsController._getDataForUpdate({ newBlockNumberDec: 999 })
await incomingTransactionsController._getDataForUpdate({ address: 'fakeAddress', newBlockNumberDec: 999 })

assert(incomingTransactionsController._fetchAll.calledOnce)

Expand All @@ -184,15 +200,18 @@ describe('IncomingTransactionsController', () => {
const incomingTransactionsController = new IncomingTransactionsController({
blockTracker: MOCK_BLOCKTRACKER,
networkController: MOCK_NETWORK_CONTROLLER,
getSelectedAddress: () => 'fakeAddress',
preferencesController: MOCK_PREFERENCES_CONTROLLER,
initState: NON_EMPTY_INIT_STATE_WITH_FAKE_NETWORK_STATE,
})
incomingTransactionsController._fetchAll = sinon.stub().returns({
latestIncomingTxBlockNumber: 444,
txs: [{ id: 555 }],
})

const result = await incomingTransactionsController._getDataForUpdate({ networkType: 'FAKE_NETWORK' })
const result = await incomingTransactionsController._getDataForUpdate({
address: 'fakeAddress',
networkType: 'FAKE_NETWORK',
})

assert.deepEqual(result, {
latestIncomingTxBlockNumber: 444,
Expand Down Expand Up @@ -239,7 +258,7 @@ describe('IncomingTransactionsController', () => {
const incomingTransactionsController = new IncomingTransactionsController({
blockTracker: MOCK_BLOCKTRACKER,
networkController: MOCK_NETWORK_CONTROLLER,
getSelectedAddress: () => 'fakeAddress',
preferencesController: MOCK_PREFERENCES_CONTROLLER,
initState: NON_EMPTY_INIT_STATE,
})
sinon.spy(incomingTransactionsController.store, 'updateState')
Expand All @@ -264,7 +283,7 @@ describe('IncomingTransactionsController', () => {
const incomingTransactionsController = new IncomingTransactionsController({
blockTracker: MOCK_BLOCKTRACKER,
networkController: MOCK_NETWORK_CONTROLLER,
getSelectedAddress: () => 'fakeAddress',
preferencesController: MOCK_PREFERENCES_CONTROLLER,
initState: NON_EMPTY_INIT_STATE,
})
sinon.spy(incomingTransactionsController.store, 'updateState')
Expand Down Expand Up @@ -305,7 +324,7 @@ describe('IncomingTransactionsController', () => {
const incomingTransactionsController = new IncomingTransactionsController({
blockTracker: MOCK_BLOCKTRACKER,
networkController: MOCK_NETWORK_CONTROLLER,
getSelectedAddress: () => 'fakeAddress',
preferencesController: MOCK_PREFERENCES_CONTROLLER,
initState: NON_EMPTY_INIT_STATE,
})

Expand All @@ -319,7 +338,7 @@ describe('IncomingTransactionsController', () => {
const incomingTransactionsController = new IncomingTransactionsController({
blockTracker: MOCK_BLOCKTRACKER,
networkController: MOCK_NETWORK_CONTROLLER,
getSelectedAddress: () => 'fakeAddress',
preferencesController: MOCK_PREFERENCES_CONTROLLER,
initState: NON_EMPTY_INIT_STATE,
})

Expand All @@ -333,7 +352,7 @@ describe('IncomingTransactionsController', () => {
const incomingTransactionsController = new IncomingTransactionsController({
blockTracker: MOCK_BLOCKTRACKER,
networkController: MOCK_NETWORK_CONTROLLER,
getSelectedAddress: () => 'fakeAddress',
preferencesController: MOCK_PREFERENCES_CONTROLLER,
initState: NON_EMPTY_INIT_STATE,
})

Expand All @@ -347,7 +366,7 @@ describe('IncomingTransactionsController', () => {
const incomingTransactionsController = new IncomingTransactionsController({
blockTracker: MOCK_BLOCKTRACKER,
networkController: MOCK_NETWORK_CONTROLLER,
getSelectedAddress: () => 'fakeAddress',
preferencesController: MOCK_PREFERENCES_CONTROLLER,
initState: NON_EMPTY_INIT_STATE,
})

Expand All @@ -361,7 +380,7 @@ describe('IncomingTransactionsController', () => {
const incomingTransactionsController = new IncomingTransactionsController({
blockTracker: MOCK_BLOCKTRACKER,
networkController: MOCK_NETWORK_CONTROLLER,
getSelectedAddress: () => 'fakeAddress',
preferencesController: MOCK_PREFERENCES_CONTROLLER,
initState: NON_EMPTY_INIT_STATE,
})

Expand All @@ -381,7 +400,7 @@ describe('IncomingTransactionsController', () => {
const incomingTransactionsController = new IncomingTransactionsController({
blockTracker: MOCK_BLOCKTRACKER,
networkController: MOCK_NETWORK_CONTROLLER,
getSelectedAddress: () => 'fakeAddress',
preferencesController: MOCK_PREFERENCES_CONTROLLER,
initState: NON_EMPTY_INIT_STATE,
})

Expand All @@ -401,7 +420,7 @@ describe('IncomingTransactionsController', () => {
const incomingTransactionsController = new IncomingTransactionsController({
blockTracker: MOCK_BLOCKTRACKER,
networkController: MOCK_NETWORK_CONTROLLER,
getSelectedAddress: () => 'fakeAddress',
preferencesController: MOCK_PREFERENCES_CONTROLLER,
initState: NON_EMPTY_INIT_STATE,
})

Expand All @@ -421,7 +440,7 @@ describe('IncomingTransactionsController', () => {
const incomingTransactionsController = new IncomingTransactionsController({
blockTracker: MOCK_BLOCKTRACKER,
networkController: MOCK_NETWORK_CONTROLLER,
getSelectedAddress: () => 'fakeAddress',
preferencesController: MOCK_PREFERENCES_CONTROLLER,
initState: NON_EMPTY_INIT_STATE,
})

Expand Down Expand Up @@ -540,7 +559,7 @@ describe('IncomingTransactionsController', () => {
const incomingTransactionsController = new IncomingTransactionsController({
blockTracker: MOCK_BLOCKTRACKER,
networkController: MOCK_NETWORK_CONTROLLER,
getSelectedAddress: () => 'fakeAddress',
preferencesController: MOCK_PREFERENCES_CONTROLLER,
initState: NON_EMPTY_INIT_STATE,
})

Expand Down Expand Up @@ -575,14 +594,12 @@ describe('IncomingTransactionsController', () => {
transactionCategory: 'incoming',
})
})
})

describe('_normalizeTxFromEtherscan', () => {
it('should return the expected data when the tx is not in error', () => {
const incomingTransactionsController = new IncomingTransactionsController({
blockTracker: MOCK_BLOCKTRACKER,
networkController: MOCK_NETWORK_CONTROLLER,
getSelectedAddress: () => 'fakeAddress',
preferencesController: MOCK_PREFERENCES_CONTROLLER,
initState: NON_EMPTY_INIT_STATE,
})

Expand Down

0 comments on commit a7eba59

Please sign in to comment.