@mysten/deepbook-v3 1.5.9 → 1.6.2

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 (177) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/dist/client.d.mts +5 -0
  3. package/dist/client.d.mts.map +1 -1
  4. package/dist/client.mjs +7 -0
  5. package/dist/client.mjs.map +1 -1
  6. package/dist/contracts/deepbook/balance_manager.mjs +413 -0
  7. package/dist/contracts/deepbook/balance_manager.mjs.map +1 -0
  8. package/dist/contracts/deepbook/balances.d.mts +4 -4
  9. package/dist/contracts/deepbook/big_vector.mjs +43 -0
  10. package/dist/contracts/deepbook/big_vector.mjs.map +1 -0
  11. package/dist/contracts/deepbook/book.mjs +29 -0
  12. package/dist/contracts/deepbook/book.mjs.map +1 -0
  13. package/dist/contracts/deepbook/deep_price.d.mts +3 -3
  14. package/dist/contracts/deepbook/deep_price.mjs +1 -1
  15. package/dist/contracts/deepbook/deps/std/type_name.mjs +17 -0
  16. package/dist/contracts/deepbook/deps/std/type_name.mjs.map +1 -0
  17. package/dist/contracts/deepbook/deps/sui/bag.mjs +43 -0
  18. package/dist/contracts/deepbook/deps/sui/bag.mjs.map +1 -0
  19. package/dist/contracts/deepbook/deps/sui/balance.mjs +21 -0
  20. package/dist/contracts/deepbook/deps/sui/balance.mjs.map +1 -0
  21. package/dist/contracts/deepbook/deps/sui/table.mjs +38 -0
  22. package/dist/contracts/deepbook/deps/sui/table.mjs.map +1 -0
  23. package/dist/contracts/deepbook/deps/sui/vec_map.mjs +37 -0
  24. package/dist/contracts/deepbook/deps/sui/vec_map.mjs.map +1 -0
  25. package/dist/contracts/deepbook/deps/sui/versioned.mjs +19 -0
  26. package/dist/contracts/deepbook/deps/sui/versioned.mjs.map +1 -0
  27. package/dist/contracts/deepbook/governance.mjs +49 -0
  28. package/dist/contracts/deepbook/governance.mjs.map +1 -0
  29. package/dist/contracts/deepbook/history.mjs +56 -0
  30. package/dist/contracts/deepbook/history.mjs.map +1 -0
  31. package/dist/contracts/deepbook/order.d.mts +12 -12
  32. package/dist/contracts/deepbook/pool.mjs +1739 -0
  33. package/dist/contracts/deepbook/pool.mjs.map +1 -0
  34. package/dist/contracts/deepbook/registry.mjs +295 -0
  35. package/dist/contracts/deepbook/registry.mjs.map +1 -0
  36. package/dist/contracts/deepbook/state.mjs +89 -0
  37. package/dist/contracts/deepbook/state.mjs.map +1 -0
  38. package/dist/contracts/deepbook/trade_params.mjs +21 -0
  39. package/dist/contracts/deepbook/trade_params.mjs.map +1 -0
  40. package/dist/contracts/deepbook/vault.mjs +42 -0
  41. package/dist/contracts/deepbook/vault.mjs.map +1 -0
  42. package/dist/contracts/deepbook_margin/deps/deepbook/balance_manager.mjs +53 -0
  43. package/dist/contracts/deepbook_margin/deps/deepbook/balance_manager.mjs.map +1 -0
  44. package/dist/contracts/deepbook_margin/deps/std/type_name.mjs +17 -0
  45. package/dist/contracts/deepbook_margin/deps/std/type_name.mjs.map +1 -0
  46. package/dist/contracts/deepbook_margin/deps/sui/bag.mjs +43 -0
  47. package/dist/contracts/deepbook_margin/deps/sui/bag.mjs.map +1 -0
  48. package/dist/contracts/deepbook_margin/deps/sui/balance.mjs +21 -0
  49. package/dist/contracts/deepbook_margin/deps/sui/balance.mjs.map +1 -0
  50. package/dist/contracts/deepbook_margin/deps/sui/table.mjs +38 -0
  51. package/dist/contracts/deepbook_margin/deps/sui/table.mjs.map +1 -0
  52. package/dist/contracts/deepbook_margin/deps/sui/vec_map.mjs +37 -0
  53. package/dist/contracts/deepbook_margin/deps/sui/vec_map.mjs.map +1 -0
  54. package/dist/contracts/deepbook_margin/deps/sui/vec_set.mjs +26 -0
  55. package/dist/contracts/deepbook_margin/deps/sui/vec_set.mjs.map +1 -0
  56. package/dist/contracts/deepbook_margin/deps/sui/versioned.mjs +19 -0
  57. package/dist/contracts/deepbook_margin/deps/sui/versioned.mjs.map +1 -0
  58. package/dist/contracts/deepbook_margin/margin_manager.mjs +989 -0
  59. package/dist/contracts/deepbook_margin/margin_manager.mjs.map +1 -0
  60. package/dist/contracts/deepbook_margin/margin_pool.mjs +550 -0
  61. package/dist/contracts/deepbook_margin/margin_pool.mjs.map +1 -0
  62. package/dist/contracts/deepbook_margin/margin_registry.mjs +786 -0
  63. package/dist/contracts/deepbook_margin/margin_registry.mjs.map +1 -0
  64. package/dist/contracts/deepbook_margin/margin_state.mjs +30 -0
  65. package/dist/contracts/deepbook_margin/margin_state.mjs.map +1 -0
  66. package/dist/contracts/deepbook_margin/oracle.mjs +82 -0
  67. package/dist/contracts/deepbook_margin/oracle.mjs.map +1 -0
  68. package/dist/contracts/deepbook_margin/pool_proxy.mjs +644 -0
  69. package/dist/contracts/deepbook_margin/pool_proxy.mjs.map +1 -0
  70. package/dist/contracts/deepbook_margin/position_manager.mjs +32 -0
  71. package/dist/contracts/deepbook_margin/position_manager.mjs.map +1 -0
  72. package/dist/contracts/deepbook_margin/protocol_config.mjs +122 -0
  73. package/dist/contracts/deepbook_margin/protocol_config.mjs.map +1 -0
  74. package/dist/contracts/deepbook_margin/protocol_fees.mjs +58 -0
  75. package/dist/contracts/deepbook_margin/protocol_fees.mjs.map +1 -0
  76. package/dist/contracts/deepbook_margin/rate_limiter.mjs +26 -0
  77. package/dist/contracts/deepbook_margin/rate_limiter.mjs.map +1 -0
  78. package/dist/contracts/deepbook_margin/tpsl.mjs +171 -0
  79. package/dist/contracts/deepbook_margin/tpsl.mjs.map +1 -0
  80. package/dist/contracts/margin_liquidation/deps/sui/bag.mjs +43 -0
  81. package/dist/contracts/margin_liquidation/deps/sui/bag.mjs.map +1 -0
  82. package/dist/contracts/margin_liquidation/liquidation_vault.mjs +179 -0
  83. package/dist/contracts/margin_liquidation/liquidation_vault.mjs.map +1 -0
  84. package/dist/contracts/utils/index.d.mts +1 -1
  85. package/dist/contracts/utils/index.mjs +92 -3
  86. package/dist/contracts/utils/index.mjs.map +1 -1
  87. package/dist/queries/registryQueries.mjs +13 -0
  88. package/dist/queries/registryQueries.mjs.map +1 -1
  89. package/dist/transactions/balanceManager.d.mts.map +1 -1
  90. package/dist/transactions/balanceManager.mjs +119 -100
  91. package/dist/transactions/balanceManager.mjs.map +1 -1
  92. package/dist/transactions/deepbook.d.mts.map +1 -1
  93. package/dist/transactions/deepbook.mjs +464 -439
  94. package/dist/transactions/deepbook.mjs.map +1 -1
  95. package/dist/transactions/deepbookAdmin.d.mts.map +1 -1
  96. package/dist/transactions/deepbookAdmin.mjs +151 -135
  97. package/dist/transactions/deepbookAdmin.mjs.map +1 -1
  98. package/dist/transactions/flashLoans.d.mts.map +1 -1
  99. package/dist/transactions/flashLoans.mjs +29 -22
  100. package/dist/transactions/flashLoans.mjs.map +1 -1
  101. package/dist/transactions/governance.d.mts.map +1 -1
  102. package/dist/transactions/governance.mjs +34 -33
  103. package/dist/transactions/governance.mjs.map +1 -1
  104. package/dist/transactions/marginAdmin.d.mts +10 -0
  105. package/dist/transactions/marginAdmin.d.mts.map +1 -1
  106. package/dist/transactions/marginAdmin.mjs +186 -169
  107. package/dist/transactions/marginAdmin.mjs.map +1 -1
  108. package/dist/transactions/marginLiquidations.d.mts +3 -3
  109. package/dist/transactions/marginLiquidations.d.mts.map +1 -1
  110. package/dist/transactions/marginLiquidations.mjs +53 -54
  111. package/dist/transactions/marginLiquidations.mjs.map +1 -1
  112. package/dist/transactions/marginMaintainer.d.mts.map +1 -1
  113. package/dist/transactions/marginMaintainer.mjs +83 -83
  114. package/dist/transactions/marginMaintainer.mjs.map +1 -1
  115. package/dist/transactions/marginManager.d.mts.map +1 -1
  116. package/dist/transactions/marginManager.mjs +330 -311
  117. package/dist/transactions/marginManager.mjs.map +1 -1
  118. package/dist/transactions/marginPool.d.mts.map +1 -1
  119. package/dist/transactions/marginPool.mjs +93 -89
  120. package/dist/transactions/marginPool.mjs.map +1 -1
  121. package/dist/transactions/marginRegistry.d.mts +22 -15
  122. package/dist/transactions/marginRegistry.d.mts.map +1 -1
  123. package/dist/transactions/marginRegistry.mjs +101 -68
  124. package/dist/transactions/marginRegistry.mjs.map +1 -1
  125. package/dist/transactions/marginTPSL.d.mts +27 -9
  126. package/dist/transactions/marginTPSL.d.mts.map +1 -1
  127. package/dist/transactions/marginTPSL.mjs +108 -82
  128. package/dist/transactions/marginTPSL.mjs.map +1 -1
  129. package/dist/transactions/poolProxy.d.mts +32 -0
  130. package/dist/transactions/poolProxy.d.mts.map +1 -1
  131. package/dist/transactions/poolProxy.mjs +269 -189
  132. package/dist/transactions/poolProxy.mjs.map +1 -1
  133. package/dist/utils/constants.mjs +4 -4
  134. package/dist/utils/constants.mjs.map +1 -1
  135. package/package.json +2 -2
  136. package/src/client.ts +8 -0
  137. package/src/contracts/deepbook/balance_manager.ts +46 -5
  138. package/src/contracts/deepbook/constants.ts +13 -0
  139. package/src/contracts/deepbook/pool.ts +133 -0
  140. package/src/contracts/deepbook/registry.ts +136 -1
  141. package/src/contracts/deepbook_margin/deps/deepbook/balance_manager.ts +49 -0
  142. package/src/contracts/deepbook_margin/deps/std/type_name.ts +24 -0
  143. package/src/contracts/deepbook_margin/deps/sui/bag.ts +41 -0
  144. package/src/contracts/deepbook_margin/deps/sui/balance.ts +19 -0
  145. package/src/contracts/deepbook_margin/deps/sui/table.ts +36 -0
  146. package/src/contracts/deepbook_margin/deps/sui/vec_map.ts +33 -0
  147. package/src/contracts/deepbook_margin/deps/sui/vec_set.ts +22 -0
  148. package/src/contracts/deepbook_margin/deps/sui/versioned.ts +13 -0
  149. package/src/contracts/deepbook_margin/margin_constants.ts +329 -0
  150. package/src/contracts/deepbook_margin/margin_manager.ts +1772 -0
  151. package/src/contracts/deepbook_margin/margin_pool.ts +1129 -0
  152. package/src/contracts/deepbook_margin/margin_registry.ts +1207 -0
  153. package/src/contracts/deepbook_margin/margin_state.ts +26 -0
  154. package/src/contracts/deepbook_margin/oracle.ts +99 -0
  155. package/src/contracts/deepbook_margin/pool_proxy.ts +1298 -0
  156. package/src/contracts/deepbook_margin/position_manager.ts +28 -0
  157. package/src/contracts/deepbook_margin/protocol_config.ts +163 -0
  158. package/src/contracts/deepbook_margin/protocol_fees.ts +153 -0
  159. package/src/contracts/deepbook_margin/rate_limiter.ts +80 -0
  160. package/src/contracts/deepbook_margin/tpsl.ts +559 -0
  161. package/src/contracts/margin_liquidation/deps/sui/bag.ts +41 -0
  162. package/src/contracts/margin_liquidation/liquidation_vault.ts +401 -0
  163. package/src/queries/registryQueries.ts +15 -0
  164. package/src/transactions/balanceManager.ts +140 -90
  165. package/src/transactions/deepbook.ts +586 -494
  166. package/src/transactions/deepbookAdmin.ts +154 -146
  167. package/src/transactions/flashLoans.ts +29 -20
  168. package/src/transactions/governance.ts +50 -37
  169. package/src/transactions/marginAdmin.ts +247 -188
  170. package/src/transactions/marginLiquidations.ts +70 -55
  171. package/src/transactions/marginMaintainer.ts +106 -88
  172. package/src/transactions/marginManager.ts +414 -348
  173. package/src/transactions/marginPool.ts +140 -103
  174. package/src/transactions/marginRegistry.ts +103 -70
  175. package/src/transactions/marginTPSL.ts +151 -87
  176. package/src/transactions/poolProxy.ts +384 -205
  177. package/src/utils/constants.ts +4 -4
@@ -11,6 +11,7 @@ import type { DeepBookConfig } from '../utils/config.js';
11
11
  import { OrderType, SelfMatchingOptions } from '../types/index.js';
12
12
  import { MAX_TIMESTAMP, FLOAT_SCALAR } from '../utils/config.js';
13
13
  import { convertQuantity, convertPrice, convertRate } from '../utils/conversion.js';
14
+ import * as poolProxyMoveCalls from '../contracts/deepbook_margin/pool_proxy.js';
14
15
 
15
16
  /**
16
17
  * PoolProxyContract class for managing PoolProxy operations.
@@ -53,28 +54,29 @@ export class PoolProxyContract {
53
54
  const quoteMarginPool = this.#config.getMarginPool(pool.quoteCoin);
54
55
  const inputPrice = convertPrice(price, FLOAT_SCALAR, quoteCoin.scalar, baseCoin.scalar);
55
56
  const inputQuantity = convertQuantity(quantity, baseCoin.scalar);
56
- return tx.moveCall({
57
- target: `${this.#config.MARGIN_PACKAGE_ID}::pool_proxy::place_limit_order_v2`,
58
- arguments: [
59
- tx.object(this.#config.MARGIN_REGISTRY_ID),
60
- tx.object(manager.address),
61
- tx.object(pool.address),
62
- tx.object(baseMarginPool.address),
63
- tx.object(quoteMarginPool.address),
64
- tx.object(baseCoin.priceInfoObjectId!),
65
- tx.object(quoteCoin.priceInfoObjectId!),
66
- tx.pure.u64(clientOrderId),
67
- tx.pure.u8(orderType),
68
- tx.pure.u8(selfMatchingOption),
69
- tx.pure.u64(inputPrice),
70
- tx.pure.u64(inputQuantity),
71
- tx.pure.bool(isBid),
72
- tx.pure.bool(payWithDeep),
73
- tx.pure.u64(expiration),
74
- tx.object.clock(),
75
- ],
76
- typeArguments: [baseCoin.type, quoteCoin.type],
77
- });
57
+ return tx.add(
58
+ poolProxyMoveCalls.placeLimitOrderV2({
59
+ package: this.#config.MARGIN_PACKAGE_ID,
60
+ arguments: {
61
+ registry: this.#config.MARGIN_REGISTRY_ID,
62
+ marginManager: manager.address,
63
+ pool: pool.address,
64
+ baseMarginPool: baseMarginPool.address,
65
+ quoteMarginPool: quoteMarginPool.address,
66
+ baseOracle: baseCoin.priceInfoObjectId!,
67
+ quoteOracle: quoteCoin.priceInfoObjectId!,
68
+ clientOrderId: BigInt(clientOrderId),
69
+ orderType,
70
+ selfMatchingOption,
71
+ price: inputPrice,
72
+ quantity: inputQuantity,
73
+ isBid,
74
+ payWithDeep,
75
+ expireTimestamp: expiration,
76
+ },
77
+ typeArguments: [baseCoin.type, quoteCoin.type],
78
+ }),
79
+ );
78
80
  };
79
81
 
80
82
  /**
@@ -101,25 +103,26 @@ export class PoolProxyContract {
101
103
  const baseMarginPool = this.#config.getMarginPool(pool.baseCoin);
102
104
  const quoteMarginPool = this.#config.getMarginPool(pool.quoteCoin);
103
105
  const inputQuantity = convertQuantity(quantity, baseCoin.scalar);
104
- return tx.moveCall({
105
- target: `${this.#config.MARGIN_PACKAGE_ID}::pool_proxy::place_market_order_v2`,
106
- arguments: [
107
- tx.object(this.#config.MARGIN_REGISTRY_ID),
108
- tx.object(manager.address),
109
- tx.object(pool.address),
110
- tx.object(baseMarginPool.address),
111
- tx.object(quoteMarginPool.address),
112
- tx.object(baseCoin.priceInfoObjectId!),
113
- tx.object(quoteCoin.priceInfoObjectId!),
114
- tx.pure.u64(clientOrderId),
115
- tx.pure.u8(selfMatchingOption),
116
- tx.pure.u64(inputQuantity),
117
- tx.pure.bool(isBid),
118
- tx.pure.bool(payWithDeep),
119
- tx.object.clock(),
120
- ],
121
- typeArguments: [baseCoin.type, quoteCoin.type],
122
- });
106
+ return tx.add(
107
+ poolProxyMoveCalls.placeMarketOrderV2({
108
+ package: this.#config.MARGIN_PACKAGE_ID,
109
+ arguments: {
110
+ registry: this.#config.MARGIN_REGISTRY_ID,
111
+ marginManager: manager.address,
112
+ pool: pool.address,
113
+ baseMarginPool: baseMarginPool.address,
114
+ quoteMarginPool: quoteMarginPool.address,
115
+ baseOracle: baseCoin.priceInfoObjectId!,
116
+ quoteOracle: quoteCoin.priceInfoObjectId!,
117
+ clientOrderId: BigInt(clientOrderId),
118
+ selfMatchingOption,
119
+ quantity: inputQuantity,
120
+ isBid,
121
+ payWithDeep,
122
+ },
123
+ typeArguments: [baseCoin.type, quoteCoin.type],
124
+ }),
125
+ );
123
126
  };
124
127
 
125
128
  /**
@@ -151,28 +154,29 @@ export class PoolProxyContract {
151
154
  const quoteMarginPool = this.#config.getMarginPool(pool.quoteCoin);
152
155
  const inputPrice = convertPrice(price, FLOAT_SCALAR, quoteCoin.scalar, baseCoin.scalar);
153
156
  const inputQuantity = convertQuantity(quantity, baseCoin.scalar);
154
- return tx.moveCall({
155
- target: `${this.#config.MARGIN_PACKAGE_ID}::pool_proxy::place_reduce_only_limit_order_v2`,
156
- arguments: [
157
- tx.object(this.#config.MARGIN_REGISTRY_ID),
158
- tx.object(manager.address),
159
- tx.object(pool.address),
160
- tx.object(baseMarginPool.address),
161
- tx.object(quoteMarginPool.address),
162
- tx.object(baseCoin.priceInfoObjectId!),
163
- tx.object(quoteCoin.priceInfoObjectId!),
164
- tx.pure.u64(clientOrderId),
165
- tx.pure.u8(orderType),
166
- tx.pure.u8(selfMatchingOption),
167
- tx.pure.u64(inputPrice),
168
- tx.pure.u64(inputQuantity),
169
- tx.pure.bool(isBid),
170
- tx.pure.bool(payWithDeep),
171
- tx.pure.u64(expiration),
172
- tx.object.clock(),
173
- ],
174
- typeArguments: [baseCoin.type, quoteCoin.type],
175
- });
157
+ return tx.add(
158
+ poolProxyMoveCalls.placeReduceOnlyLimitOrderV2({
159
+ package: this.#config.MARGIN_PACKAGE_ID,
160
+ arguments: {
161
+ registry: this.#config.MARGIN_REGISTRY_ID,
162
+ marginManager: manager.address,
163
+ pool: pool.address,
164
+ baseMarginPool: baseMarginPool.address,
165
+ quoteMarginPool: quoteMarginPool.address,
166
+ baseOracle: baseCoin.priceInfoObjectId!,
167
+ quoteOracle: quoteCoin.priceInfoObjectId!,
168
+ clientOrderId: BigInt(clientOrderId),
169
+ orderType,
170
+ selfMatchingOption,
171
+ price: inputPrice,
172
+ quantity: inputQuantity,
173
+ isBid,
174
+ payWithDeep,
175
+ expireTimestamp: expiration,
176
+ },
177
+ typeArguments: [baseCoin.type, quoteCoin.type],
178
+ }),
179
+ );
176
180
  };
177
181
 
178
182
  /**
@@ -200,27 +204,183 @@ export class PoolProxyContract {
200
204
  const baseMarginPool = this.#config.getMarginPool(pool.baseCoin);
201
205
  const quoteMarginPool = this.#config.getMarginPool(pool.quoteCoin);
202
206
  const inputQuantity = convertQuantity(quantity, baseCoin.scalar);
203
- return tx.moveCall({
204
- target: `${this.#config.MARGIN_PACKAGE_ID}::pool_proxy::place_reduce_only_market_order_v2`,
205
- arguments: [
206
- tx.object(this.#config.MARGIN_REGISTRY_ID),
207
- tx.object(manager.address),
208
- tx.object(pool.address),
209
- tx.object(baseMarginPool.address),
210
- tx.object(quoteMarginPool.address),
211
- tx.object(baseCoin.priceInfoObjectId!),
212
- tx.object(quoteCoin.priceInfoObjectId!),
213
- tx.pure.u64(clientOrderId),
214
- tx.pure.u8(selfMatchingOption),
215
- tx.pure.u64(inputQuantity),
216
- tx.pure.bool(isBid),
217
- tx.pure.bool(payWithDeep),
218
- tx.object.clock(),
219
- ],
220
- typeArguments: [baseCoin.type, quoteCoin.type],
221
- });
207
+ return tx.add(
208
+ poolProxyMoveCalls.placeReduceOnlyMarketOrderV2({
209
+ package: this.#config.MARGIN_PACKAGE_ID,
210
+ arguments: {
211
+ registry: this.#config.MARGIN_REGISTRY_ID,
212
+ marginManager: manager.address,
213
+ pool: pool.address,
214
+ baseMarginPool: baseMarginPool.address,
215
+ quoteMarginPool: quoteMarginPool.address,
216
+ baseOracle: baseCoin.priceInfoObjectId!,
217
+ quoteOracle: quoteCoin.priceInfoObjectId!,
218
+ clientOrderId: BigInt(clientOrderId),
219
+ selfMatchingOption,
220
+ quantity: inputQuantity,
221
+ isBid,
222
+ payWithDeep,
223
+ },
224
+ typeArguments: [baseCoin.type, quoteCoin.type],
225
+ }),
226
+ );
222
227
  };
223
228
 
229
+ /**
230
+ * @description Place a market order and repay the loan from the fill proceeds.
231
+ * The taker fill settles into the manager's balance, so the proceeds (plus any
232
+ * idle balance) are repaid into the debt side before the risk check; the gate
233
+ * is then the *net* post-repay `risk_ratio` being at least the pre-fill ratio.
234
+ * Unlike {@link placeMarketOrder}, which checks the post-trade ratio against
235
+ * `min_borrow_risk_ratio`, this lets a deleveraging fill go through in the
236
+ * `liquidation..min_borrow` band, where a swap alone would be rejected.
237
+ * @param {PlaceMarginMarketOrderParams} params Parameters for placing a market order
238
+ * @returns A function that takes a Transaction object
239
+ */
240
+ placeMarketOrderAndRepayLoan = (params: PlaceMarginMarketOrderParams) => (tx: Transaction) => {
241
+ const {
242
+ poolKey,
243
+ marginManagerKey,
244
+ clientOrderId,
245
+ quantity,
246
+ isBid,
247
+ selfMatchingOption = SelfMatchingOptions.SELF_MATCHING_ALLOWED,
248
+ payWithDeep = true,
249
+ } = params;
250
+ const pool = this.#config.getPool(poolKey);
251
+ const manager = this.#config.getMarginManager(marginManagerKey);
252
+ const baseCoin = this.#config.getCoin(pool.baseCoin);
253
+ const quoteCoin = this.#config.getCoin(pool.quoteCoin);
254
+ const baseMarginPool = this.#config.getMarginPool(pool.baseCoin);
255
+ const quoteMarginPool = this.#config.getMarginPool(pool.quoteCoin);
256
+ const inputQuantity = convertQuantity(quantity, baseCoin.scalar);
257
+ return tx.add(
258
+ poolProxyMoveCalls.placeMarketOrderAndRepayLoan({
259
+ package: this.#config.MARGIN_PACKAGE_ID,
260
+ arguments: {
261
+ registry: this.#config.MARGIN_REGISTRY_ID,
262
+ marginManager: manager.address,
263
+ pool: pool.address,
264
+ baseMarginPool: baseMarginPool.address,
265
+ quoteMarginPool: quoteMarginPool.address,
266
+ baseOracle: baseCoin.priceInfoObjectId!,
267
+ quoteOracle: quoteCoin.priceInfoObjectId!,
268
+ clientOrderId: BigInt(clientOrderId),
269
+ selfMatchingOption,
270
+ quantity: inputQuantity,
271
+ isBid,
272
+ payWithDeep,
273
+ },
274
+ typeArguments: [baseCoin.type, quoteCoin.type],
275
+ }),
276
+ );
277
+ };
278
+
279
+ /**
280
+ * @description Place a reduce only limit order and repay the loan from the
281
+ * fill proceeds. Requires debt on the relevant side (a bid needs base debt; an
282
+ * ask needs quote debt and sells at most the gross base held); the repay
283
+ * happens before the monotonic `risk_ratio` gate, so the check is on the net
284
+ * post-repay ratio.
285
+ * @param {PlaceMarginLimitOrderParams} params Parameters for placing a reduce only limit order
286
+ * @returns A function that takes a Transaction object
287
+ */
288
+ placeReduceOnlyLimitOrderAndRepayLoan =
289
+ (params: PlaceMarginLimitOrderParams) => (tx: Transaction) => {
290
+ const {
291
+ poolKey,
292
+ marginManagerKey,
293
+ clientOrderId,
294
+ price,
295
+ quantity,
296
+ isBid,
297
+ expiration = MAX_TIMESTAMP,
298
+ orderType = OrderType.NO_RESTRICTION,
299
+ selfMatchingOption = SelfMatchingOptions.SELF_MATCHING_ALLOWED,
300
+ payWithDeep = true,
301
+ } = params;
302
+ const pool = this.#config.getPool(poolKey);
303
+ const manager = this.#config.getMarginManager(marginManagerKey);
304
+ const baseCoin = this.#config.getCoin(pool.baseCoin);
305
+ const quoteCoin = this.#config.getCoin(pool.quoteCoin);
306
+ const baseMarginPool = this.#config.getMarginPool(pool.baseCoin);
307
+ const quoteMarginPool = this.#config.getMarginPool(pool.quoteCoin);
308
+ const inputPrice = convertPrice(price, FLOAT_SCALAR, quoteCoin.scalar, baseCoin.scalar);
309
+ const inputQuantity = convertQuantity(quantity, baseCoin.scalar);
310
+ return tx.add(
311
+ poolProxyMoveCalls.placeReduceOnlyLimitOrderAndRepayLoan({
312
+ package: this.#config.MARGIN_PACKAGE_ID,
313
+ arguments: {
314
+ registry: this.#config.MARGIN_REGISTRY_ID,
315
+ marginManager: manager.address,
316
+ pool: pool.address,
317
+ baseMarginPool: baseMarginPool.address,
318
+ quoteMarginPool: quoteMarginPool.address,
319
+ baseOracle: baseCoin.priceInfoObjectId!,
320
+ quoteOracle: quoteCoin.priceInfoObjectId!,
321
+ clientOrderId: BigInt(clientOrderId),
322
+ orderType,
323
+ selfMatchingOption,
324
+ price: inputPrice,
325
+ quantity: inputQuantity,
326
+ isBid,
327
+ payWithDeep,
328
+ expireTimestamp: expiration,
329
+ },
330
+ typeArguments: [baseCoin.type, quoteCoin.type],
331
+ }),
332
+ );
333
+ };
334
+
335
+ /**
336
+ * @description Place a reduce only market order and repay the loan from the
337
+ * fill proceeds. Same reduce-only direction guard as
338
+ * {@link placeReduceOnlyMarketOrder}, but the settled proceeds are repaid into
339
+ * the debt side before the monotonic `risk_ratio` gate, so the check is on the
340
+ * net post-repay ratio.
341
+ * @param {PlaceMarginMarketOrderParams} params Parameters for placing a reduce only market order
342
+ * @returns A function that takes a Transaction object
343
+ */
344
+ placeReduceOnlyMarketOrderAndRepayLoan =
345
+ (params: PlaceMarginMarketOrderParams) => (tx: Transaction) => {
346
+ const {
347
+ poolKey,
348
+ marginManagerKey,
349
+ clientOrderId,
350
+ quantity,
351
+ isBid,
352
+ selfMatchingOption = SelfMatchingOptions.SELF_MATCHING_ALLOWED,
353
+ payWithDeep = true,
354
+ } = params;
355
+ const pool = this.#config.getPool(poolKey);
356
+ const manager = this.#config.getMarginManager(marginManagerKey);
357
+ const baseCoin = this.#config.getCoin(pool.baseCoin);
358
+ const quoteCoin = this.#config.getCoin(pool.quoteCoin);
359
+ const baseMarginPool = this.#config.getMarginPool(pool.baseCoin);
360
+ const quoteMarginPool = this.#config.getMarginPool(pool.quoteCoin);
361
+ const inputQuantity = convertQuantity(quantity, baseCoin.scalar);
362
+ return tx.add(
363
+ poolProxyMoveCalls.placeReduceOnlyMarketOrderAndRepayLoan({
364
+ package: this.#config.MARGIN_PACKAGE_ID,
365
+ arguments: {
366
+ registry: this.#config.MARGIN_REGISTRY_ID,
367
+ marginManager: manager.address,
368
+ pool: pool.address,
369
+ baseMarginPool: baseMarginPool.address,
370
+ quoteMarginPool: quoteMarginPool.address,
371
+ baseOracle: baseCoin.priceInfoObjectId!,
372
+ quoteOracle: quoteCoin.priceInfoObjectId!,
373
+ clientOrderId: BigInt(clientOrderId),
374
+ selfMatchingOption,
375
+ quantity: inputQuantity,
376
+ isBid,
377
+ payWithDeep,
378
+ },
379
+ typeArguments: [baseCoin.type, quoteCoin.type],
380
+ }),
381
+ );
382
+ };
383
+
224
384
  /**
225
385
  * @description Modify an existing order
226
386
  * @param {string} marginManagerKey The key to identify the MarginManager
@@ -236,18 +396,19 @@ export class PoolProxyContract {
236
396
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
237
397
  const inputQuantity = convertQuantity(newQuantity, baseCoin.scalar);
238
398
 
239
- tx.moveCall({
240
- target: `${this.#config.MARGIN_PACKAGE_ID}::pool_proxy::modify_order`,
241
- arguments: [
242
- tx.object(this.#config.MARGIN_REGISTRY_ID),
243
- tx.object(marginManager.address),
244
- tx.object(pool.address),
245
- tx.pure.u128(orderId),
246
- tx.pure.u64(inputQuantity),
247
- tx.object.clock(),
248
- ],
249
- typeArguments: [baseCoin.type, quoteCoin.type],
250
- });
399
+ tx.add(
400
+ poolProxyMoveCalls.modifyOrder({
401
+ package: this.#config.MARGIN_PACKAGE_ID,
402
+ arguments: {
403
+ registry: this.#config.MARGIN_REGISTRY_ID,
404
+ marginManager: marginManager.address,
405
+ pool: pool.address,
406
+ orderId: BigInt(orderId),
407
+ newQuantity: inputQuantity,
408
+ },
409
+ typeArguments: [baseCoin.type, quoteCoin.type],
410
+ }),
411
+ );
251
412
  };
252
413
 
253
414
  /**
@@ -261,17 +422,18 @@ export class PoolProxyContract {
261
422
  const pool = this.#config.getPool(marginManager.poolKey);
262
423
  const baseCoin = this.#config.getCoin(pool.baseCoin);
263
424
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
264
- tx.moveCall({
265
- target: `${this.#config.MARGIN_PACKAGE_ID}::pool_proxy::cancel_order`,
266
- arguments: [
267
- tx.object(this.#config.MARGIN_REGISTRY_ID),
268
- tx.object(marginManager.address),
269
- tx.object(pool.address),
270
- tx.pure.u128(orderId),
271
- tx.object.clock(),
272
- ],
273
- typeArguments: [baseCoin.type, quoteCoin.type],
274
- });
425
+ tx.add(
426
+ poolProxyMoveCalls.cancelOrder({
427
+ package: this.#config.MARGIN_PACKAGE_ID,
428
+ arguments: {
429
+ registry: this.#config.MARGIN_REGISTRY_ID,
430
+ marginManager: marginManager.address,
431
+ pool: pool.address,
432
+ orderId: BigInt(orderId),
433
+ },
434
+ typeArguments: [baseCoin.type, quoteCoin.type],
435
+ }),
436
+ );
275
437
  };
276
438
 
277
439
  /**
@@ -285,17 +447,18 @@ export class PoolProxyContract {
285
447
  const pool = this.#config.getPool(marginManager.poolKey);
286
448
  const baseCoin = this.#config.getCoin(pool.baseCoin);
287
449
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
288
- tx.moveCall({
289
- target: `${this.#config.MARGIN_PACKAGE_ID}::pool_proxy::cancel_orders`,
290
- arguments: [
291
- tx.object(this.#config.MARGIN_REGISTRY_ID),
292
- tx.object(marginManager.address),
293
- tx.object(pool.address),
294
- tx.pure.vector('u128', orderIds),
295
- tx.object.clock(),
296
- ],
297
- typeArguments: [baseCoin.type, quoteCoin.type],
298
- });
450
+ tx.add(
451
+ poolProxyMoveCalls.cancelOrders({
452
+ package: this.#config.MARGIN_PACKAGE_ID,
453
+ arguments: {
454
+ registry: this.#config.MARGIN_REGISTRY_ID,
455
+ marginManager: marginManager.address,
456
+ pool: pool.address,
457
+ orderIds: orderIds.map(BigInt),
458
+ },
459
+ typeArguments: [baseCoin.type, quoteCoin.type],
460
+ }),
461
+ );
299
462
  };
300
463
 
301
464
  /**
@@ -308,16 +471,17 @@ export class PoolProxyContract {
308
471
  const pool = this.#config.getPool(marginManager.poolKey);
309
472
  const baseCoin = this.#config.getCoin(pool.baseCoin);
310
473
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
311
- tx.moveCall({
312
- target: `${this.#config.MARGIN_PACKAGE_ID}::pool_proxy::cancel_all_orders`,
313
- arguments: [
314
- tx.object(this.#config.MARGIN_REGISTRY_ID),
315
- tx.object(marginManager.address),
316
- tx.object(pool.address),
317
- tx.object.clock(),
318
- ],
319
- typeArguments: [baseCoin.type, quoteCoin.type],
320
- });
474
+ tx.add(
475
+ poolProxyMoveCalls.cancelAllOrders({
476
+ package: this.#config.MARGIN_PACKAGE_ID,
477
+ arguments: {
478
+ registry: this.#config.MARGIN_REGISTRY_ID,
479
+ marginManager: marginManager.address,
480
+ pool: pool.address,
481
+ },
482
+ typeArguments: [baseCoin.type, quoteCoin.type],
483
+ }),
484
+ );
321
485
  };
322
486
 
323
487
  /**
@@ -330,15 +494,17 @@ export class PoolProxyContract {
330
494
  const pool = this.#config.getPool(marginManager.poolKey);
331
495
  const baseCoin = this.#config.getCoin(pool.baseCoin);
332
496
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
333
- tx.moveCall({
334
- target: `${this.#config.MARGIN_PACKAGE_ID}::pool_proxy::withdraw_settled_amounts`,
335
- arguments: [
336
- tx.object(this.#config.MARGIN_REGISTRY_ID),
337
- tx.object(marginManager.address),
338
- tx.object(pool.address),
339
- ],
340
- typeArguments: [baseCoin.type, quoteCoin.type],
341
- });
497
+ tx.add(
498
+ poolProxyMoveCalls.withdrawSettledAmounts({
499
+ package: this.#config.MARGIN_PACKAGE_ID,
500
+ arguments: {
501
+ registry: this.#config.MARGIN_REGISTRY_ID,
502
+ marginManager: marginManager.address,
503
+ pool: pool.address,
504
+ },
505
+ typeArguments: [baseCoin.type, quoteCoin.type],
506
+ }),
507
+ );
342
508
  };
343
509
 
344
510
  /**
@@ -354,16 +520,18 @@ export class PoolProxyContract {
354
520
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
355
521
  const deepCoin = this.#config.getCoin('DEEP');
356
522
  const stakeInput = convertQuantity(stakeAmount, deepCoin.scalar);
357
- tx.moveCall({
358
- target: `${this.#config.MARGIN_PACKAGE_ID}::pool_proxy::stake`,
359
- arguments: [
360
- tx.object(this.#config.MARGIN_REGISTRY_ID),
361
- tx.object(marginManager.address),
362
- tx.object(pool.address),
363
- tx.pure.u64(stakeInput),
364
- ],
365
- typeArguments: [baseCoin.type, quoteCoin.type],
366
- });
523
+ tx.add(
524
+ poolProxyMoveCalls.stake({
525
+ package: this.#config.MARGIN_PACKAGE_ID,
526
+ arguments: {
527
+ registry: this.#config.MARGIN_REGISTRY_ID,
528
+ marginManager: marginManager.address,
529
+ pool: pool.address,
530
+ amount: stakeInput,
531
+ },
532
+ typeArguments: [baseCoin.type, quoteCoin.type],
533
+ }),
534
+ );
367
535
  };
368
536
 
369
537
  /**
@@ -376,15 +544,17 @@ export class PoolProxyContract {
376
544
  const pool = this.#config.getPool(marginManager.poolKey);
377
545
  const baseCoin = this.#config.getCoin(pool.baseCoin);
378
546
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
379
- tx.moveCall({
380
- target: `${this.#config.MARGIN_PACKAGE_ID}::pool_proxy::unstake`,
381
- arguments: [
382
- tx.object(this.#config.MARGIN_REGISTRY_ID),
383
- tx.object(marginManager.address),
384
- tx.object(pool.address),
385
- ],
386
- typeArguments: [baseCoin.type, quoteCoin.type],
387
- });
547
+ tx.add(
548
+ poolProxyMoveCalls.unstake({
549
+ package: this.#config.MARGIN_PACKAGE_ID,
550
+ arguments: {
551
+ registry: this.#config.MARGIN_REGISTRY_ID,
552
+ marginManager: marginManager.address,
553
+ pool: pool.address,
554
+ },
555
+ typeArguments: [baseCoin.type, quoteCoin.type],
556
+ }),
557
+ );
388
558
  };
389
559
 
390
560
  /**
@@ -403,18 +573,20 @@ export class PoolProxyContract {
403
573
  const stakeInput = convertRate(stakeRequired, FLOAT_SCALAR);
404
574
  const takerFeeInput = convertRate(takerFee, FLOAT_SCALAR);
405
575
  const makerFeeInput = convertRate(makerFee, FLOAT_SCALAR);
406
- tx.moveCall({
407
- target: `${this.#config.MARGIN_PACKAGE_ID}::pool_proxy::submit_proposal`,
408
- arguments: [
409
- tx.object(this.#config.MARGIN_REGISTRY_ID),
410
- tx.object(marginManager.address),
411
- tx.object(pool.address),
412
- tx.pure.u64(takerFeeInput),
413
- tx.pure.u64(makerFeeInput),
414
- tx.pure.u64(stakeInput),
415
- ],
416
- typeArguments: [baseCoin.type, quoteCoin.type],
417
- });
576
+ tx.add(
577
+ poolProxyMoveCalls.submitProposal({
578
+ package: this.#config.MARGIN_PACKAGE_ID,
579
+ arguments: {
580
+ registry: this.#config.MARGIN_REGISTRY_ID,
581
+ marginManager: marginManager.address,
582
+ pool: pool.address,
583
+ takerFee: takerFeeInput,
584
+ makerFee: makerFeeInput,
585
+ stakeRequired: stakeInput,
586
+ },
587
+ typeArguments: [baseCoin.type, quoteCoin.type],
588
+ }),
589
+ );
418
590
  };
419
591
 
420
592
  /**
@@ -428,16 +600,18 @@ export class PoolProxyContract {
428
600
  const pool = this.#config.getPool(marginManager.poolKey);
429
601
  const baseCoin = this.#config.getCoin(pool.baseCoin);
430
602
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
431
- tx.moveCall({
432
- target: `${this.#config.MARGIN_PACKAGE_ID}::pool_proxy::vote`,
433
- arguments: [
434
- tx.object(this.#config.MARGIN_REGISTRY_ID),
435
- tx.object(marginManager.address),
436
- tx.object(pool.address),
437
- tx.pure.id(proposalId),
438
- ],
439
- typeArguments: [baseCoin.type, quoteCoin.type],
440
- });
603
+ tx.add(
604
+ poolProxyMoveCalls.vote({
605
+ package: this.#config.MARGIN_PACKAGE_ID,
606
+ arguments: {
607
+ registry: this.#config.MARGIN_REGISTRY_ID,
608
+ marginManager: marginManager.address,
609
+ pool: pool.address,
610
+ proposalId,
611
+ },
612
+ typeArguments: [baseCoin.type, quoteCoin.type],
613
+ }),
614
+ );
441
615
  };
442
616
 
443
617
  /**
@@ -450,15 +624,17 @@ export class PoolProxyContract {
450
624
  const pool = this.#config.getPool(marginManager.poolKey);
451
625
  const baseCoin = this.#config.getCoin(pool.baseCoin);
452
626
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
453
- tx.moveCall({
454
- target: `${this.#config.MARGIN_PACKAGE_ID}::pool_proxy::claim_rebates`,
455
- arguments: [
456
- tx.object(this.#config.MARGIN_REGISTRY_ID),
457
- tx.object(marginManager.address),
458
- tx.object(pool.address),
459
- ],
460
- typeArguments: [baseCoin.type, quoteCoin.type],
461
- });
627
+ tx.add(
628
+ poolProxyMoveCalls.claimRebates({
629
+ package: this.#config.MARGIN_PACKAGE_ID,
630
+ arguments: {
631
+ registry: this.#config.MARGIN_REGISTRY_ID,
632
+ marginManager: marginManager.address,
633
+ pool: pool.address,
634
+ },
635
+ typeArguments: [baseCoin.type, quoteCoin.type],
636
+ }),
637
+ );
462
638
  };
463
639
 
464
640
  /**
@@ -472,15 +648,17 @@ export class PoolProxyContract {
472
648
  const pool = this.#config.getPool(poolKey);
473
649
  const baseCoin = this.#config.getCoin(pool.baseCoin);
474
650
  const quoteCoin = this.#config.getCoin(pool.quoteCoin);
475
- tx.moveCall({
476
- target: `${this.#config.MARGIN_PACKAGE_ID}::pool_proxy::withdraw_settled_amounts_permissionless`,
477
- arguments: [
478
- tx.object(this.#config.MARGIN_REGISTRY_ID),
479
- tx.object(marginManagerId),
480
- tx.object(pool.address),
481
- ],
482
- typeArguments: [baseCoin.type, quoteCoin.type],
483
- });
651
+ tx.add(
652
+ poolProxyMoveCalls.withdrawSettledAmountsPermissionless({
653
+ package: this.#config.MARGIN_PACKAGE_ID,
654
+ arguments: {
655
+ registry: this.#config.MARGIN_REGISTRY_ID,
656
+ marginManager: marginManagerId,
657
+ pool: pool.address,
658
+ },
659
+ typeArguments: [baseCoin.type, quoteCoin.type],
660
+ }),
661
+ );
484
662
  };
485
663
 
486
664
  /**
@@ -498,16 +676,17 @@ export class PoolProxyContract {
498
676
  if (!quoteCoin.priceInfoObjectId) {
499
677
  throw new Error(`Missing priceInfoObjectId for ${pool.quoteCoin}`);
500
678
  }
501
- tx.moveCall({
502
- target: `${this.#config.MARGIN_PACKAGE_ID}::pool_proxy::update_current_price`,
503
- arguments: [
504
- tx.object(this.#config.MARGIN_REGISTRY_ID),
505
- tx.object(pool.address),
506
- tx.object(baseCoin.priceInfoObjectId),
507
- tx.object(quoteCoin.priceInfoObjectId),
508
- tx.object.clock(),
509
- ],
510
- typeArguments: [baseCoin.type, quoteCoin.type],
511
- });
679
+ tx.add(
680
+ poolProxyMoveCalls.updateCurrentPrice({
681
+ package: this.#config.MARGIN_PACKAGE_ID,
682
+ arguments: {
683
+ registry: this.#config.MARGIN_REGISTRY_ID,
684
+ pool: pool.address,
685
+ basePriceInfoObject: baseCoin.priceInfoObjectId,
686
+ quotePriceInfoObject: quoteCoin.priceInfoObjectId,
687
+ },
688
+ typeArguments: [baseCoin.type, quoteCoin.type],
689
+ }),
690
+ );
512
691
  };
513
692
  }