mango-ui-v2/stores/useMangoStore.tsx

119 lines
2.6 KiB
TypeScript
Raw Normal View History

2021-04-02 11:26:21 -07:00
import create, { State } from 'zustand'
import { devtools } from 'zustand/middleware'
import produce from 'immer'
import { Market } from '@project-serum/serum'
import {
2021-04-06 15:11:42 -07:00
IDS,
2021-04-02 11:26:21 -07:00
MangoClient,
MangoGroup,
MarginAccount,
} from '@blockworks-foundation/mango-client'
import { AccountInfo, Connection } from '@solana/web3.js'
import { Wallet } from '@project-serum/sol-wallet-adapter'
import { EndpointInfo } from '../@types/types'
export const ENDPOINTS: EndpointInfo[] = [
{
name: 'mainnet-beta',
endpoint: 'https://solana-api.projectserum.com',
custom: false,
},
{
name: 'devnet',
endpoint: 'https://devnet.solana.com',
custom: false,
},
]
const CLUSTER = 'mainnet-beta'
const ENDPOINT_URL = ENDPOINTS.find((e) => e.name === CLUSTER).endpoint
const DEFAULT_CONNECTION = new Connection(ENDPOINT_URL, 'recent')
2021-04-06 15:11:42 -07:00
const DEFAULT_MANGO_GROUP = 'BTC_ETH_USDT'
interface AccountInfoList {
[key: string]: AccountInfo<Buffer>
}
2021-04-02 11:26:21 -07:00
interface MangoStore extends State {
accountInfos: AccountInfoList
connection: {
cluster: string
current: Connection
endpoint: string
}
2021-04-02 11:26:21 -07:00
market: {
current: Market | null
2021-04-06 15:11:42 -07:00
mangoProgramId: number | null
2021-04-02 11:26:21 -07:00
markPrice: number
orderBook: any[]
}
2021-04-06 15:11:42 -07:00
selectedMarket: {
name: string
address: string
}
2021-04-02 11:26:21 -07:00
mangoClient: MangoClient
mangoGroups: Array<MangoGroup>
selectedMangoGroup: {
name: string
current: MangoGroup | null
}
selectedMarginAccount: {
current: MarginAccount | null
}
2021-04-02 11:26:21 -07:00
tradeForm: {
currency: string
size: number
}
wallet: {
connected: boolean
current: Wallet
}
2021-04-02 11:26:21 -07:00
set: (x: any) => void
}
const useMangoStore = create<MangoStore>(
devtools((set) => ({
accountInfos: {},
connection: {
cluster: CLUSTER,
current: DEFAULT_CONNECTION,
endpoint: ENDPOINT_URL,
},
selectedMangoGroup: {
2021-04-06 15:11:42 -07:00
name: DEFAULT_MANGO_GROUP,
current: null,
},
2021-04-06 15:11:42 -07:00
selectedMarket: {
name: Object.entries(
IDS[CLUSTER].mango_groups[DEFAULT_MANGO_GROUP].spot_market_symbols
)[0][0],
address: Object.entries(
IDS[CLUSTER].mango_groups[DEFAULT_MANGO_GROUP].spot_market_symbols
)[0][1],
},
2021-04-02 11:26:21 -07:00
market: {
current: null,
2021-04-06 15:11:42 -07:00
mangoProgramId: null,
2021-04-02 11:26:21 -07:00
markPrice: 0,
orderBook: [],
},
mangoClient: new MangoClient(),
mangoGroups: [],
2021-04-02 11:26:21 -07:00
marginAccounts: [],
selectedMarginAccount: {
current: null,
},
2021-04-02 11:26:21 -07:00
tradeForm: {
size: 0,
currency: 'BTC',
},
wallet: {
connected: false,
current: null,
},
2021-04-02 11:26:21 -07:00
set: (fn) => set(produce(fn)),
}))
)
export default useMangoStore