lev-stake-sol/components/MangoProvider.tsx

159 lines
4.8 KiB
TypeScript
Raw Normal View History

2023-09-12 17:37:41 -07:00
import { useCallback, useEffect } from 'react'
import mangoStore from '@store/mangoStore'
2023-09-14 20:48:49 -07:00
import { Keypair } from '@solana/web3.js'
2023-09-12 17:37:41 -07:00
import useMangoAccount from 'hooks/useMangoAccount'
import useInterval from './shared/useInterval'
import { LAST_WALLET_NAME, PRIORITY_FEE_KEY, SECONDS } from 'utils/constants'
import useNetworkSpeed from 'hooks/useNetworkSpeed'
import { useWallet } from '@solana/wallet-adapter-react'
import useLocalStorageState from 'hooks/useLocalStorageState'
import { DEFAULT_PRIORITY_FEE_LEVEL } from './settings/RpcSettings'
const set = mangoStore.getState().set
const actions = mangoStore.getState().actions
const HydrateStore = () => {
const { mangoAccountPk, mangoAccountAddress } = useMangoAccount()
const connection = mangoStore((s) => s.connection)
const slowNetwork = useNetworkSpeed()
const { wallet } = useWallet()
const [, setLastWalletName] = useLocalStorageState(LAST_WALLET_NAME, '')
const handleWindowResize = useCallback(() => {
if (typeof window !== 'undefined') {
set((s) => {
s.window.width = window.innerWidth
s.window.height = window.innerHeight
})
}
}, [])
// store the window width and height on resize
useEffect(() => {
handleWindowResize()
window.addEventListener('resize', handleWindowResize)
return () => window.removeEventListener('resize', handleWindowResize)
}, [handleWindowResize])
useEffect(() => {
if (wallet?.adapter) {
setLastWalletName(wallet?.adapter.name)
}
}, [wallet, setLastWalletName])
useEffect(() => {
actions.fetchGroup()
2023-09-14 20:48:49 -07:00
}, [])
2023-09-12 17:37:41 -07:00
useInterval(
() => {
actions.fetchGroup()
2023-09-12 20:59:43 -07:00
actions.reloadMangoAccount()
2023-09-12 17:37:41 -07:00
},
2023-09-12 20:59:43 -07:00
(slowNetwork ? 60 : 20) * SECONDS,
2023-09-12 17:37:41 -07:00
)
// refetches open orders every 30 seconds
// only the selected market's open orders are updated via websocket
2023-09-12 20:59:43 -07:00
// useInterval(
// () => {
// if (mangoAccountAddress) {
// actions.fetchOpenOrders()
// }
// },
// (slowNetwork ? 60 : 30) * SECONDS,
// )
2023-09-12 17:37:41 -07:00
// refetch trade history and activity feed when switching accounts
useEffect(() => {
const actions = mangoStore.getState().actions
if (mangoAccountAddress) {
actions.fetchActivityFeed(mangoAccountAddress)
}
}, [mangoAccountAddress])
// reload and parse market fills from the event queue
2023-09-12 20:59:43 -07:00
// useInterval(
// async () => {
// const actions = mangoStore.getState().actions
// actions.loadMarketFills()
// },
// (slowNetwork ? 60 : 20) * SECONDS,
// )
2023-09-12 17:37:41 -07:00
// estimate the priority fee every 30 seconds
useInterval(
async () => {
if (mangoAccountAddress) {
const priorityFeeMultiplier = Number(
localStorage.getItem(PRIORITY_FEE_KEY) ??
DEFAULT_PRIORITY_FEE_LEVEL.value,
)
actions.estimatePriorityFee(priorityFeeMultiplier)
}
},
(slowNetwork ? 60 : 10) * SECONDS,
)
// The websocket library solana/web3.js uses closes its websocket connection when the subscription list
// is empty after opening its first time, preventing subsequent subscriptions from receiving responses.
// This is a hack to prevent the list from every getting empty
useEffect(() => {
const id = connection.onAccountChange(new Keypair().publicKey, () => {
return
})
return () => {
connection.removeAccountChangeListener(id)
}
}, [connection])
// watch selected Mango Account for changes
useEffect(() => {
const client = mangoStore.getState().client
if (!mangoAccountPk) return
const subscriptionId = connection.onAccountChange(
mangoAccountPk,
async (info, context) => {
if (info?.lamports === 0) return
const mangoAccount = mangoStore.getState().mangoAccount.current
if (!mangoAccount) return
const newMangoAccount = client.getMangoAccountFromAi(
mangoAccount.publicKey,
info,
)
// don't fetch serum3OpenOrders if the slot is old
if (context.slot > mangoStore.getState().mangoAccount.lastSlot) {
if (newMangoAccount.serum3Active().length > 0) {
2023-09-12 20:59:43 -07:00
// await newMangoAccount.reloadSerum3OpenOrders(client)
2023-09-12 17:37:41 -07:00
// check again that the slot is still the most recent after the reloading open orders
if (context.slot > mangoStore.getState().mangoAccount.lastSlot) {
set((s) => {
s.mangoAccount.current = newMangoAccount
s.mangoAccount.lastSlot = context.slot
})
}
}
2023-09-12 20:59:43 -07:00
// actions.fetchOpenOrders()
2023-09-12 17:37:41 -07:00
}
},
)
return () => {
connection.removeAccountChangeListener(subscriptionId)
}
}, [connection, mangoAccountPk])
return null
}
const MangoProvider = () => {
return (
<>
<HydrateStore />
</>
)
}
export default MangoProvider