Skip to content

Commit

Permalink
refactor: first request
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscotobar committed Feb 11, 2025
1 parent eff0a4c commit f241b6b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const useSetBackerRewardsForBuilder = (): SetBackerRewardsForBuilder => {

const setNewReward = async (newReward: bigint) => {
return await writeContractAsync({
address: builderRegistryAddress,
address: builderRegistryAddress!,
abi: BuilderRegistryAbi,
functionName: 'setBackerRewardPercentage',
args: [newReward],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const useCreateBuilderWhitelistProposal = () => {
if (!canCreateProposal) {
throw NoVotingPowerError
}
const builderGauge = await getBuilderGauge(builderRegistryAddress, builderAddress)
const builderGauge = await getBuilderGauge(builderRegistryAddress!, builderAddress)
if (builderGauge !== zeroAddress) {
// TODO: maybe we can use a different error here
throw AddressAlreadyWhitelistedError
Expand Down
74 changes: 48 additions & 26 deletions src/shared/context/EnvironmentsContext.test.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { describe, it, vi, expect, beforeEach, afterEach } from 'vitest'
import { useQuery, UseQueryResult } from '@tanstack/react-query'
import { renderHook } from '@testing-library/react'
import { EnvironmentsProvider, useEnvironmentsContext } from '@/shared/context/EnvironmentsContext'
import { ReactNode } from 'react'
import * as contracts from '@/lib/contracts'
import * as constants from '@/lib/constants'
import { zeroAddress } from 'viem'
import { useReadContract, UseReadContractReturnType } from 'wagmi'

vi.mock(import('@tanstack/react-query'), async importOriginal => {
vi.mock(import('wagmi'), async importOriginal => {
const actual = await importOriginal()
return {
...actual,
useQuery: vi.fn(),
useReadContract: vi.fn(),
}
})

Expand All @@ -35,15 +35,15 @@ const wrapper = ({ children }: { children: ReactNode }) => (

const renderHookWithProvider = () => renderHook(() => useEnvironmentsContext(), { wrapper })

const mockUseQuery = (data: boolean) => {
vi.mocked(useQuery).mockReturnValue({
const mockUseQuery = (data: bigint | undefined) => {
vi.mocked(useReadContract).mockReturnValue({
data,
} as UseQueryResult)
} as UseReadContractReturnType)
}

describe('EnvironmentsContext', () => {
beforeEach(() => {
mockUseQuery(false)
mockUseQuery(undefined)
})

afterEach(() => {
Expand All @@ -52,76 +52,98 @@ describe('EnvironmentsContext', () => {
})

describe('useBuilderRegistryMigration', () => {
it('should not enable the hook if BuilderRegistryAddress is zero address', async () => {
it('should not enable the hook if BuilderRegistryAddress is zero address', () => {
vi.spyOn(contracts, 'BuilderRegistryAddress', 'get').mockReturnValue(zeroAddress)

const { result } = renderHookWithProvider()

expect(result.current.builderRegistryAddress).toBe('0x123')
expect(useQuery).toBeCalledWith(
expect(useReadContract).toBeCalledWith(
expect.objectContaining({
enabled: false,
query: expect.objectContaining({
enabled: false,
}),
}),
)
})

it('should not enable the hook if CR_MIGRATING is false', async () => {
it('should not enable the hook if CR_MIGRATING is false', () => {
vi.spyOn(constants, 'CR_MIGRATING', 'get').mockReturnValue(false)

const { result } = renderHookWithProvider()

expect(result.current.builderRegistryAddress).toBe('0x123')
expect(useQuery).toBeCalledWith(
expect(useReadContract).toBeCalledWith(
expect.objectContaining({
enabled: false,
query: expect.objectContaining({
enabled: false,
}),
}),
)
})

it('should return BackersManagerAddress if migration has not occurred', async () => {
it('should return BackersManagerAddress if migration has not occurred', () => {
mockUseQuery(0n)
const { result } = renderHookWithProvider()

expect(result.current.builderRegistryAddress).toBe('0x123')
expect(useQuery).toBeCalledWith(
expect(useReadContract).toBeCalledWith(
expect.objectContaining({
enabled: true,
query: expect.objectContaining({
enabled: true,
}),
}),
)
})

it('should return BuilderRegistryAddress if migration has occurred', async () => {
mockUseQuery(true)
it('should return BuilderRegistryAddress if migration has occurred', () => {
mockUseQuery(1n)

const { result } = renderHookWithProvider()

expect(result.current.builderRegistryAddress).toBe('0x456')
expect(useQuery).toBeCalledWith(
expect(useReadContract).toBeCalledWith(
expect.objectContaining({
enabled: true,
query: expect.objectContaining({
enabled: true,
}),
}),
)
})

it('should disable the hook after migration has occurred', async () => {
it('should disable the hook after migration has occurred', () => {
mockUseQuery(0n)
const { result: priorMigration } = renderHookWithProvider()

expect(priorMigration.current.builderRegistryAddress).toBe('0x123')
expect(useQuery).toBeCalledWith(
expect(useReadContract).toBeCalledWith(
expect.objectContaining({
enabled: true,
query: expect.objectContaining({
enabled: true,
}),
}),
)

mockUseQuery(true)
mockUseQuery(1n)

const { result: afterMigration } = renderHookWithProvider()

expect(afterMigration.current.builderRegistryAddress).toBe('0x456')
expect(useQuery).toBeCalledWith(
expect(useReadContract).toBeCalledWith(
expect.objectContaining({
enabled: false,
query: expect.objectContaining({
enabled: false,
}),
}),
)
})

it('should return undefined if first query has not occurred', () => {
mockUseQuery(undefined)

const { result } = renderHookWithProvider()

expect(result.current.builderRegistryAddress).toBeUndefined()
})
})
})
58 changes: 29 additions & 29 deletions src/shared/context/EnvironmentsContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useQuery } from '@tanstack/react-query'
import {
createContext,
Dispatch,
Expand All @@ -11,64 +10,65 @@ import {
} from 'react'
import { Address, zeroAddress } from 'viem'
import { AVERAGE_BLOCKTIME, CR_MIGRATING } from '@/lib/constants'
import { readContract } from '@wagmi/core'
import { config } from '@/config'
import { BuilderRegistryAbi } from '@/lib/abis/v2/BuilderRegistryAbi'
import { BackersManagerAddress, BuilderRegistryAddress, RewardDistributorAddress } from '@/lib/contracts'
import { BackersManagerAddress, BuilderRegistryAddress } from '@/lib/contracts'
import { useReadContract } from 'wagmi'

type EnvironmentsContextType = {
backersManagerAddress: Address
builderRegistryAddress: Address
rewardDistributorAddress: Address
builderRegistryAddress: Address | undefined
}
const EnvironmentsContext = createContext<EnvironmentsContextType>({
backersManagerAddress: zeroAddress,
builderRegistryAddress: zeroAddress,
rewardDistributorAddress: zeroAddress,
backersManagerAddress: BackersManagerAddress,
builderRegistryAddress: undefined,
})

type EnvironmentsProviderProps = {
children: ReactNode
}
export const EnvironmentsProvider: FC<EnvironmentsProviderProps> = ({ children }) => {
const [builderRegistryAddress, setBuilderRegistryAddress] = useState<Address>(BackersManagerAddress)
const [builderRegistryAddress, setBuilderRegistryAddress] = useState<Address | undefined>()

useBuilderRegistryMigration(setBuilderRegistryAddress)

const valueOfContext: EnvironmentsContextType = {
backersManagerAddress: BackersManagerAddress,
builderRegistryAddress,
rewardDistributorAddress: RewardDistributorAddress,
}

return <EnvironmentsContext.Provider value={valueOfContext}>{children}</EnvironmentsContext.Provider>
}

const useBuilderRegistryMigration = (setBuilderRegistryAddress: Dispatch<SetStateAction<Address>>) => {
const useBuilderRegistryMigration = (
setBuilderRegistryAddress: Dispatch<SetStateAction<Address | undefined>>,
) => {
const [migrated, setMigrated] = useState(false)

const { data: validation } = useQuery({
queryFn: async () => {
const gaugesLength = await readContract(config, {
address: BuilderRegistryAddress,
abi: BuilderRegistryAbi,
functionName: 'getGaugesLength',
})

return gaugesLength > 0
const { data: gaugesLength } = useReadContract({
address: BuilderRegistryAddress,
abi: BuilderRegistryAbi,
functionName: 'getGaugesLength',
query: {
refetchInterval: AVERAGE_BLOCKTIME,
enabled: CR_MIGRATING && !migrated && BuilderRegistryAddress !== zeroAddress,
},
queryKey: ['builderRegistryMigration'],
refetchInterval: AVERAGE_BLOCKTIME,
initialData: false,
enabled: CR_MIGRATING && !migrated && BuilderRegistryAddress != zeroAddress,
})

useEffect(() => {
if (validation) {
setBuilderRegistryAddress(BuilderRegistryAddress)
setMigrated(true)
if (!CR_MIGRATING || BuilderRegistryAddress === zeroAddress) {
setBuilderRegistryAddress(BackersManagerAddress)
return
}

if (gaugesLength !== undefined) {
if (gaugesLength > 0n) {
setBuilderRegistryAddress(BuilderRegistryAddress)
setMigrated(true)
} else {
setBuilderRegistryAddress(BackersManagerAddress)
}
}
}, [setBuilderRegistryAddress, validation])
}, [gaugesLength, setBuilderRegistryAddress])
}

export const useEnvironmentsContext = () => useContext(EnvironmentsContext)

0 comments on commit f241b6b

Please sign in to comment.