xtraderClient/src/api/order.ts
2026-02-27 10:04:04 +08:00

139 lines
3.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { get } from './request'
/** 分页结果 */
export interface PageResult<T> {
list: T[]
page: number
pageSize: number
total: number
}
/**
* 订单项(与 doc.json definitions["model.ClobOrder"] 对齐)
* GET /clob/order/getOrderList 列表项
*/
export interface ClobOrderItem {
ID: number
assetID?: string
createdAt?: string
updatedAt?: string
expiration?: number
feeRateBps?: number
market?: string
orderType?: number
originalSize?: number
outcome?: string
price?: number
side?: number
sizeMatched?: number
status?: number
userID?: number
[key: string]: unknown
}
/** 订单列表响应 */
export interface OrderListResponse {
code: number
data: PageResult<ClobOrderItem>
msg: string
}
/**
* GET /clob/order/getOrderList 请求参数
*/
export interface GetOrderListParams {
page?: number
pageSize?: number
startCreatedAt?: string
endCreatedAt?: string
marketID?: string
tokenID?: string
userID?: number
}
/**
* 分页获取订单列表
* GET /clob/order/getOrderList
* 需鉴权x-token、x-user-id
*/
export async function getOrderList(
params: GetOrderListParams = {},
config?: { headers?: Record<string, string> },
): Promise<OrderListResponse> {
const { page = 1, pageSize = 10, startCreatedAt, endCreatedAt, marketID, tokenID, userID } = params
const query: Record<string, string | number | undefined> = { page, pageSize }
if (startCreatedAt != null && startCreatedAt !== '') query.startCreatedAt = startCreatedAt
if (endCreatedAt != null && endCreatedAt !== '') query.endCreatedAt = endCreatedAt
if (marketID != null && marketID !== '') query.marketID = marketID
if (tokenID != null && tokenID !== '') query.tokenID = tokenID
if (userID != null && Number.isFinite(userID)) query.userID = userID
return get<OrderListResponse>('/clob/order/getOrderList', query, config)
}
/** 钱包 History 展示项(与 Wallet.vue HistoryItem 一致) */
export interface HistoryDisplayItem {
id: string
market: string
side: 'Yes' | 'No'
activity: string
value: string
activityDetail?: string
profitLoss?: string
profitLossNegative?: boolean
timeAgo?: string
avgPrice?: string
shares?: string
iconChar?: string
iconClass?: string
}
/** Side: Buy=1, Sell=2 */
const Side = { Buy: 1, Sell: 2 } as const
function formatTimeAgo(createdAt: string | undefined): string {
if (!createdAt) return ''
const d = new Date(createdAt)
const now = Date.now()
const diff = now - d.getTime()
if (diff < 60000) return 'Just now'
if (diff < 3600000) return `${Math.floor(diff / 60000)} minutes ago`
if (diff < 86400000) return `${Math.floor(diff / 3600000)} hours ago`
if (diff < 604800000) return `${Math.floor(diff / 86400000)} days ago`
return d.toLocaleDateString()
}
/**
* 将 ClobOrderItem 映射为钱包 History 展示项
* price 为整数(已乘 10000sizeMatched 为已成交份额
*/
export function mapOrderToHistoryItem(order: ClobOrderItem): HistoryDisplayItem {
const id = String(order.ID ?? '')
const market = order.market ?? ''
const outcome = order.outcome ?? 'Yes'
const sideNum = order.side ?? Side.Buy
const sideLabel = sideNum === Side.Sell ? 'Sell' : 'Buy'
const activity = `${sideLabel} ${outcome}`
const priceBps = order.price ?? 0
const priceCents = Math.round(priceBps / 100)
const size = order.sizeMatched ?? order.originalSize ?? 0
const valueUsd = (priceBps / 10000) * size
const value = `$${valueUsd.toFixed(2)}`
const verb = sideNum === Side.Sell ? 'Sold' : 'Bought'
const activityDetail = `${verb} ${size} ${outcome} at ${priceCents}¢`
const avgPrice = `${priceCents}¢`
const timeAgo = formatTimeAgo(order.createdAt)
return {
id,
market,
side: outcome === 'No' ? 'No' : 'Yes',
activity,
value,
activityDetail,
profitLoss: value,
profitLossNegative: false,
timeAgo,
avgPrice,
shares: String(size),
}
}