mango-ui-v3/stores/useMangoStore.tsx

374 lines
10 KiB
TypeScript
Raw Normal View History

2021-04-02 11:26:21 -07:00
import create, { State } from 'zustand'
import produce from 'immer'
import { Market } from '@project-serum/serum'
import {
2021-04-06 15:11:42 -07:00
IDS,
Config,
MarketKind,
MerpsClient as MangoClient,
MerpsGroup as MangoGroup,
MerpsAccount as MarginAccount,
MarketConfig,
getMarketByBaseSymbolAndKind,
GroupConfig,
2021-06-17 11:03:47 -07:00
TokenConfig,
getTokenAccountsByOwnerWithWrappedSol,
getTokenByMint,
TokenAccount,
nativeToUi,
2021-06-17 11:38:53 -07:00
MerpsCache,
2021-06-17 14:07:10 -07:00
PerpMarket,
2021-04-02 11:26:21 -07:00
} from '@blockworks-foundation/mango-client'
// import { SRM_DECIMALS } from '@project-serum/serum/lib/token-instructions'
2021-06-17 11:03:47 -07:00
import { AccountInfo, Connection, PublicKey, TokenAmount } from '@solana/web3.js'
2021-04-13 22:23:50 -07:00
import { EndpointInfo, WalletAdapter } from '../@types/types'
2021-05-20 11:51:00 -07:00
import { getWalletTokenInfo } from '../utils/tokens'
2021-06-17 11:03:47 -07:00
import { isDefined } from '../utils'
2021-04-20 07:09:25 -07:00
import { notify } from '../utils/notifications'
export const ENDPOINTS: EndpointInfo[] = [
{
name: 'mainnet-beta',
url: 'https://mango.rpcpool.com/',
websocket: 'https://mango.rpcpool.com/',
custom: false,
},
{
name: 'devnet',
2021-06-11 07:41:45 -07:00
url: 'https://api.devnet.solana.com',
2021-06-12 12:10:56 -07:00
websocket: 'https://api.devnet.solana.com',
custom: false,
},
]
2021-04-25 09:22:28 -07:00
type ClusterType = 'mainnet-beta' | 'devnet'
const CLUSTER = (process.env.NEXT_PUBLIC_CLUSTER as ClusterType) || 'devnet'
const ENDPOINT = ENDPOINTS.find((e) => e.name === CLUSTER)
const DEFAULT_CONNECTION = new Connection(ENDPOINT.url, 'recent')
const WEBSOCKET_CONNECTION = new Connection(ENDPOINT.websocket, 'recent')
2021-06-16 18:55:46 -07:00
const DEFAULT_MANGO_GROUP_NAME = 'merps_test_v1'
2021-06-17 11:38:53 -07:00
const DEFAULT_MANGO_GROUP_CONFIG = Config.ids().getGroup(
CLUSTER,
DEFAULT_MANGO_GROUP_NAME
)
console.log(DEFAULT_MANGO_GROUP_CONFIG)
const defaultMangoGroupIds = IDS['groups'].find(
(group) => group.name === DEFAULT_MANGO_GROUP_NAME
)
export const programId = new PublicKey(defaultMangoGroupIds.merps_program_id)
export const serumProgramId = new PublicKey(
defaultMangoGroupIds.serum_program_id
)
const merpsGroupPk = new PublicKey(defaultMangoGroupIds.key)
2021-04-13 16:41:04 -07:00
export const INITIAL_STATE = {
WALLET: {
2021-04-13 22:23:50 -07:00
providerUrl: null,
2021-04-13 16:41:04 -07:00
connected: false,
current: null,
2021-06-17 11:03:47 -07:00
tokens: [],
2021-04-13 16:41:04 -07:00
srmAccountsForOwner: [],
contributedSrm: 0,
},
}
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
2021-06-17 11:03:47 -07:00
export interface WalletToken {
account: TokenAccount,
config: TokenConfig,
uiBalance: number
}
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: {
2021-04-25 09:22:28 -07:00
cluster: ClusterType
current: Connection
websocket: Connection
endpoint: string
}
2021-04-29 07:38:28 -07:00
selectedMarket: {
2021-06-17 11:38:53 -07:00
config: MarketConfig
2021-04-29 07:38:28 -07:00
name: string
address: string
2021-06-17 14:07:10 -07:00
current: Market | PerpMarket | null
2021-04-06 15:11:42 -07:00
mangoProgramId: number | null
2021-04-02 11:26:21 -07:00
markPrice: number
2021-06-17 14:07:10 -07:00
askInfo: AccountInfo<Buffer> | null
bidInfo: AccountInfo<Buffer> | null
2021-04-02 11:26:21 -07:00
orderBook: any[]
}
mangoClient: MangoClient
mangoGroups: Array<MangoGroup>
selectedMangoGroup: {
2021-06-17 11:38:53 -07:00
config: GroupConfig
name: string
current: MangoGroup | null
2021-04-07 08:44:22 -07:00
markets: {
2021-04-29 08:08:36 -07:00
[address: string]: Market
2021-04-07 08:44:22 -07:00
}
2021-06-16 18:55:46 -07:00
rootBanks: any[]
2021-06-17 11:38:53 -07:00
cache: MerpsCache | null
}
2021-04-24 19:10:28 -07:00
marginAccounts: MarginAccount[]
selectedMarginAccount: {
current: MarginAccount | null
initialLoad: boolean
}
2021-04-02 11:26:21 -07:00
tradeForm: {
side: 'buy' | 'sell'
price: number | ''
baseSize: number | ''
quoteSize: number | ''
tradeType: 'Market' | 'Limit'
2021-04-02 11:26:21 -07:00
}
wallet: {
2021-04-13 22:23:50 -07:00
providerUrl: string
connected: boolean
2021-04-13 22:23:50 -07:00
current: WalletAdapter | undefined
2021-06-17 11:03:47 -07:00
tokens: WalletToken[],
2021-04-13 16:41:04 -07:00
srmAccountsForOwner: any[]
contributedSrm: number
}
settings: {
uiLocked: boolean
}
2021-04-14 15:46:36 -07:00
tradeHistory: any[]
2021-04-02 11:26:21 -07:00
set: (x: any) => void
2021-04-25 09:22:28 -07:00
actions: {
[key: string]: () => void
}
2021-04-02 11:26:21 -07:00
}
2021-04-12 20:39:08 -07:00
const useMangoStore = create<MangoStore>((set, get) => ({
notifications: [],
accountInfos: {},
connection: {
cluster: CLUSTER,
current: DEFAULT_CONNECTION,
websocket: WEBSOCKET_CONNECTION,
endpoint: ENDPOINT.url,
2021-04-12 20:39:08 -07:00
},
selectedMangoGroup: {
config: DEFAULT_MANGO_GROUP_CONFIG,
2021-04-12 20:39:08 -07:00
name: DEFAULT_MANGO_GROUP_NAME,
current: null,
markets: {},
2021-06-16 18:55:46 -07:00
rootBanks: [],
2021-06-17 11:38:53 -07:00
cache: null,
2021-04-12 20:39:08 -07:00
},
selectedMarket: {
2021-06-17 11:38:53 -07:00
config: getMarketByBaseSymbolAndKind(
DEFAULT_MANGO_GROUP_CONFIG,
'BTC',
'spot'
) as MarketConfig,
kind: 'spot',
name: 'BTC/USDC',
address: defaultMangoGroupIds.spot_markets[0].key,
2021-04-12 20:39:08 -07:00
current: null,
mangoProgramId: null,
markPrice: 0,
orderBook: [],
},
mangoClient: new MangoClient(DEFAULT_CONNECTION, programId),
2021-04-12 20:39:08 -07:00
mangoGroups: [],
marginAccounts: [],
selectedMarginAccount: {
current: null,
initialLoad: false,
2021-04-12 20:39:08 -07:00
},
tradeForm: {
side: 'buy',
baseSize: '',
quoteSize: '',
tradeType: 'Limit',
price: '',
},
2021-04-13 16:41:04 -07:00
wallet: INITIAL_STATE.WALLET,
settings: {
uiLocked: true,
},
2021-04-14 15:46:36 -07:00
tradeHistory: [],
2021-04-12 20:39:08 -07:00
set: (fn) => set(produce(fn)),
actions: {
2021-06-17 11:03:47 -07:00
async fetchWalletTokens() {
2021-04-12 20:39:08 -07:00
const connection = get().connection.current
2021-06-17 11:03:47 -07:00
const groupConfig = get().selectedMangoGroup.config
2021-04-12 20:39:08 -07:00
const wallet = get().wallet.current
const connected = get().wallet.connected
const set = get().set
2021-04-14 15:46:36 -07:00
if (wallet?.publicKey && connected) {
2021-04-12 20:39:08 -07:00
const ownerAddress = wallet.publicKey
2021-06-17 11:03:47 -07:00
const ownedTokenAccounts = await getTokenAccountsByOwnerWithWrappedSol(
2021-04-12 20:39:08 -07:00
connection,
ownerAddress
)
2021-06-17 11:03:47 -07:00
const tokens = []
ownedTokenAccounts.forEach(account => {
const config = getTokenByMint(groupConfig, account.mint);
if (config) {
const uiBalance = nativeToUi(
account.amount,
config.decimals
)
tokens.push({account, config, uiBalance})
}
});
2021-04-12 20:39:08 -07:00
set((state) => {
2021-06-17 11:03:47 -07:00
state.wallet.tokens = tokens;
2021-04-12 20:39:08 -07:00
})
} else {
set((state) => {
2021-06-17 11:03:47 -07:00
state.wallet.tokens = []
2021-04-12 20:39:08 -07:00
})
}
},
2021-06-17 11:03:47 -07:00
async fetchMarginAccounts() {
const mangoClient = get().mangoClient
const mangoGroup = get().selectedMangoGroup.current
const selectedMarginAcount = get().selectedMarginAccount.current
const wallet = get().wallet.current
const set = get().set
2021-04-12 20:39:08 -07:00
2021-06-17 11:03:47 -07:00
if (!wallet?.publicKey || !wallet.publicKey) return
2021-04-12 20:39:08 -07:00
2021-06-17 11:03:47 -07:00
if (!selectedMarginAcount) {
set((state) => {
state.selectedMarginAccount.initialLoad = true
})
}
2021-06-17 11:03:47 -07:00
return mangoClient
.getMarginAccountsForOwner(
mangoGroup,
wallet.publicKey
)
.then((marginAccounts) => {
if (marginAccounts.length > 0) {
const sortedAccounts = marginAccounts
.slice()
.sort(
(a, b) =>
2021-06-17 14:07:10 -07:00
a.publicKey.toBase58() > b.publicKey.toBase58() ? 1 : -1
2021-06-17 11:03:47 -07:00
)
set((state) => {
state.marginAccounts = sortedAccounts
if (state.selectedMarginAccount.current) {
state.selectedMarginAccount.current = marginAccounts.find(
(ma) =>
ma.publicKey.equals(
state.selectedMarginAccount.current.publicKey
)
)
} else {
const lastAccount = localStorage.getItem('lastAccountViewed')
2021-06-07 13:55:10 -07:00
2021-06-17 11:03:47 -07:00
state.selectedMarginAccount.current =
marginAccounts.find(
(ma) => ma.publicKey.toString() === JSON.parse(lastAccount)
) || sortedAccounts[0]
}
})
}
set((state) => {
state.selectedMarginAccount.initialLoad = false
})
})
.catch((err) => {
console.error('Could not get margin accounts for wallet', err)
})
},
2021-04-12 20:39:08 -07:00
async fetchMangoGroup() {
const mangoClient = get().mangoClient
const set = get().set
2021-04-12 20:39:08 -07:00
if (!mangoClient) return
const mangoGroupPk = merpsGroupPk
2021-04-12 20:39:08 -07:00
return mangoClient
.getMerpsGroup(mangoGroupPk)
2021-04-12 20:39:08 -07:00
.then(async (mangoGroup) => {
console.log('we have a mango group', mangoGroup)
2021-06-17 11:38:53 -07:00
const rootBanks = await mangoGroup.loadRootBanks(DEFAULT_CONNECTION)
const merpsCache = await mangoGroup.loadCache(DEFAULT_CONNECTION)
console.log('we have merps cache::', merpsCache)
// const srmAccountInfoPromise = connection.getAccountInfo(
// mangoGroup.srmVault
// )
// const pricesPromise = mangoGroup.getPrices(connection)
// const [srmAccountInfo, prices] = await Promise.all([
// srmAccountInfoPromise,
// pricesPromise,
// ])
2021-04-12 20:39:08 -07:00
// Set the mango group
2021-04-09 17:01:00 -07:00
set((state) => {
2021-04-12 20:39:08 -07:00
state.selectedMangoGroup.current = mangoGroup
2021-06-17 11:38:53 -07:00
state.selectedMangoGroup.rootBanks = rootBanks
state.selectedMangoGroup.cache = merpsCache
// state.selectedMangoGroup.srmAccount = srmAccountInfo
// state.selectedMangoGroup.mintDecimals = mangoGroup.mintDecimals // TODO store "tokens" from merps group ids
// state.selectedMangoGroup.prices = prices
2021-04-09 17:01:00 -07:00
})
2021-04-12 20:39:08 -07:00
})
.catch((err) => {
2021-04-20 07:09:25 -07:00
notify({
message: 'Could not get mango group: ',
description: `${err}`,
type: 'error',
})
console.log('Could not get mango group: ', err)
2021-04-12 20:39:08 -07:00
})
2021-04-09 17:01:00 -07:00
},
2021-06-17 11:03:47 -07:00
async fetchTradeHistory(marginAccount = null) {
const selectedMarginAccount =
marginAccount || get().selectedMarginAccount.current
const set = get().set
2021-04-14 15:46:36 -07:00
2021-06-17 11:03:47 -07:00
if (!selectedMarginAccount) return
if (selectedMarginAccount.openOrdersAccounts.length === 0) return
2021-04-14 15:46:36 -07:00
2021-06-17 11:03:47 -07:00
const openOrdersAccounts =
selectedMarginAccount.openOrdersAccounts.filter(isDefined)
const publicKeys = openOrdersAccounts.map((act) =>
act.publicKey.toString()
)
const results = await Promise.all(
publicKeys.map(async (pk) => {
const response = await fetch(
`https://stark-fjord-45757.herokuapp.com/trades/open_orders/${pk.toString()}`
)
2021-04-14 15:46:36 -07:00
2021-06-17 11:03:47 -07:00
const parsedResponse = await response.json()
return parsedResponse?.data ? parsedResponse.data : []
})
)
set((state) => {
state.tradeHistory = results
console.log('spot-history', results);
})
},
2021-04-12 20:39:08 -07:00
},
}))
2021-04-02 11:26:21 -07:00
export default useMangoStore