2023-02-27 23:20:11 -08:00
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
2023-04-13 04:25:58 -07:00
|
|
|
import {
|
2023-07-04 17:05:03 -07:00
|
|
|
MangoAccount,
|
2023-04-13 04:25:58 -07:00
|
|
|
ParsedFillEvent,
|
|
|
|
PerpMarket,
|
2023-07-04 17:05:03 -07:00
|
|
|
PerpPosition,
|
2023-04-13 04:25:58 -07:00
|
|
|
Serum3Market,
|
|
|
|
} from '@blockworks-foundation/mango-v4'
|
2023-02-19 12:52:25 -08:00
|
|
|
import { Modify } from '@blockworks-foundation/mango-v4'
|
2023-02-15 13:49:56 -08:00
|
|
|
import { Event } from '@project-serum/serum/lib/queue'
|
2023-07-04 17:05:03 -07:00
|
|
|
import { PublicKey } from '@solana/web3.js'
|
2023-04-17 16:45:27 -07:00
|
|
|
import { formatTradeHistory } from 'hooks/useTradeHistory'
|
2022-12-18 13:04:50 -08:00
|
|
|
|
2023-02-23 18:22:24 -08:00
|
|
|
export type EmptyObject = { [K in keyof never]?: never }
|
2022-12-14 12:54:08 -08:00
|
|
|
export interface OrderbookL2 {
|
2022-09-25 19:00:19 -07:00
|
|
|
bids: number[][]
|
|
|
|
asks: number[][]
|
|
|
|
}
|
|
|
|
|
|
|
|
export type SpotBalances = Record<
|
|
|
|
string,
|
|
|
|
{ inOrders: number; unsettled: number }
|
|
|
|
>
|
2023-01-06 16:26:06 -08:00
|
|
|
|
|
|
|
export interface SpotTradeHistory {
|
|
|
|
signature: string
|
|
|
|
block_datetime: string
|
|
|
|
market: string
|
|
|
|
open_orders: string
|
|
|
|
mango_account: string
|
|
|
|
bid: boolean
|
|
|
|
maker: boolean
|
|
|
|
referrer_rebate: any
|
|
|
|
order_id: string
|
|
|
|
client_order_id: string
|
|
|
|
fee_tier: number
|
|
|
|
instruction_num: number
|
|
|
|
size: number
|
|
|
|
price: number
|
2023-02-23 13:44:54 -08:00
|
|
|
side: 'buy' | 'sell'
|
2023-01-06 16:26:06 -08:00
|
|
|
fee_cost: number
|
|
|
|
open_orders_owner: string
|
|
|
|
base_symbol: string
|
|
|
|
quote_symbol: string
|
|
|
|
}
|
2023-01-14 21:52:22 -08:00
|
|
|
|
2023-01-18 20:34:25 -08:00
|
|
|
export interface PerpTradeHistory {
|
|
|
|
signature: string
|
|
|
|
slot: number
|
|
|
|
block_datetime: string
|
|
|
|
maker: string
|
|
|
|
maker_order_id: string
|
|
|
|
maker_fee: number
|
|
|
|
taker: string
|
|
|
|
taker_order_id: string
|
|
|
|
taker_client_order_id: string
|
|
|
|
taker_fee: number
|
2023-02-23 13:44:54 -08:00
|
|
|
taker_side: 'bid' | 'ask'
|
2023-01-18 20:34:25 -08:00
|
|
|
perp_market: string
|
|
|
|
market_index: number
|
|
|
|
price: number
|
|
|
|
quantity: number
|
|
|
|
seq_num: number
|
|
|
|
}
|
|
|
|
|
2023-04-13 04:25:58 -07:00
|
|
|
export const isApiSpotTradeHistory = (
|
|
|
|
t: SpotTradeHistory | PerpTradeHistory
|
|
|
|
): t is SpotTradeHistory => {
|
|
|
|
if ('open_orders' in t) return true
|
|
|
|
else return false
|
|
|
|
}
|
|
|
|
|
|
|
|
export type PerpFillEvent = ParsedFillEvent
|
|
|
|
|
2023-04-17 16:45:27 -07:00
|
|
|
export type CombinedTradeHistory = ReturnType<typeof formatTradeHistory>
|
|
|
|
|
2023-04-13 04:25:58 -07:00
|
|
|
export type CombinedTradeHistoryTypes =
|
|
|
|
| SpotTradeHistory
|
|
|
|
| PerpTradeHistory
|
|
|
|
| PerpFillEvent
|
|
|
|
| SerumEvent
|
|
|
|
|
|
|
|
export const isSerumFillEvent = (
|
|
|
|
t: CombinedTradeHistoryTypes
|
|
|
|
): t is SerumEvent => {
|
|
|
|
if ('eventFlags' in t) return true
|
|
|
|
else return false
|
|
|
|
}
|
|
|
|
|
|
|
|
export const isPerpFillEvent = (
|
|
|
|
t: CombinedTradeHistoryTypes
|
|
|
|
): t is PerpFillEvent => {
|
|
|
|
if ('takerSide' in t) return true
|
|
|
|
else return false
|
|
|
|
}
|
|
|
|
|
2023-02-15 13:49:56 -08:00
|
|
|
export type SerumEvent = Modify<
|
|
|
|
Event,
|
|
|
|
{
|
|
|
|
price: number
|
|
|
|
size: number
|
|
|
|
side: string
|
|
|
|
feeCost: number
|
|
|
|
}
|
|
|
|
>
|
|
|
|
|
2023-01-14 21:52:22 -08:00
|
|
|
export type GenericMarket = Serum3Market | PerpMarket
|
2023-02-23 16:28:49 -08:00
|
|
|
|
2023-03-25 14:16:42 -07:00
|
|
|
export type TradeHistoryApiResponseType = {
|
2023-02-23 16:28:49 -08:00
|
|
|
trade_type: string
|
|
|
|
block_datetime: string
|
|
|
|
activity_details: PerpTradeHistory | SpotTradeHistory
|
2023-03-25 14:16:42 -07:00
|
|
|
}
|
2023-02-23 16:28:49 -08:00
|
|
|
|
|
|
|
export type AccountPerformanceData = {
|
|
|
|
[date: string]: {
|
|
|
|
account_equity: number
|
|
|
|
pnl: number
|
|
|
|
spot_value: number
|
|
|
|
transfer_balance: number
|
|
|
|
deposit_interest_cumulative_usd: number
|
|
|
|
borrow_interest_cumulative_usd: number
|
|
|
|
spot_volume_usd: number
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 03:27:37 -07:00
|
|
|
export type TotalAccountFundingItem = {
|
|
|
|
long_funding: number
|
|
|
|
short_funding: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export type HourlyFundingData = [
|
|
|
|
string,
|
|
|
|
{ [key: string]: { long_funding: number; short_funding: number } }
|
|
|
|
]
|
|
|
|
|
|
|
|
export type HourlyFundingStatsData = {
|
|
|
|
marketFunding: { long_funding: number; short_funding: number; time: string }[]
|
|
|
|
market: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface HourlyFundingChartData extends Record<string, any> {
|
|
|
|
time: string
|
|
|
|
}
|
|
|
|
|
2023-07-03 18:24:36 -07:00
|
|
|
export type AccountVolumeTotalData = [string, { volume_usd: number }]
|
2023-06-28 21:23:49 -07:00
|
|
|
|
2023-07-03 18:24:36 -07:00
|
|
|
export type HourlyAccountVolumeData = {
|
|
|
|
[market: string]: {
|
|
|
|
[timestamp: string]: {
|
|
|
|
volume_usd: number
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export type FormattedHourlyAccountVolumeData = {
|
|
|
|
time: string
|
|
|
|
total_volume_usd: number
|
|
|
|
markets: Record<string, number>
|
|
|
|
}
|
2023-06-28 21:23:49 -07:00
|
|
|
|
2023-02-23 16:28:49 -08:00
|
|
|
export interface TotalInterestDataItem {
|
|
|
|
borrow_interest: number
|
|
|
|
deposit_interest: number
|
|
|
|
borrow_interest_usd: number
|
|
|
|
deposit_interest_usd: number
|
|
|
|
symbol: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface PerformanceDataItem {
|
|
|
|
account_equity: number
|
|
|
|
borrow_interest_cumulative_usd: number
|
|
|
|
deposit_interest_cumulative_usd: number
|
|
|
|
pnl: number
|
|
|
|
spot_value: number
|
|
|
|
time: string
|
|
|
|
transfer_balance: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface DepositWithdrawFeedItem {
|
|
|
|
block_datetime: string
|
|
|
|
mango_account: string
|
|
|
|
quantity: number
|
|
|
|
signature: string
|
|
|
|
symbol: string
|
|
|
|
usd_equivalent: number
|
|
|
|
wallet_pk: string
|
|
|
|
}
|
|
|
|
|
2023-04-04 21:21:01 -07:00
|
|
|
export interface PerpTradeFeedItem {
|
|
|
|
block_datetime: string
|
|
|
|
maker: string
|
|
|
|
maker_fee: number
|
|
|
|
maker_order_id: string | null
|
|
|
|
market_index: number
|
|
|
|
perp_market: string
|
|
|
|
perp_market_name: string
|
|
|
|
price: number
|
|
|
|
quantity: number
|
|
|
|
seq_num: number
|
|
|
|
signature: number
|
|
|
|
slot: number
|
|
|
|
taker: string
|
|
|
|
taker_client_order_id: string | null
|
|
|
|
taker_fee: number
|
|
|
|
taker_order_id: string | null
|
|
|
|
taker_side: string
|
|
|
|
}
|
|
|
|
|
2023-03-22 18:26:02 -07:00
|
|
|
export interface SpotLiquidationFeedItem {
|
2023-02-23 16:28:49 -08:00
|
|
|
asset_amount: number
|
|
|
|
asset_price: number
|
|
|
|
asset_symbol: string
|
|
|
|
block_datetime: string
|
2023-03-27 16:56:23 -07:00
|
|
|
counterparty: string
|
2023-02-23 16:28:49 -08:00
|
|
|
liab_amount: number
|
|
|
|
liab_price: number
|
|
|
|
liab_symbol: string
|
|
|
|
mango_account: string
|
|
|
|
mango_group: string
|
2023-03-22 18:26:02 -07:00
|
|
|
side: 'liqor' | 'liqee'
|
2023-02-23 16:28:49 -08:00
|
|
|
signature: string
|
|
|
|
}
|
|
|
|
|
2023-03-22 18:26:02 -07:00
|
|
|
export interface PerpLiquidationFeedItem {
|
|
|
|
base_transfer: -0.5
|
|
|
|
block_datetime: string
|
|
|
|
counterparty: string
|
|
|
|
mango_account: string
|
|
|
|
mango_group: string
|
2023-03-23 05:08:59 -07:00
|
|
|
perp_market_name: string
|
2023-03-22 18:26:02 -07:00
|
|
|
pnl_settle_limit_transfer: number
|
|
|
|
pnl_transfer: number
|
|
|
|
price: number
|
|
|
|
quote_transfer: number
|
|
|
|
side: 'liqor' | 'liqee'
|
|
|
|
signature: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export type SpotOrPerpLiquidationItem =
|
|
|
|
| SpotLiquidationFeedItem
|
|
|
|
| PerpLiquidationFeedItem
|
|
|
|
|
2023-02-23 16:28:49 -08:00
|
|
|
export interface LiquidationActivity {
|
2023-03-22 18:26:02 -07:00
|
|
|
activity_details: SpotOrPerpLiquidationItem
|
2023-02-23 16:28:49 -08:00
|
|
|
block_datetime: string
|
|
|
|
activity_type: string
|
|
|
|
symbol: string
|
|
|
|
}
|
|
|
|
|
2023-04-04 21:21:01 -07:00
|
|
|
export interface PerpTradeActivity {
|
|
|
|
activity_details: PerpTradeFeedItem
|
|
|
|
block_datetime: string
|
|
|
|
activity_type: string
|
|
|
|
symbol: string
|
|
|
|
}
|
|
|
|
|
2023-02-23 16:28:49 -08:00
|
|
|
export function isLiquidationFeedItem(
|
|
|
|
item: ActivityFeed
|
|
|
|
): item is LiquidationActivity {
|
|
|
|
if (item.activity_type.includes('liquidate')) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-04-04 21:21:01 -07:00
|
|
|
export function isPerpTradeFeedItem(
|
|
|
|
item: ActivityFeed
|
|
|
|
): item is PerpTradeActivity {
|
|
|
|
if (item.activity_type === 'perp_trade') {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-03-22 18:26:02 -07:00
|
|
|
export function isPerpLiquidation(
|
|
|
|
activityDetails: SpotOrPerpLiquidationItem
|
|
|
|
): activityDetails is PerpLiquidationFeedItem {
|
|
|
|
if ((activityDetails as PerpLiquidationFeedItem).base_transfer) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-02-23 16:28:49 -08:00
|
|
|
export interface SwapHistoryItem {
|
|
|
|
block_datetime: string
|
|
|
|
mango_account: string
|
|
|
|
signature: string
|
|
|
|
swap_in_amount: number
|
|
|
|
swap_in_loan: number
|
|
|
|
swap_in_loan_origination_fee: number
|
|
|
|
swap_in_price_usd: number
|
|
|
|
swap_in_symbol: string
|
|
|
|
swap_out_amount: number
|
|
|
|
loan: number
|
|
|
|
loan_origination_fee: number
|
|
|
|
swap_out_price_usd: number
|
|
|
|
swap_out_symbol: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface NFT {
|
|
|
|
address: string
|
|
|
|
image: string
|
2023-04-14 04:48:09 -07:00
|
|
|
name: string
|
2023-02-23 16:28:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface PerpStatsItem {
|
2023-06-04 18:06:03 -07:00
|
|
|
cumulative_base_volume: number
|
|
|
|
cumulative_quote_volume: number
|
2023-02-23 16:28:49 -08:00
|
|
|
date_hour: string
|
|
|
|
fees_accrued: number
|
2023-04-23 19:54:18 -07:00
|
|
|
fees_settled: number
|
2023-02-23 16:28:49 -08:00
|
|
|
funding_rate_hourly: number
|
|
|
|
instantaneous_funding_rate: number
|
|
|
|
mango_group: string
|
|
|
|
market_index: number
|
|
|
|
open_interest: number
|
|
|
|
perp_market: string
|
|
|
|
price: number
|
|
|
|
stable_price: number
|
2023-04-23 19:54:18 -07:00
|
|
|
total_fees: number
|
2023-02-23 16:28:49 -08:00
|
|
|
}
|
|
|
|
|
2023-07-04 17:05:03 -07:00
|
|
|
export type PositionStat = {
|
|
|
|
account?: MangoAccount
|
|
|
|
mangoAccount: PublicKey
|
|
|
|
perpPosition: PerpPosition
|
|
|
|
}
|
|
|
|
|
2023-06-05 04:06:37 -07:00
|
|
|
export type GroupedDataItem = PerpStatsItem & Record<string, any>
|
|
|
|
|
2023-02-23 16:28:49 -08:00
|
|
|
export type ActivityFeed = {
|
|
|
|
activity_type: string
|
|
|
|
block_datetime: string
|
|
|
|
symbol: string
|
|
|
|
activity_details:
|
|
|
|
| DepositWithdrawFeedItem
|
2023-03-22 18:26:02 -07:00
|
|
|
| SpotLiquidationFeedItem
|
|
|
|
| PerpLiquidationFeedItem
|
2023-02-23 16:28:49 -08:00
|
|
|
| SwapHistoryItem
|
2023-04-04 21:21:01 -07:00
|
|
|
| PerpTradeFeedItem
|
2023-02-23 16:28:49 -08:00
|
|
|
| SpotTradeHistory
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ProfileDetails {
|
|
|
|
profile_image_url?: string
|
|
|
|
profile_name: string
|
|
|
|
trader_category: string
|
|
|
|
wallet_pk: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface TourSettings {
|
|
|
|
account_tour_seen: boolean
|
|
|
|
swap_tour_seen: boolean
|
|
|
|
trade_tour_seen: boolean
|
|
|
|
wallet_pk: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface TokenStatsItem {
|
|
|
|
borrow_apr: number
|
|
|
|
borrow_rate: number
|
|
|
|
collected_fees: number
|
|
|
|
date_hour: string
|
|
|
|
deposit_apr: number
|
|
|
|
deposit_rate: number
|
|
|
|
mango_group: string
|
|
|
|
price: number
|
|
|
|
symbol: string
|
|
|
|
token_index: number
|
|
|
|
total_borrows: number
|
|
|
|
total_deposits: number
|
|
|
|
}
|
|
|
|
|
2023-05-01 17:52:32 -07:00
|
|
|
export interface MangoTokenStatsItem {
|
|
|
|
date: string
|
|
|
|
borrowValue: number
|
|
|
|
depositValue: number
|
|
|
|
feesCollected: number
|
|
|
|
}
|
|
|
|
|
2023-02-23 16:28:49 -08:00
|
|
|
export interface TradeForm {
|
|
|
|
side: 'buy' | 'sell'
|
|
|
|
price: string | undefined
|
|
|
|
baseSize: string
|
|
|
|
quoteSize: string
|
|
|
|
tradeType: 'Market' | 'Limit'
|
|
|
|
postOnly: boolean
|
|
|
|
ioc: boolean
|
|
|
|
reduceOnly: boolean
|
|
|
|
}
|
2023-02-27 23:20:11 -08:00
|
|
|
|
|
|
|
export interface MangoError extends Error {
|
|
|
|
txid: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isMangoError(error: unknown): error is MangoError {
|
|
|
|
return (
|
|
|
|
typeof error === 'object' &&
|
|
|
|
error !== null &&
|
|
|
|
'message' in error &&
|
|
|
|
'txid' in error &&
|
|
|
|
typeof (error as Record<string, unknown>).message === 'string'
|
|
|
|
)
|
|
|
|
}
|
2023-05-31 05:15:50 -07:00
|
|
|
|
|
|
|
export type TickerData = {
|
|
|
|
base_currency: string
|
|
|
|
base_volume: string
|
|
|
|
high: string
|
|
|
|
last_price: string
|
|
|
|
low: string
|
|
|
|
target_currency: string
|
|
|
|
target_volume: string
|
|
|
|
ticker_id: string
|
|
|
|
}
|
2023-07-11 17:26:37 -07:00
|
|
|
|
|
|
|
export interface HealthContribution {
|
|
|
|
asset: string
|
|
|
|
contribution: number
|
2023-07-14 06:34:09 -07:00
|
|
|
contributionDetails?: ContributionDetails
|
|
|
|
hasPerp?: boolean
|
2023-07-11 17:26:37 -07:00
|
|
|
isAsset: boolean
|
|
|
|
}
|
2023-07-14 06:34:09 -07:00
|
|
|
|
|
|
|
export interface PerpMarketContribution {
|
|
|
|
market: string
|
|
|
|
contributionUi: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ContributionDetails {
|
|
|
|
perpMarketContributions: PerpMarketContribution[]
|
|
|
|
spotUi: number
|
|
|
|
}
|