mango-v4-ui/components/MangoProvider.tsx

144 lines
3.9 KiB
TypeScript
Raw Normal View History

2022-11-21 19:56:56 -08:00
import { useCallback, useEffect } from 'react'
import mangoStore from '@store/mangoStore'
2023-01-05 14:06:18 -08:00
import { Keypair, PublicKey } from '@solana/web3.js'
import { useRouter } from 'next/router'
2022-10-27 13:58:54 -07:00
import { MangoAccount } from '@blockworks-foundation/mango-v4'
2022-11-18 09:09:39 -08:00
import useMangoAccount from 'hooks/useMangoAccount'
import useInterval from './shared/useInterval'
const set = mangoStore.getState().set
const actions = mangoStore.getState().actions
const HydrateStore = () => {
2022-11-21 19:56:56 -08:00
const router = useRouter()
const { name: marketName } = router.query
const { mangoAccountPk, mangoAccountAddress } = useMangoAccount()
2023-01-05 14:06:18 -08:00
const connection = mangoStore((s) => s.connection)
2022-11-21 19:56:56 -08:00
const fetchData = useCallback(async () => {
await actions.fetchGroup()
}, [])
useEffect(() => {
2022-11-21 19:56:56 -08:00
if (marketName && typeof marketName === 'string') {
set((s) => {
s.selectedMarket.name = marketName
})
2022-11-01 23:03:50 -07:00
}
fetchData()
2022-11-21 19:56:56 -08:00
}, [marketName])
useInterval(() => {
fetchData()
}, 15000)
2023-01-04 13:30:28 -08:00
useInterval(() => {
if (mangoAccountAddress) {
2023-01-04 13:30:28 -08:00
actions.fetchOpenOrders()
}
}, 30000)
2023-01-05 14:06:18 -08:00
// 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])
2022-10-27 13:58:54 -07:00
// watch selected Mango Account for changes
useEffect(() => {
const client = mangoStore.getState().client
if (!mangoAccountPk) return
2022-10-27 13:58:54 -07:00
const subscriptionId = connection.onAccountChange(
mangoAccountPk,
2022-10-28 12:24:05 -07:00
async (info, context) => {
2022-10-27 13:58:54 -07:00
if (info?.lamports === 0) return
const lastSeenSlot = mangoStore.getState().mangoAccount.lastSlot
2022-10-27 13:58:54 -07:00
const mangoAccount = mangoStore.getState().mangoAccount.current
if (!mangoAccount) return
if (context.slot > lastSeenSlot) {
const decodedMangoAccount = client.program.coder.accounts.decode(
'mangoAccount',
info?.data
)
const newMangoAccount = MangoAccount.from(
mangoAccount.publicKey,
decodedMangoAccount
)
2023-02-09 13:22:54 -08:00
await newMangoAccount.reloadSerum3OpenOrders(client)
actions.fetchOpenOrders(newMangoAccount)
set((s) => {
s.mangoAccount.current = newMangoAccount
s.mangoAccount.lastSlot = context.slot
})
}
2022-10-27 13:58:54 -07:00
}
)
return () => {
connection.removeAccountChangeListener(subscriptionId)
}
}, [connection, mangoAccountPk])
2022-10-27 13:58:54 -07:00
return null
}
const ReadOnlyMangoAccount = () => {
const router = useRouter()
const groupLoaded = mangoStore((s) => s.groupLoaded)
2022-12-11 21:01:48 -08:00
const ma = router.query?.address
useEffect(() => {
if (!groupLoaded) return
const set = mangoStore.getState().set
const group = mangoStore.getState().group
async function loadUnownedMangoAccount() {
try {
if (!ma || !group) return
const client = mangoStore.getState().client
const pk = new PublicKey(ma)
const readOnlyMangoAccount = await client.getMangoAccount(pk)
2023-02-09 13:22:54 -08:00
await readOnlyMangoAccount.reloadSerum3OpenOrders(client)
2023-01-03 18:32:42 -08:00
await actions.fetchOpenOrders(readOnlyMangoAccount)
2022-10-27 13:58:54 -07:00
set((state) => {
state.mangoAccount.current = readOnlyMangoAccount
state.mangoAccount.initialLoad = false
})
2023-01-06 16:26:06 -08:00
actions.fetchTradeHistory()
} catch (error) {
2022-11-01 11:13:02 -07:00
console.error('error', error)
}
}
if (ma) {
set((state) => {
state.mangoAccount.initialLoad = true
})
loadUnownedMangoAccount()
}
}, [ma, groupLoaded, router])
return null
}
const MangoProvider = () => {
return (
<>
<HydrateStore />
<ReadOnlyMangoAccount />
</>
)
}
export default MangoProvider