import { hasProperty, Hex, isHexString } from '@metamask/utils'; import { BigNumber } from 'bignumber.js'; import { BASIS_POINTS_DIVISOR, HIP3_ASSET_ID_CONFIG, isUnrestrictedMarginMode, } from '../constants/hyperLiquidConfig.js'; import { DECIMAL_PRECISION_CONFIG, ORDER_SLIPPAGE_CONFIG, } from '../constants/perpsConfig.js'; import { PERPS_ERROR_CODES } from '../perpsErrorCodes.js'; import type { AssetPosition, FrontendOrder, ClearinghouseStateResponse, HyperLiquidPublicTrade, MetaResponse, SDKOrderParams, } from '../types/hyperliquid-types.js'; import type { AccountState, CrossMarginSummary, MarketInfo, Order, OrderParams as PerpsOrderParams, Position, PositionTriggerOrder, PublicTrade, RawLedgerUpdate, UserHistoryItem, } from '../types/index.js'; import type { TpslLinkage, TriggerOrderType } from '../types/perps-types.js'; import { resolveHyperLiquidMarginTiers } from './hyperLiquidPositionPreview.js'; import { buildTriggerOrderType, classifyTriggerDirection, getTriggerDirection, getTriggerExecution, isLimitExecutionOrderType, isTriggerOrderType, toSDKTimeInForce, } from './orderTypes.js'; import { roundToSignificantFiguresGrid } from './pricePrecision.js'; import { buildPublicTrade, sortPublicTradesNewestFirst, } from './publicTrades.js'; import { parseFiniteDecimal } from './stringParseUtils.js'; type FrontendOrderWithParentTpsl = FrontendOrder & { takeProfitPrice?: unknown; stopLossPrice?: unknown; takeProfitOrderId?: unknown; stopLossOrderId?: unknown; }; export const HYPERLIQUID_SCALE_CLOID_MARKER = '4d4d5343'; /** * Recover the Scale group shared by MetaMask-generated rung CLOIDs. * * @param clientOrderId - HyperLiquid client order ID. * @returns The Scale handle, or undefined for an unrelated order. */ export const readScaleGroupId = ( clientOrderId: string | null | undefined, ): string | undefined => { const normalized = clientOrderId?.toLowerCase(); if ( normalized?.length !== 34 || !normalized.startsWith(`0x${HYPERLIQUID_SCALE_CLOID_MARKER}`) ) { return undefined; } return `scale:${normalized.slice(2, -2)}`; }; const readOptionalString = (value: unknown): string | undefined => typeof value === 'string' && value.length > 0 ? value : undefined; const readOptionalOrderId = (value: unknown): string | undefined => { if (typeof value === 'string' && value.length > 0) { return value; } if (typeof value === 'number' && Number.isFinite(value)) { return value.toString(); } return undefined; }; const getParentTpslMetadata = ( rawOrder: FrontendOrderWithParentTpsl, ): { takeProfitPrice?: string; stopLossPrice?: string; takeProfitOrderId?: string; stopLossOrderId?: string; } => ({ takeProfitPrice: readOptionalString(rawOrder.takeProfitPrice), stopLossPrice: readOptionalString(rawOrder.stopLossPrice), takeProfitOrderId: readOptionalOrderId(rawOrder.takeProfitOrderId), stopLossOrderId: readOptionalOrderId(rawOrder.stopLossOrderId), }); /** * HyperLiquid SDK Adapter Utilities * * These functions transform between MetaMask Perps API types and HyperLiquid SDK types. * The SDK uses cryptic property names for efficiency, but our API uses descriptive names * to provide a consistent interface across different perps protocols. */ export function adaptOrderToSDK( order: PerpsOrderParams, symbolToAssetId: Map, ): SDKOrderParams { const assetId = symbolToAssetId.get(order.symbol); if (assetId === undefined) { const availableDexs = new Set(); symbolToAssetId.forEach((_, symbol) => { if (symbol.includes(':')) { const dex = symbol.split(':')[0]; availableDexs.add(dex); } }); const dexHint = availableDexs.size > 0 ? ` Available HIP-3 DEXs: ${Array.from(availableDexs).join(', ')}` : ' No HIP-3 DEXs currently available.'; throw new Error( `Asset ${order.symbol} not found in asset mapping.${dexHint} Check console logs for "HyperLiquidProvider: Asset mapping built" to see available assets.`, ); } return { a: assetId, b: order.isBuy, p: order.price ?? resolveTriggerCapPrice(order) ?? '0', s: order.size, r: order.reduceOnly ?? false, t: adaptOrderTypeToSDK(order), c: order.clientOrderId && isHexString(order.clientOrderId) ? order.clientOrderId : undefined, }; } /** * Derive the slippage cap a market-on-trigger order submits as its price. * * A `stop_market` / `take_profit_market` order legitimately carries no limit * price, but the SDK still requires a positive `p` — it is the cap the order * fills against once the trigger fires, not a resting price. Sending `'0'` * fails SDK validation before the request is ever made. * * The cap follows the order's own tolerance, matching `calculateOrderPriceAndSize` * on the `placeOrder` path, so the same order priced through either route gets * the same execution bound. * * @param order - Order params carrying the placement type, trigger price, and * slippage tolerance. * @returns The formatted cap price, or undefined when the order needs no cap. */ function resolveTriggerCapPrice(order: PerpsOrderParams): string | undefined { if ( !isTriggerOrderType(order.orderType) || isLimitExecutionOrderType(order.orderType) || !order.triggerPrice ) { return undefined; } const triggerPrice = parseFloat(order.triggerPrice); if (!Number.isFinite(triggerPrice)) { return undefined; } // Accept the deprecated decimal `slippage` too, normalizing it to bps the way // `placeOrder` does, so neither spelling silently falls back to the default. const effectiveBps = order.maxSlippageBps ?? (typeof order.slippage === 'number' ? Math.round(order.slippage * BASIS_POINTS_DIVISOR) : undefined) ?? ORDER_SLIPPAGE_CONFIG.DefaultTpslSlippageBps; // Buying pays up to the cap, selling accepts down to it. const slippage = effectiveBps / BASIS_POINTS_DIVISOR; const capPrice = order.isBuy ? triggerPrice * (1 + slippage) : triggerPrice * (1 - slippage); return capPrice.toString(); } /** * Map a placement type onto the SDK's order-type field. * * @param order - Order params carrying the placement type and trigger price * @returns The SDK order-type field */ function adaptOrderTypeToSDK(order: PerpsOrderParams): SDKOrderParams['t'] { if (isTriggerOrderType(order.orderType)) { if (order.timeInForce !== undefined) { throw new Error(PERPS_ERROR_CODES.ORDER_TIME_IN_FORCE_NOT_SUPPORTED); } if (!order.triggerPrice) { throw new Error(PERPS_ERROR_CODES.ORDER_TRIGGER_PRICE_REQUIRED); } return { trigger: { isMarket: !isLimitExecutionOrderType(order.orderType), triggerPx: order.triggerPrice, tpsl: getTriggerDirection(order.orderType) === 'stop' ? 'sl' : 'tp', }, }; } if (order.orderType === 'limit') { return { limit: { tif: toSDKTimeInForce(order.timeInForce) } }; } if (order.timeInForce !== undefined) { throw new Error(PERPS_ERROR_CODES.ORDER_TIME_IN_FORCE_NOT_SUPPORTED); } return { limit: { tif: 'FrontendMarket' } }; } /** * Map the provider-agnostic TP/SL linkage onto HyperLiquid's grouping vocabulary. * * @param linkage - How the attached TP/SL is linked. * @returns The HyperLiquid grouping value. */ export function adaptTpslLinkageToGrouping( linkage: TpslLinkage, ): 'na' | 'normalTpsl' | 'positionTpsl' { switch (linkage) { case 'position': return 'positionTpsl'; case 'order': return 'normalTpsl'; default: return 'na'; } } /** * Margin Hyperliquid shows for a cross position. When cross equity is below * the cross initial margin, the venue scales each cross position's margin by * `accountValue / totalMarginUsed`. * * @param marginUsed - The position's raw margin used. * @param crossMarginSummary - The clearinghouse's cross margin summary. * @returns The displayed margin, or undefined when an input is not a number. */ function deriveCrossDisplayMarginUsed( marginUsed: string, crossMarginSummary: ClearinghouseStateResponse['crossMarginSummary'], ): string | undefined { const margin = new BigNumber(marginUsed); const accountValue = new BigNumber(crossMarginSummary.accountValue); const totalMarginUsed = new BigNumber(crossMarginSummary.totalMarginUsed); if ( !margin.isFinite() || !accountValue.isFinite() || !totalMarginUsed.isFinite() ) { return undefined; } if (totalMarginUsed.lte(0) || accountValue.gte(totalMarginUsed)) { return margin.toFixed(); } return BigNumber.max( margin.times(accountValue).div(totalMarginUsed), 0, ).toFixed(); } /** * Adapt a Hyperliquid asset position. * * @param assetPosition - Position from a clearinghouse state. * @param crossMarginSummary - That clearinghouse's `crossMarginSummary`. When * given, a cross position also carries `displayMarginUsed`. * @returns The position. */ export function adaptPositionFromSDK( assetPosition: AssetPosition, crossMarginSummary?: ClearinghouseStateResponse['crossMarginSummary'], ): Position { const pos = assetPosition.position; const displayMarginUsed = pos.leverage.type === 'cross' && crossMarginSummary ? deriveCrossDisplayMarginUsed(pos.marginUsed, crossMarginSummary) : undefined; return { symbol: pos.coin, size: pos.szi, entryPrice: pos.entryPx, positionValue: pos.positionValue, unrealizedPnl: pos.unrealizedPnl, marginUsed: pos.marginUsed, ...(displayMarginUsed !== undefined && { displayMarginUsed }), leverage: { type: pos.leverage.type, value: pos.leverage.value, rawUsd: pos.leverage.type === 'isolated' ? pos.leverage.rawUsd : undefined, }, liquidationPrice: pos.liquidationPx, maxLeverage: pos.maxLeverage, returnOnEquity: pos.returnOnEquity, cumulativeFunding: pos.cumFunding, takeProfitCount: 0, stopLossCount: 0, }; } export function adaptOrderFromSDK( rawOrder: FrontendOrder, position?: Position, ): Order { // TODO: Remove this widened boundary type when FrontendOrder includes // takeProfitPrice/stopLossPrice and takeProfitOrderId/stopLossOrderId. const parentTpslMetadata = getParentTpslMetadata(rawOrder); // Extract basic fields with appropriate conversions const orderId = rawOrder.oid?.toString() || ''; const symbol = rawOrder.coin; const side: 'buy' | 'sell' = rawOrder.side === 'B' ? 'buy' : 'sell'; const detailedOrderType = rawOrder.orderType; const { isTrigger } = rawOrder; const { reduceOnly } = rawOrder; const triggerOrderType = adaptTriggerOrderTypeFromSDK(detailedOrderType); let orderType: 'limit' | 'market' = 'market'; if (triggerOrderType) { // Trigger orders always carry a limitPx (the slippage cap for // market-on-trigger execution), so the placement type is the only reliable // source for how the order actually executes. orderType = getTriggerExecution(triggerOrderType); } else if ( detailedOrderType?.toLowerCase().includes('limit') || rawOrder.limitPx ) { orderType = 'limit'; } const price = rawOrder.limitPx || rawOrder.triggerPx || '0'; let size = rawOrder.sz; let originalSize = rawOrder.origSz || size; let currentSize = parseFloat(size); let origSize = parseFloat(originalSize); if (rawOrder.isPositionTpsl && origSize === 0 && position) { const absPositionSize = Math.abs(parseFloat(position.size)); currentSize = absPositionSize; origSize = absPositionSize; size = absPositionSize.toString(); originalSize = absPositionSize.toString(); } const filledSize = origSize - currentSize; let takeProfitPrice: string | undefined; let stopLossPrice: string | undefined; let takeProfitOrderId: string | undefined; let stopLossOrderId: string | undefined; // TODO: We assume that there can only be 1 TP and 1 SL as children but there can be several TPSLs as children if (rawOrder.children && rawOrder.children.length > 0) { rawOrder.children.forEach((child) => { if (child.isTrigger && child.orderType) { if (child.orderType.includes('Take Profit')) { // HyperLiquid represents "no trigger price" as an empty string, not // null/undefined, so `||` (not `??`) is required to fall back to // limitPx when triggerPx is ''. takeProfitPrice = child.triggerPx || child.limitPx; takeProfitOrderId = child.oid.toString(); } else if (child.orderType.includes('Stop')) { stopLossPrice = child.triggerPx || child.limitPx; stopLossOrderId = child.oid.toString(); } } }); } // Fallback: preserve parent-level TP/SL metadata when children are absent. takeProfitPrice ??= parentTpslMetadata.takeProfitPrice; stopLossPrice ??= parentTpslMetadata.stopLossPrice; takeProfitOrderId ??= parentTpslMetadata.takeProfitOrderId; stopLossOrderId ??= parentTpslMetadata.stopLossOrderId; // Build the order object const order: Order = { orderId, symbol, side, orderType, size, originalSize, price, filledSize: filledSize.toString(), remainingSize: size, status: 'open' as const, timestamp: rawOrder.timestamp, detailedOrderType, isTrigger, reduceOnly, }; const strategyGroupId = readScaleGroupId(rawOrder.cloid); if (strategyGroupId) { order.strategyGroupId = strategyGroupId; } if (typeof rawOrder.isPositionTpsl === 'boolean') { order.isPositionTpsl = rawOrder.isPositionTpsl; } if (takeProfitPrice) { order.takeProfitPrice = takeProfitPrice; order.takeProfitOrderId = takeProfitOrderId; } if (stopLossPrice) { order.stopLossPrice = stopLossPrice; order.stopLossOrderId = stopLossOrderId; } if (rawOrder.triggerPx) { order.triggerPrice = rawOrder.triggerPx; } if (triggerOrderType) { order.triggerOrderType = triggerOrderType; } return order; } /** * Map HyperLiquid's human-readable order type string onto the provider-agnostic * trigger placement type. * * @param detailedOrderType - HyperLiquid `orderType` string (e.g. `'Stop Limit'`) * @returns The normalized trigger placement type, or undefined for non-trigger orders */ export function adaptTriggerOrderTypeFromSDK( detailedOrderType: string | undefined, ): TriggerOrderType | undefined { if (!detailedOrderType) { return undefined; } const isTakeProfit = detailedOrderType.includes('Take Profit'); const isStop = detailedOrderType.includes('Stop'); if (!isTakeProfit && !isStop) { return undefined; } return buildTriggerOrderType({ direction: isTakeProfit ? 'take_profit' : 'stop', execution: detailedOrderType.includes('Limit') ? 'limit' : 'market', }); } /** * Build the position-state view of a trigger order attached to a position. * * HyperLiquid encodes "the whole position" as size `0` for position-bound TP/SL, * which is resolved here against the position size so consumers always see a * concrete quantity and can tell partial triggers apart. * * @param params - Mapping parameters * @param params.rawOrder - Raw HyperLiquid frontend order * @param params.positionSize - Signed or unsigned position size * @param params.entryPrice - Entry price, used to classify a trigger the exchange left unnamed * @returns The normalized trigger order, or undefined when the order is not a trigger */ export function adaptPositionTriggerOrderFromSDK(params: { rawOrder: Pick< FrontendOrder, 'oid' | 'orderType' | 'triggerPx' | 'limitPx' | 'sz' | 'reduceOnly' >; positionSize: string; entryPrice?: string; }): PositionTriggerOrder | undefined { const { rawOrder, positionSize, entryPrice } = params; const orderType = adaptTriggerOrderTypeFromSDK(rawOrder.orderType); // HyperLiquid uses '' for "no trigger price", so `||` (not `??`) is required. const triggerPrice = rawOrder.triggerPx || rawOrder.limitPx || '0'; // Same rule as the WebSocket path: an unnamed trigger keeps its recoverable // direction and leaves its execution mode unstated, so both transports // report the same set of orders. const direction = orderType ? getTriggerDirection(orderType) : classifyTriggerDirection({ triggerPrice, entryPrice, positionSize }); if (!direction) { return undefined; } const absolutePositionSize = Math.abs(parseFloat(positionSize || '0')); const rawSize = Math.abs(parseFloat(rawOrder.sz || '0')); // Position-bound TP/SL carries size 0, meaning the whole position. const size = rawSize > 0 ? rawSize : absolutePositionSize; return { orderId: rawOrder.oid.toString(), direction, orderType, triggerPrice, size: size.toString(), isPartial: rawSize > 0 && absolutePositionSize > 0 && rawSize < absolutePositionSize, reduceOnly: Boolean(rawOrder.reduceOnly), }; } /** * Map one `meta.universe` entry to {@link MarketInfo}. * * @param sdkMarket - The venue market. * @param marginTables - `meta.marginTables` from the same response. Needed to * resolve tiered tables (ids of 50 and above); single-tier ids resolve * without it. * @returns The market, with `marginTiers` when the table resolves. */ export function adaptMarketFromSDK( sdkMarket: MetaResponse['universe'][number], marginTables?: MetaResponse['marginTables'], ): MarketInfo { // The venue also sends `normal` (no restriction), which the SDK type omits. // `MarketInfo.marginMode` carries restrictions only, so drop it. const venueMarginMode: string | undefined = sdkMarket.marginMode; const marginTiers = resolveHyperLiquidMarginTiers({ marginTableId: sdkMarket.marginTableId, maxLeverage: sdkMarket.maxLeverage, marginTables, }); return { name: sdkMarket.name, szDecimals: sdkMarket.szDecimals, maxLeverage: sdkMarket.maxLeverage, marginTableId: sdkMarket.marginTableId, onlyIsolated: sdkMarket.onlyIsolated, ...(!isUnrestrictedMarginMode(venueMarginMode) && { marginMode: sdkMarket.marginMode, }), isDelisted: sdkMarket.isDelisted, ...(marginTiers && { marginTiers }), }; } /** * Read one clearinghouse's cross-margin summary. A payload without the cross * fields reports none; present values must be finite decimals. * * @param perpsState - Clearinghouse state for one DEX. * @returns The summary, or undefined when the payload carries none. */ function adaptCrossMarginSummaryFromSDK( perpsState: ClearinghouseStateResponse, ): CrossMarginSummary | undefined { const { crossMarginSummary, crossMaintenanceMarginUsed } = perpsState; if (!crossMarginSummary || crossMaintenanceMarginUsed === undefined) { return undefined; } const summary: CrossMarginSummary = { accountValue: crossMarginSummary.accountValue, maintenanceMargin: crossMaintenanceMarginUsed, marginUsed: crossMarginSummary.totalMarginUsed, notional: crossMarginSummary.totalNtlPos, }; for (const [field, value] of Object.entries(summary)) { if (parseFiniteDecimal(value) === null) { throw new Error( `Invalid HyperLiquid cross margin ${field} '${String(value)}'`, ); } } return summary; } // Perps-only account adapter. Spot balances are layered on afterwards by // addSpotBalanceToAccountState, which enforces the USDC-only policy via // SPOT_COLLATERAL_COINS. Keeping spot logic out of here preserves a single // source of truth for spot balance math. export function adaptAccountStateFromSDK( perpsState: ClearinghouseStateResponse, ): AccountState { const { totalUnrealizedPnl, weightedReturnOnEquity } = perpsState.assetPositions.reduce( (acc, assetPos: AssetPosition) => { const unrealizedPnl = parseFloat( assetPos.position.unrealizedPnl || '0', ); const marginUsed = parseFloat(assetPos.position.marginUsed || '0'); const returnOnEquity = parseFloat( assetPos.position.returnOnEquity || '0', ); acc.totalUnrealizedPnl += unrealizedPnl; acc.weightedReturnOnEquity += returnOnEquity * marginUsed; return acc; }, { totalUnrealizedPnl: 0, weightedReturnOnEquity: 0, }, ); const totalMarginUsed = parseFloat( perpsState.marginSummary.totalMarginUsed || '0', ); const totalReturnOnEquityPercentage = totalMarginUsed > 0 ? ((weightedReturnOnEquity / totalMarginUsed) * 100).toString() : '0'; const perpsBalance = parseFloat(perpsState.marginSummary.accountValue); const withdrawable = perpsState.withdrawable || '0'; const crossMarginSummary = adaptCrossMarginSummaryFromSDK(perpsState); const accountState: AccountState = { spendableBalance: withdrawable, withdrawableBalance: withdrawable, totalBalance: perpsBalance.toString() || '0', marginUsed: perpsState.marginSummary.totalMarginUsed || '0', unrealizedPnl: totalUnrealizedPnl.toString() || '0', returnOnEquity: totalReturnOnEquityPercentage || '0', ...(crossMarginSummary ? { crossMarginSummary } : {}), }; return accountState; } export function buildAssetMapping(params: { metaUniverse: MetaResponse['universe']; dex?: string | null; perpDexIndex: number; }): { symbolToAssetId: Map; assetIdToSymbol: Map; } { const { metaUniverse, perpDexIndex } = params; const symbolToAssetId = new Map(); const assetIdToSymbol = new Map(); metaUniverse.forEach((asset, index) => { const assetId = calculateHip3AssetId(perpDexIndex, index); symbolToAssetId.set(asset.name, assetId); assetIdToSymbol.set(assetId, asset.name); }); return { symbolToAssetId, assetIdToSymbol }; } export function formatHyperLiquidPrice(params: { price: string | number; szDecimals: number; }): string { const { price, szDecimals } = params; return roundToSignificantFiguresGrid( price, DECIMAL_PRECISION_CONFIG.MaxPriceDecimals - szDecimals, DECIMAL_PRECISION_CONFIG.MaxSignificantFigures, ); } export function formatHyperLiquidSize(params: { size: string | number; szDecimals: number; }): string { const { size, szDecimals } = params; const number = typeof size === 'string' ? parseFloat(size) : size; if (isNaN(number)) { return '0'; } const formatted = number.toFixed(szDecimals); if (!formatted.includes('.')) { return formatted; } return formatted.replace(/\.?0+$/u, ''); } export function calculatePositionSize(params: { usdValue: number; leverage: number; assetPrice: number; }): number { const { usdValue, leverage, assetPrice } = params; return (usdValue * leverage) / assetPrice; } export function calculateHip3AssetId( perpDexIndex: number, indexInMeta: number, ): number { if (perpDexIndex === 0) { return indexInMeta; } return ( HIP3_ASSET_ID_CONFIG.BaseAssetId + perpDexIndex * HIP3_ASSET_ID_CONFIG.DexMultiplier + indexInMeta ); } /** * Build the provider-neutral execution id for a HyperLiquid fill. * * HyperLiquid's `tid` is a 50-bit hash, and the venue documents * `(block_time, coin, tid)` as a trade's global identity, so all three go into * the id. REST and websocket fills report the same `time`, `coin` and `tid` for * one execution, so both transports yield the same id. * * @param fill - The raw HyperLiquid fill. * @param fill.coin - Venue asset name, including any HIP-3 dex prefix. * @param fill.time - Fill block time in milliseconds. * @param fill.tid - Venue trade id. * @returns The id, or `undefined` when the payload carries no `tid`. Omitting * it, rather than stringifying a missing value, keeps malformed fills from * sharing one id. */ export function buildHyperLiquidFillId(fill: { coin: string; time: number; tid?: number | null; }): string | undefined { if (fill.tid === undefined || fill.tid === null) { return undefined; } return `${fill.coin}:${fill.time}:${fill.tid}`; } export function parseAssetName(assetName: string): { dex: string | null; symbol: string; } { const colonIndex = assetName.indexOf(':'); if (colonIndex === -1) { return { dex: null, symbol: assetName }; } return { dex: assetName.substring(0, colonIndex), symbol: assetName.substring(colonIndex + 1), }; } export function adaptHyperLiquidLedgerUpdateToUserHistoryItem( rawLedgerUpdates: RawLedgerUpdate[], ): UserHistoryItem[] { return (rawLedgerUpdates || []) .filter((update) => { if (update.delta.type === 'deposit') { return true; } if (update.delta.type === 'withdraw') { return true; } if (update.delta.type === 'internalTransfer') { const usdc = Number.parseFloat(update.delta.usdc ?? '0'); if (Number.isNaN(usdc)) { return false; } return usdc > 0; } return false; }) .map((update) => { let amount = '0'; let asset = 'USDC'; if (hasProperty(update.delta, 'usdc') && update.delta.usdc) { amount = Math.abs(parseFloat(update.delta.usdc)).toString(); } if ( hasProperty(update.delta, 'coin') && typeof update.delta.coin === 'string' ) { asset = update.delta.coin; } return { id: `history-${update.hash}`, timestamp: update.time, amount, asset, txHash: update.hash, status: 'completed' as const, type: update.delta.type === 'withdraw' ? 'withdrawal' : 'deposit', details: { source: '', bridgeContract: undefined, recipient: undefined, blockNumber: undefined, chainId: undefined, synthetic: undefined, }, }; }); } const HYPERLIQUID_ZERO_HASH = /^0x0+$/u; const HYPERLIQUID_TRADE_SIDE = { B: 'buy', A: 'sell' } as const; /** * Adapt Hyperliquid public trades (REST `recentTrades` or WS `trades`) to * {@link PublicTrade}, newest first. Trades for another coin and malformed * trades are dropped. `side` is the aggressor's: `B` bought, `A` sold. The id * comes from {@link buildHyperLiquidFillId}, so both transports give one * execution the same id. * * @param trades - SDK trades. * @param symbol - Requested coin; other coins are dropped. * @returns The trades for `symbol`, newest first. */ export function adaptPublicTradesFromHyperLiquid( trades: readonly HyperLiquidPublicTrade[], symbol: string, ): PublicTrade[] { const adapted: PublicTrade[] = []; for (const trade of trades) { if (trade.coin !== symbol) { continue; } const publicTrade = buildPublicTrade({ id: typeof trade.tid === 'number' ? buildHyperLiquidFillId(trade) : undefined, symbol, price: trade.px, size: trade.sz, side: HYPERLIQUID_TRADE_SIDE[trade.side] ?? null, timestamp: trade.time, txHash: typeof trade.hash === 'string' && !HYPERLIQUID_ZERO_HASH.test(trade.hash) ? trade.hash : undefined, providerId: 'hyperliquid', }); if (publicTrade) { adapted.push(publicTrade); } } return sortPublicTradesNewestFirst(adapted); }