mango-ui-v3/hooks/useMangoStats.tsx

77 lines
2.4 KiB
TypeScript
Raw Normal View History

import { useEffect, useState } from 'react'
import { I80F48 } from '@blockworks-foundation/mango-client'
import useMangoStore from '../stores/useMangoStore'
import useMangoGroupConfig from './useMangoGroupConfig'
const useMangoStats = () => {
2021-08-23 14:09:16 -07:00
const [stats, setStats] = useState([
{
2021-08-23 14:09:16 -07:00
name: '',
hourly: '',
depositInterest: 0,
borrowInterest: 0,
totalDeposits: 0,
totalBorrows: 0,
2021-08-23 14:09:16 -07:00
baseOraclePrice: 0,
utilization: '0',
},
])
const [latestStats, setLatestStats] = useState<any[]>([])
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
const mangoGroupName = useMangoStore((s) => s.selectedMangoGroup.name)
2021-07-21 14:26:18 -07:00
const connection = useMangoStore((s) => s.connection.current)
const config = useMangoGroupConfig()
useEffect(() => {
2021-08-23 14:09:16 -07:00
const fetchHistoricalStats = async () => {
const response = await fetch(
`https://mango-stats-v3.herokuapp.com/spot?mangoGroup=${mangoGroupName}`
)
const stats = await response.json()
setStats(stats)
}
fetchHistoricalStats()
2021-07-21 14:31:28 -07:00
}, [mangoGroupName])
useEffect(() => {
const getLatestStats = async () => {
if (mangoGroup) {
2021-07-21 14:26:18 -07:00
const rootBanks = await mangoGroup.loadRootBanks(connection)
const latestStats = config.tokens.map((token) => {
const rootBank = rootBanks.find((bank) => {
if (!bank) {
2021-07-21 14:26:18 -07:00
return false
}
2021-07-21 14:26:18 -07:00
return bank.publicKey.toBase58() == token.rootKey.toBase58()
})
2021-07-21 14:26:18 -07:00
const totalDeposits = rootBank.getUiTotalDeposit(mangoGroup)
const totalBorrows = rootBank.getUiTotalBorrow(mangoGroup)
return {
time: new Date(),
2021-08-23 14:09:16 -07:00
name: token.symbol,
2021-07-21 14:47:10 -07:00
totalDeposits: totalDeposits.toFixed(2),
totalBorrows: totalBorrows.toFixed(2),
2021-07-21 14:26:18 -07:00
depositInterest: rootBank
.getDepositRate(mangoGroup)
.mul(I80F48.fromNumber(100)),
borrowInterest: rootBank
.getBorrowRate(mangoGroup)
.mul(I80F48.fromNumber(100)),
2021-07-27 08:41:33 -07:00
utilization: totalDeposits.gt(I80F48.fromNumber(0))
? totalBorrows.div(totalDeposits)
: I80F48.fromNumber(0),
}
2021-07-21 14:26:18 -07:00
})
setLatestStats(latestStats)
2021-07-21 14:26:18 -07:00
}
}
getLatestStats()
2021-07-21 14:31:28 -07:00
}, [mangoGroup])
return { latestStats, stats }
}
export default useMangoStats