63 lines
1.7 KiB
TypeScript
63 lines
1.7 KiB
TypeScript
import {
|
|
Bank,
|
|
OracleProvider,
|
|
PerpMarket,
|
|
Serum3Market,
|
|
} from '@blockworks-foundation/mango-v4'
|
|
import mangoStore from '@store/mangoStore'
|
|
import { useMemo } from 'react'
|
|
import { formatTokenSymbol } from 'utils/tokens'
|
|
import useSelectedMarket from './useSelectedMarket'
|
|
|
|
export const getOracleProvider = (
|
|
marketOrBank: PerpMarket | Serum3Market | Bank,
|
|
) => {
|
|
let marketOrBase: PerpMarket | Bank
|
|
let name: string
|
|
if (marketOrBank instanceof Bank) {
|
|
marketOrBase = marketOrBank
|
|
name = formatTokenSymbol(marketOrBank.name)
|
|
} else if (marketOrBank instanceof PerpMarket) {
|
|
marketOrBase = marketOrBank
|
|
name = marketOrBank.name.split('-')[0]
|
|
} else {
|
|
const group = mangoStore.getState().group
|
|
if (!group) return ['Unavailable', '']
|
|
const baseBank = group.getFirstBankByTokenIndex(marketOrBank.baseTokenIndex)
|
|
marketOrBase = baseBank
|
|
name = formatTokenSymbol(baseBank.name)
|
|
}
|
|
|
|
if (name === 'USDC') return ['N/A', '']
|
|
|
|
switch (marketOrBase.oracleProvider) {
|
|
case OracleProvider.Pyth:
|
|
return [
|
|
'Pyth',
|
|
`https://pyth.network/price-feeds/crypto-${name.toLowerCase()}-usd`,
|
|
]
|
|
case OracleProvider.Switchboard:
|
|
return [
|
|
'Switchboard',
|
|
`https://switchboard.xyz/explorer/3/${marketOrBase.oracle.toString()}`,
|
|
]
|
|
case OracleProvider.Stub:
|
|
return ['Stub', '']
|
|
default:
|
|
return ['Unknown', '']
|
|
}
|
|
}
|
|
|
|
const useOracleProvider = () => {
|
|
const { selectedMarket } = useSelectedMarket()
|
|
|
|
const [oracleProvider, oracleLinkPath] = useMemo(() => {
|
|
if (!selectedMarket) return ['', '']
|
|
return getOracleProvider(selectedMarket)
|
|
}, [selectedMarket])
|
|
|
|
return { oracleProvider, oracleLinkPath }
|
|
}
|
|
|
|
export default useOracleProvider
|