Skip to content

Commit

Permalink
feat(refactor): nominatons to BalancesController
Browse files Browse the repository at this point in the history
  • Loading branch information
Ross Bulat committed Feb 11, 2024
1 parent d40c148 commit 78c8300
Show file tree
Hide file tree
Showing 23 changed files with 100 additions and 88 deletions.
1 change: 1 addition & 0 deletions src/contexts/Balances/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export const defaultBalancesContext: BalancesContextInterface = {
getLedger: (source) => defaultLedger,
getPayee: (address) => defaultPayee,
getPoolMembership: (address) => null,
getNominations: (address) => [],
};
2 changes: 2 additions & 0 deletions src/contexts/Balances/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const BalancesProvider = ({ children }: { children: ReactNode }) => {
getLedger,
getPayee,
getPoolMembership,
getNominations,
} = useActiveBalances({
accounts: [activeAccount, activeProxy, controller],
});
Expand Down Expand Up @@ -96,6 +97,7 @@ export const BalancesProvider = ({ children }: { children: ReactNode }) => {
getLedger,
getPayee,
getPoolMembership,
getNominations,
}}
>
{children}
Expand Down
9 changes: 9 additions & 0 deletions src/contexts/Balances/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface BalancesContextInterface {
getLedger: (source: ActiveLedgerSource) => Ledger;
getPayee: (address: MaybeAddress) => PayeeConfig;
getPoolMembership: (address: MaybeAddress) => PoolMembership | null;
getNominations: (address: MaybeAddress) => Targets;
}

export type ActiveBalancesState = Record<string, ActiveBalance>;
Expand All @@ -23,6 +24,7 @@ export interface ActiveBalance {
balances: Balances;
payee: PayeeConfig;
poolMembership: PoolMembership;
nominations: Nominations;
}

export interface Balances {
Expand Down Expand Up @@ -67,3 +69,10 @@ export interface Ledger {
export type ActiveLedgerSource = {
[key in 'stash' | 'key']?: MaybeAddress;
};

export interface Nominations {
targets: Targets;
submittedIn: string | number;
}

export type Targets = string[];
7 changes: 2 additions & 5 deletions src/contexts/Bonded/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
// SPDX-License-Identifier: GPL-3.0-only
/* eslint-disable @typescript-eslint/no-unused-vars */

import type {
BondedContextInterface,
Nominations,
} from 'contexts/Bonded/types';
import type { Nominations } from 'contexts/Balances/types';
import type { BondedContextInterface } from 'contexts/Bonded/types';

export const nominations: Nominations = {
targets: [],
Expand All @@ -14,6 +12,5 @@ export const nominations: Nominations = {

export const defaultBondedContext: BondedContextInterface = {
getBondedAccount: (address) => null,
getAccountNominations: (address) => [],
bondedAccounts: [],
};
39 changes: 2 additions & 37 deletions src/contexts/Bonded/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import { useOtherAccounts } from 'contexts/Connect/OtherAccounts';
import { useExternalAccounts } from 'contexts/Connect/ExternalAccounts';
import * as defaults from './defaults';
import type { BondedAccount, BondedContextInterface } from './types';
import { useActiveAccounts } from 'contexts/ActiveAccounts';
import { SyncController } from 'static/SyncController';

export const BondedContext = createContext<BondedContextInterface>(
defaults.defaultBondedContext
Expand All @@ -32,7 +30,6 @@ export const BondedProvider = ({ children }: { children: ReactNode }) => {
const { network } = useNetwork();
const { api, isReady } = useApi();
const { accounts } = useImportedAccounts();
const { activeAccount } = useActiveAccounts();
const { addExternalAccount } = useExternalAccounts();
const { addOrReplaceOtherAccount } = useOtherAccounts();

Expand Down Expand Up @@ -66,15 +63,6 @@ export const BondedProvider = ({ children }: { children: ReactNode }) => {
const added = addedTo(accounts, bondedAccountsRef.current, ['address']);

if (added.length) {
// If the current active account is being subscribed to, dispatch the `nominator` syncing
// event.
const activeAccountInAdded = added.find(
({ address }) => address === activeAccount
);
if (activeAccountInAdded) {
SyncController.dispatch('nominator', 'syncing');
}

// Subscribe to all newly added accounts bonded and nominator status.
added.map(({ address }) => subscribeToBondedAccount(address));
}
Expand All @@ -100,11 +88,8 @@ export const BondedProvider = ({ children }: { children: ReactNode }) => {
}

const unsub = await api.queryMulti<AnyApi>(
[
[api.query.staking.bonded, address],
[api.query.staking.nominators, address],
],
async ([controller, nominations]) => {
[[api.query.staking.bonded, address]],
async ([controller]) => {
const newAccount: BondedAccount = {
address,
};
Expand All @@ -127,27 +112,12 @@ export const BondedProvider = ({ children }: { children: ReactNode }) => {
}
}

// set account nominations.
const newNominations = nominations.unwrapOr(null);
newAccount.nominations =
newNominations === null
? defaults.nominations
: {
targets: newNominations.targets.toHuman(),
submittedIn: newNominations.submittedIn.toHuman(),
};

// remove stale account if it's already in list.
const newBonded = Object.values(bondedAccountsRef.current)
.filter((a) => a.address !== address)
.concat(newAccount);

setStateWithRef(newBonded, setBondedAccounts, bondedAccountsRef);

// If this callback was syncing the active account, mark `nominator` syncing as complete.
if (address === activeAccount) {
SyncController.dispatch('nominator', 'complete');
}
}
);

Expand All @@ -159,10 +129,6 @@ export const BondedProvider = ({ children }: { children: ReactNode }) => {
bondedAccountsRef.current.find((a) => a.address === address)?.bonded ||
null;

const getAccountNominations = (address: MaybeAddress) =>
bondedAccountsRef.current.find((a) => a.address === address)?.nominations
?.targets || [];

// Handle accounts sync on connected accounts change.
useEffectIgnoreInitial(() => {
if (isReady) {
Expand All @@ -182,7 +148,6 @@ export const BondedProvider = ({ children }: { children: ReactNode }) => {
<BondedContext.Provider
value={{
getBondedAccount,
getAccountNominations,
bondedAccounts,
}}
>
Expand Down
9 changes: 0 additions & 9 deletions src/contexts/Bonded/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,9 @@ import type { MaybeAddress } from 'types';
export interface BondedAccount {
address?: string;
bonded?: string;
nominations?: Nominations;
}

export interface Nominations {
targets: Targets;
submittedIn: string | number;
}

export type Targets = string[];

export interface BondedContextInterface {
getBondedAccount: (address: MaybeAddress) => string | null;
getAccountNominations: (address: MaybeAddress) => Targets;
bondedAccounts: BondedAccount[];
}
2 changes: 1 addition & 1 deletion src/contexts/Pools/ActivePool/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import type BigNumber from 'bignumber.js';
import type { PoolAddresses } from '../BondedPools/types';
import type { MaybeAddress } from '@polkadot-cloud/react/types';
import type { Nominations } from 'contexts/Bonded/types';
import type { Identity, SuperIdentity } from 'contexts/Validators/types';
import type { Nominations } from 'contexts/Balances/types';

export interface ActivePoolContextState {
isBonding: () => boolean;
Expand Down
6 changes: 3 additions & 3 deletions src/contexts/Staking/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ export const StakingContext = createContext<StakingContextInterface>(
export const useStaking = () => useContext(StakingContext);

export const StakingProvider = ({ children }: { children: ReactNode }) => {
const { getLedger } = useBalances();
const { getBondedAccount } = useBonded();
const { networkData, network } = useNetwork();
const { getLedger, getNominations } = useBalances();
const { accounts: connectAccounts } = useImportedAccounts();
const { activeAccount, getActiveAccount } = useActiveAccounts();
const { getBondedAccount, getAccountNominations } = useBonded();
const { isReady, api, apiStatus, consts, activeEra, isPagedRewardsActive } =
useApi();
const { maxExposurePageSize } = consts;
Expand Down Expand Up @@ -224,7 +224,7 @@ export const StakingProvider = ({ children }: { children: ReactNode }) => {
hasController() && getLedger({ stash: activeAccount }).unlocking.length;

// Helper function to determine whether the active account is nominating, or is yet to start.
const isNominating = () => getAccountNominations(activeAccount).length > 0;
const isNominating = () => getNominations(activeAccount).length > 0;

// Helper function to determine whether the active account is nominating, or is yet to start.
const inSetup = () =>
Expand Down
14 changes: 14 additions & 0 deletions src/hooks/useActiveBalances/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
BalanceLock,
BalanceLocks,
Ledger,
Targets,
} from 'contexts/Balances/types';
import { useNetwork } from 'contexts/Network';
import { useEffect, useRef, useState } from 'react';
Expand Down Expand Up @@ -126,6 +127,18 @@ export const useActiveBalances = ({
return new BigNumber(0);
};

// Gets an active balance's nominations.
const getNominations = (address: MaybeAddress): Targets => {
if (address) {
const maybeNominations =
activeBalances[address]?.nominations?.targets || [];
if (maybeNominations) {
return maybeNominations;
}
}
return [];
};

// Handle new account balance event being reported from `BalancesController`.
const newAccountBalancesCallback = (e: Event) => {
if (
Expand Down Expand Up @@ -191,5 +204,6 @@ export const useActiveBalances = ({
getPayee,
getPoolMembership,
getEdReserved,
getNominations,
};
};
2 changes: 1 addition & 1 deletion src/hooks/useActivePools/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023 @paritytech/polkadot-staking-dashboard authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import type { Nominations } from 'contexts/Bonded/types';
import type { Nominations } from 'contexts/Balances/types';
import type { ActivePool } from 'contexts/Pools/ActivePool/types';
import type { DetailActivePool } from 'static/ActivePoolsController/types';

Expand Down
10 changes: 5 additions & 5 deletions src/hooks/useNominationStatus/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import { planckToUnit } from '@polkadot-cloud/utils';
import BigNumber from 'bignumber.js';
import { useTranslation } from 'react-i18next';
import { useBonded } from 'contexts/Bonded';
import { useActivePool } from 'contexts/Pools/ActivePool';
import { useStaking } from 'contexts/Staking';
import { useValidators } from 'contexts/Validators/ValidatorEntries';
import type { AnyJson, BondFor, MaybeAddress } from 'types';
import { useNetwork } from 'contexts/Network';
import { useSyncing } from 'hooks/useSyncing';
import { useBalances } from 'contexts/Balances';

export const useNominationStatus = () => {
const { t } = useTranslation();
Expand All @@ -20,19 +20,19 @@ export const useNominationStatus = () => {
const {
inSetup,
eraStakers,
getNominationsStatusFromTargets,
getLowestRewardFromStaker,
getNominationsStatusFromTargets,
} = useStaking();
const { validators } = useValidators();
const { getAccountNominations } = useBonded();
const { activePoolNominations } = useActivePool();
const { getNominations } = useBalances();
const { syncing } = useSyncing(['era-stakers']);
const { activePoolNominations } = useActivePool();

// Utility to get an account's nominees alongside their status.
const getNominationSetStatus = (who: MaybeAddress, type: BondFor) => {
const nominations =
type === 'nominator'
? getAccountNominations(who)
? getNominations(who)
: activePoolNominations?.targets ?? [];

return getNominationsStatusFromTargets(who, nominations);
Expand Down
2 changes: 1 addition & 1 deletion src/library/Graphs/PayoutBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const PayoutBar = ({
const { mode } = useTheme();
const { inSetup } = useStaking();
const { getPoolMembership } = useBalances();
const { syncing } = useSyncing(['nominator']);
const { syncing } = useSyncing(['balances']);
const { activeAccount } = useActiveAccounts();

const membership = getPoolMembership(activeAccount);
Expand Down
2 changes: 1 addition & 1 deletion src/library/Graphs/PayoutLine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const PayoutLine = ({
const { t } = useTranslation('library');
const { mode } = useTheme();
const { inSetup } = useStaking();
const { syncing } = useSyncing(['nominator']);
const { syncing } = useSyncing(['balances']);
const { getPoolMembership } = useBalances();
const { activeAccount } = useActiveAccounts();

Expand Down
6 changes: 3 additions & 3 deletions src/library/Nominations/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import { faCog, faStopCircle } from '@fortawesome/free-solid-svg-icons';
import { ButtonHelp, ButtonPrimary } from '@polkadot-cloud/react';
import { useTranslation } from 'react-i18next';
import { useBonded } from 'contexts/Bonded';
import { useHelp } from 'contexts/Help';
import { useActivePool } from 'contexts/Pools/ActivePool';
import { useStaking } from 'contexts/Staking';
Expand All @@ -19,6 +18,7 @@ import { useImportedAccounts } from 'contexts/Connect/ImportedAccounts';
import { ListStatusHeader } from 'library/List';
import { Wrapper } from './Wrapper';
import { useSyncing } from 'hooks/useSyncing';
import { useBalances } from 'contexts/Balances';

export const Nominations = ({
bondFor,
Expand All @@ -41,10 +41,10 @@ export const Nominations = ({
canvas: { openCanvas },
} = useOverlay();
const { syncing } = useSyncing('*');
const { getNominations } = useBalances();
const { isFastUnstaking } = useUnstaking();
const { formatWithPrefs } = useValidators();
const { activeAccount } = useActiveAccounts();
const { getAccountNominations } = useBonded();
const { isReadOnlyAccount } = useImportedAccounts();

// Determine if pool or nominator.
Expand All @@ -53,7 +53,7 @@ export const Nominations = ({
// Derive nominations from `bondFor` type.
const nominated =
bondFor === 'nominator'
? formatWithPrefs(getAccountNominations(activeAccount))
? formatWithPrefs(getNominations(activeAccount))
: activePoolNominations
? formatWithPrefs(activePoolNominations.targets)
: [];
Expand Down
8 changes: 5 additions & 3 deletions src/modals/StopNominations/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ import { SubmitTx } from 'library/SubmitTx';
import { useTxMeta } from 'contexts/TxMeta';
import { useOverlay } from '@polkadot-cloud/react/hooks';
import { useActiveAccounts } from 'contexts/ActiveAccounts';
import { useBalances } from 'contexts/Balances';

export const StopNominations = () => {
const { t } = useTranslation('modals');
const { api } = useApi();
const { activeAccount } = useActiveAccounts();
const { notEnoughFunds } = useTxMeta();
const { getBondedAccount } = useBonded();
const { getNominations } = useBalances();
const { activeAccount } = useActiveAccounts();
const { getSignerWarnings } = useSignerWarnings();
const { getBondedAccount, getAccountNominations } = useBonded();
const {
setModalStatus,
config: { options },
Expand All @@ -44,7 +46,7 @@ export const StopNominations = () => {
const nominations =
isPool === true
? activePoolNominations?.targets || []
: getAccountNominations(activeAccount);
: getNominations(activeAccount);

// valid to submit transaction
const [valid, setValid] = useState<boolean>(false);
Expand Down
Loading

0 comments on commit 78c8300

Please sign in to comment.