diff --git a/src/common/AddressSwitcher.tsx b/src/common/AddressSwitcher.tsx index d15f09eb..bb95ce06 100644 --- a/src/common/AddressSwitcher.tsx +++ b/src/common/AddressSwitcher.tsx @@ -11,7 +11,7 @@ import { useNavigate } from "react-router-dom"; export default function AddressSwitcher() { const { logout, accounts, authenticate, selectAddress } = useLogionChain(); - const { colorTheme, balanceState } = useCommonContext(); + const { colorTheme, balanceState, accountsBalances } = useCommonContext(); const [ confirm, setConfirm ] = useState(false); const navigate = useNavigate(); @@ -64,6 +64,7 @@ export default function AddressSwitcher() { onClick={ () => account.token ? selectAddressCallback(account.accountId) : undefined } > authenticateCallback(account.accountId) } diff --git a/src/common/CommonContext.tsx b/src/common/CommonContext.tsx index 82d19301..970bbcdc 100644 --- a/src/common/CommonContext.tsx +++ b/src/common/CommonContext.tsx @@ -2,7 +2,7 @@ import React, { useContext, useEffect, useReducer, Reducer, useCallback } from " import { DateTime } from 'luxon'; import { LegalOfficerClass, BalanceState, LogionClient, Endpoint } from "@logion/client"; import { InjectedAccount } from "@logion/extension"; -import { ValidAccountId } from "@logion/node-api"; +import { ValidAccountId, TypesAccountData } from "@logion/node-api"; import { useLogionChain } from '../logion-chain'; import { Children } from './types/Helpers'; @@ -25,6 +25,7 @@ export type Viewer = 'User' | 'LegalOfficer'; export interface CommonContext { balanceState?: BalanceState; + accountsBalances: Record; colorTheme: ColorTheme; setColorTheme: ((colorTheme: ColorTheme) => void) | null; refresh: (clearOnRefresh: boolean) => void; @@ -87,6 +88,7 @@ function initialContextValue(): FullCommonContext { expectNewTransaction: () => {}, stopExpectNewTransaction: () => {}, setNotification: () => {}, + accountsBalances: {}, } } @@ -136,6 +138,7 @@ interface Action { stopExpectNewTransaction?: () => void; notificationText?: string; setNotification?: (text?: string) => void; + accountsBalances?: Record; } const MAX_REFRESH_COUNT = 12; @@ -167,6 +170,7 @@ const reducer: Reducer = (state: FullCommonContext, a return { ...state, balanceState: action.balanceState, + accountsBalances: action.accountsBalances || {}, availableLegalOfficers: action.availableLegalOfficers!, backendConfig: action.backendConfig!, nodesUp, @@ -315,6 +319,12 @@ export function CommonContextProvider(props: Props) { balanceState = await client.balanceState(); } + const accountsBalances: Record = {}; + for (const account of accounts.all || []) { + const balance = await client.logionApi.queries.getAccountData(account.accountId); + accountsBalances[account.accountId.address] = balance; + } + const configPromises = await Promise.allSettled(client.legalOfficers.map(legalOfficer => legalOfficer.getConfig())); const backendConfigs: Record = {}; for(let i = 0; i < configPromises.length; ++i) { @@ -341,6 +351,7 @@ export function CommonContextProvider(props: Props) { type: "SET_DATA", dataAddress: currentAddress.toKey(), balanceState, + accountsBalances, nodesUp, nodesDown, availableLegalOfficers, diff --git a/src/common/__mocks__/CommonContextMock.ts b/src/common/__mocks__/CommonContextMock.ts index b82f7d88..3f58c51b 100644 --- a/src/common/__mocks__/CommonContextMock.ts +++ b/src/common/__mocks__/CommonContextMock.ts @@ -29,6 +29,7 @@ export enum ExpectNewTransactionStatus { export function useCommonContext() { const commonContext:Partial = { balanceState, + accountsBalances: {}, colorTheme: COLOR_THEME, setColorTheme, refresh,