From ac8deadecee15ce6a8f04173df44e4e70e867137 Mon Sep 17 00:00:00 2001 From: Jaco Date: Wed, 30 Mar 2022 10:07:58 +0200 Subject: [PATCH] Rebag on lower limit breached (#7270) --- packages/page-staking/src/Bags/Bag.tsx | 4 +++- packages/page-staking/src/Bags/Stash.tsx | 14 +++++++++----- packages/page-staking/src/Bags/index.tsx | 9 +++++---- packages/page-staking/src/Bags/useBagsList.ts | 18 ++++++++++++++---- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/packages/page-staking/src/Bags/Bag.tsx b/packages/page-staking/src/Bags/Bag.tsx index 426188a5dc..2bec55cffe 100644 --- a/packages/page-staking/src/Bags/Bag.tsx +++ b/packages/page-staking/src/Bags/Bag.tsx @@ -18,11 +18,12 @@ import useBonded from './useBonded'; interface Props { info: PalletBagsListListBag; + lower: BN; stashNodes?: StashNode[]; upper: BN; } -function Bag ({ info, stashNodes, upper }: Props): React.ReactElement { +function Bag ({ info, lower, stashNodes, upper }: Props): React.ReactElement { const [[headId, trigger], setHeadId] = useState<[AccountId32 | null, number]>([null, 0]); const [isLoading, setLoading] = useState(true); const [isCompleted, list] = useBagEntries(headId, trigger); @@ -52,6 +53,7 @@ function Bag ({ info, stashNodes, upper }: Props): React.ReactElement { isLoading={isLoading} key={stashId} list={bonded} + lower={lower} stashId={stashId} upper={upper} /> diff --git a/packages/page-staking/src/Bags/Stash.tsx b/packages/page-staking/src/Bags/Stash.tsx index b5b2dd1081..787c41ce94 100644 --- a/packages/page-staking/src/Bags/Stash.tsx +++ b/packages/page-staking/src/Bags/Stash.tsx @@ -16,6 +16,7 @@ interface Props { className?: string; isLoading: boolean; list?: ListNode[]; + lower: BN; stashId: string; upper: BN; } @@ -27,13 +28,16 @@ interface Entry { stashInfo: ListNode | null; } -function findEntry (upper: BN, stashId: string, list: ListNode[] = []): Entry { +function findEntry (upper: BN, lower: BN, stashId: string, list: ListNode[] = []): Entry { const stashInfo = list.find((o) => o.stashId === stashId) || null; const other = (stashInfo && stashInfo.jump && list.find((o) => o.stashId === stashInfo.jump)) || null; return { canJump: !!other, - canRebag: !!stashInfo && stashInfo.bonded.gt(upper), + canRebag: !!stashInfo && ( + stashInfo.bonded.gt(upper) || + stashInfo.bonded.lt(lower) + ), jumpCount: stashInfo && other ? (stashInfo.index - other.index) : 0, @@ -41,12 +45,12 @@ function findEntry (upper: BN, stashId: string, list: ListNode[] = []): Entry { }; } -function Stash ({ className, isLoading, list, stashId, upper }: Props): React.ReactElement { +function Stash ({ className, isLoading, list, lower, stashId, upper }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const { canJump, canRebag, jumpCount, stashInfo } = useMemo( - () => findEntry(upper, stashId, list), - [list, stashId, upper] + () => findEntry(upper, lower, stashId, list), + [list, lower, stashId, upper] ); return ( diff --git a/packages/page-staking/src/Bags/index.tsx b/packages/page-staking/src/Bags/index.tsx index b0c77f4f79..c1f4d81d54 100644 --- a/packages/page-staking/src/Bags/index.tsx +++ b/packages/page-staking/src/Bags/index.tsx @@ -43,11 +43,11 @@ function Bags ({ ownStashes }: Props): React.ReactElement { ? [...list].sort((a, b) => nodes[a.key] ? nodes[b.key] - ? b.id.cmp(a.id) + ? 0 : -1 : nodes[b.key] ? 1 - : b.id.cmp(a.id) + : 0 ) : null, [list, nodes] @@ -61,12 +61,13 @@ function Bags ({ ownStashes }: Props): React.ReactElement { emptySpinner={t('Retrieving all available bags, this will take some time')} header={headerRef.current} > - {sorted && sorted.map(({ id, info, key }) => ( + {sorted && sorted.map(({ info, key, lower, upper }) => ( ))} diff --git a/packages/page-staking/src/Bags/useBagsList.ts b/packages/page-staking/src/Bags/useBagsList.ts index 5cc8594d10..a1c46591ac 100644 --- a/packages/page-staking/src/Bags/useBagsList.ts +++ b/packages/page-staking/src/Bags/useBagsList.ts @@ -8,19 +8,29 @@ import type { BN } from '@polkadot/util'; import { useEffect, useState } from 'react'; import { createNamedHook, useApi, useCall } from '@polkadot/react-hooks'; +import { BN_ZERO } from '@polkadot/util'; interface Result { - id: BN; info: PalletBagsListListBag; key: string; + lower: BN; + upper: BN; } const multiOptions = { - transform: ([[ids], opts]: [[BN[]], Option[]]): Result[] => - ids + transform: ([[ids], opts]: [[BN[]], Option[]]): Result[] => { + const sorted = ids .map((id, index): [BN, Option] => [id, opts[index]]) .filter(([, o]) => o.isSome) - .map(([id, o]): Result => ({ id, info: o.unwrap(), key: id.toString() })), + .map(([upper, o]): Result => ({ info: o.unwrap(), key: upper.toString(), lower: BN_ZERO, upper })) + .sort((a, b) => b.upper.cmp(a.upper)); + + return sorted.map((entry, index) => + (index === (sorted.length - 1)) + ? entry + : { ...entry, lower: sorted[index + 1].upper } + ); + }, withParamsTransform: true };