@nadohq/engine-client 0.1.0-alpha.3 → 0.1.0-alpha.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/dist/EngineBaseClient.cjs +8 -7
  2. package/dist/EngineBaseClient.cjs.map +1 -1
  3. package/dist/EngineBaseClient.d.cts +2 -3
  4. package/dist/EngineBaseClient.d.ts +2 -3
  5. package/dist/EngineBaseClient.js +7 -5
  6. package/dist/EngineBaseClient.js.map +1 -1
  7. package/dist/EngineClient.d.cts +1 -2
  8. package/dist/EngineClient.d.ts +1 -2
  9. package/dist/EngineExecuteBuilder.cjs +25 -50
  10. package/dist/EngineExecuteBuilder.cjs.map +1 -1
  11. package/dist/EngineExecuteBuilder.d.cts +6 -17
  12. package/dist/EngineExecuteBuilder.d.ts +6 -17
  13. package/dist/EngineExecuteBuilder.js +15 -40
  14. package/dist/EngineExecuteBuilder.js.map +1 -1
  15. package/dist/EngineExecuteClient.cjs +5 -9
  16. package/dist/EngineExecuteClient.cjs.map +1 -1
  17. package/dist/EngineExecuteClient.d.cts +3 -4
  18. package/dist/EngineExecuteClient.d.ts +3 -4
  19. package/dist/EngineExecuteClient.js +5 -9
  20. package/dist/EngineExecuteClient.js.map +1 -1
  21. package/dist/EngineQueryClient.cjs +70 -53
  22. package/dist/EngineQueryClient.cjs.map +1 -1
  23. package/dist/EngineQueryClient.d.cts +17 -12
  24. package/dist/EngineQueryClient.d.ts +17 -12
  25. package/dist/EngineQueryClient.js +50 -31
  26. package/dist/EngineQueryClient.js.map +1 -1
  27. package/dist/EngineWebClient.cjs +0 -2
  28. package/dist/EngineWebClient.cjs.map +1 -1
  29. package/dist/EngineWebClient.d.cts +1 -2
  30. package/dist/EngineWebClient.d.ts +1 -2
  31. package/dist/EngineWebClient.js +0 -2
  32. package/dist/EngineWebClient.js.map +1 -1
  33. package/dist/endpoints.cjs +6 -6
  34. package/dist/endpoints.cjs.map +1 -1
  35. package/dist/endpoints.d.cts +1 -1
  36. package/dist/endpoints.d.ts +1 -1
  37. package/dist/endpoints.js +6 -6
  38. package/dist/endpoints.js.map +1 -1
  39. package/dist/index.cjs +4 -4
  40. package/dist/index.cjs.map +1 -1
  41. package/dist/index.d.cts +11 -12
  42. package/dist/index.d.ts +11 -12
  43. package/dist/index.js +2 -2
  44. package/dist/index.js.map +1 -1
  45. package/dist/types/EngineServerFailureError.cjs +1 -1
  46. package/dist/types/EngineServerFailureError.cjs.map +1 -1
  47. package/dist/types/EngineServerFailureError.d.cts +2 -2
  48. package/dist/types/EngineServerFailureError.d.ts +2 -2
  49. package/dist/types/EngineServerFailureError.js +1 -1
  50. package/dist/types/EngineServerFailureError.js.map +1 -1
  51. package/dist/types/clientExecuteTypes.cjs.map +1 -1
  52. package/dist/types/clientExecuteTypes.d.cts +11 -12
  53. package/dist/types/clientExecuteTypes.d.ts +11 -12
  54. package/dist/types/clientQueryTypes.cjs.map +1 -1
  55. package/dist/types/clientQueryTypes.d.cts +41 -23
  56. package/dist/types/clientQueryTypes.d.ts +41 -23
  57. package/dist/types/index.cjs +10 -10
  58. package/dist/types/index.cjs.map +1 -1
  59. package/dist/types/index.d.cts +8 -9
  60. package/dist/types/index.d.ts +8 -9
  61. package/dist/types/index.js +5 -5
  62. package/dist/types/index.js.map +1 -1
  63. package/dist/types/serverExecuteTypes.cjs.map +1 -1
  64. package/dist/types/serverExecuteTypes.d.cts +13 -14
  65. package/dist/types/serverExecuteTypes.d.ts +13 -14
  66. package/dist/types/serverQueryModelTypes.cjs.map +1 -1
  67. package/dist/types/serverQueryModelTypes.d.cts +25 -2
  68. package/dist/types/serverQueryModelTypes.d.ts +25 -2
  69. package/dist/types/serverQueryTypes.cjs.map +1 -1
  70. package/dist/types/serverQueryTypes.d.cts +37 -26
  71. package/dist/types/serverQueryTypes.d.ts +37 -26
  72. package/dist/types/serverSubscriptionEventTypes.cjs.map +1 -1
  73. package/dist/types/serverSubscriptionEventTypes.d.cts +87 -23
  74. package/dist/types/serverSubscriptionEventTypes.d.ts +87 -23
  75. package/dist/types/serverSubscriptionTypes.cjs.map +1 -1
  76. package/dist/types/serverSubscriptionTypes.d.cts +21 -4
  77. package/dist/types/serverSubscriptionTypes.d.ts +21 -4
  78. package/dist/utils/index.d.cts +3 -4
  79. package/dist/utils/index.d.ts +3 -4
  80. package/dist/utils/productEngineTypeMappers.cjs +5 -5
  81. package/dist/utils/productEngineTypeMappers.cjs.map +1 -1
  82. package/dist/utils/productEngineTypeMappers.d.cts +1 -1
  83. package/dist/utils/productEngineTypeMappers.d.ts +1 -1
  84. package/dist/utils/productEngineTypeMappers.js +1 -1
  85. package/dist/utils/productEngineTypeMappers.js.map +1 -1
  86. package/dist/utils/queryDataMappers.cjs +113 -90
  87. package/dist/utils/queryDataMappers.cjs.map +1 -1
  88. package/dist/utils/queryDataMappers.d.cts +6 -5
  89. package/dist/utils/queryDataMappers.d.ts +6 -5
  90. package/dist/utils/queryDataMappers.js +39 -19
  91. package/dist/utils/queryDataMappers.js.map +1 -1
  92. package/package.json +3 -4
  93. package/src/EngineBaseClient.ts +5 -3
  94. package/src/EngineExecuteBuilder.ts +16 -53
  95. package/src/EngineExecuteClient.ts +5 -13
  96. package/src/EngineQueryClient.ts +62 -32
  97. package/src/EngineWebClient.ts +0 -2
  98. package/src/endpoints.ts +4 -5
  99. package/src/index.ts +2 -2
  100. package/src/types/EngineServerFailureError.ts +2 -2
  101. package/src/types/clientExecuteTypes.ts +12 -17
  102. package/src/types/clientQueryTypes.ts +57 -32
  103. package/src/types/index.ts +5 -5
  104. package/src/types/serverExecuteTypes.ts +14 -18
  105. package/src/types/serverQueryModelTypes.ts +26 -1
  106. package/src/types/serverQueryTypes.ts +46 -29
  107. package/src/types/serverSubscriptionEventTypes.ts +131 -21
  108. package/src/types/serverSubscriptionTypes.ts +23 -3
  109. package/src/utils/productEngineTypeMappers.ts +1 -1
  110. package/src/utils/queryDataMappers.ts +50 -20
@@ -1,22 +1,48 @@
1
1
  import { EngineServerPriceTickLiquidity } from './serverQueryTypes';
2
2
 
3
3
  /**
4
- * Event from subscribing to a `order_update` stream.
4
+ * Reasons that can trigger position change events.
5
5
  */
6
- export interface EngineServerSubscriptionOrderUpdateEvent {
7
- timestamp: string;
6
+ export type PositionChangeReason =
7
+ | 'deposit_collateral'
8
+ | 'match_orders'
9
+ | 'withdraw_collateral'
10
+ | 'transfer_quote'
11
+ | 'settle_pnl'
12
+ | 'mint_nlp'
13
+ | 'burn_nlp'
14
+ | 'liquidate_subaccount';
15
+
16
+ /**
17
+ * Possible reasons for order updates.
18
+ */
19
+ export type OrderUpdateReason = 'cancelled' | 'filled' | 'placed';
20
+
21
+ export type EngineServerSubscriptionEventType =
22
+ | 'trade'
23
+ | 'best_bid_offer'
24
+ | 'book_depth'
25
+ | 'fill'
26
+ | 'position_change'
27
+ | 'order_update'
28
+ | 'liquidation'
29
+ | 'latest_candlestick'
30
+ | 'funding_payment';
31
+
32
+ export interface EngineServerSubscriptionBaseEvent<
33
+ T extends
34
+ EngineServerSubscriptionEventType = EngineServerSubscriptionEventType,
35
+ > {
36
+ type: T;
8
37
  product_id: number;
9
- digest: string;
10
- amount: string;
11
- reason: string;
12
38
  }
13
39
 
14
40
  /**
15
41
  * Event from subscribing to a `trade` stream.
16
42
  */
17
- export interface EngineServerSubscriptionTradeEvent {
43
+ export interface EngineServerSubscriptionTradeEvent
44
+ extends EngineServerSubscriptionBaseEvent<'trade'> {
18
45
  timestamp: string;
19
- product_id: number;
20
46
  price: string;
21
47
  taker_qty: string;
22
48
  maker_qty: string;
@@ -26,49 +52,133 @@ export interface EngineServerSubscriptionTradeEvent {
26
52
  /**
27
53
  * Event from subscribing to a `best_bid_offer` stream.
28
54
  */
29
- export interface EngineServerSubscriptionBestBidOfferEvent {
55
+ export interface EngineServerSubscriptionBestBidOfferEvent
56
+ extends EngineServerSubscriptionBaseEvent<'best_bid_offer'> {
30
57
  timestamp: string;
31
- product_id: number;
32
58
  bid_price: string;
33
59
  bid_qty: string;
34
60
  ask_price: string;
35
61
  ask_qty: string;
36
62
  }
37
63
 
64
+ /**
65
+ * Event from subscribing to a `book_depth` stream.
66
+ */
67
+ export interface EngineServerSubscriptionBookDepthEvent
68
+ extends EngineServerSubscriptionBaseEvent<'book_depth'> {
69
+ last_max_timestamp: string;
70
+ min_timestamp: string;
71
+ max_timestamp: string;
72
+ bids: EngineServerPriceTickLiquidity[];
73
+ asks: EngineServerPriceTickLiquidity[];
74
+ }
75
+
38
76
  /**
39
77
  * Event from subscribing to a `fill` stream.
40
78
  */
41
- export interface EngineServerSubscriptionFillEvent {
79
+ export interface EngineServerSubscriptionFillEvent
80
+ extends EngineServerSubscriptionBaseEvent<'fill'> {
42
81
  // NOTE: `id` is excluded from the response to avoid parsing issues.
43
82
  // type of `id` on the backend is `u64` which can overflow until we introduce proper parsing on the SDK.
44
83
  timestamp: string;
45
- product_id: number;
46
84
  subaccount: string;
47
85
  order_digest: string;
48
86
  filled_qty: string;
49
87
  remaining_qty: string;
88
+ original_qty: string;
50
89
  price: string;
51
90
  is_taker: boolean;
91
+ is_bid: boolean;
92
+ fee: string;
93
+ submission_idx: string;
94
+ appendix: string;
52
95
  }
53
96
 
54
97
  /**
55
98
  * Event from subscribing to a `position_change` stream.
56
99
  */
57
- export interface EngineServerSubscriptionPositionChangeEvent {
100
+ export interface EngineServerSubscriptionPositionChangeEvent
101
+ extends EngineServerSubscriptionBaseEvent<'position_change'> {
58
102
  timestamp: string;
59
- product_id: number;
60
103
  subaccount: string;
61
104
  amount: string;
105
+ /** Zero for everything except perps */
62
106
  v_quote_amount: string;
107
+ reason: PositionChangeReason;
108
+ /**
109
+ * True if the position change was for an isolated position
110
+ */
111
+ isolated: boolean;
63
112
  }
64
113
 
65
114
  /**
66
- * Event from subscribing to a `book_depth` stream.
115
+ * Event from subscribing to an `order_update` stream.
67
116
  */
68
- export interface EngineServerSubscriptionBookDepthEvent {
69
- min_timestamp: string;
70
- max_timestamp: string;
71
- product_id: number;
72
- bids: EngineServerPriceTickLiquidity[];
73
- asks: EngineServerPriceTickLiquidity[];
117
+ export interface EngineServerSubscriptionOrderUpdateEvent
118
+ extends EngineServerSubscriptionBaseEvent<'order_update'> {
119
+ timestamp: string;
120
+ digest: string;
121
+ amount: string;
122
+ reason: OrderUpdateReason;
74
123
  }
124
+
125
+ /**
126
+ * Event from subscribing to a `liquidation` stream.
127
+ */
128
+ export interface EngineServerSubscriptionLiquidationEvent
129
+ extends EngineServerSubscriptionBaseEvent<'liquidation'> {
130
+ timestamp: string;
131
+ /** Single element for regular liquidations, two elements for spread liquidations [spotId, perpId] */
132
+ product_ids: number[];
133
+ liquidator: string;
134
+ liquidatee: string;
135
+ /** Amount liquidated (positive for long, negative for short) */
136
+ amount: string;
137
+ /** Price at which liquidation occurred */
138
+ price: string;
139
+ }
140
+
141
+ /**
142
+ * Event from subscribing to a `latest_candlestick` stream.
143
+ */
144
+ export interface EngineServerSubscriptionLatestCandlestickEvent
145
+ extends EngineServerSubscriptionBaseEvent<'latest_candlestick'> {
146
+ timestamp: string;
147
+ granularity: number;
148
+ open_x18: string;
149
+ high_x18: string;
150
+ low_x18: string;
151
+ close_x18: string;
152
+ volume: string;
153
+ }
154
+
155
+ /**
156
+ * Event from subscribing to a `funding_payment` stream.
157
+ */
158
+ export interface EngineServerSubscriptionFundingPaymentEvent
159
+ extends EngineServerSubscriptionBaseEvent<'funding_payment'> {
160
+ timestamp: string;
161
+ /** Funding payment amount (positive = receive, negative = pay) */
162
+ payment_amount: string;
163
+ /** Open interest at time of funding */
164
+ open_interest: string;
165
+ /** Current cumulative funding values */
166
+ cumulative_funding_long_x18: string;
167
+ cumulative_funding_short_x18: string;
168
+ /** Time delta over which the funding payment was calculated */
169
+ dt: string;
170
+ }
171
+
172
+ /**
173
+ * Union type for all engine server subscription events.
174
+ */
175
+ export type EngineServerSubscriptionEvent =
176
+ | EngineServerSubscriptionTradeEvent
177
+ | EngineServerSubscriptionBestBidOfferEvent
178
+ | EngineServerSubscriptionBookDepthEvent
179
+ | EngineServerSubscriptionFillEvent
180
+ | EngineServerSubscriptionPositionChangeEvent
181
+ | EngineServerSubscriptionOrderUpdateEvent
182
+ | EngineServerSubscriptionLiquidationEvent
183
+ | EngineServerSubscriptionLatestCandlestickEvent
184
+ | EngineServerSubscriptionFundingPaymentEvent;
@@ -1,5 +1,6 @@
1
1
  export interface EngineServerOrderUpdateStreamParams {
2
- product_id: number;
2
+ /** when not provided, subscribes to all products */
3
+ product_id?: number;
3
4
  subaccount: string;
4
5
  }
5
6
 
@@ -12,12 +13,14 @@ export interface EngineServerBestBidOfferStreamParams {
12
13
  }
13
14
 
14
15
  export interface EngineServerFillStreamParams {
15
- product_id: number;
16
+ /** when not provided, subscribes to all products */
17
+ product_id?: number;
16
18
  subaccount: string;
17
19
  }
18
20
 
19
21
  export interface EngineServerPositionChangeStreamParams {
20
- product_id: number;
22
+ /** when not provided, subscribes to all products */
23
+ product_id?: number;
21
24
  subaccount: string;
22
25
  }
23
26
 
@@ -25,6 +28,20 @@ export interface EngineServerBookDepthStreamParams {
25
28
  product_id: number;
26
29
  }
27
30
 
31
+ export interface EngineServerLatestCandlestickStreamParams {
32
+ product_id: number;
33
+ granularity: number;
34
+ }
35
+
36
+ export interface EngineServerLiquidationStreamParams {
37
+ /** when not provided, subscribes to all products */
38
+ product_id?: number;
39
+ }
40
+
41
+ export interface EngineServerFundingPaymentStreamParams {
42
+ product_id: number;
43
+ }
44
+
28
45
  /**
29
46
  * Available subscription streams
30
47
  */
@@ -35,6 +52,9 @@ export interface EngineServerSubscriptionStreamParamsByType {
35
52
  fill: EngineServerFillStreamParams;
36
53
  position_change: EngineServerPositionChangeStreamParams;
37
54
  book_depth: EngineServerBookDepthStreamParams;
55
+ liquidation: EngineServerLiquidationStreamParams;
56
+ latest_candlestick: EngineServerLatestCandlestickStreamParams;
57
+ funding_payment: EngineServerFundingPaymentStreamParams;
38
58
  }
39
59
 
40
60
  export type EngineServerSubscriptionStreamParamsType =
@@ -1,4 +1,4 @@
1
- import { ProductEngineType } from '@nadohq/contracts';
1
+ import { ProductEngineType } from '@nadohq/shared';
2
2
  import { EngineServerProductType } from '../types';
3
3
 
4
4
  export function mapProductEngineType(
@@ -2,24 +2,24 @@ import {
2
2
  BalanceHealthContributions,
3
3
  calcTotalBorrowed,
4
4
  calcTotalDeposited,
5
- OrderExpirationType,
5
+ mapValues,
6
6
  PerpMarket,
7
7
  ProductEngineType,
8
+ removeDecimals,
8
9
  SpotMarket,
9
10
  subaccountFromHex,
10
- } from '@nadohq/contracts';
11
- import {
12
- mapValues,
13
- removeDecimals,
14
11
  toBigDecimal,
15
- toIntegerString,
16
- } from '@nadohq/utils';
12
+ unpackOrderAppendix,
13
+ } from '@nadohq/shared';
17
14
  import {
18
15
  EngineMarketPrice,
16
+ EngineNlpLockedBalance,
19
17
  EngineOrder,
20
18
  EnginePriceTickLiquidity,
21
19
  EngineServerIsolatedPositionsResponse,
22
20
  EngineServerMarketPrice,
21
+ EngineServerNlpLockedBalancesResponse,
22
+ EngineServerNlpPoolInfoResponse,
23
23
  EngineServerOrderResponse,
24
24
  EngineServerPerpProduct,
25
25
  EngineServerPriceTickLiquidity,
@@ -30,6 +30,8 @@ import {
30
30
  EngineSymbol,
31
31
  EngineSymbolsResponse,
32
32
  GetEngineIsolatedPositionsResponse,
33
+ GetEngineNlpLockedBalancesResponse,
34
+ GetEngineNlpPoolInfoResponse,
33
35
  GetEngineSubaccountSummaryResponse,
34
36
  } from '../types';
35
37
  import { mapEngineServerProductType } from './productEngineTypeMappers';
@@ -49,7 +51,7 @@ export function mapEngineServerOrder(
49
51
  const subaccount = subaccountFromHex(order.sender);
50
52
  return {
51
53
  digest: order.digest,
52
- expiration: toBigDecimal(order.expiration),
54
+ expiration: Number(order.expiration),
53
55
  nonce: order.nonce,
54
56
  price: removeDecimals(order.price_x18),
55
57
  productId: order.product_id,
@@ -57,19 +59,8 @@ export function mapEngineServerOrder(
57
59
  subaccountName: subaccount.subaccountName,
58
60
  totalAmount: toBigDecimal(order.amount),
59
61
  unfilledAmount: toBigDecimal(order.unfilled_amount),
60
- margin: order.margin ? toBigDecimal(order.margin) : null,
61
- // Standardizes from hex
62
- // toFixed is required as toString gives values with `e`
63
- orderParams: {
64
- amount: toIntegerString(order.amount),
65
- expiration: toIntegerString(order.expiration),
66
- nonce: order.nonce,
67
- price: toIntegerString(removeDecimals(order.price_x18)),
68
- subaccountOwner: subaccount.subaccountOwner,
69
- subaccountName: subaccount.subaccountName,
70
- },
71
62
  placementTime: order.placed_at,
72
- orderType: order.order_type as OrderExpirationType,
63
+ appendix: unpackOrderAppendix(order.appendix),
73
64
  };
74
65
  }
75
66
 
@@ -100,6 +91,7 @@ export function mapEngineServerSpotProduct(
100
91
  ),
101
92
  interestLargeCap: removeDecimals(product.config.interest_large_cap_x18),
102
93
  interestSmallCap: removeDecimals(product.config.interest_small_cap_x18),
94
+ minDepositRate: removeDecimals(product.config.min_deposit_rate_x18),
103
95
  longWeightInitial: removeDecimals(product.risk.long_weight_initial_x18),
104
96
  longWeightMaintenance: removeDecimals(
105
97
  product.risk.long_weight_maintenance_x18,
@@ -301,3 +293,41 @@ export function mapEngineMarketPrice(
301
293
  productId: baseResponse.product_id,
302
294
  };
303
295
  }
296
+
297
+ export function mapEngineServerNlpLockedBalances(
298
+ baseResponse: EngineServerNlpLockedBalancesResponse,
299
+ ): GetEngineNlpLockedBalancesResponse {
300
+ const lockedBalances: EngineNlpLockedBalance[] =
301
+ baseResponse.locked_balances.map((lockedBalance) => ({
302
+ productId: lockedBalance.balance.product_id,
303
+ balance: toBigDecimal(lockedBalance.balance.balance.amount),
304
+ unlockedAt: lockedBalance.unlocked_at,
305
+ }));
306
+
307
+ return {
308
+ lockedBalances,
309
+ balanceLocked: {
310
+ productId: baseResponse.balance_locked.product_id,
311
+ balance: toBigDecimal(baseResponse.balance_locked.balance.amount),
312
+ },
313
+ balanceUnlocked: {
314
+ productId: baseResponse.balance_unlocked.product_id,
315
+ balance: toBigDecimal(baseResponse.balance_unlocked.balance.amount),
316
+ },
317
+ };
318
+ }
319
+
320
+ export function mapEngineServerNlpPoolInfo(
321
+ baseResponse: EngineServerNlpPoolInfoResponse,
322
+ ): GetEngineNlpPoolInfoResponse {
323
+ return {
324
+ nlpPools: baseResponse.nlp_pools.map((pool) => ({
325
+ poolId: pool.pool_id,
326
+ subaccountHex: pool.subaccount,
327
+ ownerAddress: pool.owner,
328
+ balanceWeight: removeDecimals(pool.balance_weight_x18),
329
+ subaccountInfo: mapSubaccountSummary(pool.subaccount_info),
330
+ openOrders: pool.open_orders.map(mapEngineServerOrder),
331
+ })),
332
+ };
333
+ }