2022-10-20 15:39:21 +00:00
|
|
|
import BigNumber from 'bignumber.js'
|
2022-12-06 09:20:22 +00:00
|
|
|
import { useMemo, useRef, useState } from 'react'
|
2022-09-29 19:21:31 +00:00
|
|
|
|
2022-11-09 09:04:06 +00:00
|
|
|
import BorrowTable from 'components/Borrow/BorrowTable'
|
|
|
|
import BorrowModal from 'components/BorrowModal'
|
2022-11-29 16:45:00 +00:00
|
|
|
import Card from 'components/Card'
|
2022-11-09 09:04:06 +00:00
|
|
|
import RepayModal from 'components/RepayModal'
|
2022-11-29 16:45:00 +00:00
|
|
|
import Text from 'components/Text'
|
2022-10-20 15:39:21 +00:00
|
|
|
import useAllowedCoins from 'hooks/useAllowedCoins'
|
|
|
|
import useCreditAccountPositions from 'hooks/useCreditAccountPositions'
|
|
|
|
import useMarkets from 'hooks/useMarkets'
|
2022-10-24 15:15:26 +00:00
|
|
|
import useRedbankBalances from 'hooks/useRedbankBalances'
|
2022-11-09 09:04:06 +00:00
|
|
|
import useTokenPrices from 'hooks/useTokenPrices'
|
|
|
|
import useCreditManagerStore from 'stores/useCreditManagerStore'
|
|
|
|
import { getTokenDecimals, getTokenInfo } from 'utils/tokens'
|
2022-09-29 19:21:31 +00:00
|
|
|
|
2022-10-28 11:14:14 +00:00
|
|
|
type ModalState = {
|
|
|
|
show: 'borrow' | 'repay' | false
|
|
|
|
data: {
|
|
|
|
tokenDenom: string
|
|
|
|
}
|
|
|
|
}
|
2022-09-02 11:51:00 +00:00
|
|
|
|
|
|
|
const Borrow = () => {
|
2022-10-28 11:14:14 +00:00
|
|
|
const [modalState, setModalState] = useState<ModalState>({
|
|
|
|
show: false,
|
|
|
|
data: { tokenDenom: '' },
|
|
|
|
})
|
2022-10-20 15:39:21 +00:00
|
|
|
|
|
|
|
const selectedAccount = useCreditManagerStore((s) => s.selectedAccount)
|
|
|
|
|
|
|
|
const { data: allowedCoinsData } = useAllowedCoins()
|
|
|
|
const { data: positionsData } = useCreditAccountPositions(selectedAccount ?? '')
|
|
|
|
const { data: marketsData } = useMarkets()
|
|
|
|
const { data: tokenPrices } = useTokenPrices()
|
2022-10-24 15:15:26 +00:00
|
|
|
const { data: redbankBalances } = useRedbankBalances()
|
2022-10-20 15:39:21 +00:00
|
|
|
|
2022-10-28 11:14:14 +00:00
|
|
|
// recreate modals and reset state whenever ref changes
|
|
|
|
const modalId = useRef(0)
|
|
|
|
|
2022-10-20 15:39:21 +00:00
|
|
|
const borrowedAssetsMap = useMemo(() => {
|
|
|
|
let borrowedAssetsMap: Map<string, string> = new Map()
|
|
|
|
|
|
|
|
positionsData?.debts.forEach((coin) => {
|
|
|
|
borrowedAssetsMap.set(coin.denom, coin.amount)
|
|
|
|
})
|
|
|
|
|
|
|
|
return borrowedAssetsMap
|
|
|
|
}, [positionsData])
|
|
|
|
|
|
|
|
const { borrowedAssets, notBorrowedAssets } = useMemo(() => {
|
|
|
|
return {
|
|
|
|
borrowedAssets:
|
|
|
|
allowedCoinsData
|
|
|
|
?.filter((denom) => borrowedAssetsMap.has(denom))
|
|
|
|
.map((denom) => {
|
|
|
|
const { symbol, chain, icon } = getTokenInfo(denom)
|
|
|
|
const borrowRate = Number(marketsData?.[denom].borrow_rate) || 0
|
2022-10-25 10:31:13 +00:00
|
|
|
const marketLiquidity = BigNumber(redbankBalances?.[denom] ?? 0)
|
2022-10-20 15:39:21 +00:00
|
|
|
.div(10 ** getTokenDecimals(denom))
|
|
|
|
.toNumber()
|
|
|
|
|
|
|
|
const borrowAmount = BigNumber(borrowedAssetsMap.get(denom) as string)
|
|
|
|
.div(10 ** getTokenDecimals(denom))
|
|
|
|
.toNumber()
|
|
|
|
const borrowValue = borrowAmount * (tokenPrices?.[denom] ?? 0)
|
|
|
|
|
|
|
|
const rowData = {
|
|
|
|
denom,
|
|
|
|
symbol,
|
|
|
|
icon,
|
|
|
|
chain,
|
|
|
|
borrowed: {
|
|
|
|
amount: borrowAmount,
|
|
|
|
value: borrowValue,
|
|
|
|
},
|
|
|
|
borrowRate,
|
|
|
|
marketLiquidity,
|
|
|
|
}
|
|
|
|
|
|
|
|
return rowData
|
|
|
|
}) ?? [],
|
|
|
|
notBorrowedAssets:
|
|
|
|
allowedCoinsData
|
|
|
|
?.filter((denom) => !borrowedAssetsMap.has(denom))
|
|
|
|
.map((denom) => {
|
|
|
|
const { symbol, chain, icon } = getTokenInfo(denom)
|
|
|
|
const borrowRate = Number(marketsData?.[denom].borrow_rate) || 0
|
2022-10-25 10:31:13 +00:00
|
|
|
const marketLiquidity = BigNumber(redbankBalances?.[denom] ?? 0)
|
2022-10-20 15:39:21 +00:00
|
|
|
.div(10 ** getTokenDecimals(denom))
|
|
|
|
.toNumber()
|
|
|
|
|
|
|
|
const rowData = {
|
|
|
|
denom,
|
|
|
|
symbol,
|
|
|
|
icon,
|
|
|
|
chain,
|
|
|
|
borrowed: null,
|
|
|
|
borrowRate,
|
|
|
|
marketLiquidity,
|
|
|
|
}
|
|
|
|
|
|
|
|
return rowData
|
|
|
|
}) ?? [],
|
|
|
|
}
|
2022-10-24 15:15:26 +00:00
|
|
|
}, [allowedCoinsData, borrowedAssetsMap, marketsData, redbankBalances, tokenPrices])
|
2022-10-20 15:39:21 +00:00
|
|
|
|
|
|
|
const handleBorrowClick = (denom: string) => {
|
2022-10-28 11:14:14 +00:00
|
|
|
setModalState({ show: 'borrow', data: { tokenDenom: denom } })
|
|
|
|
modalId.current += 1
|
2022-10-20 15:39:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 11:14:14 +00:00
|
|
|
const handleRepayClick = (denom: string) => {
|
|
|
|
setModalState({ show: 'repay', data: { tokenDenom: denom } })
|
|
|
|
modalId.current += 1
|
2022-10-20 15:39:21 +00:00
|
|
|
}
|
|
|
|
|
2022-09-02 14:55:49 +00:00
|
|
|
return (
|
2022-12-06 09:20:22 +00:00
|
|
|
<div className='flex w-full items-start gap-4'>
|
2022-11-09 09:04:06 +00:00
|
|
|
<div className='flex-1'>
|
2022-11-29 16:45:00 +00:00
|
|
|
<Card className='mb-4'>
|
2022-10-28 11:14:14 +00:00
|
|
|
<div>
|
2022-11-29 16:45:00 +00:00
|
|
|
<Text tag='h3' size='xl' uppercase={true} className='mb-7 text-center'>
|
|
|
|
Borrowings
|
|
|
|
</Text>
|
2022-10-20 15:39:21 +00:00
|
|
|
<BorrowTable
|
|
|
|
data={borrowedAssets}
|
|
|
|
onBorrowClick={handleBorrowClick}
|
|
|
|
onRepayClick={handleRepayClick}
|
|
|
|
/>
|
2022-09-29 19:21:31 +00:00
|
|
|
</div>
|
2022-11-29 16:45:00 +00:00
|
|
|
</Card>
|
|
|
|
<Card>
|
2022-10-20 15:39:21 +00:00
|
|
|
<div>
|
2022-11-29 16:45:00 +00:00
|
|
|
<Text tag='h3' size='xl' uppercase={true} className='mb-7 text-center text-lg-caps'>
|
|
|
|
Available to Borrow
|
|
|
|
</Text>
|
2022-10-20 15:39:21 +00:00
|
|
|
<BorrowTable
|
|
|
|
data={notBorrowedAssets}
|
|
|
|
onBorrowClick={handleBorrowClick}
|
|
|
|
onRepayClick={handleRepayClick}
|
|
|
|
/>
|
2022-09-29 19:21:31 +00:00
|
|
|
</div>
|
2022-11-29 16:45:00 +00:00
|
|
|
</Card>
|
2022-10-20 15:39:21 +00:00
|
|
|
</div>
|
2022-10-28 11:14:14 +00:00
|
|
|
<BorrowModal
|
|
|
|
key={`borrowModal_${modalId.current}`}
|
|
|
|
tokenDenom={modalState.data.tokenDenom}
|
|
|
|
show={modalState.show === 'borrow'}
|
|
|
|
onClose={() => setModalState({ ...modalState, show: false })}
|
|
|
|
/>
|
|
|
|
<RepayModal
|
2022-11-07 16:36:12 +00:00
|
|
|
key={`repayModal_${modalId.current}`}
|
2022-10-28 11:14:14 +00:00
|
|
|
tokenDenom={modalState.data.tokenDenom}
|
|
|
|
show={modalState.show === 'repay'}
|
|
|
|
onClose={() => setModalState({ ...modalState, show: false })}
|
|
|
|
/>
|
2022-09-02 14:55:49 +00:00
|
|
|
</div>
|
2022-09-29 19:21:31 +00:00
|
|
|
)
|
|
|
|
}
|
2022-09-02 11:51:00 +00:00
|
|
|
|
2022-09-29 19:21:31 +00:00
|
|
|
export default Borrow
|