Skip to content

Commit

Permalink
fix people status
Browse files Browse the repository at this point in the history
  • Loading branch information
rossbulat committed Sep 5, 2024
1 parent 2a6a624 commit f9d4997
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
10 changes: 8 additions & 2 deletions src/contexts/Balances/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export const BalancesContext = createContext<BalancesContextInterface>(
export const useBalances = () => useContext(BalancesContext);

export const BalancesProvider = ({ children }: { children: ReactNode }) => {
const { api, peopleApi } = useApi();
const { getBondedAccount } = useBonded();
const { accounts } = useImportedAccounts();
const createPoolAccounts = useCreatePoolAccounts();
const { api, peopleApi, peopleApiStatus } = useApi();
const { activeAccount, activeProxy } = useActiveAccounts();
const controller = getBondedAccount(activeAccount);

Expand Down Expand Up @@ -68,7 +68,13 @@ export const BalancesProvider = ({ children }: { children: ReactNode }) => {
addresses: { ...createPoolAccounts(Number(poolId)) },
});
if (peopleApi) {
ActivePoolsController.syncPools(api, peopleApi, address, newPools);
ActivePoolsController.syncPools(
api,
peopleApi,
peopleApiStatus,
address,
newPools
);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/contexts/Pools/ActivePool/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export const useActivePool = () => useContext(ActivePoolContext);
export const ActivePoolProvider = ({ children }: { children: ReactNode }) => {
const { network } = useNetwork();
const { getPoolMembership } = useBalances();
const { isReady, api, peopleApi } = useApi();
const { activeAccount } = useActiveAccounts();
const createPoolAccounts = useCreatePoolAccounts();
const { isReady, api, peopleApi, peopleApiStatus } = useApi();
const { getAccountPoolRoles, bondedPools } = useBondedPools();

const membership = getPoolMembership(activeAccount);
Expand Down Expand Up @@ -103,6 +103,7 @@ export const ActivePoolProvider = ({ children }: { children: ReactNode }) => {
ActivePoolsController.syncPools(
api,
peopleApi,
peopleApiStatus,
activeAccount,
newActivePools
);
Expand Down
17 changes: 14 additions & 3 deletions src/contexts/Validators/ValidatorEntries/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const ValidatorsProvider = ({ children }: { children: ReactNode }) => {
isReady,
api,
peopleApi,
peopleApiStatus,
consts: { historyDepth },
networkMetrics: { earliestStoredSession },
} = useApi();
Expand Down Expand Up @@ -317,7 +318,7 @@ export const ValidatorsProvider = ({ children }: { children: ReactNode }) => {
// NOTE: validators are shuffled before committed to state.
setValidators(shuffle(validatorEntries));

if (peopleApi) {
if (peopleApi && peopleApiStatus === 'ready') {
const addresses = validatorEntries.map(({ address }) => address);
const { identities, supers } = await IdentitiesController.fetch(
peopleApi,
Expand Down Expand Up @@ -532,11 +533,21 @@ export const ValidatorsProvider = ({ children }: { children: ReactNode }) => {

// Fetch validators and era reward points when fetched status changes.
useEffect(() => {
if (isReady && activeEra.index.isGreaterThan(0)) {
if (
isReady &&
peopleApiStatus === 'ready' &&
activeEra.index.isGreaterThan(0)
) {
fetchValidators();
fetchErasRewardPoints();
}
}, [validatorsFetched, erasRewardPointsFetched, isReady, activeEra]);
}, [
validatorsFetched,
erasRewardPointsFetched,
isReady,
peopleApiStatus,
activeEra,
]);

// Mark unsynced and fetch session validators and average reward when activeEra changes.
useEffectIgnoreInitial(() => {
Expand Down
7 changes: 4 additions & 3 deletions src/controllers/ActivePools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class ActivePoolsController {
static syncPools = async (
api: ApiPromise,
peopleApi: ApiPromise,
peopleApiStatus: string,
address: MaybeAddress,
newPools: ActivePoolItem[]
): Promise<void> => {
Expand Down Expand Up @@ -80,8 +81,8 @@ export class ActivePoolsController {
]): Promise<void> => {
// NOTE: async: fetches identity data for roles.
await this.handleActivePoolCallback(
api,
peopleApi,
peopleApiStatus,
address,
pool,
bondedPool,
Expand Down Expand Up @@ -116,8 +117,8 @@ export class ActivePoolsController {

// Handle active pool callback.
static handleActivePoolCallback = async (
api: ApiPromise,
peopleApi: ApiPromise,
peopleApiStatus: string,
address: string,
pool: ActivePoolItem,
bondedPoolResult: AnyApi,
Expand All @@ -129,7 +130,7 @@ export class ActivePoolsController {
const balance = accountDataResult.data;
const rewardAccountBalance = balance?.free.toString();

if (peopleApi) {
if (peopleApi && peopleApiStatus === 'ready') {
// Fetch identities for roles and expand `bondedPool` state to store them.
bondedPool.roleIdentities = await IdentitiesController.fetch(
peopleApi,
Expand Down

0 comments on commit f9d4997

Please sign in to comment.