Skip to content

Commit

Permalink
feat: migration support
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscotobar committed Feb 11, 2025
1 parent ee73a78 commit 359353c
Show file tree
Hide file tree
Showing 19 changed files with 347 additions and 299 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('useAllocateVotes', () => {
test('should call contract with allocate function in args', () => {
vi.mocked(getVoteAllocations).mockReturnValue([['0x123'], [1n]])

renderHook(async () => {
renderHook(() => {
const { saveAllocations } = useAllocateVotes()

saveAllocations()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { BackerRewardsConfig, Builder } from '@/app/collective-rewards/types'
import { BuilderRegistryAbi } from '@/lib/abis/v2/BuilderRegistryAbi'
import { AVERAGE_BLOCKTIME } from '@/lib/constants'
import { BackersManagerAddress } from '@/lib/contracts'
import { Modify } from '@/shared/utility'
import { useMemo } from 'react'
import { ContractFunctionReturnType, ReadContractErrorType } from 'viem'
import { UseReadContractReturnType, useReadContracts } from 'wagmi'
import { useEnvironmentsContext } from '@/shared/context/EnvironmentsContext'

type RawBackerRewardPercentage = ContractFunctionReturnType<
typeof BuilderRegistryAbi,
Expand All @@ -25,15 +25,17 @@ type UseGetBackerRewardsReturnType = Pick<
type UseGetBackerRewards = (builders: Builder[]) => UseGetBackerRewardsReturnType

export const useGetBackerRewards: UseGetBackerRewards = builders => {
const { builderRegistryAddress } = useEnvironmentsContext()

const backerRewardCalls = builders.map(({ address }) => ({
address: BackersManagerAddress,
address: builderRegistryAddress,
abi: BuilderRegistryAbi,
functionName: 'backerRewardPercentage',
args: [address],
}))

const activePercentageCalls = builders.map(({ address }) => ({
address: BackersManagerAddress,
address: builderRegistryAddress,
abi: BuilderRegistryAbi,
functionName: 'getRewardPercentageToApply',
args: [address],
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './BackerRewardsContex'
export * from './BackerRewardsContext'
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { BuilderRegistryAbi } from '@/lib/abis/v2/BuilderRegistryAbi'
import { AVERAGE_BLOCKTIME } from '@/lib/constants'
import { BackersManagerAddress } from '@/lib/contracts'
import { useMemo } from 'react'
import { Address } from 'viem'
import { useReadContracts } from 'wagmi'
import { BackerRewardPercentage } from '../types'
import { getBackerRewardPercentage } from '../utils'
import { useEnvironmentsContext } from '@/shared/context/EnvironmentsContext'

export const useGetBackersRewardPercentage = (builders: Address[], timestampInSeconds?: number) => {
const { builderRegistryAddress } = useEnvironmentsContext()

const contractCalls = builders?.map(
builder =>
({
address: BackersManagerAddress,
address: builderRegistryAddress,
abi: BuilderRegistryAbi,
functionName: 'backerRewardPercentage',
args: [builder],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
useWriteContract,
UseWriteContractReturnType,
} from 'wagmi'
import { useEnvironmentsContext } from '@/shared/context/EnvironmentsContext'

export type BackerReward = {
previous: bigint
Expand All @@ -28,12 +29,14 @@ export type BackerRewardResponse = Modify<
>

export const useGetBackerRewardsForBuilder = (builder: Address): BackerRewardResponse => {
const { builderRegistryAddress } = useEnvironmentsContext()

const [previous, setPrevious] = useState<bigint>(0n)
const [next, setNext] = useState<bigint>(0n)
const [cooldown, setCooldown] = useState<bigint>(0n)
const { data, ...rest } = useReadContract({
address: builderRegistryAddress,
abi: BuilderRegistryAbi,
address: BackersManagerAddress,
functionName: 'backerRewardPercentage',
args: [builder as Address],
query: {
Expand Down Expand Up @@ -69,6 +72,7 @@ export type SetBackerRewardsForBuilder = {
} & Omit<UseWriteContractReturnType, 'error' | 'writeContractAsync'>

export const useSetBackerRewardsForBuilder = (): SetBackerRewardsForBuilder => {
const { builderRegistryAddress } = useEnvironmentsContext()
const { writeContractAsync, data, isPending, isSuccess, error: writeError, ...rest } = useWriteContract()
const { data: receipt, isLoading, error: receiptError } = useWaitForTransactionReceipt({ hash: data! })

Expand All @@ -87,8 +91,8 @@ export const useSetBackerRewardsForBuilder = (): SetBackerRewardsForBuilder => {

const setNewReward = async (newReward: bigint) => {
return await writeContractAsync({
address: builderRegistryAddress,
abi: BuilderRegistryAbi,
address: BackersManagerAddress,
functionName: 'setBackerRewardPercentage',
args: [newReward],
})
Expand Down
13 changes: 9 additions & 4 deletions src/app/collective-rewards/user/hooks/useGetBuilders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { useFetchCreateBuilderProposals } from '@/app/proposals/hooks/useFetchLa
import { splitCombinedName } from '@/app/proposals/shared/utils'
import { BuilderRegistryAbi } from '@/lib/abis/v2/BuilderRegistryAbi'
import { AVERAGE_BLOCKTIME } from '@/lib/constants'
import { BackersManagerAddress } from '@/lib/contracts'
import { DateTime } from 'luxon'
import { useMemo } from 'react'
import { Address, getAddress } from 'viem'
import { useReadContracts } from 'wagmi'
import { useEnvironmentsContext } from '@/shared/context/EnvironmentsContext'

export type UseGetBuilders = () => {
data: Record<Address, Builder> // TODO review Builder type
Expand All @@ -21,6 +21,8 @@ export type UseGetBuilders = () => {
}

export const useGetBuilders: UseGetBuilders = () => {
const { builderRegistryAddress } = useEnvironmentsContext()

/*
* // TODO: we're missing builder with KYC only on v2
* get Gauges
Expand All @@ -36,7 +38,7 @@ export const useGetBuilders: UseGetBuilders = () => {
const gaugeToBuilderCalls = gauges?.map(
gauge =>
({
address: BackersManagerAddress,
address: builderRegistryAddress,
abi: BuilderRegistryAbi,
functionName: 'gaugeToBuilder',
args: [gauge],
Expand Down Expand Up @@ -72,13 +74,16 @@ export const useGetBuilders: UseGetBuilders = () => {
builders?.map(
builder =>
({
address: BackersManagerAddress,
address: builderRegistryAddress,
abi: BuilderRegistryAbi,
functionName: 'builderState',
args: [builder],
}) as const,
),
[builders],
// disable the eslint rule because we don't need to call it again
// when the builderRegistryAddress changes
// to avoid multiple calls in a short period of time
[builders], // eslint-disable-line react-hooks/exhaustive-deps
)
const {
data: builderStatesResult,
Expand Down
6 changes: 4 additions & 2 deletions src/app/collective-rewards/user/hooks/useGetGaugesArray.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useReadContracts } from 'wagmi'
import { AVERAGE_BLOCKTIME } from '@/lib/constants'
import { BuilderRegistryAbi } from '@/lib/abis/v2/BuilderRegistryAbi'
import { BackersManagerAddress } from '@/lib/contracts'
import { AbiFunction, Address } from 'viem'
import { useGetGaugesLength } from '@/app/collective-rewards/user'
import { useMemo } from 'react'
import { useEnvironmentsContext } from '@/shared/context/EnvironmentsContext'

const gaugeTypeOptions = ['active', 'halted'] as const
export type GaugeType = (typeof gaugeTypeOptions)[number]
Expand Down Expand Up @@ -75,13 +75,15 @@ export const useGetGaugesArrayByType = (type: GaugeType) => {
}

const useGetContractCalls = (type: GaugeType) => {
const { builderRegistryAddress } = useEnvironmentsContext()

const { data: gaugesLength, isLoading, error } = useGetGaugesLength(type)

const length = Number(gaugesLength) ?? 0

const data = Array.from({ length }, (_, index) => {
return {
address: BackersManagerAddress,
address: builderRegistryAddress,
abi: BuilderRegistryAbi,
functionName: gaugeType[type],
args: [index],
Expand Down
6 changes: 4 additions & 2 deletions src/app/collective-rewards/user/hooks/useGetGaugesLength.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useReadContract } from 'wagmi'
import { AVERAGE_BLOCKTIME } from '@/lib/constants'
import { BuilderRegistryAbi } from '@/lib/abis/v2/BuilderRegistryAbi'
import { BackersManagerAddress } from '@/lib/contracts'
import { AbiFunction } from 'viem'
import { GaugeType } from '@/app/collective-rewards/user'
import { useEnvironmentsContext } from '@/shared/context/EnvironmentsContext'

type FunctionEntry = Extract<(typeof BuilderRegistryAbi)[number], AbiFunction>
type FunctionName = Extract<FunctionEntry['name'], 'getGaugesLength' | 'getHaltedGaugesLength'>
Expand All @@ -14,8 +14,10 @@ const gaugeType: Record<GaugeType, FunctionName> = {
}

export const useGetGaugesLength = (type: GaugeType) => {
const { builderRegistryAddress } = useEnvironmentsContext()

const { data, isLoading, error } = useReadContract({
address: BackersManagerAddress,
address: builderRegistryAddress,
abi: BuilderRegistryAbi,
functionName: gaugeType[type],
query: {
Expand Down
8 changes: 5 additions & 3 deletions src/app/collective-rewards/utils/getBuilderGauge.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { config } from '@/config'
import { BuilderRegistryAbi } from '@/lib/abis/v2/BuilderRegistryAbi'
import { BackersManagerAddress } from '@/lib/contracts'
import { Address } from 'viem'
import { readContract } from 'wagmi/actions'

export type RawBuilderState = readonly [boolean, boolean, boolean, boolean, boolean, string, string]

export const getBuilderGauge = async (builderAddress: Address): Promise<Address> => {
export const getBuilderGauge = async (
builderRegistryAddress: Address,
builderAddress: Address,
): Promise<Address> => {
return readContract(config, {
address: BackersManagerAddress,
address: builderRegistryAddress,
abi: BuilderRegistryAbi,
functionName: 'builderToGauge',
args: [builderAddress],
Expand Down
22 changes: 0 additions & 22 deletions src/app/collective-rewards/utils/getBuilderState.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/app/collective-rewards/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './getBuilderGauge'
export * from './getBuilderState'
export * from './getCoinbaseAddress'
export * from './getMostAdvancedProposal'
export * from './handleErrors'
Expand Down
4 changes: 3 additions & 1 deletion src/app/proposals/hooks/useCreateBuilderWhitelistProposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ import { Address, encodeFunctionData, zeroAddress } from 'viem'
import { useWriteContract } from 'wagmi'
import { createProposal, encodeGovernorRelayCallData } from './proposalUtils'
import { useVotingPower } from './useVotingPower'
import { useEnvironmentsContext } from '@/shared/context/EnvironmentsContext'

export const useCreateBuilderWhitelistProposal = () => {
const { builderRegistryAddress } = useEnvironmentsContext()
const { canCreateProposal } = useVotingPower()
const { writeContractAsync: propose, isPending: isPublishing, error: transactionError } = useWriteContract()

const onCreateBuilderWhitelistProposal = async (builderAddress: Address, description: string) => {
if (!canCreateProposal) {
throw NoVotingPowerError
}
const builderGauge = await getBuilderGauge(builderAddress)
const builderGauge = await getBuilderGauge(builderRegistryAddress, builderAddress)
if (builderGauge !== zeroAddress) {
// TODO: maybe we can use a different error here
throw AddressAlreadyWhitelistedError
Expand Down
13 changes: 8 additions & 5 deletions src/app/providers/ContextProviders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { ReactNode } from 'react'
import { WagmiProvider } from 'wagmi'
import { AlertProvider } from './AlertProvider'
import ErrorBoundary from '@/components/ErrorPage/ErrorBoundary'
import { BuilderContextProviderWithPrices } from '../collective-rewards/user'
import { AllocationsContextProvider } from '../collective-rewards/allocations/context'
import { BuilderContextProviderWithPrices } from '@/app/collective-rewards/user'
import { AllocationsContextProvider } from '@/app/collective-rewards/allocations/context'
import { EnvironmentsProvider } from '@/shared/context/EnvironmentsContext'

interface Props {
children: ReactNode
Expand All @@ -19,9 +20,11 @@ export const ContextProviders = ({ children }: Props) => {
<WagmiProvider config={config}>
<QueryClientProvider client={queryClient}>
<AlertProvider>
<BuilderContextProviderWithPrices>
<AllocationsContextProvider>{children}</AllocationsContextProvider>
</BuilderContextProviderWithPrices>
<EnvironmentsProvider>
<BuilderContextProviderWithPrices>
<AllocationsContextProvider>{children}</AllocationsContextProvider>
</BuilderContextProviderWithPrices>
</EnvironmentsProvider>
</AlertProvider>
</QueryClientProvider>
</WagmiProvider>
Expand Down
Loading

0 comments on commit 359353c

Please sign in to comment.