Skip to content

Commit

Permalink
Merge pull request #1691 from oasisprotocol/lw/fix-balances-home
Browse files Browse the repository at this point in the history
Fix refreshing balances on Home page after switching network
  • Loading branch information
lukaw3d authored Sep 28, 2023
2 parents 35ed8ab + 177eb29 commit b31b357
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 33 deletions.
34 changes: 34 additions & 0 deletions playwright/tests/refreshing-balance.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { test, expect } from '@playwright/test'
import { privateKey, privateKeyAddress } from '../utils/test-inputs'
import { fillPrivateKeyWithoutPassword } from '../utils/fillPrivateKey'
import { warnSlowApi } from '../utils/warnSlowApi'
import { mockApi } from '../utils/mockApi'

test.beforeEach(async ({ page }) => {
await warnSlowApi(page)
await mockApi(page, 123)

await page.goto('/open-wallet/private-key')
await fillPrivateKeyWithoutPassword(page, {
privateKey: privateKey,
privateKeyAddress: privateKeyAddress,
persistenceCheckboxChecked: false,
persistenceCheckboxDisabled: false,
})
await expect(page.getByTestId('account-selector')).toBeVisible()
})

test('Account selector should refresh balances on network change', async ({ page }) => {
await page.getByRole('link', { name: /Home/ }).click()

await page.getByTestId('account-selector').click()
await expect(page.getByTestId('account-choice')).toContainText('123.0')
await page.getByRole('button', { name: /Select/ }).click()

await mockApi(page, 456)
await page.getByTestId('network-selector').click()
await page.getByRole('menuitem', { name: 'Testnet' }).click()
await page.getByTestId('account-selector').click()
await expect(page.getByTestId('account-choice')).toContainText('456.0')
await page.getByRole('button', { name: /Select/ }).click()
})
2 changes: 1 addition & 1 deletion playwright/utils/mockApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function mockApi(context: BrowserContext | Page, balance: number) {
available: balance.toString(),
escrow: '0',
debonding: '0',
total: '0',
total: balance.toString(),
nonce: 1,
allowances: [],
} satisfies AccountsRow,
Expand Down
19 changes: 1 addition & 18 deletions src/app/pages/AccountPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { TransactionModal } from 'app/components/TransactionModal'
import { selectSelectedNetwork } from 'app/state/network/selectors'
import { selectStaking } from 'app/state/staking/selectors'
import { selectTransaction } from 'app/state/transaction/selectors'
import { walletActions } from 'app/state/wallet'
import { Box } from 'grommet/es6/components/Box'
import { Layer } from 'grommet/es6/components/Layer'
import { Nav } from 'grommet/es6/components/Nav'
Expand All @@ -27,12 +26,7 @@ import { normalizeColor } from 'grommet/es6/utils'
import { accountActions } from 'app/state/account'
import { selectAccount } from 'app/state/account/selectors'
import { BalanceDetails } from 'app/state/account/types'
import {
selectAddress,
selectHasAccounts,
selectWallets,
selectWalletsAddresses,
} from 'app/state/wallet/selectors'
import { selectAddress, selectHasAccounts } from 'app/state/wallet/selectors'
import { AccountSummary } from './Features/AccountSummary'
import { AccountPageParams } from './validateAccountPageRoute'
import { Button } from 'grommet/es6/components/Button'
Expand Down Expand Up @@ -84,8 +78,6 @@ export function AccountPage(props: AccountPageProps) {
const walletAddress = useSelector(selectAddress)
const selectedNetwork = useSelector(selectSelectedNetwork)
const { active } = useSelector(selectTransaction)
const wallets = useSelector(selectWallets)
const walletsAddresses = useSelector(selectWalletsAddresses)

const balanceDelegations = stake.delegations?.reduce((acc, v) => acc + BigInt(v.amount), 0n)
const balanceDebondingDelegations = stake.debondingDelegations?.reduce(
Expand All @@ -110,15 +102,6 @@ export function AccountPage(props: AccountPageProps) {
}
}, [dispatch, address, selectedNetwork])

// Reload wallet balances if network changes
useEffect(() => {
for (const wallet of Object.values(wallets)) {
dispatch(walletActions.fetchWallet(wallet))
}
// Using `walletsAddresses` dependency instead of `wallets` to avoid infinite loop
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [dispatch, walletsAddresses.join(','), selectedNetwork])

return (
<Box pad="small">
{active && <TransactionModal />}
Expand Down
7 changes: 1 addition & 6 deletions src/app/state/wallet/saga.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DeepPartialRootState } from 'types/RootState'

import { walletActions } from '.'
import { transactionActions } from '../transaction'
import { addWallet, rootWalletSaga, walletSaga } from './saga'
import { addWallet, rootWalletSaga } from './saga'
import { AddWalletPayload, WalletType } from './types'
import { importAccountsActions } from '../importaccounts'
import { getAccountBalanceWithFallback } from '../../lib/getAccountBalanceWithFallback'
Expand Down Expand Up @@ -43,7 +43,6 @@ describe('Wallet Sagas', () => {
.withState(state)
.provide(providers)
.dispatch(walletActions.openWalletFromMnemonic({ choosePassword: undefined }))
.fork(walletSaga)
.silentRun(50)
})

Expand All @@ -52,7 +51,6 @@ describe('Wallet Sagas', () => {
.provide(providers)
.withState(state)
.dispatch(walletActions.openWalletFromMnemonic({ choosePassword: undefined }))
.fork(walletSaga)
.put(importAccountsActions.clear())
.call(addWallet, {
address: 'oasis1qq2vzcvxn0js5unsch5me2xz4kr43vcasv0d5eq4',
Expand All @@ -79,7 +77,6 @@ describe('Wallet Sagas', () => {
choosePassword: undefined,
}),
)
.fork(walletSaga)
.call.like({
fn: addWallet,
args: [
Expand All @@ -98,7 +95,6 @@ describe('Wallet Sagas', () => {
.provide(providers)
.withState(state)
.dispatch(walletActions.openWalletsFromLedger({ choosePassword: undefined }))
.fork(walletSaga)
.put(importAccountsActions.clear())
.put.actionType(walletActions.selectWallet.type)
.silentRun(50)
Expand Down Expand Up @@ -146,7 +142,6 @@ describe('Wallet Sagas', () => {
},
})
.dispatch(walletActions.openWalletsFromLedger({ choosePassword: undefined }))
.fork(walletSaga)
.put({
type: walletActions.selectWallet.type,
payload: 'oasis1qq2vzcvxn0js5unsch5me2xz4kr43vcasv0d5eq4',
Expand Down
18 changes: 10 additions & 8 deletions src/app/state/wallet/saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ import {
} from './types'
import { persistActions } from 'app/state/persist'
import { getAccountBalanceWithFallback } from '../../lib/getAccountBalanceWithFallback'

/**
* Opened wallet saga
* Will later be used to sign arbitrary messages
*/
export function* walletSaga() {}
import { networkActions } from '../network'

export function* rootWalletSaga() {
// Wait for an openWallet action (Mnemonic, Private Key, Ledger) and add them if requested
Expand All @@ -36,8 +31,8 @@ export function* rootWalletSaga() {
yield* fork(refreshAccountOnParaTimeTransaction)
yield* takeEvery(walletActions.fetchWallet, fetchWallet)

// Start the wallet saga in parallel
yield* fork(walletSaga)
// Reload wallet balances if network changes
yield* takeEvery(networkActions.networkSelected, refreshOnNetworkChange)
}

function* getWalletByAddress(address: string) {
Expand Down Expand Up @@ -171,3 +166,10 @@ function* refreshAccount(address: string) {
yield* put(walletActions.fetchWallet(wallet))
}
}

function* refreshOnNetworkChange() {
const wallets = yield* select(selectWallets)
for (const wallet of Object.values(wallets)) {
yield* put(walletActions.fetchWallet(wallet))
}
}

0 comments on commit b31b357

Please sign in to comment.