Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TOK-488: block allocs in distribution #438

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions src/app/collective-rewards/leaderboard/BuilderLeaderBoard.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { BuildersLeaderBoardContent } from '@/app/collective-rewards/leaderboard'
import { CycleContextProvider } from '@/app/collective-rewards/metrics'
import { useReadBackersManager } from '@/app/collective-rewards/shared'
import { Button } from '@/components/Button'
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/Collapsible'
import { Popover } from '@/components/Popover'
import { HeaderTitle } from '@/components/Typography'
import { BuildersLeaderBoardContent } from '@/app/collective-rewards/leaderboard'
import { DateTime } from 'luxon'
import { useRouter } from 'next/navigation'

export const BuildersLeaderBoard = () => {
const router = useRouter()
const { data: isInDistributionPeriod } = useReadBackersManager('onDistributionPeriod')
const { data: distributionEndTimestamp } = useReadBackersManager('endDistributionWindow', [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we already have useGetEndDistributionWindow

BigInt(DateTime.now().toUnixInteger()),
])

const onManageAllocations = () => {
router.push('/collective-rewards/allocations')
}
Expand All @@ -17,9 +25,25 @@ export const BuildersLeaderBoard = () => {
<CollapsibleTrigger>
<div className="flex items-center justify-between w-full">
<HeaderTitle className="">Rewards leaderboard</HeaderTitle>
<Button variant="primary" onClick={onManageAllocations}>
Manage Allocations
</Button>

<Popover
content={
<>
<p>Distribution in progress.</p>
<p>Ends on: {DateTime.fromSeconds(Number(distributionEndTimestamp)).toLocaleString()}</p>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not necessarily true; once the distribution started, it depends on when the last distribute method is called, so I don't think we can predict it.

</>
}
trigger="hover"
disabled={!isInDistributionPeriod}
>
<Button
variant="primary"
onClick={onManageAllocations}
disabled={isInDistributionPeriod as boolean}
>
Manage Allocations
</Button>
</Popover>
</div>
</CollapsibleTrigger>
<CollapsibleContent>
Expand Down
1 change: 1 addition & 0 deletions src/app/collective-rewards/shared/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './useReporting'
export * from './useGaugesGetFunction'
export * from './useReadBackersManager'
37 changes: 37 additions & 0 deletions src/app/collective-rewards/shared/hooks/useReadBackersManager.ts
Copy link
Collaborator

@antomor antomor Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I like the idea, I think it's more practical using specific named hooks because:

  • it's easier to understand what they do
  • return type is well-defined.
    Using the function like that assumes that the user knows where the function is defined (in which contract). Although this is valid for us, it may not be for other users.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { BackersManagerAbi } from '@/lib/abis/v2/BackersManagerAbi'
import { AVERAGE_BLOCKTIME } from '@/lib/constants'
import { BackersManagerAddress } from '@/lib/contracts'
import { AbiFunction, ContractFunctionArgs } from 'viem'
import { useReadContract, UseReadContractParameters, UseReadContractReturnType } from 'wagmi'

// TODO: add type narrowing to return only the type related to the functionName
type FunctionEntry = Extract<(typeof BackersManagerAbi)[number], AbiFunction>
type ViewFunctionEntry = Extract<
FunctionEntry,
{
stateMutability: 'view'
}
>
type BackersManagerViewFunction = Exclude<ViewFunctionEntry['name'], 'UPGRADE_INTERFACE_VERSION'>
type FunctionParams<Name extends ViewFunctionEntry['name'] = BackersManagerViewFunction> =
ContractFunctionArgs<typeof BackersManagerAbi, 'view', Name>

type UseReadBackersManager<Name extends ViewFunctionEntry['name'] = BackersManagerViewFunction> =
UseReadContractReturnType<typeof BackersManagerAbi, Name>

export const useReadBackersManager = (
functionName: BackersManagerViewFunction,
functionParams?: FunctionParams<BackersManagerViewFunction>,
query?: UseReadContractParameters<typeof BackersManagerAbi, BackersManagerViewFunction>['query'],
): UseReadBackersManager<BackersManagerViewFunction> => {
return useReadContract({
functionName,
abi: BackersManagerAbi,
address: BackersManagerAddress,
args: functionParams,
query: {
refetchInterval: AVERAGE_BLOCKTIME,
...query,
},
})
}
Loading