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,
|
2021-06-16 22:37:35 -07:00
|
|
|
Config,
|
2021-06-23 08:32:33 -07:00
|
|
|
MangoClient,
|
|
|
|
MangoGroup,
|
|
|
|
MangoAccount,
|
2021-06-16 22:37:35 -07:00
|
|
|
MarketConfig,
|
|
|
|
getMarketByBaseSymbolAndKind,
|
|
|
|
GroupConfig,
|
2021-06-17 11:03:47 -07:00
|
|
|
TokenConfig,
|
|
|
|
getTokenAccountsByOwnerWithWrappedSol,
|
|
|
|
getTokenByMint,
|
|
|
|
TokenAccount,
|
|
|
|
nativeToUi,
|
2021-06-23 08:32:33 -07:00
|
|
|
MangoCache,
|
2021-06-17 14:07:10 -07:00
|
|
|
PerpMarket,
|
2021-06-18 20:07:57 -07:00
|
|
|
getAllMarkets,
|
2021-06-18 13:46:20 -07:00
|
|
|
getMultipleAccounts,
|
2021-06-18 20:07:57 -07:00
|
|
|
PerpMarketLayout,
|
2021-04-02 11:26:21 -07:00
|
|
|
} from '@blockworks-foundation/mango-client'
|
2021-06-20 11:08:14 -07:00
|
|
|
import { AccountInfo, Commitment, Connection, PublicKey } from '@solana/web3.js'
|
2021-04-13 22:23:50 -07:00
|
|
|
import { EndpointInfo, WalletAdapter } from '../@types/types'
|
2021-08-03 10:59:16 -07:00
|
|
|
import { isDefined, zipDict } from '../utils'
|
2021-04-20 07:09:25 -07:00
|
|
|
import { notify } from '../utils/notifications'
|
2021-07-06 15:04:20 -07:00
|
|
|
import { LAST_ACCOUNT_KEY } from '../components/AccountsModal'
|
2021-09-14 09:25:23 -07:00
|
|
|
import { NODE_URL_KEY } from '../components/SettingsModal'
|
2021-04-05 07:32:11 -07:00
|
|
|
|
|
|
|
export const ENDPOINTS: EndpointInfo[] = [
|
|
|
|
{
|
2021-08-19 10:49:44 -07:00
|
|
|
name: 'mainnet',
|
2021-09-14 09:25:23 -07:00
|
|
|
url: 'https://mango.rpcpool.com',
|
|
|
|
websocket: 'https://mango.rpcpool.com',
|
2021-04-05 07:32:11 -07:00
|
|
|
custom: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'devnet',
|
2021-08-17 13:36:31 -07:00
|
|
|
// url: "https://mango.devnet.rpcpool.com",
|
|
|
|
// websocket: "https://mango.devnet.rpcpool.com",
|
2021-08-17 10:35:30 -07:00
|
|
|
url: 'https://api.devnet.solana.com',
|
2021-06-12 12:10:56 -07:00
|
|
|
websocket: 'https://api.devnet.solana.com',
|
2021-04-05 07:32:11 -07:00
|
|
|
custom: false,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
2021-08-19 10:49:44 -07:00
|
|
|
type ClusterType = 'mainnet' | 'devnet'
|
2021-04-25 09:22:28 -07:00
|
|
|
|
2021-08-20 03:10:47 -07:00
|
|
|
const CLUSTER = (process.env.NEXT_PUBLIC_CLUSTER as ClusterType) || 'mainnet'
|
2021-04-20 14:00:45 -07:00
|
|
|
const ENDPOINT = ENDPOINTS.find((e) => e.name === CLUSTER)
|
2021-09-14 09:25:23 -07:00
|
|
|
|
2021-06-18 15:38:09 -07:00
|
|
|
export const WEBSOCKET_CONNECTION = new Connection(
|
2021-06-18 13:46:20 -07:00
|
|
|
ENDPOINT.websocket,
|
|
|
|
'processed' as Commitment
|
|
|
|
)
|
2021-06-16 18:55:46 -07:00
|
|
|
|
2021-08-25 10:12:43 -07:00
|
|
|
const DEFAULT_MANGO_GROUP_NAME = process.env.NEXT_PUBLIC_GROUP || 'mainnet.1'
|
2021-06-17 11:38:53 -07:00
|
|
|
const DEFAULT_MANGO_GROUP_CONFIG = Config.ids().getGroup(
|
|
|
|
CLUSTER,
|
|
|
|
DEFAULT_MANGO_GROUP_NAME
|
|
|
|
)
|
2021-06-16 18:50:16 -07:00
|
|
|
const defaultMangoGroupIds = IDS['groups'].find(
|
|
|
|
(group) => group.name === DEFAULT_MANGO_GROUP_NAME
|
|
|
|
)
|
2021-08-04 08:42:56 -07:00
|
|
|
export const MNGO_INDEX = defaultMangoGroupIds.oracles.findIndex(
|
|
|
|
(t) => t.symbol === 'MNGO'
|
|
|
|
)
|
2021-06-16 18:50:16 -07:00
|
|
|
|
2021-06-23 08:32:33 -07:00
|
|
|
export const programId = new PublicKey(defaultMangoGroupIds.mangoProgramId)
|
2021-06-18 16:56:53 -07:00
|
|
|
export const serumProgramId = new PublicKey(defaultMangoGroupIds.serumProgramId)
|
2021-06-23 08:32:33 -07:00
|
|
|
const mangoGroupPk = new PublicKey(defaultMangoGroupIds.publicKey)
|
2021-04-05 07:32:11 -07:00
|
|
|
|
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
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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
|
2021-04-05 07:32:11 -07:00
|
|
|
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 {
|
2021-06-17 15:38:17 -07:00
|
|
|
account: TokenAccount
|
|
|
|
config: TokenConfig
|
2021-06-17 11:03:47 -07:00
|
|
|
uiBalance: number
|
|
|
|
}
|
|
|
|
|
2021-06-17 15:37:03 -07:00
|
|
|
export interface Orderbook {
|
2021-06-17 18:10:47 -07:00
|
|
|
bids: number[][]
|
2021-06-17 15:37:03 -07:00
|
|
|
asks: 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
|
2021-07-06 15:04:20 -07:00
|
|
|
title: string
|
2021-04-11 21:17:23 -07:00
|
|
|
description?: string
|
|
|
|
txid?: string
|
|
|
|
}>
|
2021-04-05 07:32:11 -07:00
|
|
|
accountInfos: AccountInfoList
|
|
|
|
connection: {
|
2021-04-25 09:22:28 -07:00
|
|
|
cluster: ClusterType
|
2021-04-05 07:32:11 -07:00
|
|
|
current: Connection
|
2021-04-20 14:00:45 -07:00
|
|
|
websocket: Connection
|
2021-04-05 07:32:11 -07:00
|
|
|
endpoint: string
|
2021-09-13 14:14:59 -07:00
|
|
|
client: MangoClient
|
2021-08-23 10:12:48 -07:00
|
|
|
slot: number
|
2021-04-05 07:32:11 -07:00
|
|
|
}
|
2021-04-29 07:38:28 -07:00
|
|
|
selectedMarket: {
|
2021-06-17 11:38:53 -07:00
|
|
|
config: MarketConfig
|
2021-06-17 14:07:10 -07:00
|
|
|
current: Market | PerpMarket | null
|
2021-04-02 11:26:21 -07:00
|
|
|
markPrice: number
|
2021-06-18 15:38:09 -07:00
|
|
|
kind: string
|
2021-06-17 14:07:10 -07:00
|
|
|
askInfo: AccountInfo<Buffer> | null
|
|
|
|
bidInfo: AccountInfo<Buffer> | null
|
2021-06-17 18:10:47 -07:00
|
|
|
orderBook: Orderbook
|
2021-06-20 11:08:14 -07:00
|
|
|
fills: any[]
|
2021-04-02 11:26:21 -07:00
|
|
|
}
|
2021-04-05 07:32:11 -07:00
|
|
|
mangoGroups: Array<MangoGroup>
|
|
|
|
selectedMangoGroup: {
|
2021-06-17 11:38:53 -07:00
|
|
|
config: GroupConfig
|
2021-04-05 07:32:11 -07:00
|
|
|
name: string
|
|
|
|
current: MangoGroup | null
|
2021-04-07 08:44:22 -07:00
|
|
|
markets: {
|
2021-06-18 20:07:57 -07:00
|
|
|
[address: string]: Market | PerpMarket
|
2021-04-07 08:44:22 -07:00
|
|
|
}
|
2021-06-23 08:32:33 -07:00
|
|
|
cache: MangoCache | null
|
2021-04-05 07:32:11 -07:00
|
|
|
}
|
2021-06-23 08:32:33 -07:00
|
|
|
mangoAccounts: MangoAccount[]
|
|
|
|
selectedMangoAccount: {
|
|
|
|
current: MangoAccount | null
|
2021-05-04 16:19:48 -07:00
|
|
|
initialLoad: boolean
|
2021-04-05 07:32:11 -07:00
|
|
|
}
|
2021-04-02 11:26:21 -07:00
|
|
|
tradeForm: {
|
2021-04-12 13:01:55 -07:00
|
|
|
side: 'buy' | 'sell'
|
|
|
|
price: number | ''
|
|
|
|
baseSize: number | ''
|
|
|
|
quoteSize: number | ''
|
|
|
|
tradeType: 'Market' | 'Limit'
|
2021-04-02 11:26:21 -07:00
|
|
|
}
|
2021-04-05 07:32:11 -07:00
|
|
|
wallet: {
|
2021-04-13 22:23:50 -07:00
|
|
|
providerUrl: string
|
2021-04-05 07:32:11 -07:00
|
|
|
connected: boolean
|
2021-04-13 22:23:50 -07:00
|
|
|
current: WalletAdapter | undefined
|
2021-06-17 15:38:17 -07:00
|
|
|
tokens: WalletToken[]
|
2021-04-05 07:32:11 -07:00
|
|
|
}
|
2021-04-13 09:51:42 -07:00
|
|
|
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: {
|
2021-07-19 17:42:55 -07:00
|
|
|
[key: string]: (args?) => void
|
2021-04-25 09:22:28 -07:00
|
|
|
}
|
2021-04-02 11:26:21 -07:00
|
|
|
}
|
|
|
|
|
2021-09-14 09:25:23 -07:00
|
|
|
const useMangoStore = create<MangoStore>((set, get) => {
|
|
|
|
const rpcUrl =
|
|
|
|
typeof window !== 'undefined'
|
|
|
|
? JSON.parse(localStorage.getItem(NODE_URL_KEY)) || ENDPOINT.url
|
|
|
|
: ENDPOINT.url
|
|
|
|
console.log('rpc url', rpcUrl, ENDPOINT.url, rpcUrl === ENDPOINT.url)
|
|
|
|
|
|
|
|
const connection = new Connection(rpcUrl, 'processed' as Commitment)
|
|
|
|
return {
|
|
|
|
notifications: [],
|
|
|
|
accountInfos: {},
|
|
|
|
connection: {
|
|
|
|
cluster: CLUSTER,
|
|
|
|
current: connection,
|
|
|
|
websocket: WEBSOCKET_CONNECTION,
|
|
|
|
client: new MangoClient(connection, programId),
|
|
|
|
endpoint: ENDPOINT.url,
|
|
|
|
slot: 0,
|
2021-04-05 07:32:11 -07:00
|
|
|
},
|
2021-09-14 09:25:23 -07:00
|
|
|
selectedMangoGroup: {
|
|
|
|
config: DEFAULT_MANGO_GROUP_CONFIG,
|
|
|
|
name: DEFAULT_MANGO_GROUP_NAME,
|
|
|
|
current: null,
|
|
|
|
markets: {},
|
|
|
|
rootBanks: [],
|
|
|
|
cache: null,
|
|
|
|
},
|
|
|
|
selectedMarket: {
|
|
|
|
config: getMarketByBaseSymbolAndKind(
|
|
|
|
DEFAULT_MANGO_GROUP_CONFIG,
|
|
|
|
'BTC',
|
|
|
|
'perp'
|
|
|
|
) as MarketConfig,
|
|
|
|
kind: 'perp',
|
|
|
|
current: null,
|
|
|
|
markPrice: 0,
|
|
|
|
askInfo: null,
|
|
|
|
bidInfo: null,
|
|
|
|
orderBook: { bids: [], asks: [] },
|
|
|
|
fills: [],
|
|
|
|
},
|
|
|
|
mangoGroups: [],
|
|
|
|
mangoAccounts: [],
|
|
|
|
selectedMangoAccount: {
|
|
|
|
current: null,
|
|
|
|
initialLoad: true,
|
|
|
|
},
|
|
|
|
tradeForm: {
|
|
|
|
side: 'buy',
|
|
|
|
baseSize: '',
|
|
|
|
quoteSize: '',
|
|
|
|
tradeType: 'Limit',
|
|
|
|
price: '',
|
|
|
|
},
|
|
|
|
wallet: INITIAL_STATE.WALLET,
|
|
|
|
settings: {
|
|
|
|
uiLocked: true,
|
|
|
|
},
|
|
|
|
tradeHistory: [],
|
|
|
|
set: (fn) => set(produce(fn)),
|
|
|
|
actions: {
|
|
|
|
async fetchWalletTokens() {
|
|
|
|
const groupConfig = get().selectedMangoGroup.config
|
|
|
|
const wallet = get().wallet.current
|
|
|
|
const connected = get().wallet.connected
|
|
|
|
const connection = get().connection.current
|
|
|
|
const set = get().set
|
|
|
|
|
|
|
|
if (wallet?.publicKey && connected) {
|
|
|
|
const ownedTokenAccounts =
|
|
|
|
await getTokenAccountsByOwnerWithWrappedSol(
|
|
|
|
connection,
|
|
|
|
wallet.publicKey
|
|
|
|
)
|
|
|
|
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-06-18 16:56:53 -07:00
|
|
|
|
2021-09-14 09:25:23 -07:00
|
|
|
set((state) => {
|
|
|
|
state.wallet.tokens = tokens
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
set((state) => {
|
|
|
|
state.wallet.tokens = []
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async fetchMangoAccounts() {
|
|
|
|
const set = get().set
|
|
|
|
const mangoGroup = get().selectedMangoGroup.current
|
|
|
|
const mangoClient = get().connection.client
|
|
|
|
const wallet = get().wallet.current
|
|
|
|
const walletPk = wallet?.publicKey
|
|
|
|
|
|
|
|
if (!walletPk) return
|
|
|
|
return mangoClient
|
|
|
|
.getMangoAccountsForOwner(mangoGroup, walletPk, true)
|
|
|
|
.then((mangoAccounts) => {
|
|
|
|
if (mangoAccounts.length > 0) {
|
|
|
|
const sortedAccounts = mangoAccounts
|
|
|
|
.slice()
|
|
|
|
.sort((a, b) =>
|
|
|
|
a.publicKey.toBase58() > b.publicKey.toBase58() ? 1 : -1
|
|
|
|
)
|
|
|
|
|
|
|
|
set((state) => {
|
|
|
|
state.selectedMangoAccount.initialLoad = false
|
|
|
|
state.mangoAccounts = sortedAccounts
|
|
|
|
if (state.selectedMangoAccount.current) {
|
|
|
|
state.selectedMangoAccount.current = mangoAccounts.find(
|
|
|
|
(ma) =>
|
|
|
|
ma.publicKey.equals(
|
|
|
|
state.selectedMangoAccount.current.publicKey
|
|
|
|
)
|
2021-07-06 15:04:20 -07:00
|
|
|
)
|
2021-09-14 09:25:23 -07:00
|
|
|
} else {
|
|
|
|
const lastAccount = localStorage.getItem(LAST_ACCOUNT_KEY)
|
|
|
|
state.selectedMangoAccount.current =
|
|
|
|
mangoAccounts.find(
|
|
|
|
(ma) =>
|
|
|
|
ma.publicKey.toString() === JSON.parse(lastAccount)
|
|
|
|
) || sortedAccounts[0]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
set((state) => {
|
|
|
|
state.selectedMangoAccount.initialLoad = false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
notify({
|
|
|
|
type: 'error',
|
|
|
|
title: 'Unable to load mango account',
|
|
|
|
description: err.message,
|
|
|
|
})
|
|
|
|
console.log('Could not get margin accounts for wallet', err)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
async fetchMangoGroup() {
|
|
|
|
const set = get().set
|
|
|
|
const mangoGroupConfig = get().selectedMangoGroup.config
|
|
|
|
const selectedMarketConfig = get().selectedMarket.config
|
|
|
|
const mangoClient = get().connection.client
|
|
|
|
const connection = get().connection.current
|
|
|
|
|
|
|
|
return mangoClient
|
|
|
|
.getMangoGroup(mangoGroupPk)
|
|
|
|
.then(async (mangoGroup) => {
|
|
|
|
const allMarketConfigs = getAllMarkets(mangoGroupConfig)
|
|
|
|
const allMarketPks = allMarketConfigs.map((m) => m.publicKey)
|
|
|
|
|
|
|
|
let allMarketAccountInfos, mangoCache
|
|
|
|
try {
|
|
|
|
const resp = await Promise.all([
|
|
|
|
getMultipleAccounts(connection, allMarketPks),
|
|
|
|
mangoGroup.loadCache(connection),
|
|
|
|
mangoGroup.loadRootBanks(connection),
|
|
|
|
])
|
|
|
|
allMarketAccountInfos = resp[0]
|
|
|
|
mangoCache = resp[1]
|
|
|
|
} catch {
|
|
|
|
notify({
|
|
|
|
type: 'error',
|
|
|
|
title: 'Failed to load the mango group. Please refresh.',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const allMarketAccounts = allMarketConfigs.map((config, i) => {
|
|
|
|
if (config.kind == 'spot') {
|
|
|
|
const decoded = Market.getLayout(programId).decode(
|
|
|
|
allMarketAccountInfos[i].accountInfo.data
|
|
|
|
)
|
|
|
|
return new Market(
|
|
|
|
decoded,
|
|
|
|
config.baseDecimals,
|
|
|
|
config.quoteDecimals,
|
|
|
|
undefined,
|
|
|
|
mangoGroupConfig.serumProgramId
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if (config.kind == 'perp') {
|
|
|
|
const decoded = PerpMarketLayout.decode(
|
|
|
|
allMarketAccountInfos[i].accountInfo.data
|
|
|
|
)
|
|
|
|
return new PerpMarket(
|
|
|
|
config.publicKey,
|
|
|
|
config.baseDecimals,
|
|
|
|
config.quoteDecimals,
|
|
|
|
decoded
|
2021-07-06 15:04:20 -07:00
|
|
|
)
|
|
|
|
}
|
2021-06-17 11:03:47 -07:00
|
|
|
})
|
2021-09-14 09:25:23 -07:00
|
|
|
|
|
|
|
const allBidsAndAsksPks = allMarketConfigs
|
|
|
|
.map((m) => [m.bidsKey, m.asksKey])
|
|
|
|
.flat()
|
|
|
|
const allBidsAndAsksAccountInfos = await getMultipleAccounts(
|
|
|
|
connection,
|
|
|
|
allBidsAndAsksPks
|
|
|
|
)
|
|
|
|
|
|
|
|
const allMarkets = zipDict(
|
|
|
|
allMarketPks.map((pk) => pk.toBase58()),
|
|
|
|
allMarketAccounts
|
|
|
|
)
|
|
|
|
|
2021-08-27 17:15:25 -07:00
|
|
|
set((state) => {
|
2021-09-14 09:25:23 -07:00
|
|
|
state.selectedMangoGroup.current = mangoGroup
|
|
|
|
state.selectedMangoGroup.cache = mangoCache
|
|
|
|
state.selectedMangoGroup.markets = allMarkets
|
|
|
|
state.selectedMarket.current =
|
|
|
|
allMarkets[selectedMarketConfig.publicKey.toBase58()]
|
|
|
|
|
|
|
|
allMarketAccountInfos
|
|
|
|
.concat(allBidsAndAsksAccountInfos)
|
|
|
|
.forEach(({ publicKey, context, accountInfo }) => {
|
|
|
|
if (context.slot >= state.connection.slot) {
|
|
|
|
state.connection.slot = context.slot
|
|
|
|
state.accountInfos[publicKey.toBase58()] = accountInfo
|
|
|
|
}
|
|
|
|
})
|
2021-08-27 17:15:25 -07:00
|
|
|
})
|
2021-08-25 08:50:39 -07:00
|
|
|
})
|
2021-09-14 09:25:23 -07:00
|
|
|
.catch((err) => {
|
2021-08-25 07:22:59 -07:00
|
|
|
notify({
|
2021-09-14 09:25:23 -07:00
|
|
|
title: 'Could not get mango group',
|
|
|
|
description: `${err}`,
|
2021-08-25 07:22:59 -07:00
|
|
|
type: 'error',
|
|
|
|
})
|
2021-09-14 09:25:23 -07:00
|
|
|
console.log('Could not get mango group: ', err)
|
2021-06-18 20:07:57 -07:00
|
|
|
})
|
2021-09-14 09:25:23 -07:00
|
|
|
},
|
|
|
|
async fetchTradeHistory(mangoAccount = null) {
|
|
|
|
const selectedMangoAccount =
|
|
|
|
mangoAccount || get().selectedMangoAccount.current
|
|
|
|
const set = get().set
|
|
|
|
if (!selectedMangoAccount) return
|
|
|
|
|
|
|
|
if (selectedMangoAccount.spotOpenOrdersAccounts.length === 0) return
|
|
|
|
const openOrdersAccounts =
|
|
|
|
selectedMangoAccount.spotOpenOrdersAccounts.filter(isDefined)
|
|
|
|
const publicKeys = openOrdersAccounts.map((act) =>
|
|
|
|
act.publicKey.toString()
|
|
|
|
)
|
|
|
|
const perpHistory = await fetch(
|
|
|
|
`https://event-history-api.herokuapp.com/perp_trades/${selectedMangoAccount.publicKey.toString()}`
|
|
|
|
)
|
|
|
|
let parsedPerpHistory = await perpHistory.json()
|
|
|
|
parsedPerpHistory = parsedPerpHistory?.data || []
|
|
|
|
|
|
|
|
const serumHistory = await Promise.all(
|
|
|
|
publicKeys.map(async (pk) => {
|
|
|
|
const response = await fetch(
|
|
|
|
`https://event-history-api.herokuapp.com/trades/open_orders/${pk.toString()}`
|
|
|
|
)
|
|
|
|
const parsedResponse = await response.json()
|
|
|
|
return parsedResponse?.data ? parsedResponse.data : []
|
2021-04-20 07:09:25 -07:00
|
|
|
})
|
2021-08-23 10:12:48 -07:00
|
|
|
)
|
2021-09-04 14:59:53 -07:00
|
|
|
set((state) => {
|
2021-09-14 09:25:23 -07:00
|
|
|
state.tradeHistory = [...serumHistory, ...parsedPerpHistory]
|
2021-09-04 14:59:53 -07:00
|
|
|
})
|
2021-09-14 09:25:23 -07:00
|
|
|
},
|
|
|
|
async reloadMangoAccount() {
|
|
|
|
const set = get().set
|
|
|
|
const mangoAccount = get().selectedMangoAccount.current
|
|
|
|
const connection = get().connection.current
|
|
|
|
const [reloadedMangoAccount, reloadedOpenOrders] = await Promise.all([
|
|
|
|
mangoAccount.reload(connection),
|
|
|
|
mangoAccount.loadOpenOrders(
|
|
|
|
connection,
|
|
|
|
new PublicKey(serumProgramId)
|
|
|
|
),
|
|
|
|
])
|
|
|
|
reloadedMangoAccount.spotOpenOrdersAccounts = reloadedOpenOrders
|
2021-09-07 16:27:14 -07:00
|
|
|
|
|
|
|
set((state) => {
|
2021-09-14 09:25:23 -07:00
|
|
|
state.selectedMangoAccount.current = reloadedMangoAccount
|
2021-09-07 16:27:14 -07:00
|
|
|
})
|
2021-09-14 09:25:23 -07:00
|
|
|
},
|
|
|
|
async updateOpenOrders() {
|
|
|
|
const set = get().set
|
|
|
|
const connection = get().connection.current
|
|
|
|
const bidAskAccounts = Object.keys(get().accountInfos).map(
|
|
|
|
(pk) => new PublicKey(pk)
|
|
|
|
)
|
2021-09-13 14:14:59 -07:00
|
|
|
|
2021-09-14 09:25:23 -07:00
|
|
|
const allBidsAndAsksAccountInfos = await getMultipleAccounts(
|
|
|
|
connection,
|
|
|
|
bidAskAccounts
|
|
|
|
)
|
2021-09-13 14:14:59 -07:00
|
|
|
|
2021-09-14 09:25:23 -07:00
|
|
|
set((state) => {
|
|
|
|
allBidsAndAsksAccountInfos.forEach(
|
|
|
|
({ publicKey, context, accountInfo }) => {
|
|
|
|
state.connection.slot = context.slot
|
|
|
|
state.accountInfos[publicKey.toBase58()] = accountInfo
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
async loadMarketFills() {
|
|
|
|
const set = get().set
|
|
|
|
const selectedMarket = get().selectedMarket.current
|
|
|
|
const connection = get().connection.current
|
|
|
|
if (!selectedMarket) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const loadedFills = await selectedMarket.loadFills(connection, 10000)
|
|
|
|
set((state) => {
|
|
|
|
state.selectedMarket.fills = loadedFills
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
console.log('Error fetching fills:', err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async fetchMangoGroupCache() {
|
|
|
|
const set = get().set
|
|
|
|
const mangoGroup = get().selectedMangoGroup.current
|
|
|
|
const connection = get().connection.current
|
|
|
|
if (mangoGroup) {
|
|
|
|
const mangoCache = await mangoGroup.loadCache(connection)
|
|
|
|
|
|
|
|
set((state) => {
|
|
|
|
state.selectedMangoGroup.cache = mangoCache
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async updateConnection(endpointUrl) {
|
|
|
|
const set = get().set
|
2021-09-13 14:14:59 -07:00
|
|
|
|
2021-09-14 09:25:23 -07:00
|
|
|
const newConnection = new Connection(endpointUrl, 'processed')
|
|
|
|
|
|
|
|
const newClient = new MangoClient(newConnection, programId)
|
|
|
|
|
|
|
|
set((state) => {
|
|
|
|
state.connection.endpoint = endpointUrl
|
|
|
|
state.connection.current = newConnection
|
|
|
|
state.connection.client = newClient
|
|
|
|
})
|
|
|
|
},
|
2021-09-13 14:14:59 -07:00
|
|
|
},
|
2021-09-14 09:25:23 -07:00
|
|
|
}
|
|
|
|
})
|
2021-04-02 11:26:21 -07:00
|
|
|
|
|
|
|
export default useMangoStore
|