@nadohq/engine-client 0.1.0-alpha.1

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 (142) hide show
  1. package/README.md +2 -0
  2. package/dist/EngineBaseClient.cjs +166 -0
  3. package/dist/EngineBaseClient.cjs.map +1 -0
  4. package/dist/EngineBaseClient.d.cts +75 -0
  5. package/dist/EngineBaseClient.d.ts +75 -0
  6. package/dist/EngineBaseClient.js +133 -0
  7. package/dist/EngineBaseClient.js.map +1 -0
  8. package/dist/EngineClient.cjs +40 -0
  9. package/dist/EngineClient.cjs.map +1 -0
  10. package/dist/EngineClient.d.cts +23 -0
  11. package/dist/EngineClient.d.ts +23 -0
  12. package/dist/EngineClient.js +15 -0
  13. package/dist/EngineClient.js.map +1 -0
  14. package/dist/EngineExecuteBuilder.cjs +304 -0
  15. package/dist/EngineExecuteBuilder.cjs.map +1 -0
  16. package/dist/EngineExecuteBuilder.d.cts +118 -0
  17. package/dist/EngineExecuteBuilder.d.ts +118 -0
  18. package/dist/EngineExecuteBuilder.js +282 -0
  19. package/dist/EngineExecuteBuilder.js.map +1 -0
  20. package/dist/EngineExecuteClient.cjs +114 -0
  21. package/dist/EngineExecuteClient.cjs.map +1 -0
  22. package/dist/EngineExecuteClient.d.cts +28 -0
  23. package/dist/EngineExecuteClient.d.ts +28 -0
  24. package/dist/EngineExecuteClient.js +89 -0
  25. package/dist/EngineExecuteClient.js.map +1 -0
  26. package/dist/EngineQueryClient.cjs +431 -0
  27. package/dist/EngineQueryClient.cjs.map +1 -0
  28. package/dist/EngineQueryClient.d.cts +143 -0
  29. package/dist/EngineQueryClient.d.ts +143 -0
  30. package/dist/EngineQueryClient.js +424 -0
  31. package/dist/EngineQueryClient.js.map +1 -0
  32. package/dist/EngineWebClient.cjs +75 -0
  33. package/dist/EngineWebClient.cjs.map +1 -0
  34. package/dist/EngineWebClient.d.cts +31 -0
  35. package/dist/EngineWebClient.d.ts +31 -0
  36. package/dist/EngineWebClient.js +50 -0
  37. package/dist/EngineWebClient.js.map +1 -0
  38. package/dist/endpoints.cjs +49 -0
  39. package/dist/endpoints.cjs.map +1 -0
  40. package/dist/endpoints.d.cts +7 -0
  41. package/dist/endpoints.d.ts +7 -0
  42. package/dist/endpoints.js +22 -0
  43. package/dist/endpoints.js.map +1 -0
  44. package/dist/index.cjs +31 -0
  45. package/dist/index.cjs.map +1 -0
  46. package/dist/index.d.cts +20 -0
  47. package/dist/index.d.ts +20 -0
  48. package/dist/index.js +6 -0
  49. package/dist/index.js.map +1 -0
  50. package/dist/types/EngineServerFailureError.cjs +36 -0
  51. package/dist/types/EngineServerFailureError.cjs.map +1 -0
  52. package/dist/types/EngineServerFailureError.d.cts +11 -0
  53. package/dist/types/EngineServerFailureError.d.ts +11 -0
  54. package/dist/types/EngineServerFailureError.js +11 -0
  55. package/dist/types/EngineServerFailureError.js.map +1 -0
  56. package/dist/types/clientExecuteTypes.cjs +19 -0
  57. package/dist/types/clientExecuteTypes.cjs.map +1 -0
  58. package/dist/types/clientExecuteTypes.d.cts +74 -0
  59. package/dist/types/clientExecuteTypes.d.ts +74 -0
  60. package/dist/types/clientExecuteTypes.js +1 -0
  61. package/dist/types/clientExecuteTypes.js.map +1 -0
  62. package/dist/types/clientQueryTypes.cjs +19 -0
  63. package/dist/types/clientQueryTypes.cjs.map +1 -0
  64. package/dist/types/clientQueryTypes.d.cts +189 -0
  65. package/dist/types/clientQueryTypes.d.ts +189 -0
  66. package/dist/types/clientQueryTypes.js +1 -0
  67. package/dist/types/clientQueryTypes.js.map +1 -0
  68. package/dist/types/index.cjs +39 -0
  69. package/dist/types/index.cjs.map +1 -0
  70. package/dist/types/index.d.cts +10 -0
  71. package/dist/types/index.d.ts +10 -0
  72. package/dist/types/index.js +10 -0
  73. package/dist/types/index.js.map +1 -0
  74. package/dist/types/serverExecuteTypes.cjs +19 -0
  75. package/dist/types/serverExecuteTypes.cjs.map +1 -0
  76. package/dist/types/serverExecuteTypes.d.cts +91 -0
  77. package/dist/types/serverExecuteTypes.d.ts +91 -0
  78. package/dist/types/serverExecuteTypes.js +1 -0
  79. package/dist/types/serverExecuteTypes.js.map +1 -0
  80. package/dist/types/serverQueryModelTypes.cjs +19 -0
  81. package/dist/types/serverQueryModelTypes.cjs.map +1 -0
  82. package/dist/types/serverQueryModelTypes.d.cts +73 -0
  83. package/dist/types/serverQueryModelTypes.d.ts +73 -0
  84. package/dist/types/serverQueryModelTypes.js +1 -0
  85. package/dist/types/serverQueryModelTypes.js.map +1 -0
  86. package/dist/types/serverQueryTypes.cjs +19 -0
  87. package/dist/types/serverQueryTypes.cjs.map +1 -0
  88. package/dist/types/serverQueryTypes.d.cts +292 -0
  89. package/dist/types/serverQueryTypes.d.ts +292 -0
  90. package/dist/types/serverQueryTypes.js +1 -0
  91. package/dist/types/serverQueryTypes.js.map +1 -0
  92. package/dist/types/serverSubscriptionEventTypes.cjs +19 -0
  93. package/dist/types/serverSubscriptionEventTypes.cjs.map +1 -0
  94. package/dist/types/serverSubscriptionEventTypes.d.cts +71 -0
  95. package/dist/types/serverSubscriptionEventTypes.d.ts +71 -0
  96. package/dist/types/serverSubscriptionEventTypes.js +1 -0
  97. package/dist/types/serverSubscriptionEventTypes.js.map +1 -0
  98. package/dist/types/serverSubscriptionTypes.cjs +19 -0
  99. package/dist/types/serverSubscriptionTypes.cjs.map +1 -0
  100. package/dist/types/serverSubscriptionTypes.d.cts +63 -0
  101. package/dist/types/serverSubscriptionTypes.d.ts +63 -0
  102. package/dist/types/serverSubscriptionTypes.js +1 -0
  103. package/dist/types/serverSubscriptionTypes.js.map +1 -0
  104. package/dist/utils/index.cjs +25 -0
  105. package/dist/utils/index.cjs.map +1 -0
  106. package/dist/utils/index.d.cts +6 -0
  107. package/dist/utils/index.d.ts +6 -0
  108. package/dist/utils/index.js +3 -0
  109. package/dist/utils/index.js.map +1 -0
  110. package/dist/utils/productEngineTypeMappers.cjs +49 -0
  111. package/dist/utils/productEngineTypeMappers.cjs.map +1 -0
  112. package/dist/utils/productEngineTypeMappers.d.cts +7 -0
  113. package/dist/utils/productEngineTypeMappers.d.ts +7 -0
  114. package/dist/utils/productEngineTypeMappers.js +23 -0
  115. package/dist/utils/productEngineTypeMappers.js.map +1 -0
  116. package/dist/utils/queryDataMappers.cjs +282 -0
  117. package/dist/utils/queryDataMappers.cjs.map +1 -0
  118. package/dist/utils/queryDataMappers.d.cts +18 -0
  119. package/dist/utils/queryDataMappers.d.ts +18 -0
  120. package/dist/utils/queryDataMappers.js +258 -0
  121. package/dist/utils/queryDataMappers.js.map +1 -0
  122. package/package.json +52 -0
  123. package/src/EngineBaseClient.ts +223 -0
  124. package/src/EngineClient.ts +13 -0
  125. package/src/EngineExecuteBuilder.ts +381 -0
  126. package/src/EngineExecuteClient.ts +122 -0
  127. package/src/EngineQueryClient.ts +553 -0
  128. package/src/EngineWebClient.ts +72 -0
  129. package/src/endpoints.ts +21 -0
  130. package/src/index.ts +4 -0
  131. package/src/types/EngineServerFailureError.ts +12 -0
  132. package/src/types/clientExecuteTypes.ts +118 -0
  133. package/src/types/clientQueryTypes.ts +267 -0
  134. package/src/types/index.ts +8 -0
  135. package/src/types/serverExecuteTypes.ts +138 -0
  136. package/src/types/serverQueryModelTypes.ts +83 -0
  137. package/src/types/serverQueryTypes.ts +382 -0
  138. package/src/types/serverSubscriptionEventTypes.ts +74 -0
  139. package/src/types/serverSubscriptionTypes.ts +79 -0
  140. package/src/utils/index.ts +1 -0
  141. package/src/utils/productEngineTypeMappers.ts +24 -0
  142. package/src/utils/queryDataMappers.ts +303 -0
@@ -0,0 +1,303 @@
1
+ import {
2
+ BalanceHealthContributions,
3
+ calcTotalBorrowed,
4
+ calcTotalDeposited,
5
+ OrderExpirationType,
6
+ PerpMarket,
7
+ ProductEngineType,
8
+ SpotMarket,
9
+ subaccountFromHex,
10
+ } from '@nadohq/contracts';
11
+ import {
12
+ mapValues,
13
+ removeDecimals,
14
+ toBigDecimal,
15
+ toIntegerString,
16
+ } from '@nadohq/utils';
17
+ import {
18
+ EngineMarketPrice,
19
+ EngineOrder,
20
+ EnginePriceTickLiquidity,
21
+ EngineServerIsolatedPositionsResponse,
22
+ EngineServerMarketPrice,
23
+ EngineServerOrderResponse,
24
+ EngineServerPerpProduct,
25
+ EngineServerPriceTickLiquidity,
26
+ EngineServerSpotProduct,
27
+ EngineServerSubaccountInfoResponse,
28
+ EngineServerSymbol,
29
+ EngineServerSymbolsResponse,
30
+ EngineSymbol,
31
+ EngineSymbolsResponse,
32
+ GetEngineIsolatedPositionsResponse,
33
+ GetEngineSubaccountSummaryResponse,
34
+ } from '../types';
35
+ import { mapEngineServerProductType } from './productEngineTypeMappers';
36
+
37
+ export function mapEngineServerTickLiquidity(
38
+ tick: EngineServerPriceTickLiquidity,
39
+ ): EnginePriceTickLiquidity {
40
+ return {
41
+ price: removeDecimals(tick[0]),
42
+ liquidity: toBigDecimal(tick[1]),
43
+ };
44
+ }
45
+
46
+ export function mapEngineServerOrder(
47
+ order: EngineServerOrderResponse,
48
+ ): EngineOrder {
49
+ const subaccount = subaccountFromHex(order.sender);
50
+ return {
51
+ digest: order.digest,
52
+ expiration: toBigDecimal(order.expiration),
53
+ nonce: order.nonce,
54
+ price: removeDecimals(order.price_x18),
55
+ productId: order.product_id,
56
+ subaccountOwner: subaccount.subaccountOwner,
57
+ subaccountName: subaccount.subaccountName,
58
+ totalAmount: toBigDecimal(order.amount),
59
+ 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
+ placementTime: order.placed_at,
72
+ orderType: order.order_type as OrderExpirationType,
73
+ };
74
+ }
75
+
76
+ export function mapEngineServerSpotProduct(
77
+ product: EngineServerSpotProduct,
78
+ ): SpotMarket {
79
+ return {
80
+ type: ProductEngineType.SPOT,
81
+ productId: product.product_id,
82
+ minSize: toBigDecimal(product.book_info.min_size),
83
+ priceIncrement: removeDecimals(product.book_info.price_increment_x18),
84
+ sizeIncrement: toBigDecimal(product.book_info.size_increment),
85
+ product: {
86
+ productId: product.product_id,
87
+ type: ProductEngineType.SPOT,
88
+ totalBorrowed: calcTotalBorrowed(
89
+ product.state.total_borrows_normalized,
90
+ product.state.cumulative_borrows_multiplier_x18,
91
+ ),
92
+ totalDeposited: calcTotalDeposited(
93
+ product.state.total_deposits_normalized,
94
+ product.state.cumulative_deposits_multiplier_x18,
95
+ ),
96
+ oraclePrice: removeDecimals(product.oracle_price_x18),
97
+ interestFloor: removeDecimals(product.config.interest_floor_x18),
98
+ interestInflectionUtil: removeDecimals(
99
+ product.config.interest_inflection_util_x18,
100
+ ),
101
+ interestLargeCap: removeDecimals(product.config.interest_large_cap_x18),
102
+ interestSmallCap: removeDecimals(product.config.interest_small_cap_x18),
103
+ longWeightInitial: removeDecimals(product.risk.long_weight_initial_x18),
104
+ longWeightMaintenance: removeDecimals(
105
+ product.risk.long_weight_maintenance_x18,
106
+ ),
107
+ shortWeightInitial: removeDecimals(product.risk.short_weight_initial_x18),
108
+ shortWeightMaintenance: removeDecimals(
109
+ product.risk.short_weight_maintenance_x18,
110
+ ),
111
+ tokenAddr: product.config.token,
112
+ },
113
+ };
114
+ }
115
+
116
+ export function mapEngineServerPerpProduct(
117
+ product: EngineServerPerpProduct,
118
+ ): PerpMarket {
119
+ return {
120
+ type: ProductEngineType.PERP,
121
+ productId: product.product_id,
122
+ minSize: toBigDecimal(product.book_info.min_size),
123
+ priceIncrement: removeDecimals(product.book_info.price_increment_x18),
124
+ sizeIncrement: toBigDecimal(product.book_info.size_increment),
125
+ product: {
126
+ productId: product.product_id,
127
+ type: ProductEngineType.PERP,
128
+ oraclePrice: removeDecimals(product.oracle_price_x18),
129
+ longWeightInitial: removeDecimals(product.risk.long_weight_initial_x18),
130
+ longWeightMaintenance: removeDecimals(
131
+ product.risk.long_weight_maintenance_x18,
132
+ ),
133
+ shortWeightInitial: removeDecimals(product.risk.short_weight_initial_x18),
134
+ shortWeightMaintenance: removeDecimals(
135
+ product.risk.short_weight_maintenance_x18,
136
+ ),
137
+ openInterest: toBigDecimal(product.state.open_interest),
138
+ cumulativeFundingLong: removeDecimals(
139
+ product.state.cumulative_funding_long_x18,
140
+ ),
141
+ cumulativeFundingShort: removeDecimals(
142
+ product.state.cumulative_funding_short_x18,
143
+ ),
144
+ },
145
+ };
146
+ }
147
+
148
+ export function mapEngineServerBalanceHealthContributions(
149
+ healthContributionsForBalance: string[],
150
+ ): BalanceHealthContributions {
151
+ return {
152
+ initial: toBigDecimal(healthContributionsForBalance[0]),
153
+ maintenance: toBigDecimal(healthContributionsForBalance[1]),
154
+ unweighted: toBigDecimal(healthContributionsForBalance[2]),
155
+ };
156
+ }
157
+
158
+ export function mapSubaccountSummary(
159
+ baseResponse: EngineServerSubaccountInfoResponse,
160
+ ): GetEngineSubaccountSummaryResponse {
161
+ const balances: GetEngineSubaccountSummaryResponse['balances'] = [];
162
+
163
+ baseResponse.spot_balances.forEach((spotBalance) => {
164
+ const product = baseResponse.spot_products.find(
165
+ (product) => product.product_id === spotBalance.product_id,
166
+ );
167
+ if (!product) {
168
+ throw Error(`Could not find product ${spotBalance.product_id}`);
169
+ }
170
+
171
+ balances.push({
172
+ amount: toBigDecimal(spotBalance.balance.amount),
173
+ healthContributions: mapEngineServerBalanceHealthContributions(
174
+ baseResponse.health_contributions[spotBalance.product_id],
175
+ ),
176
+ ...mapEngineServerSpotProduct(product).product,
177
+ });
178
+ });
179
+
180
+ baseResponse.perp_balances.forEach((perpBalance) => {
181
+ const product = baseResponse.perp_products.find(
182
+ (product) => product.product_id === perpBalance.product_id,
183
+ );
184
+ if (!product) {
185
+ throw Error(`Could not find product ${perpBalance.product_id}`);
186
+ }
187
+
188
+ balances.push({
189
+ amount: toBigDecimal(perpBalance.balance.amount),
190
+ vQuoteBalance: toBigDecimal(perpBalance.balance.v_quote_balance),
191
+ healthContributions: mapEngineServerBalanceHealthContributions(
192
+ baseResponse.health_contributions[perpBalance.product_id],
193
+ ),
194
+ ...mapEngineServerPerpProduct(product).product,
195
+ });
196
+ });
197
+
198
+ return {
199
+ balances: balances,
200
+ exists: baseResponse.exists,
201
+ health: {
202
+ initial: {
203
+ health: toBigDecimal(baseResponse.healths[0].health),
204
+ assets: toBigDecimal(baseResponse.healths[0].assets),
205
+ liabilities: toBigDecimal(baseResponse.healths[0].liabilities),
206
+ },
207
+ maintenance: {
208
+ health: toBigDecimal(baseResponse.healths[1].health),
209
+ assets: toBigDecimal(baseResponse.healths[1].assets),
210
+ liabilities: toBigDecimal(baseResponse.healths[1].liabilities),
211
+ },
212
+ unweighted: {
213
+ health: toBigDecimal(baseResponse.healths[2].health),
214
+ assets: toBigDecimal(baseResponse.healths[2].assets),
215
+ liabilities: toBigDecimal(baseResponse.healths[2].liabilities),
216
+ },
217
+ },
218
+ };
219
+ }
220
+
221
+ export function mapEngineServerIsolatedPositions(
222
+ baseResponse: EngineServerIsolatedPositionsResponse,
223
+ ): GetEngineIsolatedPositionsResponse {
224
+ return baseResponse.isolated_positions.map((position) => {
225
+ const perpBalance = position.base_balance;
226
+ const quoteBalance = position.quote_balance;
227
+
228
+ return {
229
+ subaccount: subaccountFromHex(position.subaccount),
230
+ healths: {
231
+ initial: toBigDecimal(position.healths[0].health),
232
+ maintenance: toBigDecimal(position.healths[1].health),
233
+ unweighted: toBigDecimal(position.healths[2].health),
234
+ },
235
+ baseBalance: {
236
+ amount: toBigDecimal(perpBalance.balance.amount),
237
+ vQuoteBalance: toBigDecimal(perpBalance.balance.v_quote_balance),
238
+ // Health contributions === healths for an isolated position
239
+ healthContributions: {
240
+ initial: toBigDecimal(position.base_healths[0]),
241
+ maintenance: toBigDecimal(position.base_healths[1]),
242
+ unweighted: toBigDecimal(position.base_healths[2]),
243
+ },
244
+ ...mapEngineServerPerpProduct(position.base_product).product,
245
+ },
246
+ quoteBalance: {
247
+ amount: toBigDecimal(quoteBalance.balance.amount),
248
+ healthContributions: {
249
+ initial: toBigDecimal(position.quote_healths[0]),
250
+ maintenance: toBigDecimal(position.quote_healths[1]),
251
+ unweighted: toBigDecimal(position.quote_healths[2]),
252
+ },
253
+ ...mapEngineServerSpotProduct(position.quote_product).product,
254
+ },
255
+ };
256
+ });
257
+ }
258
+
259
+ export function mapEngineServerSymbols(
260
+ baseResponse: EngineServerSymbolsResponse,
261
+ ): EngineSymbolsResponse {
262
+ const symbols: Record<string, EngineSymbol> = mapValues(
263
+ baseResponse.symbols,
264
+ mapEngineServerSymbol,
265
+ );
266
+
267
+ return {
268
+ symbols,
269
+ };
270
+ }
271
+
272
+ export function mapEngineServerSymbol(
273
+ engineServerSymbol: EngineServerSymbol,
274
+ ): EngineSymbol {
275
+ return {
276
+ type: mapEngineServerProductType(engineServerSymbol.type),
277
+ productId: engineServerSymbol.product_id,
278
+ symbol: engineServerSymbol.symbol,
279
+ priceIncrement: removeDecimals(engineServerSymbol.price_increment_x18),
280
+ sizeIncrement: toBigDecimal(engineServerSymbol.size_increment),
281
+ minSize: toBigDecimal(engineServerSymbol.min_size),
282
+ minDepth: removeDecimals(engineServerSymbol.min_depth_x18),
283
+ maxSpreadRate: removeDecimals(engineServerSymbol.max_spread_rate_x18),
284
+ makerFeeRate: removeDecimals(engineServerSymbol.maker_fee_rate_x18),
285
+ takerFeeRate: removeDecimals(engineServerSymbol.taker_fee_rate_x18),
286
+ longWeightInitial: removeDecimals(
287
+ engineServerSymbol.long_weight_initial_x18,
288
+ ),
289
+ longWeightMaintenance: removeDecimals(
290
+ engineServerSymbol.long_weight_maintenance_x18,
291
+ ),
292
+ };
293
+ }
294
+
295
+ export function mapEngineMarketPrice(
296
+ baseResponse: EngineServerMarketPrice,
297
+ ): EngineMarketPrice {
298
+ return {
299
+ ask: removeDecimals(baseResponse.ask_x18),
300
+ bid: removeDecimals(baseResponse.bid_x18),
301
+ productId: baseResponse.product_id,
302
+ };
303
+ }