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'
|
2021-04-05 07:32:11 -07:00
|
|
|
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'
|
2021-04-05 07:32:11 -07:00
|
|
|
|
|
|
|
interface AccountInfoList {
|
|
|
|
[key: string]: AccountInfo<Buffer>
|
|
|
|
}
|
2021-04-02 11:26:21 -07:00
|
|
|
|
|
|
|
interface MangoStore extends State {
|
2021-04-05 07:32:11 -07:00
|
|
|
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
|
2021-04-05 07:32:11 -07:00
|
|
|
mangoGroups: Array<MangoGroup>
|
|
|
|
selectedMangoGroup: {
|
|
|
|
name: string
|
|
|
|
current: MangoGroup | null
|
2021-04-07 14:49:37 -07:00
|
|
|
srmAccount: AccountInfo<Buffer> | null
|
2021-04-07 08:44:22 -07:00
|
|
|
markets: {
|
|
|
|
[key: string]: Market
|
|
|
|
}
|
2021-04-05 07:32:11 -07:00
|
|
|
}
|
|
|
|
selectedMarginAccount: {
|
|
|
|
current: MarginAccount | null
|
|
|
|
}
|
2021-04-02 11:26:21 -07:00
|
|
|
tradeForm: {
|
|
|
|
currency: string
|
|
|
|
size: number
|
|
|
|
}
|
2021-04-05 07:32:11 -07:00
|
|
|
wallet: {
|
|
|
|
connected: boolean
|
|
|
|
current: Wallet
|
|
|
|
}
|
2021-04-02 11:26:21 -07:00
|
|
|
set: (x: any) => void
|
|
|
|
}
|
|
|
|
|
|
|
|
const useMangoStore = create<MangoStore>(
|
|
|
|
devtools((set) => ({
|
2021-04-05 07:32:11 -07:00
|
|
|
accountInfos: {},
|
|
|
|
connection: {
|
|
|
|
cluster: CLUSTER,
|
|
|
|
current: DEFAULT_CONNECTION,
|
|
|
|
endpoint: ENDPOINT_URL,
|
|
|
|
},
|
|
|
|
selectedMangoGroup: {
|
2021-04-06 15:11:42 -07:00
|
|
|
name: DEFAULT_MANGO_GROUP,
|
2021-04-05 07:32:11 -07:00
|
|
|
current: null,
|
2021-04-07 08:44:22 -07:00
|
|
|
markets: {},
|
2021-04-07 14:49:37 -07:00
|
|
|
srmAccount: null,
|
2021-04-05 07:32:11 -07:00
|
|
|
},
|
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(),
|
2021-04-05 07:32:11 -07:00
|
|
|
mangoGroups: [],
|
2021-04-02 11:26:21 -07:00
|
|
|
marginAccounts: [],
|
2021-04-05 07:32:11 -07:00
|
|
|
selectedMarginAccount: {
|
|
|
|
current: null,
|
|
|
|
},
|
2021-04-02 11:26:21 -07:00
|
|
|
tradeForm: {
|
|
|
|
size: 0,
|
|
|
|
currency: 'BTC',
|
|
|
|
},
|
2021-04-05 07:32:11 -07:00
|
|
|
wallet: {
|
|
|
|
connected: false,
|
|
|
|
current: null,
|
|
|
|
},
|
2021-04-02 11:26:21 -07:00
|
|
|
set: (fn) => set(produce(fn)),
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
|
|
|
|
export default useMangoStore
|