-
Notifications
You must be signed in to change notification settings - Fork 44
Joinable pools filter #2158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
uiuxxx
wants to merge
18
commits into
main
Choose a base branch
from
feat/joinable-pools
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Joinable pools filter #2158
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
55582a2
New feature: Add joinable pools filter, that filters the current crit…
uiuxxx 7e765c2
Style token pills which are contained in the users wallet
uiuxxx be28234
Add a tooltip on "Joinable pools"
uiuxxx a769fff
Add a tooltip on "Joinable pools"
uiuxxx 514b603
Merge branch 'feat/joinable-pools' of https://github.com/balancer/fro…
uiuxxx 6a00f7a
change url to "joinablePools=true"
uiuxxx 26aa1a5
Bring up the spinner on tables, so it appears above the fold more often
uiuxxx 13551cb
query api for joinable pools
groninge01 a76850e
sort on tvl again
groninge01 9bbab25
remove usememo and filter out sepolia
groninge01 83bc9fd
add option to disable sorting options
groninge01 61ca618
abstract duplicate styles
groninge01 b0a0680
isJoinableBalanceLoading is enough
groninge01 f995786
url param can use only "true"
groninge01 c051286
Merge branch 'main' into feat/joinable-pools
groninge01 1997787
Merge branch 'main' into feat/joinable-pools
groninge01 57debcf
split to separate hook
groninge01 1e212ac
Merge branch 'main' into feat/joinable-pools
groninge01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,12 +1,12 @@ | ||
| 'use client' | ||
|
|
||
| import { createContext, PropsWithChildren, useEffect, useState } from 'react' | ||
| import { createContext, PropsWithChildren, useEffect, useMemo, useState } from 'react' | ||
| import { | ||
| GetPoolsDocument, | ||
| GqlChain, | ||
| GqlPoolType, | ||
| } from '@repo/lib/shared/services/api/generated/graphql' | ||
| import { useQuery } from '@apollo/client/react' | ||
| import { useApolloClient, useQuery } from '@apollo/client/react' | ||
| import { usePoolListQueryState } from './usePoolListQueryState' | ||
| import { useMandatoryContext } from '@repo/lib/shared/utils/contexts' | ||
| import { useUserAccount } from '../../web3/UserAccountProvider' | ||
|
|
@@ -15,6 +15,10 @@ import { PoolDisplayType } from '../pool.types' | |
| import { PROJECT_CONFIG } from '@repo/lib/config/getProjectConfig' | ||
| import { removeHookDataFromPoolIfNecessary } from '../pool.utils' | ||
| import { PoolListItem } from '../pool.types' | ||
| import { useQuery as useReactQuery } from '@tanstack/react-query' | ||
| import { useTokens } from '../../tokens/TokensProvider' | ||
| import { bn } from '@repo/lib/shared/utils/numbers' | ||
| import { useWalletTokenBalances } from '../../tokens/useWalletTokenBalances' | ||
|
|
||
| export function usePoolListLogic({ | ||
| fixedPoolTypes, | ||
|
|
@@ -24,12 +28,14 @@ export function usePoolListLogic({ | |
| fixedChains?: GqlChain[] | ||
| } = {}) { | ||
| const queryState = usePoolListQueryState() | ||
| const { userAddress } = useUserAccount() | ||
| const { userAddress, isConnected } = useUserAccount() | ||
| const apolloClient = useApolloClient() | ||
| const { isLoadingTokens, isLoadingTokenPrices } = useTokens() | ||
| const [poolDisplayType, setPoolDisplayType] = useState<PoolDisplayType>( | ||
| PROJECT_CONFIG.options.poolDisplayType | ||
| ) | ||
|
|
||
| const { queryVariables, toggleUserAddress } = queryState | ||
| const { queryVariables, toggleUserAddress, joinablePools } = queryState | ||
|
|
||
| const variables = { | ||
| ...queryVariables, | ||
|
|
@@ -51,6 +57,114 @@ export function usePoolListLogic({ | |
|
|
||
| const poolsData = pools.map(pool => removeHookDataFromPoolIfNecessary(pool)) as PoolListItem[] | ||
|
|
||
| const selectedChains = variables.where.chainIn || [] | ||
| const joinableChains = selectedChains.filter(chain => chain !== GqlChain.Sepolia) | ||
|
|
||
| const { | ||
| tokenBalancesByChain: walletTokenAddressesByChain, | ||
| isLoading: isWalletBalancesLoading, | ||
| errors: walletBalanceErrors, | ||
| hasBalance: hasWalletTokenBalance, | ||
| } = useWalletTokenBalances(joinableChains, joinablePools) | ||
|
|
||
| const joinablePoolsQuery = useReactQuery({ | ||
| queryKey: [ | ||
| 'pool-list-joinable-pools', | ||
| joinableChains.join(','), | ||
| joinableChains | ||
| .map(chain => `${chain}:${(walletTokenAddressesByChain.get(chain) || []).join(',')}`) | ||
| .join('|'), | ||
| queryVariables.first, | ||
| queryVariables.skip, | ||
| queryVariables.orderBy, | ||
| queryVariables.orderDirection, | ||
| queryVariables.textSearch || '', | ||
| queryVariables.where.protocolVersionIn?.join(',') || '', | ||
| queryVariables.where.poolTypeIn?.join(',') || '', | ||
| queryVariables.where.poolTypeNotIn?.join(',') || '', | ||
| queryVariables.where.tagIn?.join(',') || '', | ||
| queryVariables.where.tagNotIn?.join(',') || '', | ||
| ], | ||
| queryFn: async () => { | ||
| const chainsWithTokens = joinableChains | ||
| .map(chain => ({ | ||
| chain, | ||
| tokensIn: (walletTokenAddressesByChain.get(chain) || []).map(address => | ||
| address.toLowerCase() | ||
| ), | ||
| })) | ||
| .filter(({ tokensIn }) => tokensIn.length > 0) | ||
|
|
||
| const results = await Promise.allSettled( | ||
| chainsWithTokens.map(async ({ chain, tokensIn }) => { | ||
| const response = await apolloClient.query({ | ||
| query: GetPoolsDocument, | ||
| variables: { | ||
| ...queryVariables, | ||
| where: { | ||
| ...queryVariables.where, | ||
| chainIn: [chain], | ||
| tokensIn, | ||
| minTvl: 50_000, | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| return response.data?.pools || [] | ||
| }) | ||
| ) | ||
|
|
||
| return results.flatMap(result => (result.status === 'fulfilled' ? result.value : [])) | ||
| }, | ||
| enabled: | ||
| joinablePools && | ||
| isConnected && | ||
| isAddress(userAddress) && | ||
| !isLoadingTokens && | ||
| !isLoadingTokenPrices && | ||
| joinableChains.some(chain => (walletTokenAddressesByChain.get(chain) || []).length > 0), | ||
| staleTime: 30_000, | ||
| }) | ||
|
|
||
| const joinablePoolsData = useMemo(() => { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure we need the useMemo here.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is some deduping and sorting done so i think it's ok to keep it |
||
| if (!joinablePools || !isConnected || !isAddress(userAddress)) return poolsData | ||
| if (walletBalanceErrors.length > 0) return poolsData | ||
|
|
||
| const allJoinablePools = joinablePoolsQuery.data || [] | ||
| const uniquePools = new Map<string, PoolListItem>() | ||
|
|
||
| allJoinablePools.forEach(pool => { | ||
| if (!uniquePools.has(pool.id)) { | ||
| uniquePools.set(pool.id, removeHookDataFromPoolIfNecessary(pool) as PoolListItem) | ||
| } | ||
| }) | ||
|
|
||
| return Array.from(uniquePools.values()).sort((a, b) => { | ||
| return bn(b.dynamicData.totalLiquidity).comparedTo(bn(a.dynamicData.totalLiquidity)) ?? 0 | ||
| }) | ||
| }, [ | ||
| joinablePools, | ||
| poolsData, | ||
| isConnected, | ||
| userAddress, | ||
| joinablePoolsQuery.data, | ||
| walletBalanceErrors, | ||
| ]) | ||
|
|
||
| const isJoinableBalanceLoading = | ||
| joinablePools && | ||
| (isLoadingTokens || | ||
| isLoadingTokenPrices || | ||
| isWalletBalancesLoading || | ||
| joinablePoolsQuery.isLoading || | ||
| joinablePoolsQuery.isFetching) | ||
|
|
||
| const filteredPools = joinablePools | ||
| ? isJoinableBalanceLoading | ||
| ? poolsData | ||
| : joinablePoolsData | ||
| : poolsData | ||
|
|
||
| const isFixedPoolType = !!fixedPoolTypes && fixedPoolTypes.length > 0 | ||
|
|
||
| // If the user has previously selected to filter by their liquidity and then | ||
|
|
@@ -62,13 +176,14 @@ export function usePoolListLogic({ | |
| }, [userAddress]) | ||
|
|
||
| return { | ||
| pools: poolsData, | ||
| count: data?.count || previousData?.count, | ||
| pools: filteredPools, | ||
| count: joinablePools ? filteredPools.length : data?.count || previousData?.count, | ||
| queryState, | ||
| loading, | ||
| loading: loading || isJoinableBalanceLoading, | ||
| error, | ||
| networkStatus, | ||
| isFixedPoolType, | ||
| hasWalletTokenBalance, | ||
| refetch, | ||
| poolDisplayType, | ||
| setPoolDisplayType, | ||
|
|
||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we really want to cache each of these queries?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's caching several apollo queries so why don't do it?