Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wallet): Implemented Balance Updater Interval #12433

Merged
merged 1 commit into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions components/brave_wallet_ui/common/async/balanceUpdater.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2021 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.

export default class BalanceUpdater {
private intervalId: ReturnType<typeof setInterval> | undefined

beginUpdatingBalances (timeMs: number, onUpdateBalances: () => unknown) {
this.intervalId = setInterval(
() => {
onUpdateBalances()
}, timeMs)
}

stopUpdatingBalances () {
if (this.intervalId) {
clearTimeout(this.intervalId)
this.intervalId = undefined
}
}
}
16 changes: 13 additions & 3 deletions components/brave_wallet_ui/common/async/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,26 @@ import {
refreshSitePermissions,
refreshTransactionHistory,
refreshBalances,
refreshVisibleTokenInfo,
refreshPrices
} from './lib'
import { Store } from './types'
import InteractionNotifier from './interactionNotifier'
import BalanceUpdater from './balanceUpdater'

const handler = new AsyncActionHandler()

const interactionNotifier = new InteractionNotifier()
const balanceUpdater = new BalanceUpdater()

function getWalletState (store: Store): WalletState {
return store.getState().wallet
}

async function refreshBalancesPricesAndHistory (store: Store) {
const state = getWalletState(store)
await store.dispatch(refreshBalances(state.selectedNetwork))
await store.dispatch(refreshVisibleTokenInfo(state.selectedNetwork))
await store.dispatch(refreshBalances())
await store.dispatch(refreshPrices())
await store.dispatch(refreshTokenPriceHistory(state.selectedPortfolioTimeline))
}
Expand Down Expand Up @@ -101,7 +105,8 @@ async function updateAccountInfo (store: Store) {

handler.on(WalletActions.refreshBalancesAndPrices.getType(), async (store: Store) => {
const state = getWalletState(store)
await store.dispatch(refreshBalances(state.selectedNetwork))
await store.dispatch(refreshVisibleTokenInfo(state.selectedNetwork))
await store.dispatch(refreshBalances())
await store.dispatch(refreshPrices())
})

Expand Down Expand Up @@ -131,6 +136,7 @@ handler.on(WalletActions.keyringReset.getType(), async (store) => {

handler.on(WalletActions.locked.getType(), async (store) => {
interactionNotifier.stopWatchingForInteraction()
balanceUpdater.stopUpdatingBalances()
await refreshWalletInfo(store)
})

Expand Down Expand Up @@ -219,7 +225,11 @@ handler.on(WalletActions.initialized.getType(), async (store: Store, payload: Wa
// Fetch Balances and Prices
if (!state.isWalletLocked && state.isWalletCreated) {
const currentNetwork = await store.dispatch(refreshNetworkInfo())
await store.dispatch(refreshBalances(currentNetwork))
await store.dispatch(refreshVisibleTokenInfo(currentNetwork))
await store.dispatch(refreshBalances())
balanceUpdater.beginUpdatingBalances(15000, async () => {
await store.dispatch(refreshBalances())
})
await store.dispatch(refreshPrices())
await store.dispatch(refreshTokenPriceHistory(state.selectedPortfolioTimeline))
}
Expand Down
36 changes: 21 additions & 15 deletions components/brave_wallet_ui/common/async/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,9 @@ export async function getIsSwapSupported (network: BraveWallet.EthereumChain): P
return (await swapService.isSwapSupported(network.chainId)).result
}

export function refreshBalances (currentNetwork: BraveWallet.EthereumChain) {
return async (dispatch: Dispatch, getState: () => State) => {
const { braveWalletService, jsonRpcService } = getAPIProxy()
const { wallet: { accounts } } = getState()

const visibleTokensInfo = await braveWalletService.getUserAssets(currentNetwork.chainId)
export function refreshVisibleTokenInfo (currentNetwork: BraveWallet.EthereumChain) {
return async (dispatch: Dispatch) => {
const { braveWalletService } = getAPIProxy()

// Selected Network's Native Asset
const nativeAsset: BraveWallet.BlockchainToken = {
Expand All @@ -182,6 +179,17 @@ export function refreshBalances (currentNetwork: BraveWallet.EthereumChain) {
coingeckoId: ''
}

const visibleTokensInfo = await braveWalletService.getUserAssets(currentNetwork.chainId)
const visibleAssets: BraveWallet.BlockchainToken[] = visibleTokensInfo.tokens.length === 0 ? [nativeAsset] : visibleTokensInfo.tokens
await dispatch(WalletActions.setVisibleTokensInfo(visibleAssets))
}
}

export function refreshBalances () {
return async (dispatch: Dispatch, getState: () => State) => {
const { jsonRpcService } = getAPIProxy()
const { wallet: { accounts, userVisibleTokensInfo } } = getState()

const getBalanceReturnInfos = await Promise.all(accounts.map(async (account) => {
const balanceInfo = await jsonRpcService.getBalance(account.address, account.coin)
return balanceInfo
Expand All @@ -190,9 +198,7 @@ export function refreshBalances (currentNetwork: BraveWallet.EthereumChain) {
balances: getBalanceReturnInfos
}))

const visibleAssets: BraveWallet.BlockchainToken[] = visibleTokensInfo.tokens.length === 0 ? [nativeAsset] : visibleTokensInfo.tokens
await dispatch(WalletActions.setVisibleTokensInfo(visibleAssets))
const visibleTokens = visibleAssets.filter(asset => asset.contractAddress !== '')
const visibleTokens = userVisibleTokensInfo.filter(asset => asset.contractAddress !== '')

const getBlockchainTokenBalanceReturnInfos = await Promise.all(accounts.map(async (account) => {
return Promise.all(visibleTokens.map(async (token) => {
Expand Down Expand Up @@ -284,12 +290,12 @@ export function refreshTokenPriceHistory (selectedPortfolioTimeline: BraveWallet
const balance = token.contractAddress
? account.tokenBalanceRegistry[token.contractAddress.toLowerCase()]
: account.balance
return {
token,
balance: balance || '0',
history: priceHistory.find((t) => token.contractAddress === t.contractAddress)?.history ?? { success: true, values: [] }
}
})
return {
token,
balance: balance || '0',
history: priceHistory.find((t) => token.contractAddress === t.contractAddress)?.history ?? { success: true, values: [] }
}
})
})

dispatch(WalletActions.portfolioPriceHistoryUpdated(priceHistoryWithBalances))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ const CryptoView = (props: Props) => {
setHideNav(false)
}
}
}, [id, userVisibleTokensInfo, category, accounts])
}, [id, userVisibleTokensInfo, category])

const toggleNav = () => {
setHideNav(!hideNav)
Expand Down