1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-12-28 18:08:08 +00:00

update account tokens on each block

This commit is contained in:
Szymon Lesisz 2018-09-14 12:20:59 +02:00
parent 815fba5e6b
commit 6740b7198d
2 changed files with 13 additions and 1 deletions

View File

@ -16,6 +16,7 @@ import * as BLOCKCHAIN from 'actions/constants/blockchain';
import * as WEB3 from 'actions/constants/web3';
import * as PENDING from 'actions/constants/pendingTx';
import * as AccountsActions from './AccountsActions';
import * as Web3Actions from './Web3Actions';
import type {
@ -43,7 +44,6 @@ export const discoverAccount = (device: TrezorDevice, xpub: string, network: str
const txs = await TrezorConnect.ethereumGetAccountInfo({
account: {
address: xpub,
// block: 3984156,
block: 0,
transactions: 0
},
@ -104,7 +104,15 @@ export const onBlockMined = (network: string): PromiseAction<void> => async (dis
if (response.success) {
response.payload.forEach((a, i) => {
if (a.transactions > 0) {
// load additional data from Web3 (balance, nonce, tokens)
dispatch( Web3Actions.updateAccount(accounts[i], a, network) )
} else {
// there are no new txs, just update block
dispatch( AccountsActions.update( { ...accounts[i], ...a }) );
// HACK: since blockbook can't work with smart contracts for now
// try to update tokens balances added to this account using Web3
dispatch( Web3Actions.updateAccountTokens(accounts[i]) );
}
});
}

View File

@ -216,6 +216,10 @@ export const updateAccount = (account: Account, newAccount: EthereumAccount, net
dispatch( AccountsActions.update( { ...account, ...newAccount, balance: EthereumjsUnits.convert(balance, 'wei', 'ether'), nonce }) );
// update tokens for this account
dispatch( updateAccountTokens(account) );
}
export const updateAccountTokens = (account: Account): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const tokens = getState().tokens.filter(t => t.network === account.network && t.ethAddress === account.address);
for (const token of tokens) {
const balance = await dispatch( getTokenBalance(token) );