forked from PolkaGate/extension
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add useLockedInReferenda (PolkaGate#1608)
Co-authored-by: Amir Ekbatanifard <[email protected]>
- Loading branch information
1 parent
5a70c77
commit 7d55deb
Showing
8 changed files
with
136 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 6 additions & 5 deletions
11
packages/extension-polkagate/src/hooks/useCurrentBlockNumber.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 35 additions & 22 deletions
57
packages/extension-polkagate/src/hooks/useHasDelegated.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,66 @@ | ||
// Copyright 2019-2024 @polkadot/extension-polkagate authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// @ts-nocheck | ||
|
||
import type { ApiPromise } from '@polkadot/api'; | ||
//@ts-ignore | ||
import type { PalletConvictionVotingVoteVoting } from '@polkadot/types/lookup'; | ||
import type { BN } from '@polkadot/util'; | ||
|
||
import { useEffect, useState } from 'react'; | ||
|
||
import { BN, BN_ZERO } from '@polkadot/util'; | ||
import { BN_ZERO } from '@polkadot/util'; | ||
|
||
import useApi from './useApi'; | ||
import useFormatted from './useFormatted'; | ||
import { useChain, useTracks } from '.'; | ||
import { useInfo, useTracks } from '.'; | ||
|
||
export default function useHasDelegated(address: string | undefined, refresh?: boolean): BN | null | undefined { | ||
const api = useApi(address); | ||
const formatted = useFormatted(address); | ||
const chain = useChain(address); | ||
export default function useHasDelegated (address: string | undefined, refresh?: boolean): BN | null | undefined { | ||
const { api, chain, formatted } = useInfo(address); | ||
const { tracks } = useTracks(address); | ||
|
||
const [hasDelegated, setHasDelegated] = useState<BN | null>(); | ||
const [fetchedFor, setFetchedFor] = useState<string | undefined>(undefined); | ||
|
||
useEffect(() => { | ||
if (refresh) { | ||
setHasDelegated(undefined); | ||
setFetchedFor(undefined); | ||
} | ||
|
||
if (!api || !formatted || !tracks || !tracks?.length || !api?.query?.convictionVoting) { | ||
if (!api || !formatted || !tracks?.length || !api?.query?.['convictionVoting'] || fetchedFor === address) { | ||
return; | ||
} | ||
|
||
if (chain?.genesisHash && api && api.genesisHash.toString() !== chain.genesisHash) { | ||
return; | ||
} | ||
|
||
const params: [string, BN][] = tracks.map((t) => [String(formatted), t[0]]); | ||
const fetchDelegationData = async ( | ||
api: ApiPromise, | ||
formatted: string, | ||
tracks: [BN, unknown][] | ||
): Promise<void> => { | ||
try { | ||
setFetchedFor(address); | ||
|
||
// eslint-disable-next-line no-void | ||
void api.query.convictionVoting.votingFor.multi(params).then((votingFor: PalletConvictionVotingVoteVoting[]) => { | ||
let maxDelegated = BN_ZERO; | ||
const params: [string, BN][] = tracks.map((t) => [formatted, t[0]]); | ||
const votingFor: PalletConvictionVotingVoteVoting[] = await api.query['convictionVoting']['votingFor'].multi(params); | ||
|
||
votingFor?.filter((v) => v.isDelegating).forEach((v) => { | ||
if (v.asDelegating.balance.gt(maxDelegated)) { | ||
maxDelegated = v.asDelegating.balance; | ||
} | ||
}); | ||
const maxDelegated = votingFor | ||
.filter((v) => v.isDelegating) | ||
.reduce((max, v) => { | ||
const balance = v.asDelegating.balance; | ||
|
||
maxDelegated.isZero() ? setHasDelegated(null) : setHasDelegated(maxDelegated); | ||
}); | ||
}, [api, chain?.genesisHash, formatted, tracks, refresh]); | ||
return balance.gt(max) ? balance : max; | ||
}, BN_ZERO); | ||
|
||
setHasDelegated(maxDelegated.isZero() ? null : maxDelegated); | ||
} catch (error) { | ||
console.error('Error fetching delegation data:', error); | ||
setFetchedFor(undefined); | ||
} | ||
}; | ||
|
||
fetchDelegationData(api, formatted, tracks).catch(console.error); | ||
}, [api, chain?.genesisHash, formatted, tracks, refresh, fetchedFor, address]); | ||
|
||
return hasDelegated; | ||
} |
54 changes: 54 additions & 0 deletions
54
packages/extension-polkagate/src/hooks/useLockedInReferenda.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2019-2024 @polkadot/extension-polkagate authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import type { BN } from '@polkadot/util'; | ||
|
||
import { useMemo } from 'react'; | ||
|
||
import { BN_MAX_INTEGER } from '@polkadot/util'; | ||
|
||
import { useAccountLocks, useCurrentBlockNumber, useHasDelegated, useTimeToUnlock } from '.'; | ||
|
||
interface Lock { | ||
classId: BN; | ||
endBlock: BN; | ||
locked: string; | ||
refId: BN | 'N/A'; | ||
total: BN; | ||
} | ||
|
||
interface OutputType { | ||
classToUnlock: Lock[] | undefined; | ||
delegatedBalance: BN | null | undefined; | ||
hasDescription: boolean; | ||
isDisable: boolean; | ||
lockedInRef: BN | undefined | ||
timeToUnlock: string | null | undefined; | ||
totalLocked: BN | null | undefined; | ||
unlockableAmount: BN | undefined; | ||
} | ||
|
||
export default function useLockedInReferenda (address: string | undefined, refreshNeeded: boolean | undefined): OutputType { | ||
const delegatedBalance = useHasDelegated(address, refreshNeeded); | ||
const referendaLocks = useAccountLocks(address, 'referenda', 'convictionVoting', false, refreshNeeded); | ||
const currentBlock = useCurrentBlockNumber(address); | ||
const { lockedInRef, timeToUnlock, totalLocked, unlockableAmount } = useTimeToUnlock(address, delegatedBalance, referendaLocks, refreshNeeded); | ||
|
||
const classToUnlock = currentBlock ? referendaLocks?.filter((ref) => ref.endBlock.ltn(currentBlock) && ref.classId.lt(BN_MAX_INTEGER)) : undefined; | ||
const isDisable = useMemo(() => !unlockableAmount || unlockableAmount.isZero() || !classToUnlock || !totalLocked, [classToUnlock, totalLocked, unlockableAmount]); | ||
|
||
const hasDescription = useMemo(() => | ||
Boolean((unlockableAmount && !unlockableAmount.isZero()) || (delegatedBalance && !delegatedBalance.isZero()) || timeToUnlock) | ||
, [delegatedBalance, timeToUnlock, unlockableAmount]); | ||
|
||
return { | ||
classToUnlock, | ||
delegatedBalance, | ||
hasDescription, | ||
isDisable, | ||
lockedInRef, | ||
timeToUnlock, | ||
totalLocked, | ||
unlockableAmount | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.