mango-ui-v3/hooks/useMarket.tsx

61 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-04-06 15:11:42 -07:00
import { useMemo } from 'react'
import useConnection from './useConnection'
import { PublicKey } from '@solana/web3.js'
import useMangoStore from '../stores/useMangoStore'
import { IDS } from '@blockworks-foundation/mango-client'
const formatTokenMints = (symbols: { [name: string]: string }) => {
return Object.entries(symbols).map(([name, address]) => {
return {
address: new PublicKey(address),
name: name,
}
})
}
const useMarket = () => {
2021-04-29 07:38:28 -07:00
const market = useMangoStore((state) => state.selectedMarket.current)
const selectedMarketName = useMangoStore((state) => state.selectedMarket.name)
2021-04-06 15:11:42 -07:00
const { cluster, programId } = useConnection()
const marketAddress = useMemo(
() => (market ? market.publicKey.toString() : null),
[market]
)
const TOKEN_MINTS = useMemo(() => formatTokenMints(IDS[cluster].symbols), [
cluster,
])
const baseCurrency = useMemo(
() =>
(market?.baseMintAddress &&
TOKEN_MINTS.find((token) =>
token.address.equals(market.baseMintAddress)
)?.name) ||
2021-04-15 14:36:55 -07:00
'...',
2021-04-06 15:11:42 -07:00
[market, TOKEN_MINTS]
)
const quoteCurrency = useMemo(
() =>
(market?.quoteMintAddress &&
TOKEN_MINTS.find((token) =>
token.address.equals(market.quoteMintAddress)
)?.name) ||
2021-04-15 14:36:55 -07:00
'...',
2021-04-06 15:11:42 -07:00
[market, TOKEN_MINTS]
)
return {
market,
marketAddress,
programId,
2021-04-29 07:38:28 -07:00
marketName: selectedMarketName,
2021-04-06 15:11:42 -07:00
baseCurrency,
quoteCurrency,
}
}
export default useMarket