From 83c4c40ee8293df16b2f9fa1658ca2206cb904c1 Mon Sep 17 00:00:00 2001 From: Antonio Date: Thu, 21 Nov 2024 12:10:19 +0100 Subject: [PATCH] chore: review allocations --- .env.testnet.local | 11 +- .../components/AllocationAmount.tsx | 2 +- .../components/BuilderAllocation.tsx | 18 +- .../components/BuilderAllocationHeader.tsx | 9 +- .../context/AllocationsContext.tsx | 31 +- .../hooks/useBackerTotalAllocation.ts | 55 ++-- .../collective-rewards/allocations/page.tsx | 11 +- .../leaderboard/BuildersLeaderBoardTable.tsx | 6 +- src/app/collective-rewards/page.tsx | 1 - .../collective-rewards/rewards/MyRewards.tsx | 2 +- .../shared/components/index.ts | 1 + src/app/collective-rewards/types.ts | 7 + .../Button/BecomeABuilderButton.test.tsx | 3 - .../user/context/BuilderContext.tsx | 10 +- .../user/hooks/useGetBuilders.ts | 276 ++++++------------ .../utils/getMostAdvancedProposal.test.tsx | 12 - src/components/Slider/Slider.tsx | 12 +- 17 files changed, 186 insertions(+), 281 deletions(-) diff --git a/.env.testnet.local b/.env.testnet.local index 4956b984..baa2e961 100644 --- a/.env.testnet.local +++ b/.env.testnet.local @@ -17,12 +17,13 @@ NEXT_PUBLIC_GENERAL_BUCKET_ADDRESS=0x72Ed7d7b7835Ad62B1f9b6280bAd62618aA71461 NEXT_PUBLIC_CHAIN_ID=31 # CR-related env variables -NEXT_PUBLIC_SIMPLIFIED_REWARD_DISTRIBUTOR_ADDRESS=0x4e84FCc953dE129C6C47c5B0AD7E57B226093Ae1 -NEXT_PUBLIC_BACKERS_MANAGER_ADDRESS=0x6E587f9DdEB9640713a0D735A78ac36cA4a763ea -NEXT_PUBLIC_REWARD_DISTRIBUTOR_ADDRESS=0x6F6Be08782bc671041cF240f84a5Ca671774BECc -NEXT_PUBLIC_GOVERNANCE_MANAGER_ADDRESS=0x732C2468163F16Dfc109bb68a5F87CC04bC729EF +# TODO: To be removed +NEXT_PUBLIC_SIMPLIFIED_REWARD_DISTRIBUTOR_ADDRESS=0xc469Cc2579De5C16210e9063B4E628bF8C46bA02 -NEXT_PUBLIC_CYCLE_DURATION_IN_DAYS=2 +NEXT_PUBLIC_BACKERS_MANAGER_ADDRESS=0xec0a29Df5180A6B04496dfAf2D827e36F4a0A52F +NEXT_PUBLIC_REWARD_DISTRIBUTOR_ADDRESS=0xD476E4804551595687C1f6F0a9C22dd1Bbfa0319 +NEXT_PUBLIC_GOVERNANCE_MANAGER_ADDRESS=0xb7C6918d6aE6df2e147FF464271a94EAfF027E5D +NEXT_PUBLIC_CYCLE_DURATION_IN_DAYS=7 NEXT_PUBLIC_FIRST_CYCLE_START_DATE_ISO="1970-01-01T00:00:00Z" NEXT_PUBLIC_ENV_DATA_URL="https://raw.githubusercontent.com/RootstockCollective/dao-frontend/develop/data.testnet.qa.json" diff --git a/src/app/collective-rewards/allocations/components/AllocationAmount.tsx b/src/app/collective-rewards/allocations/components/AllocationAmount.tsx index b641eb2f..4a9d1974 100644 --- a/src/app/collective-rewards/allocations/components/AllocationAmount.tsx +++ b/src/app/collective-rewards/allocations/components/AllocationAmount.tsx @@ -33,7 +33,7 @@ export const AllocationAmount = () => { const newTotalAllocation = (BigInt(balance ?? 0n) * BigInt(percentage)) / BigInt(100) updateTotalAllocation(newTotalAllocation) setActiveButton(index) - const allocationValue = newTotalAllocation / BigInt(allocationCount) + const allocationValue = allocationCount > 0 ? newTotalAllocation / BigInt(allocationCount) : 0n updateAllocations(Array(allocationCount).fill(allocationValue)) } diff --git a/src/app/collective-rewards/allocations/components/BuilderAllocation.tsx b/src/app/collective-rewards/allocations/components/BuilderAllocation.tsx index c269ab10..a6fa5048 100644 --- a/src/app/collective-rewards/allocations/components/BuilderAllocation.tsx +++ b/src/app/collective-rewards/allocations/components/BuilderAllocation.tsx @@ -1,14 +1,14 @@ +import { AllocationsContext } from '@/app/collective-rewards/allocations/context' +import { Builder } from '@/app/collective-rewards/types' import { Input } from '@/components/Input' import { Slider } from '@/components/Slider' import { Label } from '@/components/Typography' import { useContext } from 'react' import { formatEther, parseEther } from 'viem' -import { AllocationsContext } from '@/app/collective-rewards/allocations/context' import { BuilderAllocationHeader, BuilderAllocationHeaderProps } from './BuilderAllocationHeader' -import { BuilderInfo } from '@/app/collective-rewards/types' export type BuilderAllocationProps = BuilderAllocationHeaderProps & - Pick & { + Pick & { index: number currentAllocation: bigint } @@ -26,8 +26,12 @@ export const BuilderAllocation = (builder: BuilderAllocationProps) => { updateAllocation(builder.index, parseEther(value)) } + const onSliderValueChange = (value: number[]) => { + updateAllocation(builder.index, BigInt(value[0])) + } + return ( -
+
{ onChange={onInputChange} value={formatEther(currentAllocation)} /> - +
) } diff --git a/src/app/collective-rewards/allocations/components/BuilderAllocationHeader.tsx b/src/app/collective-rewards/allocations/components/BuilderAllocationHeader.tsx index 3db8bc5e..05e1e22f 100644 --- a/src/app/collective-rewards/allocations/components/BuilderAllocationHeader.tsx +++ b/src/app/collective-rewards/allocations/components/BuilderAllocationHeader.tsx @@ -1,15 +1,12 @@ +import { Builder, BuilderStatusActive, BuilderStatusShown } from '@/app/collective-rewards/types' +import { crStatusColorClasses } from '@/app/collective-rewards/user' import { AddressOrAlias } from '@/components/Address' import { Badge } from '@/components/Badge' import { Jdenticon } from '@/components/Header/Jdenticon' import { Paragraph, Typography } from '@/components/Typography' -import { BuilderInfo, BuilderStatusActive, BuilderStatusShown } from '@/app/collective-rewards/types' -import { crStatusColorClasses } from '@/app/collective-rewards/user' import { FC } from 'react' -export type BuilderAllocationHeaderProps = Pick< - BuilderInfo, - 'address' | 'builderName' | 'status' | 'joiningDate' -> +export type BuilderAllocationHeaderProps = Pick export const BuilderAllocationHeader: FC = ({ address, diff --git a/src/app/collective-rewards/allocations/context/AllocationsContext.tsx b/src/app/collective-rewards/allocations/context/AllocationsContext.tsx index 06f7a32d..486c4aa7 100644 --- a/src/app/collective-rewards/allocations/context/AllocationsContext.tsx +++ b/src/app/collective-rewards/allocations/context/AllocationsContext.tsx @@ -3,8 +3,8 @@ import { useGetAllAllocationOf, useGetVotingPower, } from '@/app/collective-rewards/allocations/hooks' -import { BuilderInfo } from '@/app/collective-rewards/types' -import { useGetBuilders } from '@/app/collective-rewards/user' +import { Builder, BuilderInfo } from '@/app/collective-rewards/types' +import { useBuilderContext, useGetBuilders } from '@/app/collective-rewards/user' import { createContext, FC, ReactNode, useEffect, useMemo, useState } from 'react' import { zeroAddress } from 'viem' import { useAccount } from 'wagmi' @@ -25,7 +25,7 @@ type AllocationsContextValue = { backer: Backer isContextLoading: boolean contextError: Error | null - getBuilder: (index: number) => BuilderInfo | null + getBuilder: (index: number) => Builder | null } export type AllocationsActions = { @@ -101,7 +101,24 @@ export const AllocationsContextProvider: FC<{ children: ReactNode }> = ({ childr const [backer, setBacker] = useState(DEFAULT_CONTEXT.state.backer) - const { data: builders, isLoading: isLoadingBuilders, error: buildersError } = useGetBuilders() + // TODO: review this part + const { + data: buildersFromContext, + isLoading: isLoadingBuilders, + error: buildersError, + } = useBuilderContext() + const builders = buildersFromContext.map( + ({ builderName, status, address, gauge, joiningDate }) => + ({ + builderName, + status, + address, + gauge, + joiningDate, + // TODO: retrieve the kickback + kickback: 0, + }) as Builder, + ) const { data: allAllocations, @@ -174,8 +191,8 @@ export const AllocationsContextProvider: FC<{ children: ReactNode }> = ({ childr } } - const [initialAllocations, initialCummulativeAllocations] = createInitialAllocations( - allAllocations, + const [initialAllocations, initialCumulativeAllocations] = createInitialAllocations( + allAllocations || [], selections, ) @@ -184,7 +201,7 @@ export const AllocationsContextProvider: FC<{ children: ReactNode }> = ({ childr balance: votingPower ?? BigInt(0), totalAllocation: totalAllocation ?? BigInt(0), allocationCount: builders.length, - cumulativeAllocation: initialCummulativeAllocations, + cumulativeAllocation: initialCumulativeAllocations, }, allocations: initialAllocations, } diff --git a/src/app/collective-rewards/allocations/hooks/useBackerTotalAllocation.ts b/src/app/collective-rewards/allocations/hooks/useBackerTotalAllocation.ts index 844787e5..f686e97d 100644 --- a/src/app/collective-rewards/allocations/hooks/useBackerTotalAllocation.ts +++ b/src/app/collective-rewards/allocations/hooks/useBackerTotalAllocation.ts @@ -2,7 +2,7 @@ import { BackersManagerAbi } from '@/lib/abis/v2/BackersManagerAbi' import { GaugeAbi } from '@/lib/abis/v2/GaugeAbi' import { AVERAGE_BLOCKTIME } from '@/lib/constants' import { BackersManagerAddress } from '@/lib/contracts' -import { Address, parseEther } from 'viem' +import { Address } from 'viem' import { useReadContract, useReadContracts } from 'wagmi' export const useBackerTotalAllocation = (backer: Address) => { @@ -18,49 +18,28 @@ export const useBackerTotalAllocation = (backer: Address) => { }) return { - data: parseEther('200'), + data, isLoading, error, } } -// TODO: UNCOMMENT -// export const useGetAllAllocationOf = (backer: Address, gauges: Address[]) => { -// const { data, isLoading, error } = useReadContracts({ -// contracts: gauges.map(gauge => ({ -// abi: GaugeAbi, -// address: gauge, -// functionName: 'allocationOf', -// args: [backer], -// })), -// query: { -// refetchInterval: AVERAGE_BLOCKTIME, -// }, -// }) - -// return { -// data: data?.map(({ result }) => result as bigint), -// isLoading, -// error, -// } -// } - -// TODO: DELETE -const data = [ - BigInt('100000000000000000000'), // 100 ETH equivalent for builder 1 - BigInt('200000000000000000000'), // 200 ETH equivalent for builder 2 - BigInt('15000000000000000000'), // 15 ETH equivalent for builder 3 - BigInt('30000000000000000000'), // 30 ETH equivalent for builder 4 - BigInt('250000000000000000000'), // 250 ETH equivalent for builder 5 - BigInt('180000000000000000000'), // 180 ETH equivalent for builder 6 - BigInt('22000000000000000000'), // 22 ETH equivalent for builder 7 - BigInt('27000000000000000000'), // 27 ETH equivalent for builder 8 -] - export const useGetAllAllocationOf = (backer: Address, gauges: Address[]) => { + const { data, isLoading, error } = useReadContracts({ + contracts: gauges.map(gauge => ({ + abi: GaugeAbi, + address: gauge, + functionName: 'allocationOf', + args: [backer], + })), + query: { + refetchInterval: AVERAGE_BLOCKTIME, + }, + }) + return { - data, - isLoading: false, - error: null, + data: data?.map(({ result }) => result as bigint), + isLoading, + error, } } diff --git a/src/app/collective-rewards/allocations/page.tsx b/src/app/collective-rewards/allocations/page.tsx index 3347a9f7..70d1ec44 100644 --- a/src/app/collective-rewards/allocations/page.tsx +++ b/src/app/collective-rewards/allocations/page.tsx @@ -5,6 +5,7 @@ import { MainContainer } from '@/components/MainContainer/MainContainer' import { Typography } from '@/components/Typography' import { useRouter } from 'next/navigation' import { useContext } from 'react' +import { Builder } from '../types' import { AllocationAmount, AllocationMetrics, @@ -13,7 +14,6 @@ import { Header, } from './components' import { AllocationsContext } from './context' -import { BuilderInfo } from '../types' export default function Allocations() { const router = useRouter() @@ -48,7 +48,7 @@ export default function Allocations() {
{Object.entries(allocations).map(([key, currentAllocation]) => { const index = Number(key) - const builderInfo = getBuilder(index) as BuilderInfo + const builderInfo = getBuilder(index) as Builder const builder: BuilderAllocationProps = { ...builderInfo, index, @@ -59,6 +59,7 @@ export default function Allocations() {
+ {/* TODO: review disabled statuses */}
- diff --git a/src/app/collective-rewards/leaderboard/BuildersLeaderBoardTable.tsx b/src/app/collective-rewards/leaderboard/BuildersLeaderBoardTable.tsx index 79934c85..da9dd9f9 100644 --- a/src/app/collective-rewards/leaderboard/BuildersLeaderBoardTable.tsx +++ b/src/app/collective-rewards/leaderboard/BuildersLeaderBoardTable.tsx @@ -21,6 +21,7 @@ import { } from '@/app/collective-rewards/shared' import { getAddress } from 'viem' import { tokenContracts } from '@/lib/contracts' +import { useRouter } from 'next/navigation' enum RewardsColumnKeyEnum { builder = 'builder', @@ -174,9 +175,10 @@ const BuildersLeaderBoardTable: FC = ({ tokens, c } export const BuildersLeaderBoard = () => { + const router = useRouter() + const onManageAllocations = () => { - // TODO: fill the allocation context if necessary and change the route - console.log('Manage allocations') + router.push('collective-rewards/allocations') } // TODO: check where to store this information diff --git a/src/app/collective-rewards/page.tsx b/src/app/collective-rewards/page.tsx index f0c94420..eccdbd8f 100644 --- a/src/app/collective-rewards/page.tsx +++ b/src/app/collective-rewards/page.tsx @@ -4,7 +4,6 @@ import { BuildersLeaderBoard } from '@/app/collective-rewards/leaderboard' import { Metrics } from '@/app/collective-rewards/metrics' import { WhitelistContextProviderWithBuilders, WhitelistSection } from '@/app/collective-rewards/whitelist' import { MainContainer } from '@/components/MainContainer/MainContainer' -import { AllocationsContextProvider } from './allocations' export default function BuildersIncentiveMarket() { return ( diff --git a/src/app/collective-rewards/rewards/MyRewards.tsx b/src/app/collective-rewards/rewards/MyRewards.tsx index 8b1b52de..5a54cd69 100644 --- a/src/app/collective-rewards/rewards/MyRewards.tsx +++ b/src/app/collective-rewards/rewards/MyRewards.tsx @@ -48,7 +48,7 @@ export const Rewards: FC<{ builder: Address }> = ({ builder }) => { { - console.error('Not implemented') + router.push('/collective-rewards/allocations') }} title="Backer Rewards" subtext="Monitor your rewards balances and claim." diff --git a/src/app/collective-rewards/shared/components/index.ts b/src/app/collective-rewards/shared/components/index.ts index ea1efeb3..6a2dbd3a 100644 --- a/src/app/collective-rewards/shared/components/index.ts +++ b/src/app/collective-rewards/shared/components/index.ts @@ -1,2 +1,3 @@ export * from './Pagination' export * from './PaginationButton' +export * from './Table' diff --git a/src/app/collective-rewards/types.ts b/src/app/collective-rewards/types.ts index 710212a8..7580dff5 100644 --- a/src/app/collective-rewards/types.ts +++ b/src/app/collective-rewards/types.ts @@ -23,6 +23,13 @@ export type BuilderInfo = { status: BuilderStatus proposals: CreateBuilderProposalEventLog[] gauge: Address +} + +// TODO: refactor BuilderInfo & BuilderProposal +export type Builder = { + address: Address + status: BuilderStatus + gauge: Address kickback: number builderName: string joiningDate: string diff --git a/src/app/collective-rewards/user/components/Button/BecomeABuilderButton.test.tsx b/src/app/collective-rewards/user/components/Button/BecomeABuilderButton.test.tsx index e5a7e681..cafd2927 100644 --- a/src/app/collective-rewards/user/components/Button/BecomeABuilderButton.test.tsx +++ b/src/app/collective-rewards/user/components/Button/BecomeABuilderButton.test.tsx @@ -45,9 +45,6 @@ describe('BecomeABuilderButton', () => { }, ] as CreateBuilderProposalEventLog[], gauge: '0x01', - kickback: 0, - builderName: '', - joiningDate: '', } const buildersData = [builderData] const proposalsToStates = { diff --git a/src/app/collective-rewards/user/context/BuilderContext.tsx b/src/app/collective-rewards/user/context/BuilderContext.tsx index 828ef273..cd97795d 100644 --- a/src/app/collective-rewards/user/context/BuilderContext.tsx +++ b/src/app/collective-rewards/user/context/BuilderContext.tsx @@ -1,6 +1,7 @@ import { createContext, FC, ReactNode, useContext, useMemo } from 'react' import { Address } from 'viem' import { + BuilderInfo, BuilderStatus, BuilderStatusProposalCreatedMVP, BuilderStatusShown, @@ -12,7 +13,7 @@ import { DateTime } from 'luxon' import { splitCombinedName } from '@/app/proposals/shared/utils' import { withPricesContextProvider } from '@/shared/context/PricesContext' -// TODO: rename BuilderProposal and perhaps rewrite the type to Modify +// TODO: rename BuilderProposal and perhaps rewrite the type to Modify export type BuilderProposal = { builderName: string status: BuilderStatusShown @@ -58,8 +59,7 @@ export const BuilderContextProvider: FC = ({ children }) = error: proposalsStateMapError, } = useGetProposalsState(buildersProposals) - // FIXME: rename to builderdsWithProposals - const filteredBuilders = useMemo(() => { + const builderWithProposal = useMemo(() => { return builders.reduce((acc, builder) => { const { status, address, gauge } = builder const proposal = getMostAdvancedProposal(builder, proposalsStateMap) @@ -92,11 +92,11 @@ export const BuilderContextProvider: FC = ({ children }) = const error = buildersError ?? proposalsStateMapError const getBuilderByAddress = (address: Address): BuilderProposal | undefined => { - return filteredBuilders[address] + return builderWithProposal[address] } const valueOfContext: BuilderContextValue = { - data: Object.values(filteredBuilders), + data: Object.values(builderWithProposal), isLoading, error, getBuilderByAddress, diff --git a/src/app/collective-rewards/user/hooks/useGetBuilders.ts b/src/app/collective-rewards/user/hooks/useGetBuilders.ts index 4008a88c..bc25dce9 100644 --- a/src/app/collective-rewards/user/hooks/useGetBuilders.ts +++ b/src/app/collective-rewards/user/hooks/useGetBuilders.ts @@ -5,15 +5,15 @@ import { BuilderStatusInProgress, BuilderStatusProposalCreatedMVP, } from '@/app/collective-rewards/types' +import { useGetGaugesArray } from '@/app/collective-rewards/user/hooks/useGetGaugesArray' +import { BuilderStateStruct } from '@/app/collective-rewards/utils/getBuilderGauge' import { useFetchCreateBuilderProposals } from '@/app/proposals/hooks/useFetchLatestProposals' import { BuilderRegistryAbi } from '@/lib/abis/v2/BuilderRegistryAbi' import { AVERAGE_BLOCKTIME } from '@/lib/constants' import { BackersManagerAddress } from '@/lib/contracts' -import { useEffect, useMemo } from 'react' +import { useMemo } from 'react' import { Address, getAddress } from 'viem' import { useReadContracts } from 'wagmi' -import { useGetGaugesArray } from '@/app/collective-rewards/user/hooks/useGetGaugesArray' -import { BuilderStateStruct } from '@/app/collective-rewards/utils' export type BuilderLoader = { data?: BuilderInfo @@ -50,185 +50,99 @@ const getCombinedBuilderStatus = (builderState: BuilderStateStruct): BuilderStat // Default case: used to filter out builders return EXCLUDED_BUILDER_STATUS } - -// TODO: UNCOMEENT -// export const useGetBuilders = (): BuildersLoader => { -// /* -// * get Gauges -// * for each Gauge -// * get Builder from Gauge -// * get Builder state -// * ignore the builder if paused or revoked (to be confirmed) -// */ -// // get the gauges -// const { data: gauges, isLoading: gaugesLoading, error: gaugesError } = useGetGaugesArray('active') - -// // get the builders for each gauge -// const gaugeToBuilderCalls = gauges?.map( -// gauge => -// ({ -// address: BackersManagerAddress, -// abi: BuilderRegistryAbi, -// functionName: 'gaugeToBuilder', -// args: [gauge], -// }) as const, -// ) -// const { -// data: buildersResult, -// isLoading: buildersLoading, -// error: buildersError, -// } = useReadContracts({ -// contracts: gaugeToBuilderCalls, -// query: { -// refetchInterval: AVERAGE_BLOCKTIME, -// }, -// }) -// const builders = buildersResult?.map(builder => builder.result) as Address[] - -// const builderToGauge = builders?.reduce( -// (acc, builder, index) => { -// acc[builder] = gauges![index] -// return acc -// }, -// {} as Record, -// ) - -// // get the builder state for each builder -// const builderStatesCalls = builders?.map( -// builder => -// ({ -// address: BackersManagerAddress, -// abi: BuilderRegistryAbi, -// functionName: 'builderState', -// args: [builder], -// }) as const, -// ) -// const { -// data: builderStatesResult, -// isLoading: builderStatesLoading, -// error: builderStatesError, -// } = useReadContracts({ contracts: builderStatesCalls, query: { refetchInterval: AVERAGE_BLOCKTIME } }) -// const builderStates = builderStatesResult?.map(({ result }) => result as BuilderStateStruct) - -// const builderStatusMap = builders?.reduce((acc, builder, index) => { -// const builderState = (builderStates?.[index] ?? []) as BuilderStateStruct -// const status = getCombinedBuilderStatus(builderState) -// if (status !== EXCLUDED_BUILDER_STATUS) { -// acc[builder] = status as BuilderStatus -// } - -// return acc -// }, {}) - -// const { -// data: buildersProposalsMap, -// isLoading: builderProposalsMapLoading, -// error: builderProposalsMapError, -// } = useFetchCreateBuilderProposals() - -// const data = useMemo(() => { -// // we need to add the kickback and the joining date -// TODO: return Object.entries(buildersProposalsMap ?? {}).map(([builder, proposals]) => ({ -// address: getAddress(builder), -// status: -// builderStatusMap && builder in builderStatusMap -// ? builderStatusMap[builder as Address] // V2 -// : BuilderStatusProposalCreatedMVP, // MVP -// proposals: Object.values(proposals), -// gauge: builderToGauge?.[builder as Address], -// })) -// }, [builderStatusMap, buildersProposalsMap, builderToGauge]) - -// const isLoading = builderProposalsMapLoading || builderStatesLoading || buildersLoading || gaugesLoading -// const error = builderProposalsMapError ?? builderStatesError ?? buildersError ?? gaugesError - -// return { -// data, -// isLoading, -// error, -// } -// } - -// TODO: REMOVE -const builders: BuilderInfo[] = [ - { - address: '0x1234567890abcdef1234567890abcdef12345678', - status: 'Active', - proposals: [], - gauge: '0x1234567890abcdef1234567890abcdef12345678', - kickback: 0.1, - builderName: 'Builder One', - joiningDate: '2023-01-01', - }, - { - address: '0x1234567890abcdef1234567890abcdef12345679', - status: 'Active', - proposals: [], - gauge: '0x1234567890abcdef1234567890abcdef12345679', - kickback: 0.2, - builderName: 'Builder Two', - joiningDate: '2023-02-01', - }, - { - address: '0x1234567890abcdef1234567890abcdef1234567a', - status: 'Active', - proposals: [], - gauge: '0x1234567890abcdef1234567890abcdef1234567a', - kickback: 0.3, - builderName: 'Builder Three', - joiningDate: '2023-03-01', - }, - { - address: '0x1234567890abcdef1234567890abcdef1234567b', - status: 'Active', - proposals: [], - gauge: '0x1234567890abcdef1234567890abcdef1234567b', - kickback: 0.4, - builderName: 'Builder Four', - joiningDate: '2023-04-01', - }, - { - address: '0x1234567890abcdef1234567890abcdef1234567c', - status: 'Active', - proposals: [], - gauge: '0x1234567890abcdef1234567890abcdef1234567c', - kickback: 0.5, - builderName: 'Builder Five', - joiningDate: '2023-05-01', - }, - { - address: '0x1234567890abcdef1234567890abcdef1234567d', - status: 'Active', - proposals: [], - gauge: '0x1234567890abcdef1234567890abcdef1234567d', - kickback: 0.6, - builderName: 'Builder Six', - joiningDate: '2023-06-01', - }, - { - address: '0x1234567890abcdef1234567890abcdef1234567e', - status: 'Active', - proposals: [], - gauge: '0x1234567890abcdef1234567890abcdef1234567e', - kickback: 0.7, - builderName: 'Builder Seven', - joiningDate: '2023-07-01', - }, - { - address: '0x1234567890abcdef1234567890abcdef1234567f', - status: 'Active', - proposals: [], - gauge: '0x1234567890abcdef1234567890abcdef1234567f', - kickback: 0.8, - builderName: 'Builder Eight', - joiningDate: '2023-08-01', - }, -] - export const useGetBuilders = (): BuildersLoader => { + /* + * get Gauges + * for each Gauge + * get Builder from Gauge + * get Builder state + * ignore the builder if paused or revoked (to be confirmed) + */ + // get the gauges + const { data: gauges, isLoading: gaugesLoading, error: gaugesError } = useGetGaugesArray('active') + + // get the builders for each gauge + const gaugeToBuilderCalls = gauges?.map( + gauge => + ({ + address: BackersManagerAddress, + abi: BuilderRegistryAbi, + functionName: 'gaugeToBuilder', + args: [gauge], + }) as const, + ) + const { + data: buildersResult, + isLoading: buildersLoading, + error: buildersError, + } = useReadContracts({ + contracts: gaugeToBuilderCalls, + query: { + refetchInterval: AVERAGE_BLOCKTIME, + }, + }) + const builders = buildersResult?.map(builder => builder.result) as Address[] + + const builderToGauge = builders?.reduce( + (acc, builder, index) => { + acc[builder] = gauges![index] + return acc + }, + {} as Record, + ) + + // get the builder state for each builder + const builderStatesCalls = builders?.map( + builder => + ({ + address: BackersManagerAddress, + abi: BuilderRegistryAbi, + functionName: 'builderState', + args: [builder], + }) as const, + ) + const { + data: builderStatesResult, + isLoading: builderStatesLoading, + error: builderStatesError, + } = useReadContracts({ contracts: builderStatesCalls, query: { refetchInterval: AVERAGE_BLOCKTIME } }) + const builderStates = builderStatesResult?.map(({ result }) => result as BuilderStateStruct) + + const builderStatusMap = builders?.reduce((acc, builder, index) => { + const builderState = (builderStates?.[index] ?? []) as BuilderStateStruct + const status = getCombinedBuilderStatus(builderState) + if (status !== EXCLUDED_BUILDER_STATUS) { + acc[builder] = status as BuilderStatus + } + + return acc + }, {}) + + const { + data: buildersProposalsMap, + isLoading: builderProposalsMapLoading, + error: builderProposalsMapError, + } = useFetchCreateBuilderProposals() + + const data = useMemo(() => { + // we need to add the kickback and the joining date + return Object.entries(buildersProposalsMap ?? {}).map(([builder, proposals]) => ({ + address: getAddress(builder), + status: + builderStatusMap && builder in builderStatusMap + ? builderStatusMap[builder as Address] // V2 + : BuilderStatusProposalCreatedMVP, // MVP + proposals: Object.values(proposals), + gauge: builderToGauge?.[builder as Address], + })) + }, [builderStatusMap, buildersProposalsMap, builderToGauge]) + + const isLoading = builderProposalsMapLoading || builderStatesLoading || buildersLoading || gaugesLoading + const error = builderProposalsMapError ?? builderStatesError ?? buildersError ?? gaugesError + return { - data: builders, - isLoading: false, - error: null, + data, + isLoading, + error, } } diff --git a/src/app/collective-rewards/utils/getMostAdvancedProposal.test.tsx b/src/app/collective-rewards/utils/getMostAdvancedProposal.test.tsx index b5c9f9a3..e2db3124 100644 --- a/src/app/collective-rewards/utils/getMostAdvancedProposal.test.tsx +++ b/src/app/collective-rewards/utils/getMostAdvancedProposal.test.tsx @@ -25,9 +25,6 @@ describe('getValidProposal', () => { }, ] as CreateBuilderProposalEventLog[], gauge: '0x01', - kickback: 0, - builderName: '', - joiningDate: '', }, { 1: ProposalState.Executed, @@ -53,9 +50,6 @@ describe('getValidProposal', () => { }, ] as CreateBuilderProposalEventLog[], gauge: '0x01', - kickback: 0, - builderName: '', - joiningDate: '', }, { 1: ProposalState.Active, @@ -87,9 +81,6 @@ describe('getValidProposal', () => { }, ] as CreateBuilderProposalEventLog[], gauge: '0x01', - kickback: 0, - builderName: '', - joiningDate: '', }, { 1: ProposalState.Active, @@ -121,9 +112,6 @@ describe('getValidProposal', () => { }, ] as CreateBuilderProposalEventLog[], gauge: '0x01', - kickback: 0, - builderName: '', - joiningDate: '', }, { 1: ProposalState.Canceled, diff --git a/src/components/Slider/Slider.tsx b/src/components/Slider/Slider.tsx index e04f3fa9..c4babe3c 100644 --- a/src/components/Slider/Slider.tsx +++ b/src/components/Slider/Slider.tsx @@ -7,21 +7,12 @@ export const Slider = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >((props, forwardedRef) => { - const initialValue = props.value || props.defaultValue - const [value, setValue] = React.useState(initialValue) - - const onPercentageClick = (percentage: number) => { - setValue([percentage]) - console.log('percentage', percentage) - } - return ( <> setValue(nextValue)} > @@ -32,14 +23,13 @@ export const Slider = React.forwardRef< onPercentageClick(v)} > {v}% ))} - {value?.map((_, i) => ( + {props.value?.map((_, i) => (