From 53c3a1643a35548cfdeb45ab99a14cf0fbb38c65 Mon Sep 17 00:00:00 2001 From: Thibaut Sardan Date: Mon, 23 Sep 2024 18:50:14 +0100 Subject: [PATCH 1/2] partial unlock --- src/contexts/LocksContext.tsx | 42 +++++++++++++++++-- src/hooks/useGetSigningCallback.tsx | 2 +- src/pages/Delegate/MultiTransactionDialog.tsx | 9 +--- 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/src/contexts/LocksContext.tsx b/src/contexts/LocksContext.tsx index aa5c6fd..9f86f6f 100644 --- a/src/contexts/LocksContext.tsx +++ b/src/contexts/LocksContext.tsx @@ -75,6 +75,11 @@ type LocksContextProps = { children: React.ReactNode | React.ReactNode[] } +export interface TrackLock { + trackId: number + amount: bigint +} + export interface ConvictionDisplay { multiplier?: number display?: string @@ -88,6 +93,7 @@ export interface ILocksContext { conviction: number | string, ) => ConvictionDisplay refreshLocks: () => void + maxLocked: bigint } const LocksContext = createContext(undefined) @@ -97,7 +103,7 @@ const LocksContextProvider = ({ children }: LocksContextProps) => { const { api } = useNetwork() const [forcerefresh, setForceRefresh] = useState(0) - const [lockTracks, setLockTracks] = useState([]) + const [lockTracks, setLockTracks] = useState([]) const [stateOfRefs, setStateOfRefs] = useState({}) const [currentVoteLocks, setCurrentVoteLocks] = useState< { @@ -109,6 +115,13 @@ const LocksContextProvider = ({ children }: LocksContextProps) => { const [convictionLocksMap, setConvictionLocksMap] = useState< Record >({}) + const maxLocked = useMemo(() => { + let max = 0n + lockTracks.forEach(({ amount }) => { + if (amount > max) max = amount + }) + return max + }, [lockTracks]) const refreshLocks = useCallback(() => { setForceRefresh((prev) => prev + 1) @@ -131,7 +144,10 @@ const LocksContextProvider = ({ children }: LocksContextProps) => { selectedAccount.address, 'best', ).subscribe((value) => { - const trackIdArray = value.map(([trackId]) => trackId) + const trackIdArray = value.map(([trackId, amount]) => ({ + trackId, + amount, + })) setLockTracks(trackIdArray) }) @@ -177,6 +193,7 @@ const LocksContextProvider = ({ children }: LocksContextProps) => { currentVoteLocks.forEach(({ trackId, vote: { type, value } }) => { // when the account is currently delegating // when it undelegated, this will have the type Casting + // unless it's delegated again if (type === 'Delegating') { const prev = delegations[value.target] || [] @@ -188,6 +205,8 @@ const LocksContextProvider = ({ children }: LocksContextProps) => { conviction: value.conviction, }, ] + + // it may have a prior, if it's been delegating before } else if (type === 'Casting') { // this is when the account has casted a vote directly // or it's undelegating in which `votes` is empty @@ -198,9 +217,23 @@ const LocksContextProvider = ({ children }: LocksContextProps) => { vote, } }) + } + + // this is when the account is undelegating + if (value.prior[1] > 0) { + // if we're now delegating less than before + if (type === 'Delegating' && value.prior[1] - value.balance > 0) { + delegationLocks.push({ + type: LockType.Delegating, + trackId, + amount: value.prior[1] - value.balance, + endBlock: value.prior[0], + }) + } - // this is when the account is undelegating - if (value.prior[1] > 0) { + // we're not delegating any more on this track + // so we have some lock from previous delegations + if (type === 'Casting') { delegationLocks.push({ type: LockType.Delegating, trackId, @@ -413,6 +446,7 @@ const LocksContextProvider = ({ children }: LocksContextProps) => { getConvictionLockTimeDisplay, delegationLocks, refreshLocks, + maxLocked, }} > {children} diff --git a/src/hooks/useGetSigningCallback.tsx b/src/hooks/useGetSigningCallback.tsx index 67a7dff..ec0f336 100644 --- a/src/hooks/useGetSigningCallback.tsx +++ b/src/hooks/useGetSigningCallback.tsx @@ -25,8 +25,8 @@ export const useGetSigningCallback = } if (event.type === 'txBestBlocksState') { toast.success(`Tx in block`) + onInBlock && onInBlock() } - onInBlock && onInBlock() if (event.type === 'finalized') { toast.success(`Tx finalized in block: ${event.block.number}`) onFinalized && onFinalized() diff --git a/src/pages/Delegate/MultiTransactionDialog.tsx b/src/pages/Delegate/MultiTransactionDialog.tsx index 80ee015..ec9117f 100644 --- a/src/pages/Delegate/MultiTransactionDialog.tsx +++ b/src/pages/Delegate/MultiTransactionDialog.tsx @@ -34,7 +34,6 @@ export const MultiTransactionDialog = ({ const [isTxInitiated, setIsTxInitiated] = useState(false) const { isExhaustsResources } = useTestTx() const { selectedAccount } = useAccounts() - const [waitingForFinalization, setWaitingForFinalization] = useState(false) const [promptForHelpCallData, setPromptForHelpCallData] = useState('') const getSubscriptionCallBack = useGetSigningCallback() @@ -105,16 +104,11 @@ export const MultiTransactionDialog = ({ const subscriptionCallBack2 = getSubscriptionCallBack({ onError: () => { setIsTxInitiated(false) - setWaitingForFinalization(true) }, onInBlock: () => { - setWaitingForFinalization(true) onProcessFinished() setIsTxInitiated(false) }, - onFinalized: () => { - setWaitingForFinalization(false) - }, }) await step2Txs @@ -149,8 +143,7 @@ export const MultiTransactionDialog = ({ disabled={isTxInitiated} loading={isTxInitiated} > - {!waitingForFinalization && `Sign transaction ${step} / 2`} - {waitingForFinalization && 'Waiting for finalization'} + Sign transaction ${step} / 2 From 868c62752a42a64169b65b0a56452123f98e08f6 Mon Sep 17 00:00:00 2001 From: Thibaut Sardan Date: Mon, 23 Sep 2024 18:52:03 +0100 Subject: [PATCH 2/2] nit --- src/pages/Delegate/MultiTransactionDialog.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/Delegate/MultiTransactionDialog.tsx b/src/pages/Delegate/MultiTransactionDialog.tsx index ec9117f..c82957e 100644 --- a/src/pages/Delegate/MultiTransactionDialog.tsx +++ b/src/pages/Delegate/MultiTransactionDialog.tsx @@ -143,7 +143,7 @@ export const MultiTransactionDialog = ({ disabled={isTxInitiated} loading={isTxInitiated} > - Sign transaction ${step} / 2 + Sign transaction {step} / 2