2023-05-11 21:08:06 -07:00
|
|
|
|
import { ExclamationTriangleIcon } from '@heroicons/react/24/outline'
|
2023-04-25 13:21:32 -07:00
|
|
|
|
import useSelectedMarket from 'hooks/useSelectedMarket'
|
|
|
|
|
import Tooltip from '@components/shared/Tooltip'
|
|
|
|
|
import { useTranslation } from 'next-i18next'
|
|
|
|
|
import mangoStore from '@store/mangoStore'
|
|
|
|
|
import { useEffect, useState } from 'react'
|
2023-04-30 21:42:29 -07:00
|
|
|
|
import { PerpMarket, Bank } from '@blockworks-foundation/mango-v4'
|
2023-04-25 13:21:32 -07:00
|
|
|
|
import { BorshAccountsCoder } from '@coral-xyz/anchor'
|
|
|
|
|
import {
|
|
|
|
|
floorToDecimal,
|
|
|
|
|
formatCurrencyValue,
|
|
|
|
|
getDecimalCount,
|
|
|
|
|
} from 'utils/numbers'
|
2023-04-27 09:23:18 -07:00
|
|
|
|
import dayjs from 'dayjs'
|
|
|
|
|
import duration from 'dayjs/plugin/duration'
|
|
|
|
|
import relativeTime from 'dayjs/plugin/relativeTime'
|
2023-04-30 21:42:29 -07:00
|
|
|
|
import useOracleProvider from 'hooks/useOracleProvider'
|
|
|
|
|
import { ArrowTopRightOnSquareIcon } from '@heroicons/react/20/solid'
|
2023-04-25 13:21:32 -07:00
|
|
|
|
|
2023-04-28 05:22:40 -07:00
|
|
|
|
const OraclePrice = ({
|
|
|
|
|
setChangePrice,
|
|
|
|
|
}: {
|
|
|
|
|
setChangePrice: (price: number) => void
|
|
|
|
|
}) => {
|
2023-04-25 13:21:32 -07:00
|
|
|
|
const {
|
|
|
|
|
serumOrPerpMarket,
|
|
|
|
|
price: stalePrice,
|
|
|
|
|
selectedMarket,
|
|
|
|
|
quoteBank,
|
|
|
|
|
} = useSelectedMarket()
|
2023-04-27 09:23:18 -07:00
|
|
|
|
dayjs.extend(duration)
|
|
|
|
|
dayjs.extend(relativeTime)
|
|
|
|
|
|
2023-04-25 13:21:32 -07:00
|
|
|
|
const connection = mangoStore((s) => s.connection)
|
|
|
|
|
const [price, setPrice] = useState(stalePrice)
|
|
|
|
|
const [oracleLastUpdatedSlot, setOracleLastUpdatedSlot] = useState(0)
|
2023-04-27 09:23:18 -07:00
|
|
|
|
const [highestSlot, setHighestSlot] = useState(0)
|
|
|
|
|
const [isStale, setIsStale] = useState(false)
|
2023-04-30 21:42:29 -07:00
|
|
|
|
const { oracleProvider, oracleLinkPath } = useOracleProvider()
|
2023-04-27 09:23:18 -07:00
|
|
|
|
|
2023-04-25 13:21:32 -07:00
|
|
|
|
const { t } = useTranslation(['common', 'trade'])
|
|
|
|
|
|
|
|
|
|
//subscribe to the market oracle account
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const client = mangoStore.getState().client
|
|
|
|
|
const group = mangoStore.getState().group
|
|
|
|
|
if (!group || !selectedMarket) return
|
|
|
|
|
|
|
|
|
|
let marketOrBank: PerpMarket | Bank
|
|
|
|
|
let decimals: number
|
|
|
|
|
if (selectedMarket instanceof PerpMarket) {
|
|
|
|
|
marketOrBank = selectedMarket
|
|
|
|
|
decimals = selectedMarket.baseDecimals
|
|
|
|
|
} else {
|
|
|
|
|
const baseBank = group.getFirstBankByTokenIndex(
|
|
|
|
|
selectedMarket.baseTokenIndex
|
|
|
|
|
)
|
|
|
|
|
marketOrBank = baseBank
|
|
|
|
|
decimals = group.getMintDecimals(baseBank.mint)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const coder = new BorshAccountsCoder(client.program.idl)
|
2023-05-14 09:43:42 -07:00
|
|
|
|
setPrice(stalePrice)
|
2023-04-25 13:21:32 -07:00
|
|
|
|
const subId = connection.onAccountChange(
|
|
|
|
|
marketOrBank.oracle,
|
2023-04-27 09:23:18 -07:00
|
|
|
|
async (info, context) => {
|
2023-04-25 13:21:32 -07:00
|
|
|
|
const { price, uiPrice, lastUpdatedSlot } =
|
|
|
|
|
await group.decodePriceFromOracleAi(
|
|
|
|
|
coder,
|
|
|
|
|
marketOrBank.oracle,
|
|
|
|
|
info,
|
|
|
|
|
decimals,
|
|
|
|
|
client
|
|
|
|
|
)
|
|
|
|
|
marketOrBank._price = price
|
|
|
|
|
marketOrBank._uiPrice = uiPrice
|
|
|
|
|
marketOrBank._oracleLastUpdatedSlot = lastUpdatedSlot
|
|
|
|
|
setOracleLastUpdatedSlot(lastUpdatedSlot)
|
2023-04-27 09:23:18 -07:00
|
|
|
|
|
|
|
|
|
const marketSlot = mangoStore.getState().selectedMarket.lastSeenSlot
|
|
|
|
|
const oracleWriteSlot = context.slot
|
|
|
|
|
const accountSlot = mangoStore.getState().mangoAccount.lastSlot
|
|
|
|
|
const highestSlot = Math.max(
|
|
|
|
|
marketSlot.bids,
|
|
|
|
|
marketSlot.asks,
|
|
|
|
|
oracleWriteSlot,
|
|
|
|
|
accountSlot
|
|
|
|
|
)
|
2023-04-27 11:27:56 -07:00
|
|
|
|
const maxStalenessSlots =
|
|
|
|
|
marketOrBank.oracleConfig.maxStalenessSlots.toNumber()
|
2023-04-27 09:23:18 -07:00
|
|
|
|
setHighestSlot(highestSlot)
|
|
|
|
|
setIsStale(
|
2023-04-27 11:01:52 -07:00
|
|
|
|
maxStalenessSlots > 0 &&
|
2023-04-27 11:27:56 -07:00
|
|
|
|
highestSlot - lastUpdatedSlot > maxStalenessSlots
|
2023-04-27 09:23:18 -07:00
|
|
|
|
)
|
|
|
|
|
|
2023-04-25 13:21:32 -07:00
|
|
|
|
if (selectedMarket instanceof PerpMarket) {
|
|
|
|
|
setPrice(uiPrice)
|
2023-04-28 05:22:40 -07:00
|
|
|
|
setChangePrice(uiPrice)
|
2023-04-25 13:21:32 -07:00
|
|
|
|
} else {
|
|
|
|
|
let price
|
|
|
|
|
if (quoteBank && serumOrPerpMarket) {
|
|
|
|
|
price = floorToDecimal(
|
|
|
|
|
uiPrice / quoteBank.uiPrice,
|
|
|
|
|
getDecimalCount(serumOrPerpMarket.tickSize)
|
|
|
|
|
).toNumber()
|
|
|
|
|
} else {
|
|
|
|
|
price = 0
|
|
|
|
|
}
|
|
|
|
|
setPrice(price)
|
2023-04-28 05:22:40 -07:00
|
|
|
|
setChangePrice(price)
|
2023-04-25 13:21:32 -07:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
'processed'
|
|
|
|
|
)
|
|
|
|
|
return () => {
|
|
|
|
|
if (typeof subId !== 'undefined') {
|
|
|
|
|
connection.removeAccountChangeListener(subId)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-14 09:43:42 -07:00
|
|
|
|
}, [
|
|
|
|
|
connection,
|
|
|
|
|
selectedMarket,
|
|
|
|
|
serumOrPerpMarket,
|
|
|
|
|
setChangePrice,
|
|
|
|
|
quoteBank,
|
|
|
|
|
stalePrice,
|
|
|
|
|
])
|
2023-04-25 13:21:32 -07:00
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<div id="trade-step-two" className="flex-col whitespace-nowrap md:ml-6">
|
|
|
|
|
<Tooltip
|
2023-04-27 09:23:18 -07:00
|
|
|
|
placement="bottom"
|
2023-04-25 13:21:32 -07:00
|
|
|
|
content={
|
|
|
|
|
<>
|
2023-04-30 21:42:29 -07:00
|
|
|
|
<div className="flex">
|
|
|
|
|
<span className="mr-1">{t('trade:price-provided-by')}</span>
|
|
|
|
|
{oracleLinkPath ? (
|
|
|
|
|
<a
|
|
|
|
|
href={oracleLinkPath}
|
|
|
|
|
target="_blank"
|
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
|
className="flex items-center"
|
|
|
|
|
>
|
|
|
|
|
<span className="mr-1">{oracleProvider}</span>
|
|
|
|
|
<ArrowTopRightOnSquareIcon className="h-4 w-4" />
|
|
|
|
|
</a>
|
|
|
|
|
) : (
|
|
|
|
|
<span className="text-th-fgd-2">{oracleProvider}</span>
|
|
|
|
|
)}
|
2023-04-27 09:43:36 -07:00
|
|
|
|
</div>
|
2023-04-27 09:23:18 -07:00
|
|
|
|
<div className="mt-2">
|
2023-04-27 09:43:36 -07:00
|
|
|
|
{t('trade:last-updated')}{' '}
|
2023-04-27 09:23:18 -07:00
|
|
|
|
{dayjs
|
|
|
|
|
.duration({
|
|
|
|
|
seconds: -((highestSlot - oracleLastUpdatedSlot) * 0.5),
|
|
|
|
|
})
|
|
|
|
|
.humanize(true)}
|
|
|
|
|
.
|
|
|
|
|
</div>
|
|
|
|
|
{isStale ? (
|
|
|
|
|
<div className="mt-2 font-black">
|
2023-04-27 09:43:36 -07:00
|
|
|
|
{t('trade:oracle-not-updated')}
|
2023-04-27 09:23:18 -07:00
|
|
|
|
<br />
|
2023-04-27 09:43:36 -07:00
|
|
|
|
{t('trade:oracle-not-updated-warning')}
|
2023-04-27 09:23:18 -07:00
|
|
|
|
</div>
|
|
|
|
|
) : undefined}
|
2023-04-25 13:21:32 -07:00
|
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<div className="flex items-center">
|
2023-05-11 21:08:06 -07:00
|
|
|
|
<div className="tooltip-underline mb-0.5 text-xs text-th-fgd-4">
|
2023-04-25 13:21:32 -07:00
|
|
|
|
{t('trade:oracle-price')}
|
|
|
|
|
</div>
|
2023-04-27 09:23:18 -07:00
|
|
|
|
{isStale ? (
|
|
|
|
|
<ExclamationTriangleIcon className="ml-1 h-4 w-4 text-th-warning" />
|
2023-05-11 21:08:06 -07:00
|
|
|
|
) : null}
|
2023-04-25 13:21:32 -07:00
|
|
|
|
</div>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
<div className="font-mono text-xs text-th-fgd-2">
|
|
|
|
|
{price ? (
|
|
|
|
|
`${formatCurrencyValue(
|
|
|
|
|
price,
|
|
|
|
|
getDecimalCount(serumOrPerpMarket?.tickSize || 0.01)
|
|
|
|
|
)}`
|
|
|
|
|
) : (
|
|
|
|
|
<span className="text-th-fgd-4">–</span>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default OraclePrice
|