@waterx/sdk 2.4.1 → 3.0.0

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 (222) hide show
  1. package/README.md +25 -21
  2. package/dist/src/{utils → account}/account-request.d.ts +2 -2
  3. package/dist/src/{user → account}/account.d.ts +23 -21
  4. package/dist/src/{user → account}/account.js +5 -5
  5. package/dist/src/account/client.d.ts +42 -0
  6. package/dist/src/account/client.js +17 -0
  7. package/dist/src/account/config.d.ts +151 -0
  8. package/dist/src/account/config.js +8 -0
  9. package/dist/src/account/constants.d.ts +5 -0
  10. package/dist/src/account/constants.js +5 -0
  11. package/dist/src/account/fetch/referral.d.ts +15 -0
  12. package/dist/src/account/fetch/referral.js +51 -0
  13. package/dist/src/account/fetch/simulate.d.ts +34 -0
  14. package/dist/src/account/fetch/simulate.js +43 -0
  15. package/dist/src/{utils/consolidate-balance.d.ts → account/funding/balance.d.ts} +3 -8
  16. package/dist/src/{utils/consolidate-balance.js → account/funding/balance.js} +6 -1
  17. package/dist/src/account/funding/consolidate.d.ts +46 -0
  18. package/dist/src/account/funding/consolidate.js +137 -0
  19. package/dist/src/{user → account/funding}/credit.d.ts +10 -10
  20. package/dist/src/{user → account/funding}/credit.js +6 -6
  21. package/dist/src/{user → account/funding}/custody.d.ts +4 -4
  22. package/dist/src/{user → account/funding}/custody.js +2 -2
  23. package/dist/src/{utils → account/funding}/wormhole.d.ts +4 -4
  24. package/dist/src/{utils → account/funding}/wormhole.js +1 -1
  25. package/dist/src/account/index.d.ts +29 -0
  26. package/dist/src/account/index.js +29 -0
  27. package/dist/src/{user → account}/referral.d.ts +3 -3
  28. package/dist/src/{user → account}/referral.js +1 -1
  29. package/dist/src/base-client.d.ts +74 -0
  30. package/dist/src/base-client.js +97 -0
  31. package/dist/src/constants.d.ts +4 -46
  32. package/dist/src/constants.js +8 -52
  33. package/dist/src/generated/bucket_v2_framework/liability.d.ts +0 -26
  34. package/dist/src/generated/bucket_v2_framework/liability.js +0 -28
  35. package/dist/src/generated/waterx_account/account.d.ts +50 -15
  36. package/dist/src/generated/waterx_account/account.js +51 -15
  37. package/dist/src/generated/waterx_oracle/oracle.d.ts +7 -1
  38. package/dist/src/generated/waterx_oracle/oracle.js +7 -2
  39. package/dist/src/{prediction/generated → generated}/waterx_prediction/account_data.d.ts +19 -2
  40. package/dist/src/{prediction/generated → generated}/waterx_prediction/admin.d.ts +3 -1
  41. package/dist/src/{prediction/generated → generated}/waterx_prediction/deps/sui/balance.d.ts +3 -1
  42. package/dist/src/{prediction/generated/waterx_account → generated/waterx_prediction}/deps/sui/table.d.ts +6 -1
  43. package/dist/src/generated/waterx_prediction/events.d.ts +288 -0
  44. package/dist/src/{prediction/generated → generated}/waterx_prediction/events.js +39 -0
  45. package/dist/src/{prediction/generated → generated}/waterx_prediction/global_config.d.ts +6 -1
  46. package/dist/src/{prediction/generated → generated}/waterx_prediction/order.d.ts +34 -1
  47. package/dist/src/{prediction/generated → generated}/waterx_prediction/position.d.ts +16 -1
  48. package/dist/src/{prediction/generated → generated}/waterx_prediction/view.d.ts +69 -4
  49. package/dist/src/{prediction/generated → generated}/waterx_prediction/waterx_prediction.d.ts +162 -2
  50. package/dist/src/{prediction/generated → generated}/waterx_prediction/waterx_prediction.js +99 -0
  51. package/dist/src/generated/waterx_pyth_rule/pyth_rule.d.ts +102 -0
  52. package/dist/src/generated/waterx_pyth_rule/pyth_rule.js +99 -0
  53. package/dist/src/generated/waterx_supra_rule/supra_rule.d.ts +5 -5
  54. package/dist/src/generated/waterx_supra_rule/supra_rule.js +1 -2
  55. package/dist/src/generated/withdrawal_queue/withdrawal_queue.d.ts +23 -23
  56. package/dist/src/generated/withdrawal_queue/withdrawal_queue.js +3 -6
  57. package/dist/src/oracle/aggregate.d.ts +74 -0
  58. package/dist/src/oracle/aggregate.js +112 -0
  59. package/dist/src/oracle/config.d.ts +99 -0
  60. package/dist/src/oracle/config.js +23 -0
  61. package/dist/src/oracle/host.d.ts +32 -0
  62. package/dist/src/oracle/host.js +10 -0
  63. package/dist/src/oracle/index.d.ts +16 -0
  64. package/dist/src/oracle/index.js +18 -0
  65. package/dist/src/oracle/pyth.d.ts +52 -0
  66. package/dist/src/oracle/pyth.js +220 -0
  67. package/dist/src/oracle/rules/constant-rule.d.ts +9 -0
  68. package/dist/src/oracle/rules/constant-rule.js +14 -0
  69. package/dist/src/oracle/rules/pyth-rule.d.ts +11 -0
  70. package/dist/src/oracle/rules/pyth-rule.js +20 -0
  71. package/dist/src/oracle/rules/sponsor.d.ts +28 -0
  72. package/dist/src/oracle/rules/sponsor.js +48 -0
  73. package/dist/src/oracle/rules/supra-rule.d.ts +11 -0
  74. package/dist/src/oracle/rules/supra-rule.js +22 -0
  75. package/dist/src/perp/client.d.ts +78 -0
  76. package/dist/src/perp/client.js +108 -0
  77. package/dist/src/{client.d.ts → perp/config-view.d.ts} +15 -60
  78. package/dist/src/{client.js → perp/config-view.js} +23 -95
  79. package/dist/src/perp/config.d.ts +126 -0
  80. package/dist/src/{config.js → perp/config.js} +11 -12
  81. package/dist/src/perp/constants.d.ts +49 -0
  82. package/dist/src/perp/constants.js +61 -0
  83. package/dist/src/perp/fetch/account.d.ts +43 -0
  84. package/dist/src/perp/fetch/account.js +64 -0
  85. package/dist/src/perp/fetch/bridge.d.ts +90 -0
  86. package/dist/src/perp/fetch/bridge.js +127 -0
  87. package/dist/src/perp/fetch/custody.d.ts +32 -0
  88. package/dist/src/perp/fetch/custody.js +62 -0
  89. package/dist/src/perp/fetch/market.d.ts +30 -0
  90. package/dist/src/perp/fetch/market.js +68 -0
  91. package/dist/src/perp/fetch/positions.d.ts +70 -0
  92. package/dist/src/perp/fetch/positions.js +185 -0
  93. package/dist/src/perp/fetch/simulate.d.ts +12 -0
  94. package/dist/src/perp/fetch/simulate.js +13 -0
  95. package/dist/src/perp/fetch.d.ts +23 -0
  96. package/dist/src/perp/fetch.js +23 -0
  97. package/dist/src/{index.d.ts → perp/index.d.ts} +21 -21
  98. package/dist/src/{index.js → perp/index.js} +21 -21
  99. package/dist/src/perp/tx-builders/common.d.ts +83 -0
  100. package/dist/src/perp/tx-builders/common.js +70 -0
  101. package/dist/src/perp/tx-builders/credit.d.ts +65 -0
  102. package/dist/src/perp/tx-builders/credit.js +62 -0
  103. package/dist/src/perp/tx-builders/rewards.d.ts +30 -0
  104. package/dist/src/perp/tx-builders/rewards.js +34 -0
  105. package/dist/src/perp/tx-builders/trading.d.ts +51 -0
  106. package/dist/src/perp/tx-builders/trading.js +59 -0
  107. package/dist/src/perp/tx-builders/wlp.d.ts +90 -0
  108. package/dist/src/perp/tx-builders/wlp.js +120 -0
  109. package/dist/src/perp/tx-builders.d.ts +23 -0
  110. package/dist/src/perp/tx-builders.js +23 -0
  111. package/dist/src/perp/user/index.d.ts +8 -0
  112. package/dist/src/perp/user/index.js +8 -0
  113. package/dist/src/{user → perp/user}/order.d.ts +7 -7
  114. package/dist/src/{user → perp/user}/order.js +3 -3
  115. package/dist/src/{user → perp/user}/staking.d.ts +4 -4
  116. package/dist/src/{user → perp/user}/staking.js +2 -2
  117. package/dist/src/{user → perp/user}/trading.d.ts +13 -13
  118. package/dist/src/{user → perp/user}/trading.js +2 -2
  119. package/dist/src/{user → perp/user}/wlp.d.ts +7 -7
  120. package/dist/src/{user → perp/user}/wlp.js +3 -3
  121. package/dist/src/prediction/account.js +40 -76
  122. package/dist/src/prediction/client.d.ts +8 -38
  123. package/dist/src/prediction/client.js +11 -67
  124. package/dist/src/prediction/config.d.ts +16 -2
  125. package/dist/src/prediction/config.js +10 -4
  126. package/dist/src/prediction/index.d.ts +11 -11
  127. package/dist/src/prediction/index.js +10 -10
  128. package/dist/src/prediction/prediction.d.ts +45 -0
  129. package/dist/src/prediction/prediction.js +71 -0
  130. package/dist/src/prediction/tx-builders.d.ts +4 -4
  131. package/dist/src/prediction/tx-builders.js +2 -2
  132. package/dist/src/prediction/user/index.d.ts +2 -2
  133. package/dist/src/prediction/user/index.js +1 -1
  134. package/dist/src/prediction/user/position.d.ts +2 -2
  135. package/dist/src/prediction/user/position.js +1 -1
  136. package/dist/src/prediction/utils/bcs.d.ts +5 -0
  137. package/dist/src/prediction/utils/bcs.js +5 -0
  138. package/dist/src/prediction/utils/index.d.ts +7 -0
  139. package/dist/src/prediction/utils/index.js +7 -0
  140. package/dist/src/sdk.d.ts +10 -6
  141. package/dist/src/sdk.js +9 -5
  142. package/dist/src/unified-client.d.ts +140 -145
  143. package/dist/src/unified-client.js +98 -40
  144. package/dist/src/utils/config.d.ts +1 -1
  145. package/package.json +22 -5
  146. package/dist/src/config.d.ts +0 -292
  147. package/dist/src/fetch.d.ts +0 -254
  148. package/dist/src/fetch.js +0 -564
  149. package/dist/src/prediction/generated/bucket_v2_framework/account.d.ts +0 -100
  150. package/dist/src/prediction/generated/bucket_v2_framework/account.js +0 -129
  151. package/dist/src/prediction/generated/bucket_v2_framework/deps/std/type_name.d.ts +0 -6
  152. package/dist/src/prediction/generated/bucket_v2_framework/deps/std/type_name.js +0 -19
  153. package/dist/src/prediction/generated/bucket_v2_framework/deps/sui/balance.d.ts +0 -10
  154. package/dist/src/prediction/generated/bucket_v2_framework/deps/sui/vec_map.d.ts +0 -36
  155. package/dist/src/prediction/generated/bucket_v2_framework/deps/sui/vec_map.js +0 -27
  156. package/dist/src/prediction/generated/bucket_v2_framework/double.d.ts +0 -382
  157. package/dist/src/prediction/generated/bucket_v2_framework/double.js +0 -466
  158. package/dist/src/prediction/generated/bucket_v2_framework/float.d.ts +0 -362
  159. package/dist/src/prediction/generated/bucket_v2_framework/float.js +0 -440
  160. package/dist/src/prediction/generated/bucket_v2_framework/liability.d.ts +0 -193
  161. package/dist/src/prediction/generated/bucket_v2_framework/liability.js +0 -205
  162. package/dist/src/prediction/generated/bucket_v2_framework/linked_table.d.ts +0 -384
  163. package/dist/src/prediction/generated/bucket_v2_framework/linked_table.js +0 -382
  164. package/dist/src/prediction/generated/bucket_v2_framework/sheet.d.ts +0 -344
  165. package/dist/src/prediction/generated/bucket_v2_framework/sheet.js +0 -338
  166. package/dist/src/prediction/generated/utils/index.d.ts +0 -30
  167. package/dist/src/prediction/generated/utils/index.js +0 -160
  168. package/dist/src/prediction/generated/waterx_account/account.d.ts +0 -1589
  169. package/dist/src/prediction/generated/waterx_account/account.js +0 -1758
  170. package/dist/src/prediction/generated/waterx_account/deps/bucket_v2_framework/liability.d.ts +0 -7
  171. package/dist/src/prediction/generated/waterx_account/deps/bucket_v2_framework/liability.js +0 -13
  172. package/dist/src/prediction/generated/waterx_account/deps/bucket_v2_framework/sheet.d.ts +0 -7
  173. package/dist/src/prediction/generated/waterx_account/deps/bucket_v2_framework/sheet.js +0 -16
  174. package/dist/src/prediction/generated/waterx_account/deps/std/type_name.d.ts +0 -6
  175. package/dist/src/prediction/generated/waterx_account/deps/std/type_name.js +0 -19
  176. package/dist/src/prediction/generated/waterx_account/deps/sui/balance.d.ts +0 -10
  177. package/dist/src/prediction/generated/waterx_account/deps/sui/balance.js +0 -14
  178. package/dist/src/prediction/generated/waterx_account/deps/sui/object_table.d.ts +0 -12
  179. package/dist/src/prediction/generated/waterx_account/deps/sui/object_table.js +0 -19
  180. package/dist/src/prediction/generated/waterx_account/deps/sui/vec_map.d.ts +0 -36
  181. package/dist/src/prediction/generated/waterx_account/deps/sui/vec_map.js +0 -27
  182. package/dist/src/prediction/generated/waterx_account/deps/sui/vec_set.d.ts +0 -16
  183. package/dist/src/prediction/generated/waterx_account/deps/sui/vec_set.js +0 -19
  184. package/dist/src/prediction/generated/waterx_account/direct_rule.d.ts +0 -75
  185. package/dist/src/prediction/generated/waterx_account/direct_rule.js +0 -78
  186. package/dist/src/prediction/generated/waterx_account/events.d.ts +0 -33
  187. package/dist/src/prediction/generated/waterx_account/events.js +0 -137
  188. package/dist/src/prediction/generated/waterx_account/version.js +0 -8
  189. package/dist/src/prediction/generated/waterx_prediction/bet_sharing.d.ts +0 -60
  190. package/dist/src/prediction/generated/waterx_prediction/bet_sharing.js +0 -83
  191. package/dist/src/prediction/generated/waterx_prediction/deps/sui/balance.js +0 -14
  192. package/dist/src/prediction/generated/waterx_prediction/deps/sui/table.d.ts +0 -24
  193. package/dist/src/prediction/generated/waterx_prediction/deps/sui/table.js +0 -31
  194. package/dist/src/prediction/generated/waterx_prediction/deps/sui/vec_set.d.ts +0 -16
  195. package/dist/src/prediction/generated/waterx_prediction/deps/sui/vec_set.js +0 -19
  196. package/dist/src/prediction/generated/waterx_prediction/events.d.ts +0 -22
  197. package/dist/src/prediction/generated/waterx_prediction/version.d.ts +0 -10
  198. package/dist/src/tx-builders.d.ts +0 -296
  199. package/dist/src/tx-builders.js +0 -461
  200. package/dist/src/user/index.d.ts +0 -8
  201. package/dist/src/user/index.js +0 -8
  202. package/dist/src/utils/pyth.d.ts +0 -124
  203. package/dist/src/utils/pyth.js +0 -392
  204. /package/dist/src/{utils → account}/account-request.js +0 -0
  205. /package/dist/src/{core → account}/waterx-account.d.ts +0 -0
  206. /package/dist/src/{core → account}/waterx-account.js +0 -0
  207. /package/dist/src/{prediction/generated → generated}/waterx_prediction/account_data.js +0 -0
  208. /package/dist/src/{prediction/generated → generated}/waterx_prediction/admin.js +0 -0
  209. /package/dist/src/{prediction/generated → generated}/waterx_prediction/deps/bucket_v2_framework/linked_table.d.ts +0 -0
  210. /package/dist/src/{prediction/generated → generated}/waterx_prediction/deps/bucket_v2_framework/linked_table.js +0 -0
  211. /package/dist/src/{prediction/generated/bucket_v2_framework → generated/waterx_prediction}/deps/sui/balance.js +0 -0
  212. /package/dist/src/{prediction/generated/waterx_account → generated/waterx_prediction}/deps/sui/table.js +0 -0
  213. /package/dist/src/{prediction/generated/bucket_v2_framework → generated/waterx_prediction}/deps/sui/vec_set.d.ts +0 -0
  214. /package/dist/src/{prediction/generated/bucket_v2_framework → generated/waterx_prediction}/deps/sui/vec_set.js +0 -0
  215. /package/dist/src/{prediction/generated → generated}/waterx_prediction/global_config.js +0 -0
  216. /package/dist/src/{prediction/generated → generated}/waterx_prediction/order.js +0 -0
  217. /package/dist/src/{prediction/generated → generated}/waterx_prediction/outcome.d.ts +0 -0
  218. /package/dist/src/{prediction/generated → generated}/waterx_prediction/outcome.js +0 -0
  219. /package/dist/src/{prediction/generated → generated}/waterx_prediction/position.js +0 -0
  220. /package/dist/src/{prediction/generated/waterx_account → generated/waterx_prediction}/version.d.ts +0 -0
  221. /package/dist/src/{prediction/generated → generated}/waterx_prediction/version.js +0 -0
  222. /package/dist/src/{prediction/generated → generated}/waterx_prediction/view.js +0 -0
@@ -1,461 +0,0 @@
1
- /**
2
- * High-level transaction builders that compose oracle refresh + the
3
- * appropriate `*_request` + `execute` for a single user-side action.
4
- *
5
- * Each top-level builder:
6
- * 1. Creates a fresh `Transaction` (or appends to one you pass in).
7
- * 2. Refreshes the on-chain `Oracle` for the base + collateral tickers
8
- * via Pyth (uses `utils/pyth.ts`).
9
- * 3. Calls the request builder.
10
- * 4. If `useSponsor` (default true on testnet): opens a Fund via
11
- * `pyth_sponsor_rule::request`, then closes it with `reimburse`
12
- * which attaches the `PythSponsorRule` witness to the
13
- * TradingRequest so the market's `request_checklist` is satisfied.
14
- * 5. Executes the trading request via `trading::execute`.
15
- */
16
- import { Transaction } from "@mysten/sui/transactions";
17
- import { consumeDepositDirect } from "./generated/waterx_account/direct_rule.js";
18
- import { requestDepositFromFunds, requestDepositFromReceivings } from "./user/account.js";
19
- import { consumeCreditDeposit, enqueueWithdrawal, executeWithdrawalNative, executeWithdrawalWormhole, redeemVaa, requestCreditWithdraw, routeNative, routeWormhole, } from "./user/credit.js";
20
- import { mintCreditFromRequest } from "./user/custody.js";
21
- import { addPreOrderRequest, cancelOrderRequest, cancelPreOrderRequest, placeOrderRequest, updateOrderRequest, } from "./user/order.js";
22
- import { claimReward, stake, unstake } from "./user/staking.js";
23
- import { closePositionRequest, decreasePositionRequest, depositCollateralRequest, executeTrading, increasePositionRequest, withdrawCollateralRequest, } from "./user/trading.js";
24
- import { cancelRedeemWlp, mintWlp, requestRedeemWlp, updateTokenValue, } from "./user/wlp.js";
25
- import { getCollateralAssets } from "./utils/config.js";
26
- import { probeAddressCreditBalance, probeParkedBackingAssets, } from "./utils/consolidate-balance.js";
27
- import { openPythSponsorFund, PythCache, refreshOraclePrices, reimbursePythSponsor, updatePythPrices, } from "./utils/pyth.js";
28
- function newTx(opts) {
29
- return opts?.tx ?? new Transaction();
30
- }
31
- /**
32
- * Refresh every WLP pool-token oracle (+ caller-supplied extra tickers) and
33
- * bump each pool token's `last_price_refresh_timestamp` so the pool's
34
- * `assert_prices_fresh` passes when the next `mint_wlp` / `request_redeem` /
35
- * trading `execute` runs in the same PTB.
36
- */
37
- async function refreshWlpPoolOracles(tx, client, extraTickers, opts) {
38
- const poolTickers = getCollateralAssets(client.config);
39
- const oracleTickers = Array.from(new Set([...extraTickers, ...poolTickers]));
40
- await refreshOraclePrices(tx, client, oracleTickers, {
41
- cache: opts.cache,
42
- sponsorFund: opts.sponsorFund,
43
- });
44
- for (const tokenType of Object.values(client.config.packages.wlp.pool_tokens)) {
45
- updateTokenValue(client, tx, { tokenType, lpType: opts.lpType });
46
- }
47
- }
48
- /**
49
- * Build the *Request + execute envelope with optional Pyth sponsor flow:
50
- *
51
- * [fund = sponsor.request()]
52
- * refreshOraclePrices(..., sponsorFund?)
53
- * req = buildRequest()
54
- * [sponsor.reimburse(fund, req)]
55
- * trading::execute(req)
56
- */
57
- async function wrapRequestAndExecute(client, tx, req, collateralTicker, opts, buildRequest) {
58
- await maybeConsolidate(client, tx, req.accountId, opts);
59
- const useSponsor = opts?.useSponsor ?? true;
60
- let sponsorFund;
61
- if (useSponsor)
62
- sponsorFund = openPythSponsorFund(tx, client);
63
- if (!opts?.skipOraclePriceRefresh) {
64
- await refreshWlpPoolOracles(tx, client, [req.ticker, collateralTicker], {
65
- cache: opts?.pythCache,
66
- sponsorFund,
67
- lpType: req.lpType,
68
- });
69
- }
70
- const tradingReq = buildRequest(sponsorFund);
71
- if (sponsorFund) {
72
- reimbursePythSponsor(tx, client, sponsorFund.fund, tradingReq, req.collateralType);
73
- }
74
- executeTrading(client, tx, {
75
- ticker: req.ticker,
76
- collateralType: req.collateralType,
77
- lpType: req.lpType,
78
- request: tradingReq,
79
- });
80
- }
81
- // ============================================================================
82
- // Consolidate parked backing assets → USD credit (pre-action sweep)
83
- // ============================================================================
84
- /**
85
- * For every backing asset registered on `native_custody`, append two drain
86
- * legs to `tx`:
87
- *
88
- * `requestDepositFromFunds<T>` → `mintCreditFromRequest<T, USD>` → `consumeDepositDirect<USD>`
89
- * `requestDepositFromReceivings<T>` → `mintCreditFromRequest<T, USD>` → `consumeDepositDirect<USD>`
90
- *
91
- * Empty legs are skipped — the on-chain `mint` rejects zero-amount deposits.
92
- * Returns the number of drain legs added (useful for logging / early-out).
93
- *
94
- * Silently no-ops when `native_custody` / `waterx_credit` aren't configured
95
- * for the loaded deployment.
96
- */
97
- export async function appendConsolidateToUsd(client, tx, accountId) {
98
- const parked = await probeParkedBackingAssets(client, accountId);
99
- let legs = 0;
100
- for (const row of parked) {
101
- if (row.fundsRaw > 0n) {
102
- const fromFunds = requestDepositFromFunds(client, tx, {
103
- accountId,
104
- coinType: row.assetType,
105
- });
106
- foldDepositRequestToUsd(client, tx, fromFunds, row.assetType);
107
- legs += 1;
108
- }
109
- if (row.coinsRaw > 0n) {
110
- const coins = (await client.listCoins({
111
- owner: accountId,
112
- coinType: row.assetType,
113
- }));
114
- const refs = (coins.objects ?? []).filter((c) => !!c.objectId && !!c.version && !!c.digest);
115
- if (refs.length === 0)
116
- continue;
117
- const receivings = refs.map((c) => tx.receivingRef({ objectId: c.objectId, version: c.version, digest: c.digest }));
118
- const fromReceivings = requestDepositFromReceivings(client, tx, {
119
- accountId,
120
- coinType: row.assetType,
121
- receivings,
122
- });
123
- foldDepositRequestToUsd(client, tx, fromReceivings, row.assetType);
124
- legs += 1;
125
- }
126
- }
127
- return legs;
128
- }
129
- /**
130
- * Drain CREDIT parked at `accountId`'s Sui address (funds accumulator +
131
- * owned `Coin<CREDIT>`) into the wxa internal slot via
132
- * `consumeDepositDirect<CREDIT>`. Complements {@link appendConsolidateToUsd}
133
- * for address-owned wxUSD that predict / perp actions debit from the internal
134
- * balance.
135
- */
136
- export async function appendConsolidateAddressCredit(client, tx, accountId) {
137
- if (!client.config.packages.waterx_credit?.credit_type)
138
- return 0;
139
- const creditType = client.creditType();
140
- const { fundsRaw, coinsRaw } = await probeAddressCreditBalance(client, accountId);
141
- let legs = 0;
142
- if (fundsRaw > 0n) {
143
- const fromFunds = requestDepositFromFunds(client, tx, {
144
- accountId,
145
- coinType: creditType,
146
- });
147
- consumeDepositRequest(client, tx, fromFunds, creditType);
148
- legs += 1;
149
- }
150
- if (coinsRaw > 0n) {
151
- const coins = (await client.listCoins({
152
- owner: accountId,
153
- coinType: creditType,
154
- }));
155
- const refs = (coins.objects ?? []).filter((c) => !!c.objectId && !!c.version && !!c.digest);
156
- if (refs.length > 0) {
157
- const receivings = refs.map((c) => tx.receivingRef({ objectId: c.objectId, version: c.version, digest: c.digest }));
158
- const fromReceivings = requestDepositFromReceivings(client, tx, {
159
- accountId,
160
- coinType: creditType,
161
- receivings,
162
- });
163
- consumeDepositRequest(client, tx, fromReceivings, creditType);
164
- legs += 1;
165
- }
166
- }
167
- return legs;
168
- }
169
- /**
170
- * Full pre-action sweep: backing assets → wxUSD credit (PSM) plus address
171
- * CREDIT → internal slot. Used by async tx-builders when `consolidateToUsd`
172
- * is enabled (default).
173
- */
174
- export async function appendConsolidateForSpend(client, tx, accountId) {
175
- const backingLegs = await appendConsolidateToUsd(client, tx, accountId);
176
- const creditLegs = await appendConsolidateAddressCredit(client, tx, accountId);
177
- return backingLegs + creditLegs;
178
- }
179
- function consumeDepositRequest(client, tx, depositRequest, coinType) {
180
- consumeDepositDirect({
181
- package: client.config.packages.waterx_account.published_at,
182
- arguments: {
183
- registry: tx.object(client.config.packages.waterx_account.account_registry),
184
- req: depositRequest,
185
- },
186
- typeArguments: [coinType],
187
- })(tx);
188
- }
189
- function foldDepositRequestToUsd(client, tx, depositRequest, assetType) {
190
- const usdReq = mintCreditFromRequest(client, tx, {
191
- depositRequest,
192
- assetType,
193
- });
194
- consumeDepositRequest(client, tx, usdReq, client.creditType());
195
- }
196
- /** Internal: run the async sweep iff `consolidateToUsd !== false`. */
197
- async function maybeConsolidate(client, tx, accountId, opts) {
198
- if (opts?.consolidateToUsd === false)
199
- return;
200
- await appendConsolidateForSpend(client, tx, accountId);
201
- }
202
- /**
203
- * Standalone PTB that drains every backing asset parked at the account's
204
- * address into USD credit under the account — see {@link appendConsolidateToUsd}.
205
- *
206
- * Returns an empty `Transaction` when nothing is parked. Callers can
207
- * `client.simulate(tx)` to detect a no-op before submitting.
208
- */
209
- export async function buildConsolidateToUsdTx(client, accountId, tx) {
210
- const txOut = tx ?? new Transaction();
211
- await appendConsolidateToUsd(client, txOut, accountId);
212
- return txOut;
213
- }
214
- export async function buildClosePositionTx(client, params) {
215
- const tx = newTx(params);
216
- await wrapRequestAndExecute(client, tx, params, params.collateralTicker ?? "USDCUSD", params, () => closePositionRequest(client, tx, params));
217
- return tx;
218
- }
219
- export async function buildIncreasePositionTx(client, params) {
220
- const tx = newTx(params);
221
- await wrapRequestAndExecute(client, tx, params, params.collateralTicker ?? "USDCUSD", params, () => increasePositionRequest(client, tx, params));
222
- return tx;
223
- }
224
- export async function buildDecreasePositionTx(client, params) {
225
- const tx = newTx(params);
226
- await wrapRequestAndExecute(client, tx, params, params.collateralTicker ?? "USDCUSD", params, () => decreasePositionRequest(client, tx, params));
227
- return tx;
228
- }
229
- export async function buildDepositCollateralTx(client, params) {
230
- const tx = newTx(params);
231
- await wrapRequestAndExecute(client, tx, params, params.collateralTicker ?? "USDCUSD", params, () => depositCollateralRequest(client, tx, params));
232
- return tx;
233
- }
234
- export async function buildWithdrawCollateralTx(client, params) {
235
- const tx = newTx(params);
236
- await wrapRequestAndExecute(client, tx, params, params.collateralTicker ?? "USDCUSD", params, () => withdrawCollateralRequest(client, tx, params));
237
- return tx;
238
- }
239
- export async function buildPlaceOrderTx(client, params) {
240
- const tx = newTx(params);
241
- await wrapRequestAndExecute(client, tx, params, params.collateralTicker ?? "USDCUSD", params, () => placeOrderRequest(client, tx, params));
242
- return tx;
243
- }
244
- export async function buildCancelOrderTx(client, params) {
245
- const tx = newTx(params);
246
- await wrapRequestAndExecute(client, tx, params, params.collateralTicker ?? "USDCUSD", params, () => cancelOrderRequest(client, tx, params));
247
- return tx;
248
- }
249
- export async function buildUpdateOrderTx(client, params) {
250
- const tx = newTx(params);
251
- await wrapRequestAndExecute(client, tx, params, params.collateralTicker ?? "USDCUSD", params, () => updateOrderRequest(client, tx, params));
252
- return tx;
253
- }
254
- export async function buildCancelPreOrderTx(client, params) {
255
- const tx = newTx(params);
256
- await wrapRequestAndExecute(client, tx, params, params.collateralTicker ?? "USDCUSD", params, () => cancelPreOrderRequest(client, tx, params));
257
- return tx;
258
- }
259
- export async function buildAddPreOrderTx(client, params) {
260
- const tx = newTx(params);
261
- await wrapRequestAndExecute(client, tx, params, params.collateralTicker ?? "USDCUSD", params, () => addPreOrderRequest(client, tx, params));
262
- return tx;
263
- }
264
- /**
265
- * Mints WLP from a deposit asset already in the wxa account's stored
266
- * balance. Refreshes every pool-token oracle + bumps each pool token's
267
- * `last_price_refresh_timestamp` so `assert_prices_fresh` inside
268
- * `mint_wlp` passes.
269
- *
270
- * Does NOT use the pyth_sponsor flow — `mint_wlp` produces no
271
- * `TradingRequest`, so there's nothing for the sponsor to attach its
272
- * witness to. Pyth update fees come from `tx.gas`.
273
- */
274
- export async function buildMintWlpTx(client, params) {
275
- const tx = newTx(params);
276
- await maybeConsolidate(client, tx, params.accountId, params);
277
- if (!params.skipOraclePriceRefresh) {
278
- await refreshWlpPoolOracles(tx, client, [params.depositTicker], {
279
- cache: params.pythCache,
280
- lpType: params.lpType,
281
- });
282
- }
283
- mintWlp(client, tx, params);
284
- return tx;
285
- }
286
- /**
287
- * Mints WLP and immediately stakes the minted amount into the WLP staking pool
288
- * in the same PTB. The mint's `lp_amount` return is piped directly into the
289
- * `stake` call so no precision loss / dust can occur.
290
- *
291
- * Use this instead of `buildMintWlpTx` when the product flow is "deposit USD
292
- * and earn rewards" — un-staked WLP sitting in the wxa account's `Balance<WLP>`
293
- * slot earns nothing.
294
- */
295
- export async function buildMintAndStakeWlpTx(client, params) {
296
- const tx = newTx(params);
297
- await maybeConsolidate(client, tx, params.accountId, params);
298
- if (!params.skipOraclePriceRefresh) {
299
- await refreshWlpPoolOracles(tx, client, [params.depositTicker], {
300
- cache: params.pythCache,
301
- lpType: params.lpType,
302
- });
303
- }
304
- const stakeAlias = params.stakeAlias ?? "WLP";
305
- const lpAmount = mintWlp(client, tx, params);
306
- stake(client, tx, {
307
- accountId: params.accountId,
308
- stakeAlias,
309
- stakeType: params.lpType ?? client.wlpType(),
310
- stakeAmount: lpAmount,
311
- rewarderTypes: params.rewarderTypes ?? client.getRewarderTypes(stakeAlias),
312
- bucketAccount: params.bucketAccount,
313
- });
314
- return tx;
315
- }
316
- /**
317
- * Unstakes WLP from the staking pool and immediately enqueues a redeem request
318
- * in the same PTB. Mirror of `buildMintAndStakeWlpTx` for withdrawals.
319
- *
320
- * Refreshes every WLP pool-token oracle by default — `request_redeem` runs
321
- * `assert_prices_fresh` internally, so a stale oracle would abort the PTB.
322
- * Pass `skipOraclePriceRefresh: true` only when the caller is composing this
323
- * into a larger PTB that already pre-pumps prices.
324
- */
325
- export async function buildUnstakeAndRequestRedeemWlpTx(client, params) {
326
- const tx = newTx(params);
327
- const stakeAlias = params.stakeAlias ?? "WLP";
328
- await maybeConsolidate(client, tx, params.accountId, params);
329
- if (!params.skipOraclePriceRefresh) {
330
- await refreshWlpPoolOracles(tx, client, [], {
331
- cache: params.pythCache,
332
- lpType: params.lpType,
333
- });
334
- }
335
- unstake(client, tx, {
336
- accountId: params.accountId,
337
- stakeAlias,
338
- stakeType: params.lpType ?? client.wlpType(),
339
- withdrawalAmount: params.withdrawalAmount,
340
- rewarderTypes: params.rewarderTypes ?? client.getRewarderTypes(stakeAlias),
341
- bucketAccount: params.bucketAccount,
342
- });
343
- requestRedeemWlp(client, tx, {
344
- accountId: params.accountId,
345
- redeemTokenType: params.redeemTokenType,
346
- lpAmount: params.withdrawalAmount,
347
- lpType: params.lpType,
348
- bucketAccount: params.bucketAccount,
349
- });
350
- return tx;
351
- }
352
- /**
353
- * Cancels a pending WLP redeem request and re-stakes the returned WLP in the
354
- * same PTB. Third partner of `buildMintAndStakeWlpTx` /
355
- * `buildUnstakeAndRequestRedeemWlpTx`; together they keep the product
356
- * invariant that user-held WLP is always staked.
357
- */
358
- export function buildCancelRedeemAndStakeWlpTx(client, params) {
359
- const tx = newTx(params);
360
- const stakeAlias = params.stakeAlias ?? "WLP";
361
- cancelRedeemWlp(client, tx, {
362
- requestId: params.requestId,
363
- lpType: params.lpType,
364
- bucketAccount: params.bucketAccount,
365
- });
366
- stake(client, tx, {
367
- accountId: params.accountId,
368
- stakeAlias,
369
- stakeType: params.lpType ?? client.wlpType(),
370
- stakeAmount: params.stakeAmount,
371
- rewarderTypes: params.rewarderTypes ?? client.getRewarderTypes(stakeAlias),
372
- bucketAccount: params.bucketAccount,
373
- });
374
- return tx;
375
- }
376
- /**
377
- * Claims every accrued reward for `accountId` on the given stake pool. Each
378
- * resulting `Coin<R>` is TTO'd to the wxa account's UID address via
379
- * `wxa_account::transfer_coin<R>` (the default behaviour of the Move
380
- * `staking::claim` entry — no deposit-policy registration required for the
381
- * reward token). The account owner collects each coin via
382
- * `wxa_account::receive` in a separate user action.
383
- */
384
- export function buildClaimRewardsToAccountTx(client, params) {
385
- const tx = newTx(params);
386
- const stakeAlias = params.stakeAlias ?? "WLP";
387
- const stakeType = params.stakeType ?? client.wlpType();
388
- const rewarderTypes = params.rewarderTypes ?? client.getRewarderTypes(stakeAlias);
389
- if (rewarderTypes.length === 0) {
390
- throw new Error(`buildClaimRewardsToAccountTx: no rewarders configured for stakeAlias=${stakeAlias}`);
391
- }
392
- for (const rewardType of rewarderTypes) {
393
- claimReward(client, tx, {
394
- accountId: params.accountId,
395
- stakeAlias,
396
- stakeType,
397
- rewardType,
398
- bucketAccount: params.bucketAccount,
399
- });
400
- }
401
- return tx;
402
- }
403
- /**
404
- * Mint (EVM → Sui): `redeem_vaa<CREDIT>` + consume the resulting
405
- * `DepositRequest<CREDIT>` via the configured wxa deposit policy, in one
406
- * PTB. The recipient wxa account is encoded in the VAA payload.
407
- */
408
- export function buildRedeemVaaTx(client, params) {
409
- const tx = params.tx ?? new Transaction();
410
- const req = redeemVaa(client, tx, params);
411
- consumeCreditDeposit(client, tx, { depositRequest: req, creditType: params.creditType });
412
- return tx;
413
- }
414
- /**
415
- * Withdraw (user side): encode the route, `account::request_withdraw<CREDIT>`,
416
- * then `withdrawal_queue::enqueue<CREDIT>` — all in one PTB. A keeper later
417
- * drains the parked entry via {@link buildExecuteWithdrawalTx}.
418
- */
419
- export function buildRequestCreditWithdrawTx(client, params) {
420
- const tx = params.tx ?? new Transaction();
421
- const route = params.route.kind === "wormhole"
422
- ? routeWormhole(client, tx, params.route)
423
- : routeNative(client, tx, { assetType: params.route.assetType });
424
- const wreq = requestCreditWithdraw(client, tx, {
425
- accountId: params.accountId,
426
- amount: params.amount,
427
- recipient: params.recipient,
428
- route,
429
- creditType: params.creditType,
430
- bucketAccount: params.bucketAccount,
431
- });
432
- enqueueWithdrawal(client, tx, { withdrawRequest: wreq, creditType: params.creditType });
433
- return tx;
434
- }
435
- /** Keeper: drain one parked queue entry. */
436
- export function buildExecuteWithdrawalTx(client, params) {
437
- const tx = params.tx ?? new Transaction();
438
- if (params.route.kind === "wormhole") {
439
- const fee = params.route.wormholeFeeCoin ??
440
- tx.moveCall({ target: "0x2::coin::zero", typeArguments: ["0x2::sui::SUI"] });
441
- executeWithdrawalWormhole(client, tx, {
442
- key: params.key,
443
- wormholeFee: fee,
444
- creditType: params.creditType,
445
- bucketAccount: params.bucketAccount,
446
- });
447
- }
448
- else {
449
- executeWithdrawalNative(client, tx, {
450
- key: params.key,
451
- assetType: params.route.assetType,
452
- creditType: params.creditType,
453
- bucketAccount: params.bucketAccount,
454
- });
455
- }
456
- return tx;
457
- }
458
- // ============================================================================
459
- // Re-exports
460
- // ============================================================================
461
- export { openPythSponsorFund, PythCache, refreshOraclePrices, reimbursePythSponsor, updatePythPrices, };
@@ -1,8 +0,0 @@
1
- export * from "./account.ts";
2
- export * from "./credit.ts";
3
- export * from "./custody.ts";
4
- export * from "./order.ts";
5
- export * from "./referral.ts";
6
- export * from "./staking.ts";
7
- export * from "./trading.ts";
8
- export * from "./wlp.ts";
@@ -1,8 +0,0 @@
1
- export * from "./account.js";
2
- export * from "./credit.js";
3
- export * from "./custody.js";
4
- export * from "./order.js";
5
- export * from "./referral.js";
6
- export * from "./staking.js";
7
- export * from "./trading.js";
8
- export * from "./wlp.js";
@@ -1,124 +0,0 @@
1
- /**
2
- * Pyth price feed integration for the WaterX v3 oracle (ticker-based).
3
- *
4
- * Hermes REST fetches price update VAAs; on-chain a single PTB:
5
- * 1. wormhole::vaa::parse_and_verify
6
- * 2. pyth::create_authenticated_price_infos_using_accumulator
7
- * 3. pyth::update_single_price_feed (one per feed)
8
- * 4. for each ticker: oracle::new_collector(symbol) → pyth_rule::feed → oracle::aggregate
9
- *
10
- * No type parameters on `feed` / `aggregate` anymore — the oracle is one
11
- * shared object keyed by ticker. `pyth_rule::feed` looks up the on-chain
12
- * `PriceInfoObject` ID by ticker via the `pyth_rule::Config` identifier map.
13
- */
14
- import type { Transaction, TransactionArgument } from "@mysten/sui/transactions";
15
- import type { WaterXClient } from "../client.ts";
16
- type PriceTableInfo = {
17
- id: string;
18
- fieldType: string;
19
- };
20
- type PythStateInfo = {
21
- packageId: string;
22
- baseUpdateFee: bigint;
23
- };
24
- export declare class PythCache {
25
- pythStateInfo?: PythStateInfo;
26
- wormholePackageId?: string;
27
- priceTableInfo?: PriceTableInfo;
28
- priceFeedObjectIdCache: Map<string, string | undefined>;
29
- }
30
- export declare function fetchPriceFeedsUpdateData(endpoint: string, priceIds: string[]): Promise<Uint8Array[]>;
31
- /**
32
- * Append the on-chain Pyth update PTB block. Returns `PriceInfoObject` IDs
33
- * (one per `feedIds`, same order). After this you can call
34
- * `pyth_rule::feed` per ticker against the matching `PriceInfoObject`.
35
- *
36
- * If `sponsorFund` is provided, the per-feed update fee comes from the
37
- * sponsor pool (`pyth_sponsor_rule::split`) instead of `tx.gas`.
38
- */
39
- export declare function buildPythPriceUpdateCalls(tx: Transaction, client: WaterXClient, updates: Uint8Array[], feedIds: string[], cache?: PythCache, sponsorFund?: {
40
- fund: TransactionArgument;
41
- packageId: string;
42
- }): Promise<string[]>;
43
- /** All-in-one: fetch from Hermes, append update calls. Returns PriceInfoObject IDs. */
44
- export declare function updatePythPrices(tx: Transaction, client: WaterXClient, feedIds: string[], cache?: PythCache, sponsorFund?: {
45
- fund: TransactionArgument;
46
- packageId: string;
47
- }): Promise<string[]>;
48
- /**
49
- * Aggregate one ticker's price into the shared `Oracle`: build a collector, feed
50
- * every rule the ticker is configured for, then `aggregate`. The fed rule set must
51
- * match the on-chain weighted set for the ticker — `aggregator::remove_outliers`
52
- * aborts `EMissingPriceSource` if a weighted rule is missing from the collector:
53
- *
54
- * - **Pyth** — fed when `priceInfoObjectId` is supplied (i.e. the ticker has a
55
- * `pyth_rule.feeds` entry). Caller must run the Pyth update first so the
56
- * `PriceInfoObject` is fresh.
57
- * - **Supra** — fed alongside Pyth when supra is enabled + wired (abstains on-chain
58
- * for symbols it has no pair for).
59
- * - **Constant** — fed when the ticker is a constant ticker
60
- * ({@link WaterXClient.isConstantTicker}).
61
- *
62
- * "Dual-feed" (Pyth + Constant) and "constant-only" are not special cases — they
63
- * fall out of which rules the ticker is in: a constant ticker that also has a Pyth
64
- * feed gets both; one with no Pyth feed (no `priceInfoObjectId`) gets constant only.
65
- * Throws if no rule applies to the ticker.
66
- */
67
- export declare function aggregateTicker(tx: Transaction, client: WaterXClient, args: {
68
- ticker: string;
69
- priceInfoObjectId?: string;
70
- }): void;
71
- /**
72
- * Thin wrapper over {@link aggregateTicker} for a Pyth-fed ticker. Kept for
73
- * back-compat (e.g. WLP mint builds). Caller must run the Pyth update first.
74
- */
75
- export declare function aggregateTickerWithPyth(tx: Transaction, client: WaterXClient, args: {
76
- ticker: string;
77
- priceInfoObjectId: string;
78
- }): void;
79
- /**
80
- * {@link aggregateTicker} for a **constant-only** ticker (no Pyth update needed —
81
- * the price comes from the on-chain `constant_rule::Config`).
82
- *
83
- * Throws if the ticker ALSO has a `pyth_rule.feeds` entry (a dual-feed transition
84
- * ticker): feeding only the constant leg would leave the still-weighted Pyth rule
85
- * absent from the collector and abort `aggregate` with `EMissingPriceSource`. Such
86
- * tickers must go through {@link aggregateTicker} with a `priceInfoObjectId` (or
87
- * {@link refreshOraclePrices}), which feeds both.
88
- */
89
- export declare function aggregateTickerWithConstant(tx: Transaction, client: WaterXClient, args: {
90
- ticker: string;
91
- }): void;
92
- /**
93
- * Refresh multiple tickers in one PTB. For each ticker {@link aggregateTicker}
94
- * feeds whichever rules it is configured for (Pyth if it has a `pyth_rule.feeds`
95
- * entry, Supra when enabled, Constant when it's a constant ticker). Tickers with a
96
- * Pyth feed are updated on-chain via one shared Pyth accumulator first; the rest
97
- * (constant-only) skip Pyth entirely.
98
- */
99
- export declare function refreshOraclePrices(tx: Transaction, client: WaterXClient, tickers: string[], opts?: {
100
- cache?: PythCache;
101
- sponsorFund?: {
102
- fund: TransactionArgument;
103
- packageId: string;
104
- };
105
- }): Promise<void>;
106
- /**
107
- * Opens a `Fund` hot potato from the shared PythSponsor pool. Pass the
108
- * returned `{ fund, packageId }` straight to `refreshOraclePrices` as
109
- * `sponsorFund`, then `reimbursePythSponsor` once the TradingRequest is
110
- * built to attach the `PythSponsorRule` witness and return leftover
111
- * balance.
112
- */
113
- export declare function openPythSponsorFund(tx: Transaction, client: WaterXClient): {
114
- fund: TransactionArgument;
115
- packageId: string;
116
- };
117
- /**
118
- * Consumes the `Fund` hot potato from `openPythSponsorFund`, returns any
119
- * leftover SUI to the sponsor pool, and attaches the `PythSponsorRule`
120
- * witness to the given `TradingRequest<C_TOKEN>` so `trading::execute`
121
- * can satisfy a checklist that includes `PythSponsorRule`.
122
- */
123
- export declare function reimbursePythSponsor(tx: Transaction, client: WaterXClient, fund: TransactionArgument, tradingRequest: TransactionArgument, collateralType: string): void;
124
- export {};