mango-ui-v3/stores/useMangoStore.tsx

166 lines
4.0 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'
2021-04-09 17:01:00 -07:00
import { AccountInfo, Connection, PublicKey } from '@solana/web3.js'
import { Wallet } from '@project-serum/sol-wallet-adapter'
import { EndpointInfo } from '../@types/types'
2021-04-09 17:01:00 -07:00
import { getOwnedTokenAccounts } from '../utils/tokens'
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-09 17:01:00 -07:00
const DEFAULT_MANGO_GROUP_NAME = 'BTC_ETH_USDT'
2021-04-10 14:12:15 -07:00
// an object with keys of Solana account addresses that we are
// subscribing to with connection.onAccountChange() in the
// useHydrateStore hook
interface AccountInfoList {
[key: string]: AccountInfo<Buffer>
}
2021-04-02 11:26:21 -07:00
interface MangoStore extends State {
2021-04-11 21:17:23 -07:00
notifications: Array<{
type: string
message: string
description?: string
txid?: string
}>
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
srmAccount: AccountInfo<Buffer> | null
2021-04-07 08:44:22 -07:00
markets: {
[key: string]: Market
}
2021-04-09 17:01:00 -07:00
mintDecimals: number[]
}
selectedMarginAccount: {
current: MarginAccount | null
}
2021-04-02 11:26:21 -07:00
tradeForm: {
2021-04-09 07:27:49 -07:00
side: string
2021-04-02 11:26:21 -07:00
currency: string
size: number
}
wallet: {
connected: boolean
current: Wallet
2021-04-09 17:01:00 -07:00
balances: Array<{ account: any; publicKey: PublicKey }>
}
2021-04-02 11:26:21 -07:00
set: (x: any) => void
2021-04-09 17:01:00 -07:00
actions: any
2021-04-02 11:26:21 -07:00
}
const useMangoStore = create<MangoStore>(
2021-04-09 17:01:00 -07:00
devtools((set, get) => ({
2021-04-11 21:17:23 -07:00
notifications: [],
accountInfos: {},
connection: {
cluster: CLUSTER,
current: DEFAULT_CONNECTION,
endpoint: ENDPOINT_URL,
},
selectedMangoGroup: {
2021-04-09 17:01:00 -07:00
name: DEFAULT_MANGO_GROUP_NAME,
current: null,
2021-04-07 08:44:22 -07:00
markets: {},
srmAccount: null,
2021-04-09 17:01:00 -07:00
mintDecimals: [],
},
2021-04-06 15:11:42 -07:00
selectedMarket: {
name: Object.entries(
2021-04-09 17:01:00 -07:00
IDS[CLUSTER].mango_groups[DEFAULT_MANGO_GROUP_NAME].spot_market_symbols
2021-04-06 15:11:42 -07:00
)[0][0],
address: Object.entries(
2021-04-09 17:01:00 -07:00
IDS[CLUSTER].mango_groups[DEFAULT_MANGO_GROUP_NAME].spot_market_symbols
2021-04-06 15:11:42 -07:00
)[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: {
2021-04-09 07:27:49 -07:00
side: 'buy',
2021-04-02 11:26:21 -07:00
size: 0,
currency: 'BTC',
},
wallet: {
connected: false,
current: null,
2021-04-09 17:01:00 -07:00
balances: [],
},
2021-04-02 11:26:21 -07:00
set: (fn) => set(produce(fn)),
2021-04-09 17:01:00 -07:00
actions: {
async fetchWalletBalances() {
const connection = get().connection.current
const wallet = get().wallet.current
const connected = get().wallet.connected
const set = get().set
console.log('fetchingWalletBalances', connected, wallet)
if (wallet && connected) {
const ownerAddress = wallet.publicKey
const ownedTokenAccounts = await getOwnedTokenAccounts(
connection,
ownerAddress
)
set((state) => {
state.wallet.balances = ownedTokenAccounts
})
} else {
set((state) => {
state.wallet.balances = []
})
}
},
},
2021-04-02 11:26:21 -07:00
}))
)
export default useMangoStore