Skip to content

Commit

Permalink
feat(refactor): Persist all imported accounts active pools. (#2066)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ross Bulat authored Apr 7, 2024
1 parent a452723 commit 1a1847d
Show file tree
Hide file tree
Showing 13 changed files with 263 additions and 116 deletions.
2 changes: 1 addition & 1 deletion src/canvas/JoinPool/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const JoinPool = () => {
rewardPoints.every((points) => Number(points) > 0) &&
rewardPoints.length === MaxEraRewardPointsEras;

return pool.state === 'Open' && activeDaily;
return activeDaily;
})
// Ensure the pool is currently in the active set of backers.
.filter((pool) =>
Expand Down
24 changes: 22 additions & 2 deletions src/contexts/Balances/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import { useActiveAccounts } from 'contexts/ActiveAccounts';
import { useActiveBalances } from 'hooks/useActiveBalances';
import { useBonded } from 'contexts/Bonded';
import { SyncController } from 'controllers/SyncController';
import { useApi } from 'contexts/Api';
import { ActivePoolsController } from 'controllers/ActivePoolsController';
import { useCreatePoolAccounts } from 'hooks/useCreatePoolAccounts';

export const BalancesContext = createContext<BalancesContextInterface>(
defaults.defaultBalancesContext
Expand All @@ -22,8 +25,10 @@ export const BalancesContext = createContext<BalancesContextInterface>(
export const useBalances = () => useContext(BalancesContext);

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

Expand All @@ -46,9 +51,24 @@ export const BalancesProvider = ({ children }: { children: ReactNode }) => {
isCustomEvent(e) &&
BalancesController.isValidNewAccountBalanceEvent(e)
) {
// Update whether all account balances have been synced. Uses greater than to account for
// possible errors on the API side.
// Update whether all account balances have been synced.
checkBalancesSynced();

const { address, ...newBalances } = e.detail;
const { poolMembership } = newBalances;

// If a pool membership exists, let `ActivePools` know of pool membership to re-sync pool
// details and nominations.
if (api && poolMembership) {
const { poolId } = poolMembership;
const newPools = ActivePoolsController.getformattedPoolItems(
address
).concat({
id: String(poolId),
addresses: { ...createPoolAccounts(Number(poolId)) },
});
ActivePoolsController.syncPools(api, address, newPools);
}
}
};

Expand Down
47 changes: 24 additions & 23 deletions src/contexts/Pools/ActivePool/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,35 +68,34 @@ export const ActivePoolProvider = ({ children }: { children: ReactNode }) => {
setStateWithRef(id, setActivePoolIdState, activePoolIdRef);
};

// Only listen to the currently selected active pool, otherwise return an empty array.
const poolIds = activePoolIdRef.current ? [activePoolIdRef.current] : [];

// Listen for active pools. NOTE: `activePoolsRef` is needed to check if the pool has changed
// after the async call of fetching pending rewards.
const { activePools, activePoolsRef, poolNominations } = useActivePools({
poolIds,
onCallback: async () => {
// Sync: active pools synced once all account pools have been reported.
if (accountPoolIds.length <= ActivePoolsController.pools.length) {
SyncController.dispatch('active-pools', 'complete');
}
},
});
// Only listen to the active account's active pools, otherwise return an empty array. NOTE:
// `activePoolsRef` is needed to check if the pool has changed after the async call of fetching
// pending rewards.
const { getActivePools, activePoolsRef, getPoolNominations } = useActivePools(
{
who: activeAccount,
onCallback: async () => {
// Sync: active pools synced once all account pools have been reported.
if (
accountPoolIds.length <=
ActivePoolsController.getPools(activeAccount).length
) {
SyncController.dispatch('active-pools', 'complete');
}
},
}
);

// Store the currently active pool's pending rewards for the active account.
const [pendingPoolRewards, setPendingPoolRewards] = useState<BigNumber>(
new BigNumber(0)
);

const activePool =
activePoolId && activePools[activePoolId]
? activePools[activePoolId]
: null;
const activePool = activePoolId ? getActivePools(activePoolId) : null;

const activePoolNominations =
activePoolId && poolNominations[activePoolId]
? poolNominations[activePoolId]
: null;
const activePoolNominations = activePoolId
? getPoolNominations(activePoolId)
: null;

// Sync active pool subscriptions.
const syncActivePoolSubscriptions = async () => {
Expand All @@ -105,7 +104,9 @@ export const ActivePoolProvider = ({ children }: { children: ReactNode }) => {
id: pool,
addresses: { ...createPoolAccounts(Number(pool)) },
}));
ActivePoolsController.syncPools(api, newActivePools);

SyncController.dispatch('active-pools', 'syncing');
ActivePoolsController.syncPools(api, activeAccount, newActivePools);
} else {
// No active pools to sync. Mark as complete.
SyncController.dispatch('active-pools', 'complete');
Expand Down
4 changes: 2 additions & 2 deletions src/contexts/Pools/JoinPools/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ export const JoinPoolsProvider = ({ children }: { children: ReactNode }) => {
({ state }) => state === 'Open'
);

// Filter pools that do not have at least double the minimum active stake in points. NOTE:
// assumes that points are a 1:1 ratio between balance and points.
// Filter pools that do not have at least double the minimum stake to earn rewards, in points.
// NOTE: assumes that points are a 1:1 ratio between balance and points.
const rewardBondedPools = activeBondedPools.filter(({ points }) => {
const pointsBn = new BigNumber(rmCommas(points));
const threshold = minimumActiveStake.multipliedBy(2);
Expand Down
2 changes: 1 addition & 1 deletion src/contexts/Staking/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ export const StakingProvider = ({ children }: { children: ReactNode }) => {
}
}, [apiStatus]);

// handle syncing with eraStakers
// handle syncing with eraStakers.
useEffectIgnoreInitial(() => {
if (isReady) {
fetchActiveEraStakers();
Expand Down
Loading

0 comments on commit 1a1847d

Please sign in to comment.