Skip to content

Commit

Permalink
Merge branch 'main' into nik-prepare-people-parachain
Browse files Browse the repository at this point in the history
  • Loading branch information
wirednkod authored Sep 24, 2024
2 parents e808560 + 795576c commit 8bc86c1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
42 changes: 38 additions & 4 deletions src/contexts/LocksContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -88,6 +93,7 @@ export interface ILocksContext {
conviction: number | string,
) => ConvictionDisplay
refreshLocks: () => void
maxLocked: bigint
}

const LocksContext = createContext<ILocksContext | undefined>(undefined)
Expand All @@ -97,7 +103,7 @@ const LocksContextProvider = ({ children }: LocksContextProps) => {
const { api } = useNetwork()

const [forcerefresh, setForceRefresh] = useState(0)
const [lockTracks, setLockTracks] = useState<number[]>([])
const [lockTracks, setLockTracks] = useState<TrackLock[]>([])
const [stateOfRefs, setStateOfRefs] = useState<StateOfRefs>({})
const [currentVoteLocks, setCurrentVoteLocks] = useState<
{
Expand All @@ -109,6 +115,13 @@ const LocksContextProvider = ({ children }: LocksContextProps) => {
const [convictionLocksMap, setConvictionLocksMap] = useState<
Record<string, bigint>
>({})
const maxLocked = useMemo(() => {
let max = 0n
lockTracks.forEach(({ amount }) => {
if (amount > max) max = amount
})
return max
}, [lockTracks])

const refreshLocks = useCallback(() => {
setForceRefresh((prev) => prev + 1)
Expand All @@ -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)
})

Expand Down Expand Up @@ -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] || []

Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -413,6 +446,7 @@ const LocksContextProvider = ({ children }: LocksContextProps) => {
getConvictionLockTimeDisplay,
delegationLocks,
refreshLocks,
maxLocked,
}}
>
{children}
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useGetSigningCallback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 1 addition & 8 deletions src/pages/Delegate/MultiTransactionDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -149,8 +143,7 @@ export const MultiTransactionDialog = ({
disabled={isTxInitiated}
loading={isTxInitiated}
>
{!waitingForFinalization && `Sign transaction ${step} / 2`}
{waitingForFinalization && 'Waiting for finalization'}
Sign transaction {step} / 2
</Button>
</div>
</DialogDescription>
Expand Down

0 comments on commit 8bc86c1

Please sign in to comment.