mango-ui-v3/hooks/useConnection.tsx

55 lines
1.5 KiB
TypeScript
Raw Normal View History

import { useEffect, useMemo } from 'react'
import { Account, Connection } from '@solana/web3.js'
// import { IDS } from '@blockworks-foundation/mango-client'
import useMangoStore, {
programId,
serumProgramId,
} from '../stores/useMangoStore'
const useConnection = () => {
2021-04-14 23:16:36 -07:00
const setMangoStore = useMangoStore((s) => s.set)
2021-06-05 09:14:34 -07:00
const {
cluster,
current: connection,
endpoint,
2021-06-05 09:14:34 -07:00
} = useMangoStore((s) => s.connection)
const sendConnection = useMemo(
() => new Connection(endpoint, 'recent'),
[endpoint]
)
useEffect(() => {
2021-04-14 23:16:36 -07:00
if (connection && endpoint === connection['_rpcEndpoint']) return
2021-04-02 11:26:21 -07:00
const newConnection = new Connection(endpoint, 'recent')
2021-04-14 23:16:36 -07:00
setMangoStore((state) => {
2021-04-02 11:26:21 -07:00
state.connection.current = newConnection
})
}, [endpoint])
useEffect(() => {
2021-04-14 23:16:36 -07:00
if (connection && endpoint === connection['_rpcEndpoint']) return
const id = connection.onAccountChange(new Account().publicKey, () => {})
return () => {
connection.removeAccountChangeListener(id)
}
2021-04-07 08:44:22 -07:00
}, [endpoint])
useEffect(() => {
2021-04-14 23:16:36 -07:00
if (connection && endpoint === connection['_rpcEndpoint']) return
const id = connection.onSlotChange(() => null)
return () => {
connection.removeSlotChangeListener(id)
}
2021-04-07 08:44:22 -07:00
}, [endpoint])
// const programId = useMemo(() => IDS[cluster].mango_program_id, [cluster])
// const dexProgramId = useMemo(() => IDS[cluster]?.dex_program_id, [cluster])
const dexProgramId = serumProgramId
2021-04-02 11:26:21 -07:00
return { connection, dexProgramId, cluster, programId, sendConnection }
}
export default useConnection