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 9ffdda6
Show file tree
Hide file tree
Showing 20 changed files with 342 additions and 293 deletions.
1 change: 1 addition & 0 deletions .env.testnet.local
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ NEXT_PUBLIC_RNS_REGISTRY_ADDRESS=0x7d284aaac6e925aad802a53c0c69efe3764597b8

# CR-related env variables
NEXT_PUBLIC_BACKERS_MANAGER_ADDRESS=0x41841e316F85933247fC7b51c79B76F22eB239bc
NEXT_PUBLIC_BUILDER_REGISTRY_ADDRESS=0x54a795748371193EC5495C94028aa250e85a95d9
NEXT_PUBLIC_REWARD_DISTRIBUTOR_ADDRESS=0x83Eac3Abe6AAF4a4AAe8B067888FAD600c4cF6B3

# Local testnet
Expand Down
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
Expand Up @@ -6,12 +6,15 @@ 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'

Check failure on line 17 in src/app/collective-rewards/settings/builder/hooks/useBuilderConfig.ts

View workflow job for this annotation

GitHub Actions / test

Cannot find module '../@/shared/context/EnvironmentsContext' or its corresponding type declarations.

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
7 changes: 5 additions & 2 deletions src/app/collective-rewards/user/hooks/useGetBuilders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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 +22,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 +39,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,7 +75,7 @@ export const useGetBuilders: UseGetBuilders = () => {
builders?.map(
builder =>
({
address: BackersManagerAddress,
address: builderRegistryAddress,
abi: BuilderRegistryAbi,
functionName: 'builderState',
args: [builder],
Expand Down
5 changes: 4 additions & 1 deletion src/app/collective-rewards/user/hooks/useGetGaugesArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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 +76,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
9 changes: 6 additions & 3 deletions src/app/providers/ContextProviders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { AlertProvider } from './AlertProvider'
import ErrorBoundary from '@/components/ErrorPage/ErrorBoundary'
import { BuilderContextProviderWithPrices } from '../collective-rewards/user'
import { AllocationsContextProvider } from '../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 9ffdda6

Please sign in to comment.