Skip to content
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

fix: display all pools in lending "Available Pools" #7754

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/pages/Lending/AvailablePools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,20 @@ export const AvailablePools = () => {
[history, path],
)
const headerComponent = useMemo(() => <LendingHeader />, [])
const { data: lendingSupportedAssets } = useLendingSupportedAssets({
type: 'collateral',
statusFilter: 'All',
})
const { isLoading: isLendingSupportedAssetsLoading, data: lendingSupportedAssets } =
useLendingSupportedAssets({
type: 'collateral',
statusFilter: 'All',
})

const lendingRows = useMemo(() => {
if (!lendingSupportedAssets)
if (isLendingSupportedAssetsLoading)
return new Array(2).fill(null).map((_, i) => <Skeleton key={i} height={16} />)

return lendingSupportedAssets.map(asset => (
return (lendingSupportedAssets ?? []).map(asset => (
<LendingPoolButton key={asset.assetId} asset={asset} onPoolClick={handlePoolClick} />
))
}, [handlePoolClick, lendingSupportedAssets])
}, [handlePoolClick, isLendingSupportedAssetsLoading, lendingSupportedAssets])

return (
<Main headerComponent={headerComponent} isSubPage>
Expand Down
1 change: 0 additions & 1 deletion src/pages/Lending/YourLoans.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ export const YourLoans = () => {

const { data: lendingSupportedAssets } = useLendingSupportedAssets({
type: 'collateral',
hasLoanCollateral: false,
})

const history = useHistory()
Expand Down
1 change: 0 additions & 1 deletion src/pages/Lending/hooks/useAllLendingPositionsData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type UseAllLendingPositionsDataProps = {
export const useAllLendingPositionsData = ({ assetId }: UseAllLendingPositionsDataProps = {}) => {
const { data: lendingSupportedAssets } = useLendingSupportedAssets({
type: 'collateral',
hasLoanCollateral: false,
})

const accountIds = useAppSelector(selectWalletAccountIds)
Expand Down
32 changes: 27 additions & 5 deletions src/pages/Lending/hooks/useLendingSupportedAssets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import { useSelector } from 'react-redux'
import { useIsSnapInstalled } from 'hooks/useIsSnapInstalled/useIsSnapInstalled'
import { useWallet } from 'hooks/useWallet/useWallet'
import { walletSupportsChain } from 'hooks/useWalletSupportsChain/useWalletSupportsChain'
import { bnOrZero } from 'lib/bignumber/bignumber'
import { isSome } from 'lib/utils'
import { thorchainBlockTimeMs } from 'lib/utils/thorchain/constants'
import {
selectAccountIdsByChainId,
selectAssetById,
Expand All @@ -25,14 +25,14 @@ import { store, useAppSelector } from 'state/store'

const queryKey = ['lendingSupportedAssets']

const lendingThorRegex = /^LENDING-THOR-(\w+)$/

export const useLendingSupportedAssets = ({
type,
statusFilter = 'Available',
hasLoanCollateral = true,
}: {
type: 'collateral' | 'borrow'
statusFilter?: ThornodePoolStatuses | 'All'
hasLoanCollateral?: boolean
}) => {
const wallet = useWallet().state.wallet
const { isSnapInstalled } = useIsSnapInstalled()
Expand All @@ -48,6 +48,11 @@ export const useLendingSupportedAssets = ({
: undefined,
})

const { data: mimir } = useQuery({
...reactQueries.thornode.mimir(),
staleTime: thorchainBlockTimeMs,
})

const accountIdsByChainId = useAppSelector(selectAccountIdsByChainId)
const walletSupportChains = useMemo(
() =>
Expand All @@ -68,9 +73,26 @@ export const useLendingSupportedAssets = ({
const selectSupportedAssets = useCallback(
(data: ThornodePoolResponse[] | undefined) => {
if (!data) return []
if (!mimir) return []

const pools = (availablePools ?? []).filter(pool => {
if (type === 'borrow') return true
if (type === 'collateral') return !hasLoanCollateral || bnOrZero(pool.loan_collateral).gt(0)
if (type === 'collateral') {
const mimirLendingEnabledPools = Object.keys(mimir)
.filter(key => lendingThorRegex.test(key))
.map(key => {
const match = key.match(lendingThorRegex)?.[1]
if (!match) return undefined

// i.e LENDING-THOR-BTC, LENDING-THOR-ETH
// No token pools support, so that works, and lending's pretty much going hasta la vista
// so we don't need to worry about this not generalizing to assets
return `${match}.${match}`
})
.filter(Boolean)

return mimirLendingEnabledPools.includes(pool.asset)
}
return false
})

Expand Down Expand Up @@ -103,7 +125,7 @@ export const useLendingSupportedAssets = ({
}
return supportedAssets
},
[availablePools, hasLoanCollateral, type, wallet, walletChainIds, walletSupportChains],
[availablePools, mimir, type, wallet, walletChainIds, walletSupportChains],
)

const lendingSupportedAssetsQuery = useQuery({
Expand Down
Loading