From a8e8d9db27d4853a4d1ace7d2d8ea08cb12dcba4 Mon Sep 17 00:00:00 2001 From: Juraj Piar Date: Wed, 20 Nov 2024 14:34:32 +0000 Subject: [PATCH] back to state --- .../components/AllocationAmount.tsx | 62 ++-- .../components/AllocationMetrics.tsx | 49 +-- .../components/BuilderAllocation.tsx | 53 +-- .../components/BuilderAllocationHeader.tsx | 4 +- .../allocations/context/Action.d.ts | 43 --- .../context/AllocationsContext.tsx | 320 ++++++++++++------ .../allocations/context/Context.d.ts | 24 -- .../allocations/context/reducer.ts | 51 --- .../allocations/hooks/index.ts | 1 + .../hooks/useBackerTotalAllocation.ts | 52 ++- .../allocations/hooks/useGetVotingPower.ts | 24 ++ .../collective-rewards/allocations/page.tsx | 75 +++- src/app/collective-rewards/page.tsx | 1 + src/app/collective-rewards/types.ts | 4 + .../user/hooks/useGetBuilders.ts | 317 +++++++++-------- src/app/providers/ContextProviders.tsx | 4 +- src/app/user/Balances/balanceUtils.ts | 7 +- 17 files changed, 589 insertions(+), 502 deletions(-) delete mode 100644 src/app/collective-rewards/allocations/context/Action.d.ts delete mode 100644 src/app/collective-rewards/allocations/context/Context.d.ts delete mode 100644 src/app/collective-rewards/allocations/context/reducer.ts create mode 100644 src/app/collective-rewards/allocations/hooks/useGetVotingPower.ts diff --git a/src/app/collective-rewards/allocations/components/AllocationAmount.tsx b/src/app/collective-rewards/allocations/components/AllocationAmount.tsx index a6d84450..c8c95700 100644 --- a/src/app/collective-rewards/allocations/components/AllocationAmount.tsx +++ b/src/app/collective-rewards/allocations/components/AllocationAmount.tsx @@ -1,10 +1,13 @@ 'use client' -import { formatBalanceToHuman } from '@/app/user/Balances/balanceUtils' import { Button, ButtonProps } from '@/components/Button' import { Input } from '@/components/Input' import { cn } from '@/lib/utils' -import { useState } from 'react' +import { useContext, useState } from 'react' +import { AllocationsContext } from '../context' +import { ethers } from 'ethers' +import { formatEther, parseEther } from 'viem' +import { StakeHint } from './StakeHint' const PercentageButton = ({ children, variant, ...rest }: ButtonProps) => ( + + + + diff --git a/src/app/collective-rewards/page.tsx b/src/app/collective-rewards/page.tsx index eccdbd8f..f0c94420 100644 --- a/src/app/collective-rewards/page.tsx +++ b/src/app/collective-rewards/page.tsx @@ -4,6 +4,7 @@ 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/types.ts b/src/app/collective-rewards/types.ts index 24d879cd..57e97ffa 100644 --- a/src/app/collective-rewards/types.ts +++ b/src/app/collective-rewards/types.ts @@ -11,6 +11,7 @@ export const builderStatusOptions = [ BuilderStatusProposalCreatedMVP, ] as const +// TODO: type BuilderStatus should include 'Paused' & 'Deactivated' export type BuilderStatus = (typeof builderStatusOptions)[number] export type BuilderStatusShown = Exclude @@ -19,6 +20,9 @@ export type BuilderInfo = { status: BuilderStatus proposals: CreateBuilderProposalEventLog[] gauge: Address + kickback: number + builderName: string + joiningDate: string } export type ProposalsToState = Record diff --git a/src/app/collective-rewards/user/hooks/useGetBuilders.ts b/src/app/collective-rewards/user/hooks/useGetBuilders.ts index ae1353c1..eb216198 100644 --- a/src/app/collective-rewards/user/hooks/useGetBuilders.ts +++ b/src/app/collective-rewards/user/hooks/useGetBuilders.ts @@ -51,162 +51,179 @@ const getCombinedBuilderStatus = (builderState: BuilderStateStruct): BuilderStat return EXCLUDED_BUILDER_STATUS } -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(() => { - 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, - } -} - -// export const useGetActiveBuilders = (): BuildersLoader => { -// const { data, isLoading, error } = useGetBuilders() +// 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: data.filter(builder => builder.status === BuilderStatusActive), +// data, // isLoading, // error, // } // } -export const useGetActiveBuilders = (): BuildersLoader => { - const builders: BuilderInfo[] = [ - { - address: '0x1234567890abcdef1234567890abcdef12345678', - status: 'Active', - proposals: [], - gauge: '0x1234567890abcdef1234567890abcdef12345678', - }, - { - address: '0x1234567890abcdef1234567890abcdef12345679', - status: 'Active', - proposals: [], - gauge: '0x1234567890abcdef1234567890abcdef12345679', - }, - { - address: '0x1234567890abcdef1234567890abcdef1234567a', - status: 'Active', - proposals: [], - gauge: '0x1234567890abcdef1234567890abcdef1234567a', - }, - { - address: '0x1234567890abcdef1234567890abcdef1234567b', - status: 'Active', - proposals: [], - gauge: '0x1234567890abcdef1234567890abcdef1234567b', - }, - { - address: '0x1234567890abcdef1234567890abcdef1234567c', - status: 'Active', - proposals: [], - gauge: '0x1234567890abcdef1234567890abcdef1234567c', - }, - { - address: '0x1234567890abcdef1234567890abcdef1234567d', - status: 'Active', - proposals: [], - gauge: '0x1234567890abcdef1234567890abcdef1234567d', - }, - { - address: '0x1234567890abcdef1234567890abcdef1234567e', - status: 'Active', - proposals: [], - gauge: '0x1234567890abcdef1234567890abcdef1234567e', - }, - { - address: '0x1234567890abcdef1234567890abcdef1234567f', - status: 'Active', - proposals: [], - gauge: '0x1234567890abcdef1234567890abcdef1234567', - }, - ] + +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 => { return { data: builders, isLoading: false, diff --git a/src/app/providers/ContextProviders.tsx b/src/app/providers/ContextProviders.tsx index b0868c88..64a51876 100644 --- a/src/app/providers/ContextProviders.tsx +++ b/src/app/providers/ContextProviders.tsx @@ -18,9 +18,7 @@ export const ContextProviders = ({ children }: Props) => { - - {children} {} - + {children} diff --git a/src/app/user/Balances/balanceUtils.ts b/src/app/user/Balances/balanceUtils.ts index 8d7c40a0..359cbf3a 100644 --- a/src/app/user/Balances/balanceUtils.ts +++ b/src/app/user/Balances/balanceUtils.ts @@ -1,9 +1,10 @@ import { BigNumberish, ethers } from 'ethers' import { GetAddressTokenResult, TokenBalance } from '@/app/user/types' import { tokenContracts } from '@/lib/contracts' +import { formatEther } from 'viem' -export const formatBalanceToHuman = (balance: BigNumberish, decimal = 18) => - ethers.formatUnits(balance, decimal) +// export const formatBalanceToHuman = (balance: BigNumberish, decimal = 18) => +// ethers.formatUnits(balance, decimal) const symbolsToGetFromArray = { RIF: { equivalentSymbols: ['tRIF', 'RIF'], currentContract: tokenContracts.RIF }, @@ -34,7 +35,7 @@ export const getTokenBalance = ( token.contractAddress.toLowerCase() === currentContract.toLowerCase(), ) if (tokenData) { - resultToReturn.balance = formatBalanceToHuman(tokenData.balance) + resultToReturn.balance = formatEther(BigInt(tokenData.balance)) resultToReturn.symbol = tokenData.symbol return resultToReturn }