Skip to content

Commit

Permalink
add synced state to pool performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Ross Bulat committed Oct 24, 2023
1 parent 09e5ecb commit 49d293a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/contexts/Pools/PoolPerformance/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
import type { PoolPerformanceContextInterface } from './types';

export const defaultPoolPerformanceContext: PoolPerformanceContextInterface = {
poolRewardPointsFetched: 'unsynced',
poolRewardPoints: {},
};
26 changes: 24 additions & 2 deletions src/contexts/Pools/PoolPerformance/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useBondedPools } from 'contexts/Pools/BondedPools';
import type { AnyJson } from 'types';
import { useNetworkMetrics } from 'contexts/NetworkMetrics';
import { useApi } from 'contexts/Api';
import type { Sync } from '@polkadot-cloud/react/types';
import type { PoolPerformanceContextInterface } from './types';
import { defaultPoolPerformanceContext } from './defaults';

Expand All @@ -24,11 +25,16 @@ export const PoolPerformanceProvider = ({
const {
networkData: { endpoints },
} = useNetwork();
const { network } = useNetwork();
const { isLightClient } = useApi();
const { bondedPools } = useBondedPools();
const { activeEra } = useNetworkMetrics();
const { erasRewardPointsFetched, erasRewardPoints } = useValidators();

// Store whether pool performance data is being fetched.
const [poolRewardPointsFetched, setPoolRewardPointsFetched] =
useState<Sync>('unsynced');

// Store pool performance data.
const [poolRewardPoints, setPoolRewardPoints] = useState<AnyJson>({});

Expand All @@ -42,6 +48,7 @@ export const PoolPerformanceProvider = ({

const { poolRewardData } = data;
setPoolRewardPoints(poolRewardData);
setPoolRewardPointsFetched('synced');
}
};

Expand All @@ -56,8 +63,11 @@ export const PoolPerformanceProvider = ({
if (
bondedPools.length &&
activeEra.index.isGreaterThan(0) &&
erasRewardPointsFetched === 'synced'
erasRewardPointsFetched === 'synced' &&
poolRewardPointsFetched === 'unsynced'
) {
setPoolRewardPointsFetched('syncing');

worker.postMessage({
task: 'processNominationPoolsRewardData',
activeEra: activeEra.index.toString(),
Expand All @@ -68,11 +78,23 @@ export const PoolPerformanceProvider = ({
maxEras: MaxEraRewardPointsEras,
});
}
}, [bondedPools, activeEra, erasRewardPointsFetched]);
}, [
bondedPools,
activeEra,
erasRewardPointsFetched,
poolRewardPointsFetched,
]);

// Reset state data on network change.
useEffectIgnoreInitial(() => {
setPoolRewardPoints({});
setPoolRewardPointsFetched('unsynced');
}, [network]);

return (
<PoolPerformanceContext.Provider
value={{
poolRewardPointsFetched,
poolRewardPoints,
}}
>
Expand Down
2 changes: 2 additions & 0 deletions src/contexts/Pools/PoolPerformance/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// SPDX-License-Identifier: GPL-3.0-only

import type { AnyJson } from '@polkadot-cloud/react/types';
import type { Sync } from 'types';

export interface PoolPerformanceContextInterface {
poolRewardPointsFetched: Sync;
poolRewardPoints: AnyJson;
}

0 comments on commit 49d293a

Please sign in to comment.