Skip to content

Commit

Permalink
feat: lending support all thor-supported assets as borrow (#5675)
Browse files Browse the repository at this point in the history
  • Loading branch information
gomesalexandre authored Nov 22, 2023
1 parent 519842f commit 61c87f3
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/pages/Lending/AvailablePools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const AvailablePools = () => {
[history, path],
)
const headerComponent = useMemo(() => <LendingHeader />, [])
const { data: lendingSupportedAssets } = useLendingSupportedAssets()
const { data: lendingSupportedAssets } = useLendingSupportedAssets({ type: 'collateral' })

const lendingRows = useMemo(() => {
if (!lendingSupportedAssets) return new Array(2).fill(null).map(() => <Skeleton height={16} />)
Expand Down
21 changes: 11 additions & 10 deletions src/pages/Lending/Pool/components/Borrow/BorrowInput.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Button, CardFooter, Collapse, Skeleton, Stack } from '@chakra-ui/react'
import { type AccountId, type AssetId } from '@shapeshiftoss/caip'
import noop from 'lodash/noop'
import { useCallback, useEffect, useMemo, useState } from 'react'
import { useTranslate } from 'react-polyglot'
import { useHistory } from 'react-router'
Expand Down Expand Up @@ -69,13 +70,13 @@ export const BorrowInput = ({
const translate = useTranslate()
const history = useHistory()

const { data: lendingSupportedAssets } = useLendingSupportedAssets()
const { data: borrowAssets } = useLendingSupportedAssets({ type: 'borrow' })

useEffect(() => {
if (!lendingSupportedAssets) return
if (!borrowAssets) return

setBorrowAsset(lendingSupportedAssets[0])
}, [lendingSupportedAssets, setBorrowAsset])
setBorrowAsset(borrowAssets[0])
}, [borrowAssets, setBorrowAsset])

const collateralAsset = useAppSelector(state => selectAssetById(state, collateralAssetId))

Expand All @@ -86,9 +87,9 @@ export const BorrowInput = ({
buyAssetSearch.open({
onClick: setBorrowAsset,
title: 'lending.borrow',
assets: lendingSupportedAssets,
assets: borrowAssets,
})
}, [buyAssetSearch, lendingSupportedAssets, setBorrowAsset])
}, [borrowAssets, buyAssetSearch, setBorrowAsset])

const handleAssetChange = useCallback((asset: Asset) => {
return console.info(asset)
Expand Down Expand Up @@ -239,16 +240,16 @@ export const BorrowInput = ({
history.push(BorrowRoutePaths.Sweep)
}, [history, isSweepNeeded])

const depositAssetSelectComponent = useMemo(() => {
const collateralAssetSelectComponent = useMemo(() => {
return (
<TradeAssetSelect
assetId={collateralAssetId}
onAssetClick={handleBorrowAssetClick}
onAssetClick={noop}
onAssetChange={handleAssetChange}
isReadOnly
/>
)
}, [collateralAssetId, handleAssetChange, handleBorrowAssetClick])
}, [collateralAssetId, handleAssetChange])

const borrowAssetSelectComponent = useMemo(() => {
return (
Expand Down Expand Up @@ -327,7 +328,7 @@ export const BorrowInput = ({
onAccountIdChange={handleCollateralAccountIdChange}
formControlProps={formControlProps}
layout='inline'
labelPostFix={depositAssetSelectComponent}
labelPostFix={collateralAssetSelectComponent}
/>
<TradeAssetInput
assetId={borrowAsset?.assetId ?? ''}
Expand Down
28 changes: 9 additions & 19 deletions src/pages/Lending/Pool/components/Repay/RepayInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ import { Text } from 'components/Text'
import { useModal } from 'hooks/useModal/useModal'
import type { Asset } from 'lib/asset-service'
import { bn, bnOrZero } from 'lib/bignumber/bignumber'
import { thorchainSwapper } from 'lib/swapper/swappers/ThorchainSwapper/ThorchainSwapper'
import { isSome } from 'lib/utils'
import { useLendingQuoteCloseQuery } from 'pages/Lending/hooks/useLendingCloseQuery'
import { useLendingPositionData } from 'pages/Lending/hooks/useLendingPositionData'
import { useLendingSupportedAssets } from 'pages/Lending/hooks/useLendingSupportedAssets'
import { useQuoteEstimatedFeesQuery } from 'pages/Lending/hooks/useQuoteEstimatedFees'
import {
selectAssetById,
selectAssets,
selectMarketDataById,
selectPortfolioCryptoBalanceBaseUnitByFilter,
selectUserCurrencyToUsdRate,
Expand Down Expand Up @@ -87,21 +85,13 @@ export const RepayInput = ({

const percentOptions = useMemo(() => [0], [])

const assetsById = useAppSelector(selectAssets)

const [repaymentSupportedAssets, setRepaymentSupportedAssets] = useState<Asset[]>([])
const { data: lendingSupportedAssets } = useLendingSupportedAssets({ type: 'borrow' })

useEffect(() => {
;(async () => {
if (!repaymentAsset) setRepaymentAsset(assetsById[collateralAssetId] as Asset)
if (!lendingSupportedAssets) return

const assets = Object.values(assetsById) as Asset[]
const thorSellAssets = (await thorchainSwapper.filterAssetIdsBySellable(assets))
.map(assetId => assetsById[assetId])
.filter(isSome)
setRepaymentSupportedAssets(thorSellAssets)
})()
}, [assetsById, collateralAssetId, repaymentAsset, setRepaymentAsset])
setRepaymentAsset(lendingSupportedAssets[0])
}, [lendingSupportedAssets, setRepaymentAsset])

const useLendingQuoteCloseQueryArgs = useMemo(
() => ({
Expand Down Expand Up @@ -130,14 +120,14 @@ export const RepayInput = ({

const buyAssetSearch = useModal('buyAssetSearch')
const handleRepaymentAssetClick = useCallback(() => {
if (!repaymentSupportedAssets.length) return
if (!lendingSupportedAssets?.length) return

buyAssetSearch.open({
onClick: setRepaymentAsset,
title: 'lending.borrow',
assets: repaymentSupportedAssets,
title: 'lending.repay',
assets: lendingSupportedAssets,
})
}, [buyAssetSearch, repaymentSupportedAssets, setRepaymentAsset])
}, [buyAssetSearch, lendingSupportedAssets, setRepaymentAsset])

const handleAssetChange = useCallback((asset: Asset) => {
return console.info(asset)
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Lending/YourLoans.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export const YourLoans = () => {
const translate = useTranslate()
const lendingHeader = useMemo(() => <LendingHeader />, [])

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

const history = useHistory()

Expand Down
2 changes: 1 addition & 1 deletion src/pages/Lending/hooks/useAllLendingPositionsData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type UseAllLendingPositionsDataProps = {
}

export const useAllLendingPositionsData = ({ assetId }: UseAllLendingPositionsDataProps = {}) => {
const { data: lendingSupportedAssets } = useLendingSupportedAssets()
const { data: lendingSupportedAssets } = useLendingSupportedAssets({ type: 'collateral' })

const accounts = useMemo(
() =>
Expand Down
45 changes: 25 additions & 20 deletions src/pages/Lending/hooks/useLendingSupportedAssets.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useQuery } from '@tanstack/react-query'
import { getConfig } from 'config'
import { useCallback } from 'react'
import { bnOrZero } from 'lib/bignumber/bignumber'
import type { ThornodePoolResponse } from 'lib/swapper/swappers/ThorchainSwapper/types'
import { poolAssetIdToAssetId } from 'lib/swapper/swappers/ThorchainSwapper/utils/poolAssetHelpers/poolAssetHelpers'
import { thorService } from 'lib/swapper/swappers/ThorchainSwapper/utils/thorService'
Expand All @@ -9,29 +11,14 @@ import { store } from 'state/store'

const queryKey = ['lendingSupportedAssets']

export const useLendingSupportedAssets = () => {
const lendingPositionData = useQuery({
staleTime: Infinity,
queryKey,
queryFn: async () => {
const daemonUrl = getConfig().REACT_APP_THORCHAIN_NODE_URL
const poolResponse = await thorService.get<ThornodePoolResponse[]>(
`${daemonUrl}/lcd/thorchain/pools`,
)
if (poolResponse.isOk()) {
const allPools = poolResponse.unwrap().data

return allPools
}
},
select: data => {
export const useLendingSupportedAssets = ({ type }: { type: 'collateral' | 'borrow' }) => {
const selectSupportedAssets = useCallback(
(data: ThornodePoolResponse[] | undefined) => {
if (!data) return []
const availablePools = data.filter(
pool =>
pool.status === 'Available' &&
// This is weird, but THORChain API is currently returning a loan_cr of 20000 for pools which don't support lending
pool.loan_cr !== '20000' &&
pool.loan_cr !== '0',
(type === 'borrow' || bnOrZero(pool.loan_collateral).gt(0)),
)

return availablePools
Expand All @@ -42,7 +29,25 @@ export const useLendingSupportedAssets = () => {
})
.filter(isSome)
},
[type],
)

const lendingSupportedAssetsQuery = useQuery({
staleTime: Infinity,
queryKey,
queryFn: async () => {
const daemonUrl = getConfig().REACT_APP_THORCHAIN_NODE_URL
const poolResponse = await thorService.get<ThornodePoolResponse[]>(
`${daemonUrl}/lcd/thorchain/pools`,
)
if (poolResponse.isOk()) {
const allPools = poolResponse.unwrap().data

return allPools
}
},
select: selectSupportedAssets,
})

return lendingPositionData
return lendingSupportedAssetsQuery
}

0 comments on commit 61c87f3

Please sign in to comment.