@mysten/deepbook-v3 2.0.1 → 2.1.3

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 (272) hide show
  1. package/CHANGELOG.md +83 -0
  2. package/PREDICT.md +256 -0
  3. package/README.md +116 -0
  4. package/dist/account.d.mts +139 -0
  5. package/dist/account.d.mts.map +1 -0
  6. package/dist/account.mjs +181 -0
  7. package/dist/account.mjs.map +1 -0
  8. package/dist/bcs/integers.mjs +23 -0
  9. package/dist/bcs/integers.mjs.map +1 -0
  10. package/dist/contracts/account/account.d.mts +382 -0
  11. package/dist/contracts/account/account.d.mts.map +1 -0
  12. package/dist/contracts/account/account.mjs +443 -0
  13. package/dist/contracts/account/account.mjs.map +1 -0
  14. package/dist/contracts/account/account_events.d.mts +41 -0
  15. package/dist/contracts/account/account_events.d.mts.map +1 -0
  16. package/dist/contracts/account/account_events.mjs +72 -0
  17. package/dist/contracts/account/account_events.mjs.map +1 -0
  18. package/dist/contracts/account/account_registry.d.mts +210 -0
  19. package/dist/contracts/account/account_registry.d.mts.map +1 -0
  20. package/dist/contracts/account/account_registry.mjs +263 -0
  21. package/dist/contracts/account/account_registry.mjs.map +1 -0
  22. package/dist/contracts/account/config-arguments.d.mts +10 -0
  23. package/dist/contracts/account/config-arguments.d.mts.map +1 -0
  24. package/dist/contracts/account/deps/sui/bag.mjs +44 -0
  25. package/dist/contracts/account/deps/sui/bag.mjs.map +1 -0
  26. package/dist/contracts/deepbook/account.d.mts +18 -18
  27. package/dist/contracts/deepbook/balances.d.mts +4 -4
  28. package/dist/contracts/deepbook/deep_price.d.mts +3 -3
  29. package/dist/contracts/deepbook/deep_price.d.mts.map +1 -1
  30. package/dist/contracts/deepbook/order.d.mts +12 -12
  31. package/dist/contracts/deepbook_predict/builder_code_events.mjs +38 -0
  32. package/dist/contracts/deepbook_predict/builder_code_events.mjs.map +1 -0
  33. package/dist/contracts/deepbook_predict/config-arguments.d.mts +13 -0
  34. package/dist/contracts/deepbook_predict/config-arguments.d.mts.map +1 -0
  35. package/dist/contracts/deepbook_predict/deps/fixed_math/i64.mjs +25 -0
  36. package/dist/contracts/deepbook_predict/deps/fixed_math/i64.mjs.map +1 -0
  37. package/dist/contracts/deepbook_predict/deps/sui/balance.mjs +25 -0
  38. package/dist/contracts/deepbook_predict/deps/sui/balance.mjs.map +1 -0
  39. package/dist/contracts/deepbook_predict/deps/sui/coin.mjs +24 -0
  40. package/dist/contracts/deepbook_predict/deps/sui/coin.mjs.map +1 -0
  41. package/dist/contracts/deepbook_predict/deps/sui/table.mjs +39 -0
  42. package/dist/contracts/deepbook_predict/deps/sui/table.mjs.map +1 -0
  43. package/dist/contracts/deepbook_predict/deps/sui/vec_set.mjs +26 -0
  44. package/dist/contracts/deepbook_predict/deps/sui/vec_set.mjs.map +1 -0
  45. package/dist/contracts/deepbook_predict/ewma.mjs +29 -0
  46. package/dist/contracts/deepbook_predict/ewma.mjs.map +1 -0
  47. package/dist/contracts/deepbook_predict/expiry_cash.mjs +27 -0
  48. package/dist/contracts/deepbook_predict/expiry_cash.mjs.map +1 -0
  49. package/dist/contracts/deepbook_predict/expiry_market.mjs +367 -0
  50. package/dist/contracts/deepbook_predict/expiry_market.mjs.map +1 -0
  51. package/dist/contracts/deepbook_predict/lp_book.mjs +92 -0
  52. package/dist/contracts/deepbook_predict/lp_book.mjs.map +1 -0
  53. package/dist/contracts/deepbook_predict/market_manager.mjs +59 -0
  54. package/dist/contracts/deepbook_predict/market_manager.mjs.map +1 -0
  55. package/dist/contracts/deepbook_predict/order.mjs +26 -0
  56. package/dist/contracts/deepbook_predict/order.mjs.map +1 -0
  57. package/dist/contracts/deepbook_predict/order_events.mjs +84 -0
  58. package/dist/contracts/deepbook_predict/order_events.mjs.map +1 -0
  59. package/dist/contracts/deepbook_predict/plp.mjs +284 -0
  60. package/dist/contracts/deepbook_predict/plp.mjs.map +1 -0
  61. package/dist/contracts/deepbook_predict/pool_accounting.mjs +58 -0
  62. package/dist/contracts/deepbook_predict/pool_accounting.mjs.map +1 -0
  63. package/dist/contracts/deepbook_predict/predict_account.mjs +106 -0
  64. package/dist/contracts/deepbook_predict/predict_account.mjs.map +1 -0
  65. package/dist/contracts/deepbook_predict/pricing.mjs +80 -0
  66. package/dist/contracts/deepbook_predict/pricing.mjs.map +1 -0
  67. package/dist/contracts/deepbook_predict/range_codec.mjs +41 -0
  68. package/dist/contracts/deepbook_predict/range_codec.mjs.map +1 -0
  69. package/dist/contracts/deepbook_predict/registry.mjs +55 -0
  70. package/dist/contracts/deepbook_predict/registry.mjs.map +1 -0
  71. package/dist/contracts/deepbook_predict/strike_exposure.mjs +65 -0
  72. package/dist/contracts/deepbook_predict/strike_exposure.mjs.map +1 -0
  73. package/dist/contracts/deepbook_predict/strike_exposure_config.mjs +33 -0
  74. package/dist/contracts/deepbook_predict/strike_exposure_config.mjs.map +1 -0
  75. package/dist/contracts/deepbook_predict/strike_payout_tree.mjs +72 -0
  76. package/dist/contracts/deepbook_predict/strike_payout_tree.mjs.map +1 -0
  77. package/dist/contracts/deepbook_predict/vault_events.mjs +184 -0
  78. package/dist/contracts/deepbook_predict/vault_events.mjs.map +1 -0
  79. package/dist/contracts/deepbook_sessions/config-arguments.d.mts +10 -0
  80. package/dist/contracts/deepbook_sessions/config-arguments.d.mts.map +1 -0
  81. package/dist/contracts/deepbook_sessions/deps/sui/vec_map.mjs +37 -0
  82. package/dist/contracts/deepbook_sessions/deps/sui/vec_map.mjs.map +1 -0
  83. package/dist/contracts/deepbook_sessions/session_config.d.mts +63 -0
  84. package/dist/contracts/deepbook_sessions/session_config.d.mts.map +1 -0
  85. package/dist/contracts/deepbook_sessions/session_config.mjs +82 -0
  86. package/dist/contracts/deepbook_sessions/session_config.mjs.map +1 -0
  87. package/dist/contracts/deepbook_sessions/sessions.d.mts +275 -0
  88. package/dist/contracts/deepbook_sessions/sessions.d.mts.map +1 -0
  89. package/dist/contracts/deepbook_sessions/sessions.mjs +443 -0
  90. package/dist/contracts/deepbook_sessions/sessions.mjs.map +1 -0
  91. package/dist/contracts/utils/index.d.mts +18 -1
  92. package/dist/contracts/utils/index.d.mts.map +1 -1
  93. package/dist/deployments/index.d.mts +31 -0
  94. package/dist/deployments/index.d.mts.map +1 -0
  95. package/dist/deployments/index.mjs +30 -0
  96. package/dist/deployments/index.mjs.map +1 -0
  97. package/dist/deployments/testnet.d.mts +14 -0
  98. package/dist/deployments/testnet.d.mts.map +1 -0
  99. package/dist/deployments/testnet.mjs +73 -0
  100. package/dist/deployments/testnet.mjs.map +1 -0
  101. package/dist/deployments/types.d.mts +36 -0
  102. package/dist/deployments/types.d.mts.map +1 -0
  103. package/dist/index.mjs +0 -1
  104. package/dist/predict/client.d.mts +255 -0
  105. package/dist/predict/client.d.mts.map +1 -0
  106. package/dist/predict/client.mjs +436 -0
  107. package/dist/predict/client.mjs.map +1 -0
  108. package/dist/predict/config/generated.d.mts +29 -0
  109. package/dist/predict/config/generated.d.mts.map +1 -0
  110. package/dist/predict/config/generated.mjs +16 -0
  111. package/dist/predict/config/generated.mjs.map +1 -0
  112. package/dist/predict/config/index.d.mts +10 -0
  113. package/dist/predict/config/index.d.mts.map +1 -0
  114. package/dist/predict/config/index.mjs +13 -0
  115. package/dist/predict/config/index.mjs.map +1 -0
  116. package/dist/predict/config/testnet.d.mts +17 -0
  117. package/dist/predict/config/testnet.d.mts.map +1 -0
  118. package/dist/predict/config/testnet.mjs +26 -0
  119. package/dist/predict/config/testnet.mjs.map +1 -0
  120. package/dist/predict/config/types.d.mts +55 -0
  121. package/dist/predict/config/types.d.mts.map +1 -0
  122. package/dist/predict/decode.d.mts +162 -0
  123. package/dist/predict/decode.d.mts.map +1 -0
  124. package/dist/predict/decode.mjs +175 -0
  125. package/dist/predict/decode.mjs.map +1 -0
  126. package/dist/predict/errors.d.mts +42 -0
  127. package/dist/predict/errors.d.mts.map +1 -0
  128. package/dist/predict/errors.mjs +37 -0
  129. package/dist/predict/errors.mjs.map +1 -0
  130. package/dist/predict/index.d.mts +18 -0
  131. package/dist/predict/index.mjs +14 -0
  132. package/dist/predict/pricing.d.mts +73 -0
  133. package/dist/predict/pricing.d.mts.map +1 -0
  134. package/dist/predict/pricing.mjs +129 -0
  135. package/dist/predict/pricing.mjs.map +1 -0
  136. package/dist/predict/reads/balances.mjs +41 -0
  137. package/dist/predict/reads/balances.mjs.map +1 -0
  138. package/dist/predict/reads/inspect.d.mts +8 -0
  139. package/dist/predict/reads/inspect.d.mts.map +1 -0
  140. package/dist/predict/reads/inspect.mjs +37 -0
  141. package/dist/predict/reads/inspect.mjs.map +1 -0
  142. package/dist/predict/reads/markets.mjs +130 -0
  143. package/dist/predict/reads/markets.mjs.map +1 -0
  144. package/dist/predict/reads/parse.mjs +20 -0
  145. package/dist/predict/reads/parse.mjs.map +1 -0
  146. package/dist/predict/reads/pool.mjs +26 -0
  147. package/dist/predict/reads/pool.mjs.map +1 -0
  148. package/dist/predict/reads/positions.d.mts +12 -0
  149. package/dist/predict/reads/positions.d.mts.map +1 -0
  150. package/dist/predict/reads/positions.mjs +77 -0
  151. package/dist/predict/reads/positions.mjs.map +1 -0
  152. package/dist/predict/reads/pricing.d.mts +20 -0
  153. package/dist/predict/reads/pricing.d.mts.map +1 -0
  154. package/dist/predict/reads/pricing.mjs +43 -0
  155. package/dist/predict/reads/pricing.mjs.map +1 -0
  156. package/dist/predict/ticks.d.mts +11 -0
  157. package/dist/predict/ticks.d.mts.map +1 -0
  158. package/dist/predict/ticks.mjs +21 -0
  159. package/dist/predict/ticks.mjs.map +1 -0
  160. package/dist/predict/tx/authed.mjs +18 -0
  161. package/dist/predict/tx/authed.mjs.map +1 -0
  162. package/dist/predict/tx/common.d.mts +18 -0
  163. package/dist/predict/tx/common.d.mts.map +1 -0
  164. package/dist/predict/tx/common.mjs +52 -0
  165. package/dist/predict/tx/common.mjs.map +1 -0
  166. package/dist/predict/tx/trade.d.mts +15 -0
  167. package/dist/predict/tx/trade.d.mts.map +1 -0
  168. package/dist/predict/tx/trade.mjs +86 -0
  169. package/dist/predict/tx/trade.mjs.map +1 -0
  170. package/dist/predict/units.d.mts +11 -0
  171. package/dist/predict/units.d.mts.map +1 -0
  172. package/dist/predict/units.mjs +44 -0
  173. package/dist/predict/units.mjs.map +1 -0
  174. package/dist/queries/accountQueries.mjs +0 -1
  175. package/dist/queries/accountQueries.mjs.map +1 -1
  176. package/dist/queries/orderQueries.mjs +0 -1
  177. package/dist/queries/orderQueries.mjs.map +1 -1
  178. package/dist/queries/registryQueries.mjs +0 -1
  179. package/dist/queries/registryQueries.mjs.map +1 -1
  180. package/dist/sessions.d.mts +244 -0
  181. package/dist/sessions.d.mts.map +1 -0
  182. package/dist/sessions.mjs +291 -0
  183. package/dist/sessions.mjs.map +1 -0
  184. package/dist/transactions/balanceManager.d.mts +12 -12
  185. package/dist/transactions/balanceManager.d.mts.map +1 -1
  186. package/dist/transactions/deepbook.d.mts +20 -20
  187. package/dist/transactions/deepbook.d.mts.map +1 -1
  188. package/dist/transactions/deepbookAdmin.d.mts +4 -4
  189. package/dist/transactions/marginAdmin.d.mts +7 -7
  190. package/dist/transactions/marginLiquidations.d.mts +3 -3
  191. package/dist/transactions/marginMaintainer.d.mts +5 -5
  192. package/dist/transactions/marginManager.d.mts +32 -32
  193. package/dist/transactions/marginPool.d.mts +18 -18
  194. package/dist/transactions/marginRegistry.d.mts +16 -16
  195. package/dist/transactions/marginTPSL.d.mts +10 -10
  196. package/dist/transactions/poolProxy.d.mts +8 -8
  197. package/package.json +25 -4
  198. package/src/account.ts +239 -0
  199. package/src/bcs/integers.ts +35 -0
  200. package/src/contracts/account/account.ts +706 -0
  201. package/src/contracts/account/account_events.ts +64 -0
  202. package/src/contracts/account/account_registry.ts +468 -0
  203. package/src/contracts/account/config-arguments.ts +8 -0
  204. package/src/contracts/account/deps/sui/bag.ts +42 -0
  205. package/src/contracts/deepbook_predict/admin.ts +44 -0
  206. package/src/contracts/deepbook_predict/builder_code.ts +155 -0
  207. package/src/contracts/deepbook_predict/builder_code_events.ts +34 -0
  208. package/src/contracts/deepbook_predict/config-arguments.ts +11 -0
  209. package/src/contracts/deepbook_predict/config_events.ts +140 -0
  210. package/src/contracts/deepbook_predict/deps/fixed_math/i64.ts +21 -0
  211. package/src/contracts/deepbook_predict/deps/sui/balance.ts +25 -0
  212. package/src/contracts/deepbook_predict/deps/sui/coin.ts +20 -0
  213. package/src/contracts/deepbook_predict/deps/sui/table.ts +37 -0
  214. package/src/contracts/deepbook_predict/deps/sui/vec_set.ts +22 -0
  215. package/src/contracts/deepbook_predict/ewma.ts +26 -0
  216. package/src/contracts/deepbook_predict/ewma_config.ts +28 -0
  217. package/src/contracts/deepbook_predict/expiry_cash.ts +24 -0
  218. package/src/contracts/deepbook_predict/expiry_market.ts +1549 -0
  219. package/src/contracts/deepbook_predict/lp_book.ts +105 -0
  220. package/src/contracts/deepbook_predict/market_lifecycle_cap.ts +75 -0
  221. package/src/contracts/deepbook_predict/market_manager.ts +218 -0
  222. package/src/contracts/deepbook_predict/order.ts +24 -0
  223. package/src/contracts/deepbook_predict/order_events.ts +129 -0
  224. package/src/contracts/deepbook_predict/pause_cap.ts +68 -0
  225. package/src/contracts/deepbook_predict/plp.ts +987 -0
  226. package/src/contracts/deepbook_predict/pool_accounting.ts +79 -0
  227. package/src/contracts/deepbook_predict/predict_account.ts +180 -0
  228. package/src/contracts/deepbook_predict/pricing.ts +124 -0
  229. package/src/contracts/deepbook_predict/pricing_config.ts +41 -0
  230. package/src/contracts/deepbook_predict/protocol_config.ts +1066 -0
  231. package/src/contracts/deepbook_predict/range_codec.ts +53 -0
  232. package/src/contracts/deepbook_predict/registry.ts +694 -0
  233. package/src/contracts/deepbook_predict/strike_exposure.ts +77 -0
  234. package/src/contracts/deepbook_predict/strike_exposure_config.ts +46 -0
  235. package/src/contracts/deepbook_predict/strike_payout_tree.ts +89 -0
  236. package/src/contracts/deepbook_predict/vault_events.ts +222 -0
  237. package/src/contracts/deepbook_sessions/config-arguments.ts +8 -0
  238. package/src/contracts/deepbook_sessions/deps/sui/vec_map.ts +33 -0
  239. package/src/contracts/deepbook_sessions/session_config.ts +130 -0
  240. package/src/contracts/deepbook_sessions/sessions.ts +663 -0
  241. package/src/contracts/propbook/block_scholes_store.ts +842 -0
  242. package/src/contracts/propbook/deps/sui/table.ts +37 -0
  243. package/src/contracts/propbook/oracle_lane.ts +170 -0
  244. package/src/contracts/propbook/pyth_feed.ts +414 -0
  245. package/src/contracts/propbook/registry.ts +596 -0
  246. package/src/deployments/index.ts +73 -0
  247. package/src/deployments/testnet.ts +107 -0
  248. package/src/deployments/types.ts +81 -0
  249. package/src/index.ts +5 -0
  250. package/src/predict/client.ts +941 -0
  251. package/src/predict/config/generated.ts +39 -0
  252. package/src/predict/config/index.ts +25 -0
  253. package/src/predict/config/testnet.ts +27 -0
  254. package/src/predict/config/types.ts +52 -0
  255. package/src/predict/decode.ts +476 -0
  256. package/src/predict/errors.ts +83 -0
  257. package/src/predict/index.ts +94 -0
  258. package/src/predict/pricing.ts +199 -0
  259. package/src/predict/reads/balances.ts +55 -0
  260. package/src/predict/reads/inspect.ts +78 -0
  261. package/src/predict/reads/markets.ts +222 -0
  262. package/src/predict/reads/parse.ts +30 -0
  263. package/src/predict/reads/pool.ts +37 -0
  264. package/src/predict/reads/positions.ts +144 -0
  265. package/src/predict/reads/pricing.ts +79 -0
  266. package/src/predict/ticks.ts +42 -0
  267. package/src/predict/tx/authed.ts +20 -0
  268. package/src/predict/tx/common.ts +92 -0
  269. package/src/predict/tx/trade.ts +183 -0
  270. package/src/predict/units.ts +49 -0
  271. package/src/sessions.ts +471 -0
  272. package/dist/types/bcs.mjs +0 -7
@@ -0,0 +1,471 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { bcs } from '@mysten/sui/bcs';
4
+ import type { Transaction, TransactionArgument, TransactionResult } from '@mysten/sui/transactions';
5
+ import { deriveDynamicFieldID, deriveObjectID } from '@mysten/sui/utils';
6
+
7
+ import { TESTNET_SESSIONS } from './deployments/testnet.js';
8
+ import type { NetworkArg } from './deployments/index.js';
9
+
10
+ // Provenance and scale constants, so a sessions-only consumer can answer "which deployment
11
+ // is this pinned to?" and format a custody balance without importing another subpath.
12
+ export { getDeployment, getUnits, TESTNET_DEPLOYMENT, TESTNET_UNITS } from './deployments/index.js';
13
+ export type { DeployedNetwork, NetworkArg } from './deployments/index.js';
14
+
15
+ import { AccountContract } from './account.js';
16
+ import type { DeepbookSessionsConfig } from './contracts/deepbook_sessions/config-arguments.js';
17
+ import * as sessions from './contracts/deepbook_sessions/sessions.js';
18
+ import { SessionsData } from './contracts/deepbook_sessions/sessions.js';
19
+
20
+ /**
21
+ * Deployed ids the sessions builders address.
22
+ *
23
+ * `accountPackageId` / `accountRegistry` are the same shared-account ids
24
+ * {@link AccountContract} takes — sessions is an Account app, so it addresses the same
25
+ * registry. `sessionsPackageId` and `sessionsConfig` come from the sessions deployment.
26
+ */
27
+ export interface SessionsConfig extends DeepbookSessionsConfig {
28
+ /** The `deepbook_sessions` Move package id. */
29
+ sessionsPackageId: string;
30
+ /** The shared `SessionsConfig` object id. */
31
+ sessionsConfig: string;
32
+ /** The shared `account` Move package id. */
33
+ accountPackageId: string;
34
+ /** The shared `AccountRegistry` object id. */
35
+ accountRegistry: string;
36
+ }
37
+
38
+ /**
39
+ * Ids the DeepBook **spot** session wrappers need on top of {@link SessionsConfig}. The
40
+ * Predict wrappers never take these, so they are kept off the main config rather than made
41
+ * optional there.
42
+ */
43
+ export interface SessionsSpotIds {
44
+ /** DeepBook's shared `Registry` — `deepbook_registry` on the spot entrypoints. */
45
+ deepbookRegistry: string;
46
+ /** The `deepbook_core_account` Move package id. */
47
+ deepbookCoreAccountPackageId: string;
48
+ }
49
+
50
+ /** Ids the Predict session wrappers need beyond {@link SessionsConfig}. */
51
+ export interface SessionsPredictIds {
52
+ /** Every Predict wrapper takes `config: &ProtocolConfig`. */
53
+ protocolConfig: string;
54
+ }
55
+
56
+ /**
57
+ * @description The deployed sessions ids for `network`, so a caller does not transcribe
58
+ * them. Generated from the deploy manifest — see `src/deployments/`. The returned object
59
+ * also carries {@link SessionsSpotIds} for the generated spot wrappers.
60
+ * @throws if the network has no recorded deployment, rather than returning placeholder ids.
61
+ *
62
+ * ```ts
63
+ * const sessions = new SessionsContract(getSessionsConfig('testnet'));
64
+ * ```
65
+ */
66
+ export function getSessionsConfig(
67
+ network: NetworkArg,
68
+ ): SessionsConfig & SessionsSpotIds & SessionsPredictIds {
69
+ // The frozen record itself, matching `/account` — returning a spread here would have
70
+ // quietly exempted this subpath from the immutability the others guarantee.
71
+ if (network === 'testnet') return TESTNET_SESSIONS;
72
+ throw new Error(
73
+ `@mysten/deepbook-v3/sessions: no sessions deployment recorded for network '${network}'. ` +
74
+ 'Sessions is testnet-only today; for your own deployment construct a `SessionsConfig` ' +
75
+ 'and pass it to SessionsContract directly.',
76
+ );
77
+ }
78
+
79
+ /** The maximum session duration the contract accepts: 30 days, in milliseconds. */
80
+ export const MAX_SESSION_DURATION_MS = 30 * 24 * 60 * 60 * 1000;
81
+
82
+ /** The maximum number of distinct session addresses one Account may store. */
83
+ export const MAX_SESSIONS_PER_ACCOUNT = 20;
84
+
85
+ // `sui::dynamic_field::Field<DataKey<SessionsApp>, SessionsData>` — what a core `getObject`
86
+ // on the grant field actually returns. `DataKey` is source-empty, but Move inserts a
87
+ // hidden `dummy_field: bool` into empty structs, so the name occupies ONE zero byte
88
+ // between the id and the value. Decoding the value alone would read the field id's first
89
+ // byte as the VecMap length: garbage grants, or a silent empty list.
90
+ const SessionsDataField = bcs.struct('Field<DataKey,SessionsData>', {
91
+ id: bcs.Address,
92
+ name: bcs.bool(), // DataKey's hidden dummy_field
93
+ value: SessionsData,
94
+ });
95
+
96
+ // `AccountKey(owner)` — the canonical ACCOUNT identity, a different derived object from
97
+ // the wrapper. Grant data hangs off this one.
98
+ const AccountKey = bcs.struct('AccountKey', { pos0: bcs.Address });
99
+
100
+ /** One stored session grant. */
101
+ export interface SessionGrant {
102
+ /** The authorized ephemeral address. */
103
+ session: string;
104
+ /** Absolute expiry, ms since epoch. The grant is dead AT this timestamp (strict `<`). */
105
+ expiresAtMs: bigint;
106
+ }
107
+
108
+ /**
109
+ * SessionsContract — time-limited trading sessions over a canonical Account.
110
+ *
111
+ * An Account owner authorizes an ephemeral address to submit a bounded set of
112
+ * transactions on the Account's behalf until a fixed expiry. The session key never
113
+ * receives a reusable `Auth`: each wrapper mints app authorization internally and
114
+ * consumes it in the same call.
115
+ *
116
+ * WHAT A SESSION KEY CAN DO. It cannot withdraw to an address, cannot grant or revoke
117
+ * sessions, and cannot outlive its expiry — those all require owner auth. It CAN trade
118
+ * the Account's full balance: the spot wrappers take a caller-chosen `Pool` and, through
119
+ * `deepbook_core_account`, pull the account's entire Base, Quote and DEEP balance
120
+ * (stored plus unsettled) into the embedded manager for the duration of the call, with
121
+ * `price_limit` supplied by the caller. Nothing caps notional, restricts which pools are
122
+ * reachable, or bounds loss to adverse pricing. Treat a session key as authority over
123
+ * everything the Account holds, and fund an ephemeral-session Account accordingly.
124
+ *
125
+ * Operational precondition: an admin must have authorized `SessionsApp` on the account
126
+ * registry. Until then — or after a `deauthorize_app` — the TRADING wrappers abort with
127
+ * `EAppNotAuthorized`; `authorizeSession`, `revokeSession` and `sessionExpirationMs` use
128
+ * owner auth or no auth and keep working. Note that `deauthorize_app` does not clear
129
+ * `SessionsData`, so re-authorizing makes every still-unexpired grant live again at once
130
+ * — it is a pause, not a kill switch. Revoking, and reading expirations, also keep
131
+ * working if the sessions package is later version-gated.
132
+ *
133
+ * This class wraps the session lifecycle and the **Predict** entrypoints. The DeepBook
134
+ * spot session wrappers are generated (see `sessionsMoveCalls`) but are not wrapped here
135
+ * — note `placeLimitOrder` and `placeMarketOrder` put `accountRegistry` at index 2 and
136
+ * `sessionsConfig` at 4, because `deepbookRegistry` sits between them; the other three
137
+ * (`cancelLiveOrder`, `cancelLiveOrders`, `withdrawSettledAmounts`) take no
138
+ * `deepbookRegistry` and so keep 1 and 3 like the Predict wrappers:
139
+ * the surrounding spot-over-Account workflow — discovering the embedded balance manager,
140
+ * reading resting orders and locked balances — is not modelled yet, so a wrapped builder
141
+ * would be hard to use well. They are reachable from the generated bindings meanwhile.
142
+ */
143
+ export class SessionsContract {
144
+ #config: SessionsConfig;
145
+
146
+ constructor(config: SessionsConfig) {
147
+ this.#config = config;
148
+ }
149
+
150
+ // The generated thunks resolve the package address and auto-inject the shared
151
+ // `SessionsConfig` from this object. `accountRegistry` stays an explicit argument —
152
+ // it belongs to the account package, which is a separate codegen entry.
153
+ get #generatedConfig() {
154
+ return {
155
+ sessionsPackageId: this.#config.sessionsPackageId,
156
+ sessionsConfig: this.#config.sessionsConfig,
157
+ };
158
+ }
159
+
160
+ /**
161
+ * @description The owner's canonical `AccountWrapper` id — derived off-chain, no read.
162
+ * Every builder here takes that id.
163
+ */
164
+ deriveAccountWrapperId(owner: string): string {
165
+ return new AccountContract({
166
+ accountPackageId: this.#config.accountPackageId,
167
+ accountRegistry: this.#config.accountRegistry,
168
+ }).deriveAccountWrapperId(owner);
169
+ }
170
+
171
+ /**
172
+ * @description The owner's canonical ACCOUNT id — a different derived object from the
173
+ * wrapper. The session grants hang off this one, so this is what
174
+ * {@link deriveSessionsFieldId} and {@link decodeSessions} work from.
175
+ */
176
+ deriveAccountId(owner: string): string {
177
+ return deriveObjectID(
178
+ this.#config.accountRegistry,
179
+ `${this.#config.accountPackageId}::account_registry::AccountKey`,
180
+ AccountKey.serialize({ pos0: owner }).toBytes(),
181
+ );
182
+ }
183
+
184
+ /**
185
+ * @description The object id of the owner's `DataKey<SessionsApp>` dynamic field —
186
+ * fetch this object and pass its BCS contents to {@link decodeSessions}. There is no
187
+ * bulk on-chain read, so this is the route to enumerating grants.
188
+ */
189
+ deriveSessionsFieldId(owner: string): string {
190
+ // A PLAIN dynamic field, not a derived object: `account::attach` writes it with
191
+ // `df::add` (`use fun df::add as UID.add`), whereas the account and wrapper ids are
192
+ // claimed through `derived_object::claim`. `deriveObjectID` would wrap the tag in
193
+ // `0x2::derived_object::DerivedObjectKey<..>` and yield an id that points at nothing.
194
+ return deriveDynamicFieldID(
195
+ this.deriveAccountId(owner),
196
+ `${this.#config.accountPackageId}::account::DataKey<${this.#config.sessionsPackageId}::sessions::SessionsApp>`,
197
+ // DataKey is source-empty; Move's hidden `dummy_field: bool` is the key's one byte.
198
+ new Uint8Array([0]),
199
+ );
200
+ }
201
+
202
+ /**
203
+ * @description Grant `session` authority over the Account until `now + durationMs`.
204
+ * Authority is derived from the transaction SENDER, so the owner must sign this.
205
+ * `durationMs` must be > 0 and <= {@link MAX_SESSION_DURATION_MS}; an Account holds at
206
+ * most {@link MAX_SESSIONS_PER_ACCOUNT} distinct addresses. Re-authorizing an address
207
+ * replaces its expiry in place and consumes no additional slot.
208
+ * @returns A function that takes a Transaction object
209
+ */
210
+ authorizeSession(params: { wrapperId: string; session: string; durationMs: number | bigint }) {
211
+ return (tx: Transaction): void => {
212
+ tx.add(
213
+ sessions.authorizeSession({
214
+ config: this.#generatedConfig,
215
+ arguments: {
216
+ wrapper: params.wrapperId,
217
+ session: params.session,
218
+ durationMs: params.durationMs,
219
+ },
220
+ }),
221
+ );
222
+ };
223
+ }
224
+
225
+ /**
226
+ * @description Remove `session`'s grant. Owner-signed, like `authorizeSession`.
227
+ * Deliberately takes no `SessionsConfig`: revocation is not version-gated, so it keeps
228
+ * working after the package is retired. Revoking an address that holds no grant is a
229
+ * silent no-op — it neither aborts nor emits, so read before and after if you need to
230
+ * distinguish "revoked" from "was never granted".
231
+ * @returns A function that takes a Transaction object
232
+ */
233
+ revokeSession(params: { wrapperId: string; session: string }) {
234
+ return (tx: Transaction): void => {
235
+ tx.add(
236
+ sessions.revokeSession({
237
+ config: this.#generatedConfig,
238
+ arguments: { wrapper: params.wrapperId, session: params.session },
239
+ }),
240
+ );
241
+ };
242
+ }
243
+
244
+ /**
245
+ * @description Read one session's absolute expiry as `Option<u64>`. Compose in a
246
+ * dev-inspect/simulate PTB and decode the returned BCS. Not version-gated.
247
+ * @returns A function that takes a Transaction object
248
+ */
249
+ sessionExpirationMs(params: { wrapperId: string; session: string }) {
250
+ return (tx: Transaction): TransactionResult =>
251
+ tx.add(
252
+ sessions.sessionExpirationMs({
253
+ config: this.#generatedConfig,
254
+ arguments: { wrapper: params.wrapperId, session: params.session },
255
+ }),
256
+ );
257
+ }
258
+
259
+ // === Predict wrappers ===
260
+ //
261
+ // Each mirrors the Predict entrypoint of the same name, with two differences: the
262
+ // caller supplies NO `Auth` (the wrapper mints and consumes app authorization
263
+ // internally), and `accountRegistry` + `sessionsConfig` are threaded in. `pricer` is a
264
+ // PTB RESULT, not an object — it comes from a preceding `expiry_market::load_live_pricer`
265
+ // command in the same transaction. Everything else matches Predict exactly, and Predict
266
+ // still performs all parameter validation.
267
+
268
+ /**
269
+ * @description Mint a position of an exact payout quantity, as `session`. Pass
270
+ * `u64::MAX` for `maxCost` / `maxProbability` to leave either slippage cap
271
+ * effectively unbounded — the chain asserts `value <= cap`, so the max value can never
272
+ * trip. Both are required; there is no default.
273
+ * @returns A function that takes a Transaction object and returns the new order id (u256)
274
+ */
275
+ mintExactQuantity(params: {
276
+ expiryMarketId: string;
277
+ wrapperId: string;
278
+ protocolConfig: string;
279
+ pricer: TransactionArgument;
280
+ lowerTick: number | bigint;
281
+ higherTick: number | bigint;
282
+ quantity: number | bigint;
283
+ maxCost: number | bigint;
284
+ maxProbability: number | bigint;
285
+ }) {
286
+ return (tx: Transaction): TransactionResult =>
287
+ tx.add(
288
+ sessions.mintExactQuantity({
289
+ config: this.#generatedConfig,
290
+ arguments: {
291
+ market: params.expiryMarketId,
292
+ accountRegistry: this.#config.accountRegistry,
293
+ wrapper: params.wrapperId,
294
+ config: params.protocolConfig,
295
+ pricer: params.pricer,
296
+ lowerTick: params.lowerTick,
297
+ higherTick: params.higherTick,
298
+ quantity: params.quantity,
299
+ maxCost: params.maxCost,
300
+ maxProbability: params.maxProbability,
301
+ },
302
+ }),
303
+ );
304
+ }
305
+
306
+ /**
307
+ * @description Mint by spending up to a premium budget, flooring the quantity received.
308
+ * The chain requires `maxCost > 0`.
309
+ * @returns A function that takes a Transaction object and returns the new order id (u256)
310
+ */
311
+ mintExactAmount(params: {
312
+ expiryMarketId: string;
313
+ wrapperId: string;
314
+ protocolConfig: string;
315
+ pricer: TransactionArgument;
316
+ lowerTick: number | bigint;
317
+ higherTick: number | bigint;
318
+ maxPremium: number | bigint;
319
+ minQuantity: number | bigint;
320
+ maxCost: number | bigint;
321
+ }) {
322
+ return (tx: Transaction): TransactionResult =>
323
+ tx.add(
324
+ sessions.mintExactAmount({
325
+ config: this.#generatedConfig,
326
+ arguments: {
327
+ market: params.expiryMarketId,
328
+ accountRegistry: this.#config.accountRegistry,
329
+ wrapper: params.wrapperId,
330
+ config: params.protocolConfig,
331
+ pricer: params.pricer,
332
+ lowerTick: params.lowerTick,
333
+ higherTick: params.higherTick,
334
+ maxPremium: params.maxPremium,
335
+ minQuantity: params.minQuantity,
336
+ maxCost: params.maxCost,
337
+ },
338
+ }),
339
+ );
340
+ }
341
+
342
+ /**
343
+ * @description Close part or all of a live position at the pricer's mark, as `session`.
344
+ * `minProbability` / `minProceeds` are close-side slippage floors; `0` disables either,
345
+ * and OMITTING them is `0` — unlike the mint caps, which are required. On a delegated
346
+ * key this is the direction that closes a position at any price, so pass real floors
347
+ * unless you mean to accept whatever the mark gives you.
348
+ * @returns A function that takes a Transaction object and returns `Option<u256>` — the
349
+ * replacement order id when a partial close leaves quantity open
350
+ */
351
+ redeemLive(params: {
352
+ expiryMarketId: string;
353
+ wrapperId: string;
354
+ protocolConfig: string;
355
+ pricer: TransactionArgument;
356
+ orderId: bigint;
357
+ closeQuantity: number | bigint;
358
+ minProbability?: number | bigint;
359
+ minProceeds?: number | bigint;
360
+ }) {
361
+ return (tx: Transaction): TransactionResult =>
362
+ tx.add(
363
+ sessions.redeemLive({
364
+ config: this.#generatedConfig,
365
+ arguments: {
366
+ market: params.expiryMarketId,
367
+ accountRegistry: this.#config.accountRegistry,
368
+ wrapper: params.wrapperId,
369
+ config: params.protocolConfig,
370
+ pricer: params.pricer,
371
+ orderId: params.orderId,
372
+ closeQuantity: params.closeQuantity,
373
+ minProbability: params.minProbability ?? 0,
374
+ minProceeds: params.minProceeds ?? 0,
375
+ },
376
+ }),
377
+ );
378
+ }
379
+
380
+ /**
381
+ * @description Claim a settled position in full, as `session`. Takes no pricer — the
382
+ * settlement price is fixed — and no quantity: a settled claim is all-or-nothing.
383
+ * @returns A function that takes a Transaction object
384
+ */
385
+ redeemSettled(params: {
386
+ expiryMarketId: string;
387
+ wrapperId: string;
388
+ protocolConfig: string;
389
+ orderId: bigint;
390
+ }) {
391
+ return (tx: Transaction): void => {
392
+ tx.add(
393
+ sessions.redeemSettled({
394
+ config: this.#generatedConfig,
395
+ arguments: {
396
+ market: params.expiryMarketId,
397
+ accountRegistry: this.#config.accountRegistry,
398
+ wrapper: params.wrapperId,
399
+ config: params.protocolConfig,
400
+ orderId: params.orderId,
401
+ },
402
+ }),
403
+ );
404
+ };
405
+ }
406
+
407
+ /**
408
+ * @description Decode an Account's stored grants from the raw BCS content of its
409
+ * `DataKey<SessionsApp>` dynamic FIELD object — the whole
410
+ * `Field<DataKey<SessionsApp>, SessionsData>`, as the core API returns it, not the
411
+ * inner `SessionsData`. Get the id from {@link deriveSessionsFieldId}.
412
+ *
413
+ * There is no bulk on-chain read — `sessionExpirationMs` answers one address at a time —
414
+ * so listing grants means fetching that field and decoding it here. Note the field hangs
415
+ * off the DERIVED ACCOUNT address, not the wrapper address; they are different objects.
416
+ *
417
+ * Expired grants are never pruned automatically and keep occupying slots, so callers
418
+ * managing the {@link MAX_SESSIONS_PER_ACCOUNT} cap should list, drop anything already
419
+ * expired, and revoke before granting again.
420
+ */
421
+ static decodeSessions(contents: Uint8Array): SessionGrant[] {
422
+ // Assert full consumption. `@mysten/bcs`'s ULEB reader indexes the underlying
423
+ // ArrayBuffer rather than the view, so a truncated SUBARRAY — which is exactly what a
424
+ // gRPC `content` field is — can read past its bound and decode to `[]` with no error.
425
+ // Re-serializing and comparing lengths turns both truncation and trailing junk into a
426
+ // throw, so "no grants" can only ever mean no grants.
427
+ const field = SessionsDataField.parse(contents);
428
+ const reencoded = SessionsDataField.serialize(field).toBytes();
429
+ if (reencoded.length !== contents.length) {
430
+ throw new Error(
431
+ `sessions field is ${contents.length} bytes but its contents encode to ${reencoded.length}; ` +
432
+ 'the bytes are truncated or carry trailing data',
433
+ );
434
+ }
435
+ return field.value.sessions.contents.map((entry) => ({
436
+ session: entry.key,
437
+ expiresAtMs: BigInt(entry.value),
438
+ }));
439
+ }
440
+
441
+ /** Grants from {@link decodeSessions} that are still live at `nowMs`. */
442
+ static activeSessions(grants: readonly SessionGrant[], nowMs: number | bigint): SessionGrant[] {
443
+ const now = BigInt(nowMs);
444
+ // The chain asserts `now < expiresAtMs`, so a grant is dead AT its expiry.
445
+ return grants.filter((g) => now < g.expiresAtMs);
446
+ }
447
+
448
+ /**
449
+ * @description Grants that are already dead at `nowMs` — the complement of
450
+ * {@link activeSessions}, and the list to revoke when reclaiming slots. Use this rather
451
+ * than filtering by hand: `nowMs > expiresAtMs` looks equivalent but leaves the grant
452
+ * expiring exactly at `nowMs` occupying a slot forever.
453
+ */
454
+ static expiredSessions(grants: readonly SessionGrant[], nowMs: number | bigint): SessionGrant[] {
455
+ const now = BigInt(nowMs);
456
+ return grants.filter((g) => now >= g.expiresAtMs);
457
+ }
458
+ }
459
+
460
+ // === Generated bindings ===
461
+ // NOTE: this namespace carries the DeepBook spot session calls too
462
+ // (`placeLimitOrder`, `placeMarketOrder`, `cancelLiveOrder(s)`, `withdrawSettledAmounts`).
463
+ // They are generated and callable; they are simply not wrapped on `SessionsContract`.
464
+ export * as sessionsMoveCalls from './contracts/deepbook_sessions/sessions.js';
465
+ export * as sessionConfigMoveCalls from './contracts/deepbook_sessions/session_config.js';
466
+ export {
467
+ SessionsApp,
468
+ SessionsData,
469
+ SessionAuthorized,
470
+ SessionRevoked,
471
+ } from './contracts/deepbook_sessions/sessions.js';
@@ -1,7 +0,0 @@
1
- import { VecSet } from "../contracts/deepbook/deps/sui/vec_set.mjs";
2
- import { Balances } from "../contracts/deepbook/balances.mjs";
3
- import { Account } from "../contracts/deepbook/account.mjs";
4
- import { OrderDeepPrice } from "../contracts/deepbook/deep_price.mjs";
5
- import { Order } from "../contracts/deepbook/order.mjs";
6
-
7
- export { };