From 8d851c5c8fae060c3782c4ff68a3b0232403895c Mon Sep 17 00:00:00 2001 From: Szymon Lesisz Date: Thu, 10 May 2018 13:52:51 +0200 Subject: [PATCH] add deviceState to setBalance & setNonce actions --- src/js/actions/AddressActions.js | 15 ++++++++++++++- src/js/actions/DiscoveryActions.js | 9 ++------- src/js/actions/Web3Actions.js | 15 +++------------ src/js/reducers/AccountsReducer.js | 4 ++-- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/js/actions/AddressActions.js b/src/js/actions/AddressActions.js index 3a8bbeb5..e4766cb7 100644 --- a/src/js/actions/AddressActions.js +++ b/src/js/actions/AddressActions.js @@ -29,6 +29,7 @@ export type AddressSetBalanceAction = { type: typeof ADDRESS.SET_BALANCE, address: string, network: string, + deviceState: string, balance: string } @@ -36,14 +37,26 @@ export type AddressSetNonceAction = { type: typeof ADDRESS.SET_NONCE, address: string, network: string, + deviceState: string, nonce: number } -export const setBalance = (address: string, network: string, balance: string): Action => { +export const setBalance = (address: string, network: string, deviceState: string, balance: string): Action => { return { type: ADDRESS.SET_BALANCE, address, network, + deviceState, balance } +} + +export const setNonce = (address: string, network: string, deviceState: string, nonce: number): Action => { + return { + type: ADDRESS.SET_NONCE, + address, + network, + deviceState, + nonce + } } \ No newline at end of file diff --git a/src/js/actions/DiscoveryActions.js b/src/js/actions/DiscoveryActions.js index 13bf3be9..7dca76a5 100644 --- a/src/js/actions/DiscoveryActions.js +++ b/src/js/actions/DiscoveryActions.js @@ -285,7 +285,7 @@ const discoverAddress = (device: TrezorDevice, discoveryProcess: Discovery): Asy const balance = await getBalanceAsync(web3instance.web3, ethAddress); if (discoveryProcess.interrupted) return; dispatch( - AddressActions.setBalance(ethAddress, network, web3instance.web3.fromWei(balance.toString(), 'ether')) + AddressActions.setBalance(ethAddress, network, device.state || 'undefined', web3instance.web3.fromWei(balance.toString(), 'ether')) ); const userTokens = []; @@ -302,12 +302,7 @@ const discoverAddress = (device: TrezorDevice, discoveryProcess: Discovery): Asy const nonce: number = await getNonceAsync(web3instance.web3, ethAddress); if (discoveryProcess.interrupted) return; - dispatch({ - type: ADDRESS.SET_NONCE, - address: ethAddress, - network, - nonce: nonce - }); + dispatch(AddressActions.setNonce(ethAddress, network, device.state || 'undefined', nonce)); const addressIsEmpty = nonce < 1 && !balance.greaterThan(0); diff --git a/src/js/actions/Web3Actions.js b/src/js/actions/Web3Actions.js index 8a34832f..44f356a6 100644 --- a/src/js/actions/Web3Actions.js +++ b/src/js/actions/Web3Actions.js @@ -172,12 +172,7 @@ export function init(instance: ?Web3, coinIndex: number = 0): AsyncAction { for (const account of accounts) { const nonce = await getNonceAsync(web3, account.address); if (nonce !== account.nonce) { - dispatch({ - type: ADDRESS.SET_NONCE, - address: account.address, - network: account.network, - nonce - }); + dispatch(AddressActions.setNonce(account.address, account.network, account.deviceState, nonce)); // dispatch( getBalance(account) ); // TODO: check if nonce was updated, @@ -254,6 +249,7 @@ export function getBalance(account: Account): AsyncAction { dispatch(AddressActions.setBalance( account.address, account.network, + account.deviceState, newBalance )); @@ -296,12 +292,7 @@ export function getNonce(account: Account): AsyncAction { web3.eth.getTransactionCount(account.address, (error: Error, result: number) => { if (!error) { if (account.nonce !== result) { - dispatch({ - type: ADDRESS.SET_NONCE, - address: account.address, - network: account.network, - nonce: result - }); + dispatch(AddressActions.setNonce(account.address, account.network, account.deviceState, result)); } } }); diff --git a/src/js/reducers/AccountsReducer.js b/src/js/reducers/AccountsReducer.js index de1d2f9c..46ea6764 100644 --- a/src/js/reducers/AccountsReducer.js +++ b/src/js/reducers/AccountsReducer.js @@ -66,7 +66,7 @@ const removeAccounts = (state: State, device: TrezorDevice): State => { // } const setBalance = (state: State, action: AddressSetBalanceAction): State => { - const index: number = state.findIndex(account => account.address === action.address && account.network === action.network); + const index: number = state.findIndex(account => account.address === action.address && account.network === action.network && account.deviceState === action.deviceState); const newState: State = [ ...state ]; newState[index].loaded = true; newState[index].balance = action.balance; @@ -74,7 +74,7 @@ const setBalance = (state: State, action: AddressSetBalanceAction): State => { } const setNonce = (state: State, action: AddressSetNonceAction): State => { - const index: number = state.findIndex(account => account.address === action.address && account.network === action.network); + const index: number = state.findIndex(account => account.address === action.address && account.network === action.network && account.deviceState === action.deviceState); const newState: State = [ ...state ]; newState[index].loaded = true; newState[index].nonce = action.nonce;