2021-07-17 06:49:52 -07:00
|
|
|
import { useEffect } from 'react'
|
|
|
|
import { useRouter } from 'next/router'
|
2021-11-14 10:37:02 -08:00
|
|
|
import useMangoGroupConfig from '../hooks/useMangoGroupConfig'
|
2022-01-16 15:16:47 -08:00
|
|
|
import useMangoStore, { serumProgramId } from '../stores/useMangoStore'
|
2021-08-18 07:23:55 -07:00
|
|
|
import {
|
|
|
|
getMarketByBaseSymbolAndKind,
|
|
|
|
getMarketIndexBySymbol,
|
|
|
|
} from '@blockworks-foundation/mango-client'
|
2021-11-14 10:37:02 -08:00
|
|
|
import TopBar from '../components/TopBar'
|
|
|
|
import TradePageGrid from '../components/TradePageGrid'
|
|
|
|
import useLocalStorageState from '../hooks/useLocalStorageState'
|
|
|
|
import AlphaModal, { ALPHA_MODAL_KEY } from '../components/AlphaModal'
|
|
|
|
import { PageBodyWrapper } from '../components/styles'
|
2021-10-20 05:42:40 -07:00
|
|
|
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
|
2021-11-23 15:08:14 -08:00
|
|
|
import IntroTips, { SHOW_TOUR_KEY } from '../components/IntroTips'
|
|
|
|
import { useViewport } from '../hooks/useViewport'
|
|
|
|
import { breakpoints } from '../components/TradePageGrid'
|
2021-12-07 19:40:20 -08:00
|
|
|
import {
|
2022-01-16 15:16:47 -08:00
|
|
|
actionsSelector,
|
|
|
|
mangoAccountSelector,
|
2021-12-07 19:40:20 -08:00
|
|
|
marketConfigSelector,
|
|
|
|
walletConnectedSelector,
|
|
|
|
} from '../stores/selectors'
|
2022-01-16 15:16:47 -08:00
|
|
|
import { PublicKey } from '@solana/web3.js'
|
2022-02-09 02:55:54 -08:00
|
|
|
import FavoritesShortcutBar from '../components/FavoritesShortcutBar'
|
2021-10-20 05:42:40 -07:00
|
|
|
|
2021-11-14 10:37:02 -08:00
|
|
|
export async function getStaticProps({ locale }) {
|
2021-10-20 05:42:40 -07:00
|
|
|
return {
|
|
|
|
props: {
|
2022-02-19 13:55:21 -08:00
|
|
|
...(await serverSideTranslations(locale, [
|
|
|
|
'common',
|
|
|
|
'tv-chart',
|
|
|
|
'alerts',
|
|
|
|
])),
|
2021-10-20 05:42:40 -07:00
|
|
|
// Will be passed to the page component as props
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2021-07-17 06:49:52 -07:00
|
|
|
|
|
|
|
const PerpMarket = () => {
|
2021-08-30 11:50:37 -07:00
|
|
|
const [alphaAccepted] = useLocalStorageState(ALPHA_MODAL_KEY, false)
|
2021-10-29 05:05:55 -07:00
|
|
|
const [showTour] = useLocalStorageState(SHOW_TOUR_KEY, false)
|
2021-07-17 06:49:52 -07:00
|
|
|
const groupConfig = useMangoGroupConfig()
|
|
|
|
const setMangoStore = useMangoStore((s) => s.set)
|
2021-12-07 19:40:20 -08:00
|
|
|
const connected = useMangoStore(walletConnectedSelector)
|
2022-01-16 15:16:47 -08:00
|
|
|
const mangoAccount = useMangoStore(mangoAccountSelector)
|
|
|
|
const mangoClient = useMangoStore((s) => s.connection.client)
|
|
|
|
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
|
2021-12-07 19:40:20 -08:00
|
|
|
const marketConfig = useMangoStore(marketConfigSelector)
|
2022-01-16 15:16:47 -08:00
|
|
|
const actions = useMangoStore(actionsSelector)
|
2021-07-17 06:49:52 -07:00
|
|
|
const router = useRouter()
|
2022-01-16 15:16:47 -08:00
|
|
|
const { pubkey } = router.query
|
2021-10-31 04:21:22 -07:00
|
|
|
const { width } = useViewport()
|
|
|
|
const hideTips = width ? width < breakpoints.md : false
|
2021-07-17 06:49:52 -07:00
|
|
|
|
2022-01-16 15:16:47 -08:00
|
|
|
useEffect(() => {
|
|
|
|
async function loadUnownedMangoAccount() {
|
|
|
|
try {
|
|
|
|
const unownedMangoAccountPubkey = new PublicKey(pubkey)
|
|
|
|
if (mangoGroup) {
|
|
|
|
const unOwnedMangoAccount = await mangoClient.getMangoAccount(
|
|
|
|
unownedMangoAccountPubkey,
|
|
|
|
serumProgramId
|
|
|
|
)
|
|
|
|
console.log('unOwnedMangoAccount: ', unOwnedMangoAccount)
|
|
|
|
|
|
|
|
setMangoStore((state) => {
|
|
|
|
state.selectedMangoAccount.current = unOwnedMangoAccount
|
|
|
|
state.selectedMangoAccount.initialLoad = false
|
|
|
|
state.wallet.connected = true
|
|
|
|
})
|
|
|
|
actions.fetchTradeHistory()
|
|
|
|
actions.reloadOrders()
|
|
|
|
// setResetOnLeave(true)
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
router.push('/account')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pubkey) {
|
|
|
|
loadUnownedMangoAccount()
|
|
|
|
}
|
|
|
|
}, [pubkey, mangoClient, mangoGroup])
|
|
|
|
|
2021-07-17 06:49:52 -07:00
|
|
|
useEffect(() => {
|
2021-11-14 10:37:02 -08:00
|
|
|
const name = decodeURIComponent(router.asPath).split('name=')[1]
|
2021-12-07 19:40:20 -08:00
|
|
|
const mangoGroup = useMangoStore.getState().selectedMangoGroup.current
|
2021-11-14 10:37:02 -08:00
|
|
|
|
2021-12-08 09:47:36 -08:00
|
|
|
let marketQueryParam, marketBaseSymbol, marketType, newMarket, marketIndex
|
|
|
|
if (name) {
|
|
|
|
marketQueryParam = name.toString().split(/-|\//)
|
|
|
|
marketBaseSymbol = marketQueryParam[0]
|
|
|
|
marketType = marketQueryParam[1] === 'PERP' ? 'perp' : 'spot'
|
2021-11-14 10:37:02 -08:00
|
|
|
|
2021-12-08 09:47:36 -08:00
|
|
|
newMarket = getMarketByBaseSymbolAndKind(
|
2021-07-17 06:49:52 -07:00
|
|
|
groupConfig,
|
2021-11-14 10:37:02 -08:00
|
|
|
marketBaseSymbol.toUpperCase(),
|
|
|
|
marketType
|
2021-07-17 06:49:52 -07:00
|
|
|
)
|
2021-12-08 09:47:36 -08:00
|
|
|
marketIndex = getMarketIndexBySymbol(
|
2021-08-18 07:23:55 -07:00
|
|
|
groupConfig,
|
2021-11-14 10:37:02 -08:00
|
|
|
marketBaseSymbol.toUpperCase()
|
2021-08-18 07:23:55 -07:00
|
|
|
)
|
2021-12-08 09:47:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (name && mangoGroup) {
|
2021-12-07 19:40:20 -08:00
|
|
|
const mangoCache = useMangoStore.getState().selectedMangoGroup.cache
|
2021-07-17 06:49:52 -07:00
|
|
|
setMangoStore((state) => {
|
2021-11-14 10:37:02 -08:00
|
|
|
state.selectedMarket.kind = marketType
|
2021-08-14 13:05:31 -07:00
|
|
|
if (newMarket.name !== marketConfig.name) {
|
2021-12-09 09:23:19 -08:00
|
|
|
// state.selectedMarket.current = null
|
2021-08-14 13:05:31 -07:00
|
|
|
state.selectedMarket.config = newMarket
|
2021-08-18 13:15:17 -07:00
|
|
|
state.tradeForm.price =
|
|
|
|
state.tradeForm.tradeType === 'Limit'
|
2022-02-10 09:49:59 -08:00
|
|
|
? parseFloat(
|
|
|
|
mangoGroup.getPrice(marketIndex, mangoCache).toFixed(2)
|
|
|
|
)
|
2021-08-18 13:15:17 -07:00
|
|
|
: ''
|
2021-08-14 13:05:31 -07:00
|
|
|
}
|
2021-07-17 06:49:52 -07:00
|
|
|
})
|
2021-12-08 09:47:36 -08:00
|
|
|
} else if (name && marketConfig) {
|
|
|
|
// if mangoGroup hasn't loaded yet, set the marketConfig to the query param if different
|
|
|
|
if (newMarket.name !== marketConfig.name) {
|
|
|
|
setMangoStore((state) => {
|
|
|
|
state.selectedMarket.kind = marketType
|
|
|
|
state.selectedMarket.config = newMarket
|
|
|
|
})
|
|
|
|
}
|
2021-07-17 06:49:52 -07:00
|
|
|
}
|
2021-12-07 19:40:20 -08:00
|
|
|
}, [router, marketConfig])
|
2021-07-17 06:49:52 -07:00
|
|
|
|
|
|
|
return (
|
2021-12-14 14:42:02 -08:00
|
|
|
<div className={`bg-th-bkg-1 text-th-fgd-1 transition-all`}>
|
2021-12-17 04:23:58 -08:00
|
|
|
{showTour && !hideTips ? (
|
|
|
|
<IntroTips connected={connected} mangoAccount={mangoAccount} />
|
|
|
|
) : null}
|
2021-07-17 06:49:52 -07:00
|
|
|
<TopBar />
|
2022-02-09 02:55:54 -08:00
|
|
|
<FavoritesShortcutBar />
|
|
|
|
<PageBodyWrapper className="p-1 sm:px-2 sm:py-1 md:px-2 md:py-1 xl:px-4">
|
2021-07-17 06:49:52 -07:00
|
|
|
<TradePageGrid />
|
2021-09-19 17:36:02 -07:00
|
|
|
</PageBodyWrapper>
|
2021-08-29 21:56:27 -07:00
|
|
|
{!alphaAccepted && (
|
|
|
|
<AlphaModal isOpen={!alphaAccepted} onClose={() => {}} />
|
|
|
|
)}
|
2021-07-17 06:49:52 -07:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default PerpMarket
|