-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
377 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
165 changes: 165 additions & 0 deletions
165
src/app/collective-rewards/allocations/context/AllocationsContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import { createContext, FC, ReactNode, useContext, useEffect, useMemo, useState } from 'react' | ||
import { Address } from 'viem' | ||
import { useAccount } from 'wagmi' | ||
import { BuilderProposal, useGetActiveBuilders, useGetBuilders } from '@/app/collective-rewards/user' | ||
import { readContract } from 'wagmi/actions' | ||
import { config } from '@/config' | ||
import { BackersManagerAddress } from '@/lib/contracts' | ||
import { BuilderRegistryAbi } from '@/lib/abis/v2/BuilderRegistryAbi' | ||
import { GaugeAbi } from '@/lib/abis/v2/GaugeAbi' | ||
import { BuilderStateStruct } from '@/app/collective-rewards/utils/getBuilderGauge' | ||
import { useGetAllAllocationOf } from '@/app/collective-rewards/allocations/hooks' | ||
import { withPagination } from '@/app/collective-rewards/context/PaginatedDataContext' | ||
|
||
type AllocationsContextValue = { | ||
// buildersByAddress: Record<Address, BuilderInfo> | ||
// isLoading: boolean | ||
// user: { | ||
// address: Address | ||
// backees: Address[] | ||
// isBacker: boolean | ||
// } | ||
// actions: { | ||
// updateBacking: (address: Address, value: bigint) => unknown | ||
// } | ||
} | ||
|
||
export const AllocationsContext = createContext<AllocationsContextValue | null>(null) | ||
|
||
export const useAllocationsContext = () => { | ||
const context = useContext(AllocationsContext) | ||
if (!context) { | ||
throw new Error('useAllocationsContext must be used within a AllocationsContextProvider') | ||
} | ||
return context | ||
} | ||
|
||
type SelectedBuilder = { | ||
index: number | ||
hasAllocationsByUser: boolean | ||
isSelected: boolean | ||
// allocate: (value: bigint) => void | ||
} | ||
|
||
type BuildersByAddress = Record<Address, SelectedBuilder> | ||
|
||
export const AllocationsContextProvider: FC<{ children: ReactNode }> = ({ children }) => { | ||
const { address: backerAddress } = useAccount() | ||
if (!backerAddress) { | ||
throw new Error('Provider must be used with a defined account') | ||
} | ||
const { | ||
data: activeBuilders, | ||
isLoading: isLoadingActiveBuilders, | ||
error: activeBuildersError, | ||
} = useGetActiveBuilders() | ||
|
||
const [selectedBuilders, setSelectedBuilders] = useState<BuildersByAddress>({}) | ||
|
||
const { | ||
data: onchianAllocations, | ||
isLoading: isOnchianAllocationsLoading, | ||
error: OnchianAllocationsError, | ||
} = useGetAllAllocationOf( | ||
backerAddress!, | ||
Object.values(selectedBuilders).map(builder => activeBuilders[builder.index].gauge), | ||
) | ||
|
||
// useEffect(() => { | ||
// if (!backer) {return} | ||
|
||
// const getBuilderGauge = async (builderAddress: Address): Promise<Address> => { | ||
// return readContract(config, { | ||
// address: BackersManagerAddress, | ||
// abi: BuilderRegistryAbi, | ||
// functionName: 'builderToGauge', | ||
// args: [builderAddress], | ||
// }) | ||
// } | ||
// const getBuilderState = async (gauge: Address): Promise<BuilderStateStruct> => { | ||
// return readContract(config, { | ||
// address: gauge, | ||
// abi: GaugeAbi, | ||
// functionName: 'builderState', | ||
// }) | ||
// } | ||
// const getBuilderStatus = async (builderAddress: Address): Promise<BuilderStatusWithExcluded> => { | ||
// const gauge = await getBuilderGauge(builderAddress) | ||
// const builderState = await getBuilderState(gauge) | ||
// return getCombinedBuilderStatus(builderState) | ||
// } | ||
// const getBuilders = async () => { | ||
// const builders = await Promise.all( | ||
// whitelistedBuilders.map(async builder => { | ||
// const status = await getBuilderStatus(builder.address) | ||
// return { | ||
// ...builder, | ||
// status, | ||
// } | ||
// }), | ||
// ) | ||
// setBuilders(builders) | ||
// } | ||
// getBuilders() | ||
// }, [builders]) | ||
|
||
function addSelectedBuilder(builderAddress: Address, isSelected: boolean) { | ||
const index = activeBuilders.findIndex(builder => builder.address === builderAddress) | ||
|
||
setSelectedBuilders({ | ||
...selectedBuilders, | ||
[builderAddress]: { | ||
index, | ||
hasAllocationsByUser: (onchianAllocations && onchianAllocations[index]) ?? false, | ||
isSelected, | ||
}, | ||
}) | ||
} | ||
|
||
const configActions = { | ||
toggleSelectedBuilder: (builderAddress: Address, isSelected: boolean) => { | ||
const existingSelection = selectedBuilders[builderAddress] | ||
if (existingSelection) { | ||
return setSelectedBuilders({ | ||
...selectedBuilders, | ||
[builderAddress]: { | ||
...existingSelection, | ||
isSelected, | ||
}, | ||
}) | ||
} | ||
|
||
addSelectedBuilder(builderAddress, isSelected) | ||
}, | ||
} | ||
|
||
const configA = { | ||
selectedBuilders, | ||
...configActions, | ||
} | ||
|
||
const value: AllocationsContextValue = { | ||
// buildersByAddress: whitelistedBuilders.reduce( | ||
// (acc, builder) => { | ||
// acc[builder.address] = builder | ||
// return acc | ||
// }, | ||
// {} as Record<Address, BuilderInfo>, | ||
// ), | ||
// isLoading, | ||
// user: { | ||
// address, | ||
// backees: [], | ||
// isBacker: false, | ||
// }, | ||
// actions: { | ||
// updateBacking: () => {}, | ||
// }, | ||
} | ||
return withPagination(AllocationsContext.Provider)({ | ||
data: activeBuilders, | ||
pageSize: 10, | ||
currentPage: 0, | ||
props: { value, children }, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './AllocationsContext' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useBackerTotalAllocation' |
45 changes: 45 additions & 0 deletions
45
src/app/collective-rewards/allocations/hooks/useBackerTotalAllocation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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 } from 'viem' | ||
import { useReadContract, useReadContracts } from 'wagmi' | ||
|
||
export const useBackerTotalAllocation = (backer: Address) => { | ||
const { data, isLoading, error } = useReadContract({ | ||
abi: BackersManagerAbi, | ||
address: BackersManagerAddress, | ||
functionName: 'backerTotalAllocation', | ||
args: [backer], | ||
query: { | ||
refetchInterval: AVERAGE_BLOCKTIME, | ||
initialData: 0n, | ||
}, | ||
}) | ||
|
||
return { | ||
data, | ||
isLoading, | ||
error, | ||
} | ||
} | ||
|
||
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, | ||
error, | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './context' | ||
export * from './hooks' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/app/collective-rewards/context/PaginatedDataContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Context, createContext, FC, useContext, useEffect, useState } from 'react' | ||
|
||
type PaginationConfig<T> = { | ||
data: T[] | ||
pageSize: number | ||
currentPage: number | ||
} | ||
|
||
type PaginatedData<T extends Object = {}> = PaginationConfig<T> & { | ||
getDataIndex: (index: number) => number | ||
updateData: (data: T[]) => void | ||
updatePageSize: (pageSize: number) => void | ||
updateCurrentPage: (currentPage: number) => void | ||
} | ||
|
||
export const PaginatedDataContext = createContext<PaginatedData<any>>({ | ||
data: [], | ||
pageSize: 0, | ||
currentPage: 0, | ||
getDataIndex: () => 0, | ||
updateData: () => {}, | ||
updatePageSize: () => {}, | ||
updateCurrentPage: () => {}, | ||
}) | ||
|
||
export const usePaginatedDataContext = <T extends Object = {}>() => { | ||
const context = useContext<PaginatedData<T>>(PaginatedDataContext) | ||
if (!context) { | ||
throw new Error('usePaginatedDataContext must be used within a PaginatedDataContextProvider') | ||
} | ||
return context | ||
} | ||
|
||
type PaginatedDataContextProviderProps<T extends Object = {}> = { | ||
children: React.ReactNode | ||
config: PaginationConfig<T> | ||
} | ||
|
||
export const PaginatedDataContextProvider: FC<PaginatedDataContextProviderProps> = ({ children, config }) => { | ||
const [data, setData] = useState(config.data) | ||
const [pageSize, setPageSize] = useState(config.pageSize) | ||
const [currentPage, setCurrentPage] = useState(config.currentPage) | ||
|
||
const contextMethods = { | ||
getDataIndex: (pagedIndex: number) => pagedIndex + pageSize * currentPage, | ||
updateData: (newData: typeof data) => setData(newData), | ||
updatePageSize: (newPageSize: number) => setPageSize(newPageSize), | ||
updateCurrentPage: (newCurrentPage: number) => setCurrentPage(newCurrentPage), | ||
} | ||
|
||
return ( | ||
<PaginatedDataContext.Provider value={{ data, pageSize, currentPage, ...contextMethods }}> | ||
{children} | ||
</PaginatedDataContext.Provider> | ||
) | ||
} | ||
|
||
export const withPagination = <T extends Object = {}, TProps extends Object = {}>( | ||
Component: FC<TProps>, | ||
): FC< | ||
PaginationConfig<T> & { | ||
props: TProps | ||
} | ||
> => { | ||
return ({ props, ...rest }) => { | ||
return ( | ||
<PaginatedDataContextProvider config={{ ...rest }}> | ||
<Component {...props} /> | ||
</PaginatedDataContextProvider> | ||
) | ||
} | ||
} |
Oops, something went wrong.