@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,39 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import type { AccountConfig } from '../../account.js';
4
+ import type { DeepbookPredictConfig } from '../../contracts/deepbook_predict/config-arguments.js';
5
+ import type { PredictConfig } from './types.js';
6
+
7
+ /**
8
+ * The flat config slice the generated bindings resolve `options.config` against, projected from
9
+ * the nested public `PredictConfig`. Each generated call declares only the keys it consumes, so a
10
+ * single object carrying all of them satisfies every call site.
11
+ *
12
+ * Typed as the intersection of the two generated interfaces on purpose: if codegen adds, drops, or
13
+ * renames a config key, this file stops compiling instead of silently building a PTB against the
14
+ * wrong object.
15
+ *
16
+ * `predictPackageId` is narrowed to `string` because codegen types package ids as optional, while
17
+ * this projection always supplies one — and the helper that hand-builds the `DataKey` type tag
18
+ * needs a `string`, not a maybe-absent `ConfigValue`. The two account ids restate a narrowing
19
+ * `AccountConfig` already applies, so they are redundant today; they are kept so this projection
20
+ * still compiles to plain ids if that package ever widens them back.
21
+ */
22
+ export type GeneratedConfig = DeepbookPredictConfig &
23
+ AccountConfig & {
24
+ predictPackageId: string;
25
+ accountPackageId: string;
26
+ accountRegistry: string;
27
+ };
28
+
29
+ export function toGeneratedConfig(cfg: PredictConfig): GeneratedConfig {
30
+ return {
31
+ predictPackageId: cfg.packages.predict,
32
+ accountPackageId: cfg.packages.account,
33
+ protocolConfig: cfg.objects.protocolConfig,
34
+ poolVault: cfg.objects.poolVault,
35
+ registry: cfg.objects.registry,
36
+ oracleRegistry: cfg.objects.oracleRegistry,
37
+ accountRegistry: cfg.objects.accountRegistry,
38
+ };
39
+ }
@@ -0,0 +1,25 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { TESTNET_CONFIG } from './testnet.js';
4
+ import type { PredictConfig } from './types.js';
5
+ import type { NetworkArg } from '../../deployments/index.js';
6
+
7
+ export { TESTNET_CONFIG };
8
+ export type { PredictConfig, PredictPackages, UnderlyingConfig } from './types.js';
9
+
10
+ export function getConfig(network: NetworkArg): PredictConfig {
11
+ if (network === 'testnet') return TESTNET_CONFIG;
12
+ throw new Error(
13
+ `@mysten/deepbook-v3/predict: no Predict deployment recorded for network '${network}'. ` +
14
+ 'Predict is testnet-only today; for your own deployment pass `config` to PredictClient ' +
15
+ 'or `predict({ config })`.',
16
+ );
17
+ }
18
+
19
+ // Provenance: which on-chain deployment these ids came from.
20
+ export {
21
+ getDeployment,
22
+ getUnits,
23
+ TESTNET_DEPLOYMENT,
24
+ TESTNET_UNITS,
25
+ } from '../../deployments/index.js';
@@ -0,0 +1,27 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { TESTNET_PREDICT } from '../../deployments/testnet.js';
5
+ import type { PredictConfig } from './types.js';
6
+
7
+ /**
8
+ * Testnet deployment constants, read from the shared generated record in
9
+ * `src/deployments/` rather than transcribed here. `/account` and `/sessions` read slices of
10
+ * that same record, so a redeploy (`pnpm sync-deployment`) moves every subpath together and
11
+ * they cannot end up addressing different deployments.
12
+ *
13
+ * The record is generated from the deploy tooling's `deployment.testnet.json`; see
14
+ * `scripts/sync-deployment.ts`. `getDeployment('testnet')` names the deployment and the
15
+ * deepbookv3 commit these ids came from.
16
+ */
17
+ export const TESTNET_CONFIG: PredictConfig = Object.freeze({
18
+ // The record widens `network` to `string` so the published types don't pin a literal;
19
+ // this file is the testnet config by construction.
20
+ network: 'testnet',
21
+ packages: TESTNET_PREDICT.packages,
22
+ objects: TESTNET_PREDICT.objects,
23
+ quoteCoinType: TESTNET_PREDICT.quoteCoinType,
24
+ coinTypes: TESTNET_PREDICT.coinTypes,
25
+ units: TESTNET_PREDICT.units,
26
+ underlyings: TESTNET_PREDICT.underlyings,
27
+ });
@@ -0,0 +1,52 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ /** The three published Move packages a Predict deployment spans. */
4
+ export interface PredictPackages {
5
+ predict: string;
6
+ account: string;
7
+ propbook: string;
8
+ }
9
+
10
+ /**
11
+ * Per-underlying oracle wiring: the propbook underlying id plus the feed object ids the
12
+ * live pricer reads. Names mirror the deployment manifest
13
+ * (`packages/predict/deployment/deployment.testnet.json` → `underlyings`).
14
+ */
15
+ export interface UnderlyingConfig {
16
+ symbol: string;
17
+ propbookUnderlyingId: number;
18
+ /** `&PythFeed` — spot price feed. */
19
+ pythFeed: string;
20
+ /** `&BlockScholesValueStore` — Black-Scholes value store. */
21
+ blockScholesValueStore: string;
22
+ /** `&BlockScholesSVIStore` — SVI-surface store. */
23
+ blockScholesSviStore: string;
24
+ }
25
+
26
+ /** Everything a tx builder or read needs to address a Predict deployment on one network. */
27
+ export interface PredictConfig {
28
+ network: 'testnet' | 'mainnet' | 'custom';
29
+ packages: PredictPackages;
30
+ objects: {
31
+ registry: string;
32
+ protocolConfig: string;
33
+ poolVault: string;
34
+ oracleRegistry: string;
35
+ accountRegistry: string;
36
+ };
37
+ quoteCoinType: string; // DUSDC on testnet
38
+ /**
39
+ * Coin types the deployment owns. `plp` is NOT derivable from `packages.predict`: a Move
40
+ * type tag keeps the ORIGINAL package id across an upgrade, while `packages.predict`
41
+ * moves to the latest.
42
+ */
43
+ coinTypes: { plp: string; deep: string };
44
+ /** Scale constants the deploy owns rather than the SDK. */
45
+ units: {
46
+ positionLotSize: number;
47
+ fixedPointScale: number;
48
+ quoteCoinDecimals: number;
49
+ positionQuantityDecimals: number;
50
+ };
51
+ underlyings: Record<string, UnderlyingConfig>; // keyed by symbol, e.g. "BTC"
52
+ }
@@ -0,0 +1,476 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { fromBase64, normalizeSuiAddress } from '@mysten/sui/utils';
4
+ import type { PredictConfig } from './config/index.js';
5
+ import { accountEvents } from '../account.js';
6
+ import * as builderCodeEvents from '../contracts/deepbook_predict/builder_code_events.js';
7
+ import * as orderEvents from '../contracts/deepbook_predict/order_events.js';
8
+ import * as vaultEvents from '../contracts/deepbook_predict/vault_events.js';
9
+ import { PredictInputError } from './errors.js';
10
+ import { fromRaw } from './units.js';
11
+
12
+ // ============================================================================
13
+ // Execution-result decoders.
14
+ //
15
+ // Every tx.* builder returns a Transaction; after the app executes it (with
16
+ // events included), the deployed contracts emit typed events carrying the full
17
+ // receipt — order ids, fills, fees, queue indexes, new balances. These pure
18
+ // functions turn that result into typed receipts. No network, no client.
19
+ //
20
+ // Decoding uses each event's BCS bytes, not its `json`: the client typings
21
+ // warn the JSON rendering varies across transports (JSON-RPC/gRPC/GraphQL),
22
+ // while BCS is canonical. Layouts come from the GENERATED event MoveStructs
23
+ // (src/contracts/{deepbook_predict,account}/*_events.ts), whose field order
24
+ // mirrors the deployed Move — regenerate the bindings if the package changes.
25
+ // ============================================================================
26
+
27
+ /** The slice of an executed/simulated transaction result the decoders need. */
28
+ export interface DecodableEvent {
29
+ /** Full type tag `0xpkg::module::Name` (gRPC `eventType`, legacy `type`). */
30
+ eventType?: string;
31
+ type?: string;
32
+ packageId?: string;
33
+ module?: string;
34
+ /** Struct name, when the transport supplies it separately from a full type tag. */
35
+ name?: string;
36
+ /** Canonical BCS payload; some transports deliver it base64-encoded. */
37
+ bcs?: Uint8Array | string;
38
+ }
39
+
40
+ export interface DecodableTransactionResult {
41
+ events?: readonly DecodableEvent[] | null;
42
+ }
43
+
44
+ // --- matching + plumbing ----------------------------------------------------
45
+
46
+ function eventBytes(e: DecodableEvent): Uint8Array | null {
47
+ if (e.bcs instanceof Uint8Array) return e.bcs;
48
+ if (typeof e.bcs === 'string') return fromBase64(e.bcs);
49
+ return null;
50
+ }
51
+
52
+ // Match by defining package + module + struct name. Events are typed by the
53
+ // ORIGINAL package id; for the current v1 deployments that equals the config
54
+ // package id.
55
+ function matches(e: DecodableEvent, pkg: string, module: string, name: string): boolean {
56
+ const tag = e.eventType ?? e.type;
57
+ if (tag) {
58
+ const parts = tag.split('::');
59
+ if (parts.length !== 3) return false;
60
+ return (
61
+ normalizeSuiAddress(parts[0]) === normalizeSuiAddress(pkg) &&
62
+ parts[1] === module &&
63
+ parts[2] === name
64
+ );
65
+ }
66
+ // No type tag. Match on what the transport did give us — and on `name` when it is
67
+ // present, because module+package alone matches EVERY struct in the module, so one
68
+ // decoder would happily take another's event and fail deep inside BCS.
69
+ if (e.module !== module || e.packageId == null) return false;
70
+ if (normalizeSuiAddress(e.packageId) !== normalizeSuiAddress(pkg)) return false;
71
+ return e.name == null || e.name === name;
72
+ }
73
+
74
+ function decodeAll<T>(
75
+ result: DecodableTransactionResult,
76
+ pkg: string,
77
+ module: string,
78
+ name: string,
79
+ layout: { parse(bytes: Uint8Array): T },
80
+ ): T[] {
81
+ const out: T[] = [];
82
+ for (const e of result.events ?? []) {
83
+ if (!matches(e, pkg, module, name)) continue;
84
+ const bytes = eventBytes(e);
85
+ if (!bytes) {
86
+ throw new PredictInputError(
87
+ `${module}::${name} event has no BCS payload — execute/simulate with events included`,
88
+ );
89
+ }
90
+ out.push(layout.parse(bytes));
91
+ }
92
+ return out;
93
+ }
94
+
95
+ function exactlyOne<T>(items: T[], what: string): T {
96
+ if (items.length !== 1) {
97
+ throw new PredictInputError(`expected exactly one ${what} event, found ${items.length}`);
98
+ }
99
+ return items[0];
100
+ }
101
+
102
+ const optId = (v: string | null | undefined): string | null =>
103
+ v == null ? null : normalizeSuiAddress(v);
104
+
105
+ // --- receipts ---------------------------------------------------------------
106
+
107
+ export interface MintReceipt {
108
+ marketId: string;
109
+ accountId: string;
110
+ owner: string;
111
+ /** Persist this: required to redeem/claim the position later. */
112
+ orderId: bigint;
113
+ /** Stable across partial-close replacements; equals orderId at mint. */
114
+ positionRootId: bigint;
115
+ lowerTick: bigint;
116
+ higherTick: bigint;
117
+ /** 0..1 range probability quoted at entry (your fill price per $1 payout). */
118
+ entryProbability: number;
119
+ /** Max payout actually minted, in quote units (mintAmount: chain-floored). */
120
+ quantity: number;
121
+ /** Premium paid into LP backing, in quote units. */
122
+ premium: number;
123
+ /**
124
+ * Fee breakdown. `referral` is the slice of the trader-paid trading fee and
125
+ * congestion surcharge delivered to the referrer; `inventoryImpact` is the
126
+ * path-independent inventory-impact charge assessed on the mint.
127
+ */
128
+ fees: {
129
+ trading: number;
130
+ subsidy: number;
131
+ builder: number;
132
+ penalty: number;
133
+ referral: number;
134
+ inventoryImpact: number;
135
+ };
136
+ builderCodeId: string | null;
137
+ raw: {
138
+ quantity: bigint;
139
+ premium: bigint;
140
+ tradingFee: bigint;
141
+ feeIncentiveSubsidy: bigint;
142
+ builderFee: bigint;
143
+ penaltyFee: bigint;
144
+ referralFee: bigint;
145
+ inventoryImpactCharge: bigint;
146
+ entryProbability: bigint;
147
+ };
148
+ }
149
+
150
+ export interface RedeemReceipt {
151
+ marketId: string;
152
+ accountId: string;
153
+ owner: string;
154
+ orderId: bigint;
155
+ positionRootId: bigint;
156
+ quantityClosed: number;
157
+ remaining: number;
158
+ /**
159
+ * A partial close RETIRES the old order id and issues this replacement for
160
+ * the remaining quantity — update your stored id or it goes silently stale.
161
+ */
162
+ replacementOrderId: bigint | null;
163
+ /** NET quote credited to the account: gross + inventoryImpactRebate − trading −
164
+ * builder − penalty — the same expression the chain asserts `min_proceeds`
165
+ * against, and what `settle_live_redeem_payment` actually deposits. */
166
+ proceeds: number;
167
+ /** Gross close value before fees (the event's redeem_amount). */
168
+ gross: number;
169
+ /**
170
+ * Fee breakdown. `inventoryImpactRebate` is the path-independent
171
+ * inventory-impact rebate credited back on the close.
172
+ */
173
+ fees: { trading: number; builder: number; penalty: number; inventoryImpactRebate: number };
174
+ builderCodeId: string | null;
175
+ raw: {
176
+ quantityClosed: bigint;
177
+ remaining: bigint;
178
+ proceeds: bigint;
179
+ gross: bigint;
180
+ tradingFee: bigint;
181
+ builderFee: bigint;
182
+ penaltyFee: bigint;
183
+ inventoryImpactRebate: bigint;
184
+ };
185
+ }
186
+
187
+ export interface ClaimReceipt {
188
+ marketId: string;
189
+ accountId: string;
190
+ owner: string;
191
+ orderId: bigint;
192
+ positionRootId: bigint;
193
+ /** Quote units paid out. A settled claim closes the order in full. */
194
+ payout: number;
195
+ raw: { payout: bigint };
196
+ }
197
+
198
+ export interface CreateManagerReceipt {
199
+ accountId: string;
200
+ wrapperId: string;
201
+ owner: string;
202
+ selfOwned: boolean;
203
+ }
204
+
205
+ export interface BalanceChangeReceipt {
206
+ accountId: string;
207
+ coinType: string;
208
+ /** Display value assuming a 6-decimal coin (quote + PLP both are). */
209
+ amount: number;
210
+ newBalance: number;
211
+ raw: { amount: bigint; newBalance: bigint };
212
+ }
213
+
214
+ export interface PlpRequestReceipt {
215
+ kind: 'supply' | 'withdraw';
216
+ vaultId: string;
217
+ accountId: string;
218
+ recipient: string;
219
+ /** Feed straight into cancelSupplyPlp / cancelWithdrawPlp. */
220
+ index: bigint;
221
+ /** Quote units for supply; PLP shares (6-dec) for withdraw. */
222
+ amount: number;
223
+ raw: { amount: bigint };
224
+ }
225
+
226
+ export interface PlpCancelReceipt {
227
+ vaultId: string;
228
+ accountId: string;
229
+ recipient: string;
230
+ index: bigint;
231
+ isSupply: boolean;
232
+ /** Refund returned to the account (quote for supply, PLP for withdraw). */
233
+ amount: number;
234
+ raw: { amount: bigint };
235
+ }
236
+
237
+ export interface BuilderCodeReceipt {
238
+ accountId: string;
239
+ owner: string;
240
+ /** Null after unsetBuilderCode. */
241
+ builderCodeId: string | null;
242
+ }
243
+
244
+ // --- decoders (plural = all matching events, in event order) ----------------
245
+
246
+ export function decodeMints(cfg: PredictConfig, result: DecodableTransactionResult): MintReceipt[] {
247
+ return decodeAll(
248
+ result,
249
+ cfg.packages.predict,
250
+ 'order_events',
251
+ 'OrderMinted',
252
+ orderEvents.OrderMinted,
253
+ ).map((e) => ({
254
+ marketId: normalizeSuiAddress(e.expiry_market_id),
255
+ accountId: normalizeSuiAddress(e.account_id),
256
+ owner: normalizeSuiAddress(e.owner),
257
+ orderId: e.order_id,
258
+ positionRootId: e.position_root_id,
259
+ lowerTick: e.lower_tick,
260
+ higherTick: e.higher_tick,
261
+ entryProbability: fromRaw(e.entry_probability, 9),
262
+ quantity: fromRaw(e.quantity, 6),
263
+ premium: fromRaw(e.premium, 6),
264
+ fees: {
265
+ trading: fromRaw(e.trading_fee, 6),
266
+ subsidy: fromRaw(e.fee_incentive_subsidy, 6),
267
+ builder: fromRaw(e.builder_fee, 6),
268
+ penalty: fromRaw(e.penalty_fee, 6),
269
+ referral: fromRaw(e.referral_fee, 6),
270
+ inventoryImpact: fromRaw(e.inventory_impact_charge, 6),
271
+ },
272
+ builderCodeId: optId(e.builder_code_id),
273
+ raw: {
274
+ quantity: e.quantity,
275
+ premium: e.premium,
276
+ tradingFee: e.trading_fee,
277
+ feeIncentiveSubsidy: e.fee_incentive_subsidy,
278
+ builderFee: e.builder_fee,
279
+ penaltyFee: e.penalty_fee,
280
+ referralFee: e.referral_fee,
281
+ inventoryImpactCharge: e.inventory_impact_charge,
282
+ entryProbability: e.entry_probability,
283
+ },
284
+ }));
285
+ }
286
+
287
+ export function decodeRedeems(
288
+ cfg: PredictConfig,
289
+ result: DecodableTransactionResult,
290
+ ): RedeemReceipt[] {
291
+ const pkg = cfg.packages.predict;
292
+ const live = decodeAll(
293
+ result,
294
+ pkg,
295
+ 'order_events',
296
+ 'LiveOrderRedeemed',
297
+ orderEvents.LiveOrderRedeemed,
298
+ ).map((e): RedeemReceipt => ({
299
+ marketId: normalizeSuiAddress(e.expiry_market_id),
300
+ accountId: normalizeSuiAddress(e.account_id),
301
+ owner: normalizeSuiAddress(e.owner),
302
+ orderId: e.order_id,
303
+ positionRootId: e.position_root_id,
304
+ quantityClosed: fromRaw(e.quantity_closed, 6),
305
+ remaining: fromRaw(e.remaining_quantity, 6),
306
+ replacementOrderId: e.replacement_order_id,
307
+ // Mirrors the deployed close-side accounting exactly (the same expression
308
+ // `min_proceeds` is asserted against): the net credited to the account is
309
+ // redeem_amount PLUS the inventory-impact rebate, minus trading, builder and
310
+ // penalty fees. The event's redeem_amount is GROSS.
311
+ proceeds: fromRaw(
312
+ e.redeem_amount + e.inventory_impact_rebate - e.trading_fee - e.builder_fee - e.penalty_fee,
313
+ 6,
314
+ ),
315
+ gross: fromRaw(e.redeem_amount, 6),
316
+ fees: {
317
+ trading: fromRaw(e.trading_fee, 6),
318
+ builder: fromRaw(e.builder_fee, 6),
319
+ penalty: fromRaw(e.penalty_fee, 6),
320
+ inventoryImpactRebate: fromRaw(e.inventory_impact_rebate, 6),
321
+ },
322
+ builderCodeId: optId(e.builder_code_id),
323
+ raw: {
324
+ quantityClosed: e.quantity_closed,
325
+ remaining: e.remaining_quantity,
326
+ proceeds:
327
+ e.redeem_amount + e.inventory_impact_rebate - e.trading_fee - e.builder_fee - e.penalty_fee,
328
+ gross: e.redeem_amount,
329
+ tradingFee: e.trading_fee,
330
+ builderFee: e.builder_fee,
331
+ penaltyFee: e.penalty_fee,
332
+ inventoryImpactRebate: e.inventory_impact_rebate,
333
+ },
334
+ }));
335
+ return live;
336
+ }
337
+
338
+ export function decodeClaims(
339
+ cfg: PredictConfig,
340
+ result: DecodableTransactionResult,
341
+ ): ClaimReceipt[] {
342
+ return decodeAll(
343
+ result,
344
+ cfg.packages.predict,
345
+ 'order_events',
346
+ 'SettledOrderRedeemed',
347
+ orderEvents.SettledOrderRedeemed,
348
+ ).map((e) => ({
349
+ marketId: normalizeSuiAddress(e.expiry_market_id),
350
+ accountId: normalizeSuiAddress(e.account_id),
351
+ owner: normalizeSuiAddress(e.owner),
352
+ orderId: e.order_id,
353
+ positionRootId: e.position_root_id,
354
+ payout: fromRaw(e.payout_amount, 6),
355
+ raw: { payout: e.payout_amount },
356
+ }));
357
+ }
358
+
359
+ export function decodeAccountsCreated(
360
+ cfg: PredictConfig,
361
+ result: DecodableTransactionResult,
362
+ ): CreateManagerReceipt[] {
363
+ return decodeAll(
364
+ result,
365
+ cfg.packages.account,
366
+ 'account_events',
367
+ 'AccountCreated',
368
+ accountEvents.AccountCreated,
369
+ ).map((e) => ({
370
+ accountId: normalizeSuiAddress(e.account_id),
371
+ wrapperId: normalizeSuiAddress(e.wrapper_id),
372
+ owner: normalizeSuiAddress(e.owner),
373
+ selfOwned: e.self_owned,
374
+ }));
375
+ }
376
+
377
+ function decodeBalanceChanges(
378
+ cfg: PredictConfig,
379
+ result: DecodableTransactionResult,
380
+ name: 'Deposited' | 'Withdrawn',
381
+ ): BalanceChangeReceipt[] {
382
+ const layout = name === 'Deposited' ? accountEvents.Deposited : accountEvents.Withdrawn;
383
+ return decodeAll(result, cfg.packages.account, 'account_events', name, layout).map((e) => ({
384
+ accountId: normalizeSuiAddress(e.account_id),
385
+ coinType: e.coin_type,
386
+ amount: fromRaw(e.amount, 6),
387
+ newBalance: fromRaw(e.new_balance, 6),
388
+ raw: { amount: e.amount, newBalance: e.new_balance },
389
+ }));
390
+ }
391
+
392
+ export const decodeDeposits = (
393
+ cfg: PredictConfig,
394
+ result: DecodableTransactionResult,
395
+ ): BalanceChangeReceipt[] => decodeBalanceChanges(cfg, result, 'Deposited');
396
+
397
+ export const decodeWithdrawals = (
398
+ cfg: PredictConfig,
399
+ result: DecodableTransactionResult,
400
+ ): BalanceChangeReceipt[] => decodeBalanceChanges(cfg, result, 'Withdrawn');
401
+
402
+ export function decodePlpRequests(
403
+ cfg: PredictConfig,
404
+ result: DecodableTransactionResult,
405
+ ): PlpRequestReceipt[] {
406
+ const pkg = cfg.packages.predict;
407
+ // Supply/WithdrawRequested differ only in their min-out field (unused here);
408
+ // pick the fields both share so either generated struct's parse feeds `make`.
409
+ type RequestedCommon = Pick<
410
+ (typeof vaultEvents.SupplyRequested)['$inferType'],
411
+ 'pool_vault_id' | 'account_id' | 'recipient' | 'index' | 'amount'
412
+ >;
413
+ const make =
414
+ (kind: 'supply' | 'withdraw') =>
415
+ (e: RequestedCommon): PlpRequestReceipt => ({
416
+ kind,
417
+ vaultId: normalizeSuiAddress(e.pool_vault_id),
418
+ accountId: normalizeSuiAddress(e.account_id),
419
+ recipient: normalizeSuiAddress(e.recipient),
420
+ index: e.index,
421
+ amount: fromRaw(e.amount, 6),
422
+ raw: { amount: e.amount },
423
+ });
424
+ return [
425
+ ...decodeAll(result, pkg, 'vault_events', 'SupplyRequested', vaultEvents.SupplyRequested).map(
426
+ make('supply'),
427
+ ),
428
+ ...decodeAll(
429
+ result,
430
+ pkg,
431
+ 'vault_events',
432
+ 'WithdrawRequested',
433
+ vaultEvents.WithdrawRequested,
434
+ ).map(make('withdraw')),
435
+ ];
436
+ }
437
+
438
+ export function decodePlpCancels(
439
+ cfg: PredictConfig,
440
+ result: DecodableTransactionResult,
441
+ ): PlpCancelReceipt[] {
442
+ return decodeAll(
443
+ result,
444
+ cfg.packages.predict,
445
+ 'vault_events',
446
+ 'RequestCancelled',
447
+ vaultEvents.RequestCancelled,
448
+ ).map((e) => ({
449
+ vaultId: normalizeSuiAddress(e.pool_vault_id),
450
+ accountId: normalizeSuiAddress(e.account_id),
451
+ recipient: normalizeSuiAddress(e.recipient),
452
+ index: e.index,
453
+ isSupply: e.is_supply,
454
+ amount: fromRaw(e.amount, 6),
455
+ raw: { amount: e.amount },
456
+ }));
457
+ }
458
+
459
+ export function decodeBuilderCodeSets(
460
+ cfg: PredictConfig,
461
+ result: DecodableTransactionResult,
462
+ ): BuilderCodeReceipt[] {
463
+ return decodeAll(
464
+ result,
465
+ cfg.packages.predict,
466
+ 'builder_code_events',
467
+ 'BuilderCodeSet',
468
+ builderCodeEvents.BuilderCodeSet,
469
+ ).map((e) => ({
470
+ accountId: normalizeSuiAddress(e.account_id),
471
+ owner: normalizeSuiAddress(e.owner),
472
+ builderCodeId: optId(e.builder_code_id),
473
+ }));
474
+ }
475
+
476
+ export { exactlyOne };