Skip to content

Commit

Permalink
feat: add sync state for eraRewardPoints fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
Ross Bulat committed Oct 20, 2023
1 parent 0ab87f4 commit aae111c
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/contexts/Validators/ValidatorEntries/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ export const ValidatorsProvider = ({
// Stores a randomised validator community dataset.
const [validatorCommunity] = useState([...shuffle(ValidatorCommunity)]);

// Track whether the validator list has been fetched.
const [erasRewardPointsFetched, setErasRewawrdPointsFetched] =
useState<Sync>('unsynced');

// Store era reward points, keyed by era.
const [erasRewardPoints, setErasRewardPoints] = useState<ErasRewardPoints>(
{}
Expand Down Expand Up @@ -114,7 +118,14 @@ export const ValidatorsProvider = ({

// Fetches era reward points for eligible eras.
const fetchErasRewardPoints = async () => {
if (activeEra.index.isZero() || !api) return;
if (
activeEra.index.isZero() ||
!api ||
erasRewardPointsFetched !== 'unsynced'
)
return;

setErasRewawrdPointsFetched('syncing');

// start fetching from the current era.
let currentEra = BigNumber.max(activeEra.index.minus(1), 1);
Expand Down Expand Up @@ -403,6 +414,7 @@ export const ValidatorsProvider = ({
high: high || new BigNumber(0),
low: low || new BigNumber(0),
});
setErasRewawrdPointsFetched('synced');
};

// Gets either `nominated` or `poolNominated` depending on bondFor, and injects the validator
Expand Down Expand Up @@ -438,6 +450,7 @@ export const ValidatorsProvider = ({
// Reset validator state data on network change.
useEffectIgnoreInitial(() => {
setValidatorsFetched('unsynced');
setErasRewawrdPointsFetched('unsynced');
setSessionValidators([]);
setSessionParaValidators([]);
setAvgCommission(0);
Expand All @@ -448,22 +461,28 @@ export const ValidatorsProvider = ({
setEraPointsBoundaries(null);
}, [network]);

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

// Mark unsynced and fetch session validators when activeEra changes.
useEffectIgnoreInitial(() => {
if (isReady && activeEra.index.isGreaterThan(0)) {
if (erasRewardPointsFetched === 'synced')
setErasRewawrdPointsFetched('unsynced');

if (validatorsFetched === 'synced') setValidatorsFetched('unsynced');
fetchSessionValidators();
}
}, [isReady, activeEra]);

// Fetch era points boundaries when `erasRewardPoints` ready.
useEffectIgnoreInitial(() => {
if (
isReady &&
Object.values(erasRewardPoints).length &&
!eraPointsBoundaries
)
if (isReady && Object.values(erasRewardPoints).length)
calculateEraPointsBoundaries();
}, [isReady, erasRewardPoints]);

Expand Down

0 comments on commit aae111c

Please sign in to comment.