@waterx/sdk 4.0.1 → 4.3.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 (130) hide show
  1. package/README.md +37 -17
  2. package/dist/cjs/src/account/account.js +2 -1
  3. package/dist/cjs/src/account/config.d.ts +3 -2
  4. package/dist/cjs/src/account/funding/balance.d.ts +25 -2
  5. package/dist/cjs/src/account/funding/balance.js +36 -24
  6. package/dist/cjs/src/account/funding/credit.js +6 -10
  7. package/dist/cjs/src/constants.d.ts +15 -1
  8. package/dist/cjs/src/constants.js +18 -4
  9. package/dist/cjs/src/generated/waterx_rule/deps/bucket_v2_framework/float.d.ts +8 -0
  10. package/dist/cjs/src/generated/waterx_rule/deps/bucket_v2_framework/float.js +13 -0
  11. package/dist/cjs/src/generated/waterx_rule/deps/sui/vec_map.d.ts +36 -0
  12. package/dist/cjs/src/generated/waterx_rule/deps/sui/vec_map.js +31 -0
  13. package/dist/cjs/src/generated/waterx_rule/waterx_rule.d.ts +813 -0
  14. package/dist/cjs/src/generated/waterx_rule/waterx_rule.js +946 -0
  15. package/dist/cjs/src/oracle/aggregate.d.ts +23 -21
  16. package/dist/cjs/src/oracle/aggregate.js +133 -59
  17. package/dist/cjs/src/oracle/config.d.ts +82 -41
  18. package/dist/cjs/src/oracle/config.js +0 -42
  19. package/dist/cjs/src/oracle/host.d.ts +26 -11
  20. package/dist/cjs/src/oracle/index.d.ts +6 -2
  21. package/dist/cjs/src/oracle/index.js +36 -8
  22. package/dist/cjs/src/oracle/price-update-rule.d.ts +16 -10
  23. package/dist/cjs/src/oracle/price-update-rule.js +3 -3
  24. package/dist/cjs/src/oracle/pyth.d.ts +44 -0
  25. package/dist/cjs/src/oracle/pyth.js +48 -6
  26. package/dist/cjs/src/oracle/read-plane.d.ts +70 -0
  27. package/dist/cjs/src/oracle/read-plane.js +78 -0
  28. package/dist/cjs/src/oracle/rule-registry.d.ts +14 -11
  29. package/dist/cjs/src/oracle/rule-registry.js +16 -11
  30. package/dist/cjs/src/oracle/rules/pyth-core-rule.js +4 -4
  31. package/dist/cjs/src/oracle/rules/pyth-lazer-rule.d.ts +21 -0
  32. package/dist/cjs/src/oracle/rules/pyth-lazer-rule.js +29 -4
  33. package/dist/cjs/src/oracle/rules/pyth-rule.js +5 -4
  34. package/dist/cjs/src/oracle/rules/waterx-rule.d.ts +126 -0
  35. package/dist/cjs/src/oracle/rules/waterx-rule.js +312 -0
  36. package/dist/cjs/src/perp/client.d.ts +71 -20
  37. package/dist/cjs/src/perp/client.js +35 -13
  38. package/dist/cjs/src/perp/config.d.ts +6 -7
  39. package/dist/cjs/src/perp/config.js +12 -11
  40. package/dist/cjs/src/perp/constants.d.ts +0 -6
  41. package/dist/cjs/src/perp/constants.js +11 -9
  42. package/dist/cjs/src/perp/fetch/account.js +3 -0
  43. package/dist/cjs/src/perp/fetch/bridge.js +2 -1
  44. package/dist/cjs/src/perp/fetch/market.js +2 -1
  45. package/dist/cjs/src/perp/fetch/positions.d.ts +16 -10
  46. package/dist/cjs/src/perp/fetch/positions.js +23 -20
  47. package/dist/cjs/src/perp/index.d.ts +9 -5
  48. package/dist/cjs/src/perp/index.js +16 -7
  49. package/dist/cjs/src/perp/liq-view.d.ts +64 -0
  50. package/dist/cjs/src/perp/liq-view.js +74 -0
  51. package/dist/cjs/src/perp/user/order.d.ts +13 -0
  52. package/dist/cjs/src/perp/user/order.js +30 -16
  53. package/dist/cjs/src/perp/user/staking.js +3 -2
  54. package/dist/cjs/src/perp/user/trading.js +25 -24
  55. package/dist/cjs/src/perp/user/wlp.js +6 -5
  56. package/dist/cjs/src/prediction/utils.d.ts +11 -2
  57. package/dist/cjs/src/prediction/utils.js +22 -22
  58. package/dist/cjs/src/unified-client.d.ts +41 -15
  59. package/dist/cjs/src/unified-client.js +4 -2
  60. package/dist/cjs/src/utils/format.d.ts +14 -0
  61. package/dist/cjs/src/utils/format.js +24 -0
  62. package/dist/cjs/src/utils/math.d.ts +304 -12
  63. package/dist/cjs/src/utils/math.js +397 -17
  64. package/dist/cjs/src/utils/validate.d.ts +69 -0
  65. package/dist/cjs/src/utils/validate.js +183 -0
  66. package/dist/src/account/account.js +2 -1
  67. package/dist/src/account/config.d.ts +3 -2
  68. package/dist/src/account/funding/balance.d.ts +25 -2
  69. package/dist/src/account/funding/balance.js +36 -24
  70. package/dist/src/account/funding/credit.js +6 -10
  71. package/dist/src/constants.d.ts +15 -1
  72. package/dist/src/constants.js +17 -3
  73. package/dist/src/generated/waterx_rule/deps/bucket_v2_framework/float.d.ts +8 -0
  74. package/dist/src/generated/waterx_rule/deps/bucket_v2_framework/float.js +10 -0
  75. package/dist/src/generated/waterx_rule/deps/sui/vec_map.d.ts +36 -0
  76. package/dist/src/generated/waterx_rule/deps/sui/vec_map.js +27 -0
  77. package/dist/src/generated/waterx_rule/waterx_rule.d.ts +813 -0
  78. package/dist/src/generated/waterx_rule/waterx_rule.js +855 -0
  79. package/dist/src/oracle/aggregate.d.ts +23 -21
  80. package/dist/src/oracle/aggregate.js +133 -59
  81. package/dist/src/oracle/config.d.ts +82 -41
  82. package/dist/src/oracle/config.js +1 -41
  83. package/dist/src/oracle/host.d.ts +26 -11
  84. package/dist/src/oracle/index.d.ts +6 -2
  85. package/dist/src/oracle/index.js +27 -9
  86. package/dist/src/oracle/price-update-rule.d.ts +16 -10
  87. package/dist/src/oracle/price-update-rule.js +3 -3
  88. package/dist/src/oracle/pyth.d.ts +44 -0
  89. package/dist/src/oracle/pyth.js +45 -5
  90. package/dist/src/oracle/read-plane.d.ts +70 -0
  91. package/dist/src/oracle/read-plane.js +74 -0
  92. package/dist/src/oracle/rule-registry.d.ts +14 -11
  93. package/dist/src/oracle/rule-registry.js +16 -11
  94. package/dist/src/oracle/rules/pyth-core-rule.js +5 -5
  95. package/dist/src/oracle/rules/pyth-lazer-rule.d.ts +21 -0
  96. package/dist/src/oracle/rules/pyth-lazer-rule.js +28 -3
  97. package/dist/src/oracle/rules/pyth-rule.js +5 -4
  98. package/dist/src/oracle/rules/waterx-rule.d.ts +126 -0
  99. package/dist/src/oracle/rules/waterx-rule.js +305 -0
  100. package/dist/src/perp/client.d.ts +71 -20
  101. package/dist/src/perp/client.js +36 -14
  102. package/dist/src/perp/config.d.ts +6 -7
  103. package/dist/src/perp/config.js +11 -9
  104. package/dist/src/perp/constants.d.ts +0 -6
  105. package/dist/src/perp/constants.js +10 -8
  106. package/dist/src/perp/fetch/account.js +3 -0
  107. package/dist/src/perp/fetch/bridge.js +2 -1
  108. package/dist/src/perp/fetch/market.js +2 -1
  109. package/dist/src/perp/fetch/positions.d.ts +16 -10
  110. package/dist/src/perp/fetch/positions.js +28 -20
  111. package/dist/src/perp/index.d.ts +9 -5
  112. package/dist/src/perp/index.js +6 -4
  113. package/dist/src/perp/liq-view.d.ts +64 -0
  114. package/dist/src/perp/liq-view.js +71 -0
  115. package/dist/src/perp/user/order.d.ts +13 -0
  116. package/dist/src/perp/user/order.js +30 -16
  117. package/dist/src/perp/user/staking.js +3 -2
  118. package/dist/src/perp/user/trading.js +25 -24
  119. package/dist/src/perp/user/wlp.js +6 -5
  120. package/dist/src/prediction/utils.d.ts +11 -2
  121. package/dist/src/prediction/utils.js +22 -22
  122. package/dist/src/unified-client.d.ts +41 -15
  123. package/dist/src/unified-client.js +4 -2
  124. package/dist/src/utils/format.d.ts +14 -0
  125. package/dist/src/utils/format.js +21 -0
  126. package/dist/src/utils/math.d.ts +304 -12
  127. package/dist/src/utils/math.js +394 -17
  128. package/dist/src/utils/validate.d.ts +69 -0
  129. package/dist/src/utils/validate.js +167 -0
  130. package/package.json +4 -1
@@ -0,0 +1,64 @@
1
+ /**
2
+ * View→raw adapter for the canonical liquidation-price estimate.
3
+ *
4
+ * `calcEstLiqPriceRaw` (`utils/math.ts`) takes the twelve RAW on-chain values
5
+ * the Move view takes, so every consumer that already holds a fetched
6
+ * `PositionDataView` row had to hand-map nine of them 1:1 off the row (the
7
+ * other three are the probe prices + maintenance margin, which the row does not
8
+ * carry) — and hand-carry the price invariant below in prose. That mapping is
9
+ * the SDK's job, so it lives here.
10
+ *
11
+ * ## Why perp-side and not in `utils/math.ts`
12
+ *
13
+ * `PositionDataView` is a perp read type (`perp/fetch/positions.ts`, decoded
14
+ * from the `waterx_perp_view` BCS struct). `utils/` is the shared base that
15
+ * `perp/` imports FROM — pulling a perp view type down into `utils/math.ts`
16
+ * would invert that direction and couple the line-agnostic math to the perp
17
+ * read layer. So the pure math stays in `utils/`, and the adapter that knows
18
+ * the perp row shape sits here, one layer up. Kept out of
19
+ * `perp/fetch/positions.ts` too: that module is transport (build PTB →
20
+ * simulate → decode), this is a pure field mapping with no client.
21
+ */
22
+ import type { PositionDataView } from "./fetch/positions.ts";
23
+ /**
24
+ * Probe prices the position row was READ AT — see the invariant on
25
+ * {@link calcEstLiqPriceRawFromView}. Whole-dollar u64, exactly as passed to
26
+ * the `perp/fetch` read (`WholeDollarUsdPrice`), plus the market's maintenance
27
+ * margin, which lives on `MarketData`, not on the position row.
28
+ */
29
+ export type EstLiqPriceViewOpts = {
30
+ /** `MarketData.maintenance_margin` — raw 1e9-scaled Float value. */
31
+ maintenanceMarginRaw: bigint;
32
+ /** The SAME `basePriceUsd` passed to the read that produced `position`. */
33
+ basePriceUsd: bigint;
34
+ /** The SAME `collateralPriceUsd` passed to the read that produced `position`. */
35
+ collateralPriceUsd: bigint;
36
+ };
37
+ /**
38
+ * Estimated liquidation price from a fetched `PositionDataView` row —
39
+ * bit-identical to that row's `est_liq_price`.
40
+ *
41
+ * Maps the row's nine raw fields onto {@link calcEstLiqPriceRaw} (the op-for-op
42
+ * mirror of `view.move::calculate_est_liq_price`) and takes the remaining
43
+ * three — the two probe prices plus the market's maintenance margin — from
44
+ * `opts`. Returns the raw 1e9-scaled u128 price; `0n` = already liquidatable /
45
+ * zero size.
46
+ *
47
+ * ## INVARIANT — the prices must be the ones the row was READ AT
48
+ *
49
+ * `opts.basePriceUsd` / `opts.collateralPriceUsd` MUST be the same whole-dollar
50
+ * values you passed to the `perp/fetch` read that produced `position`
51
+ * (`getPosition`, `getMarketPositions`, `getAccountPositions`, …).
52
+ *
53
+ * `PositionDataView` does NOT carry the probe prices it was computed at, so
54
+ * NOTHING — not this adapter, not the type system — can check this for you.
55
+ * Feed different prices and the row's fee / notional-derived fields were
56
+ * computed against one price while the estimate is computed against another:
57
+ * the result is a plausible-looking number that silently disagrees with
58
+ * `position.est_liq_price`. Thread the prices through from the read call site;
59
+ * never re-fetch or re-guess them here.
60
+ *
61
+ * @throws RangeError via `calcEstLiqPriceRaw` when any raw value is negative or
62
+ * `collateral_decimal` is outside `[0, 19]`.
63
+ */
64
+ export declare function calcEstLiqPriceRawFromView(position: PositionDataView, opts: EstLiqPriceViewOpts): bigint;
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ /**
3
+ * View→raw adapter for the canonical liquidation-price estimate.
4
+ *
5
+ * `calcEstLiqPriceRaw` (`utils/math.ts`) takes the twelve RAW on-chain values
6
+ * the Move view takes, so every consumer that already holds a fetched
7
+ * `PositionDataView` row had to hand-map nine of them 1:1 off the row (the
8
+ * other three are the probe prices + maintenance margin, which the row does not
9
+ * carry) — and hand-carry the price invariant below in prose. That mapping is
10
+ * the SDK's job, so it lives here.
11
+ *
12
+ * ## Why perp-side and not in `utils/math.ts`
13
+ *
14
+ * `PositionDataView` is a perp read type (`perp/fetch/positions.ts`, decoded
15
+ * from the `waterx_perp_view` BCS struct). `utils/` is the shared base that
16
+ * `perp/` imports FROM — pulling a perp view type down into `utils/math.ts`
17
+ * would invert that direction and couple the line-agnostic math to the perp
18
+ * read layer. So the pure math stays in `utils/`, and the adapter that knows
19
+ * the perp row shape sits here, one layer up. Kept out of
20
+ * `perp/fetch/positions.ts` too: that module is transport (build PTB →
21
+ * simulate → decode), this is a pure field mapping with no client.
22
+ */
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.calcEstLiqPriceRawFromView = calcEstLiqPriceRawFromView;
25
+ const math_ts_1 = require("../utils/math.js");
26
+ /**
27
+ * Estimated liquidation price from a fetched `PositionDataView` row —
28
+ * bit-identical to that row's `est_liq_price`.
29
+ *
30
+ * Maps the row's nine raw fields onto {@link calcEstLiqPriceRaw} (the op-for-op
31
+ * mirror of `view.move::calculate_est_liq_price`) and takes the remaining
32
+ * three — the two probe prices plus the market's maintenance margin — from
33
+ * `opts`. Returns the raw 1e9-scaled u128 price; `0n` = already liquidatable /
34
+ * zero size.
35
+ *
36
+ * ## INVARIANT — the prices must be the ones the row was READ AT
37
+ *
38
+ * `opts.basePriceUsd` / `opts.collateralPriceUsd` MUST be the same whole-dollar
39
+ * values you passed to the `perp/fetch` read that produced `position`
40
+ * (`getPosition`, `getMarketPositions`, `getAccountPositions`, …).
41
+ *
42
+ * `PositionDataView` does NOT carry the probe prices it was computed at, so
43
+ * NOTHING — not this adapter, not the type system — can check this for you.
44
+ * Feed different prices and the row's fee / notional-derived fields were
45
+ * computed against one price while the estimate is computed against another:
46
+ * the result is a plausible-looking number that silently disagrees with
47
+ * `position.est_liq_price`. Thread the prices through from the read call site;
48
+ * never re-fetch or re-guess them here.
49
+ *
50
+ * @throws RangeError via `calcEstLiqPriceRaw` when any raw value is negative or
51
+ * `collateral_decimal` is outside `[0, 19]`.
52
+ */
53
+ function calcEstLiqPriceRawFromView(position, opts) {
54
+ return (0, math_ts_1.calcEstLiqPriceRaw)({
55
+ isLong: position.is_long,
56
+ sizeRaw: BigInt(position.size),
57
+ avgPriceRaw: BigInt(position.average_price),
58
+ collateralAmountRaw: BigInt(position.collateral_amount),
59
+ collateralDecimal: position.collateral_decimal,
60
+ basePriceUsd: opts.basePriceUsd,
61
+ collateralPriceUsd: opts.collateralPriceUsd,
62
+ maintenanceMarginRaw: opts.maintenanceMarginRaw,
63
+ // The view pre-combines accrued + unrealized into `borrow_fee` / `funding_fee`
64
+ // — take those, NOT the `unrealized_*` pair, which would UNDER-count:
65
+ // `view.move` sets `borrow_fee = calculate_borrow_fee(cumul) +
66
+ // unrealized_borrow_fee`, and `position.move::calculate_funding_fee` returns
67
+ // the unrealized leg combined with the current period's, so the row's
68
+ // `unrealized_*` fields are strict SUBSETS of the combined pair.
69
+ borrowFeeRaw: BigInt(position.borrow_fee),
70
+ fundingSign: position.funding_fee_positive,
71
+ fundingFeeRaw: BigInt(position.funding_fee),
72
+ tradingFeeRaw: BigInt(position.unrealized_trading_fee),
73
+ });
74
+ }
@@ -7,6 +7,19 @@
7
7
  *
8
8
  * Use `triggerPrice === undefined` (market form) to park an order at
9
9
  * tick 0 in the limit book; a keeper picks it up via `match_orders`.
10
+ *
11
+ * ## Trigger prices are exact order-book KEYS
12
+ *
13
+ * Every `triggerPrice` / `currentTriggerPrice` / `newTriggerPrice` below is the
14
+ * raw 1e9-scaled value the book is keyed by — off by a single 1e-9 unit and the
15
+ * lookup silently finds nothing. Build them with `rawPrice` in its EXACT mode
16
+ * (`rawPrice("95000.5")`, an `ExactDecimalUsd` string); the `number` mode
17
+ * round-trips through f64 and is exact only below ≈ $9,007,199.
18
+ *
19
+ * The params stay `bigint | number` (the RAW scaled value, not USD): both are
20
+ * legitimate raw inputs, `toU128` already rejects a non-safe-integer `number`
21
+ * before it can serialize wrong, and dropping `number` would break published
22
+ * call shapes. The mode choice belongs one level up, at `rawPrice`.
10
23
  */
11
24
  import type { Transaction, TransactionArgument } from "@mysten/sui/transactions";
12
25
  import type { PerpClient } from "../client.ts";
@@ -8,6 +8,19 @@
8
8
  *
9
9
  * Use `triggerPrice === undefined` (market form) to park an order at
10
10
  * tick 0 in the limit book; a keeper picks it up via `match_orders`.
11
+ *
12
+ * ## Trigger prices are exact order-book KEYS
13
+ *
14
+ * Every `triggerPrice` / `currentTriggerPrice` / `newTriggerPrice` below is the
15
+ * raw 1e9-scaled value the book is keyed by — off by a single 1e-9 unit and the
16
+ * lookup silently finds nothing. Build them with `rawPrice` in its EXACT mode
17
+ * (`rawPrice("95000.5")`, an `ExactDecimalUsd` string); the `number` mode
18
+ * round-trips through f64 and is exact only below ≈ $9,007,199.
19
+ *
20
+ * The params stay `bigint | number` (the RAW scaled value, not USD): both are
21
+ * legitimate raw inputs, `toU128` already rejects a non-safe-integer `number`
22
+ * before it can serialize wrong, and dropping `number` would break published
23
+ * call shapes. The mode choice belongs one level up, at `rawPrice`.
11
24
  */
12
25
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
13
26
  if (k2 === undefined) k2 = k;
@@ -52,6 +65,7 @@ exports.addPreOrderRequest = addPreOrderRequest;
52
65
  const account_request_ts_1 = require("../../account/account-request.js");
53
66
  const request_ts_1 = require("../../generated/waterx_perp/request.js");
54
67
  const trading = __importStar(require("../../generated/waterx_perp/trading.js"));
68
+ const validate_ts_1 = require("../../utils/validate.js");
55
69
  const constants_ts_1 = require("../constants.js");
56
70
  /** Build a `request::PlaceOrderArgument` Move struct in the PTB. */
57
71
  function buildPlaceOrderArgument(client, tx, p) {
@@ -61,11 +75,11 @@ function buildPlaceOrderArgument(client, tx, p) {
61
75
  isLong: p.isLong,
62
76
  isStopOrder: p.isStopOrder,
63
77
  reduceOnly: p.reduceOnly,
64
- size: p.size,
65
- triggerPrice: p.triggerPrice ?? null,
66
- linkedPositionId: p.linkedPositionId ?? null,
67
- acceptablePrice: p.acceptablePrice ?? null,
68
- collateralAmount: p.collateralAmount,
78
+ size: (0, validate_ts_1.toU128)(p.size, "size"),
79
+ triggerPrice: (0, validate_ts_1.toU128OrNull)(p.triggerPrice, "triggerPrice"),
80
+ linkedPositionId: (0, validate_ts_1.toU64OrNull)(p.linkedPositionId, "linkedPositionId"),
81
+ acceptablePrice: (0, validate_ts_1.toU64OrNull)(p.acceptablePrice, "acceptablePrice"),
82
+ collateralAmount: (0, validate_ts_1.toU64)(p.collateralAmount, "collateralAmount"),
69
83
  },
70
84
  })(tx);
71
85
  return arg;
@@ -107,9 +121,9 @@ function cancelOrderRequest(client, tx, params) {
107
121
  ticker: params.ticker,
108
122
  senderRequest: req,
109
123
  accountId: params.accountId,
110
- orderId: params.orderId,
111
- triggerPrice: params.triggerPrice ?? 0n,
112
- orderTypeTag: params.orderTypeTag ?? constants_ts_1.ORDER_TAG_WILDCARD,
124
+ orderId: (0, validate_ts_1.toU64)(params.orderId, "orderId"),
125
+ triggerPrice: (0, validate_ts_1.toU128)(params.triggerPrice ?? 0n, "triggerPrice"),
126
+ orderTypeTag: (0, validate_ts_1.toU8)(params.orderTypeTag ?? constants_ts_1.ORDER_TAG_WILDCARD, "orderTypeTag"),
113
127
  },
114
128
  typeArguments: [params.collateralType, params.lpType ?? client.wlpType()],
115
129
  })(tx);
@@ -126,11 +140,11 @@ function updateOrderRequest(client, tx, params) {
126
140
  ticker: params.ticker,
127
141
  senderRequest: req,
128
142
  accountId: params.accountId,
129
- orderId: params.orderId,
130
- currentTriggerPrice: params.currentTriggerPrice,
131
- orderTypeTag: params.orderTypeTag,
132
- newSize: params.newSize,
133
- newTriggerPrice: params.newTriggerPrice,
143
+ orderId: (0, validate_ts_1.toU64)(params.orderId, "orderId"),
144
+ currentTriggerPrice: (0, validate_ts_1.toU128)(params.currentTriggerPrice, "currentTriggerPrice"),
145
+ orderTypeTag: (0, validate_ts_1.toU8)(params.orderTypeTag, "orderTypeTag"),
146
+ newSize: (0, validate_ts_1.toU128)(params.newSize, "newSize"),
147
+ newTriggerPrice: (0, validate_ts_1.toU128)(params.newTriggerPrice, "newTriggerPrice"),
134
148
  },
135
149
  typeArguments: [params.collateralType, params.lpType ?? client.wlpType()],
136
150
  })(tx);
@@ -147,8 +161,8 @@ function cancelPreOrderRequest(client, tx, params) {
147
161
  ticker: params.ticker,
148
162
  senderRequest: req,
149
163
  accountId: params.accountId,
150
- mainOrderId: params.mainOrderId,
151
- preOrderId: params.preOrderId,
164
+ mainOrderId: (0, validate_ts_1.toU64)(params.mainOrderId, "mainOrderId"),
165
+ preOrderId: (0, validate_ts_1.toU64)(params.preOrderId, "preOrderId"),
152
166
  },
153
167
  typeArguments: [params.collateralType, params.lpType ?? client.wlpType()],
154
168
  })(tx);
@@ -166,7 +180,7 @@ function addPreOrderRequest(client, tx, params) {
166
180
  ticker: params.ticker,
167
181
  senderRequest: req,
168
182
  accountId: params.accountId,
169
- mainOrderId: params.mainOrderId,
183
+ mainOrderId: (0, validate_ts_1.toU64)(params.mainOrderId, "mainOrderId"),
170
184
  preOrder: preArg,
171
185
  },
172
186
  typeArguments: [params.collateralType, params.lpType ?? client.wlpType()],
@@ -55,6 +55,7 @@ exports.unstake = unstake;
55
55
  exports.claimReward = claimReward;
56
56
  const account_request_ts_1 = require("../../account/account-request.js");
57
57
  const staking = __importStar(require("../../generated/waterx_staking/waterx_staking.js"));
58
+ const validate_ts_1 = require("../../utils/validate.js");
58
59
  function pool(client, stakeAlias) {
59
60
  const id = client.config.packages.waterx_staking?.pools?.[stakeAlias];
60
61
  if (!id) {
@@ -79,7 +80,7 @@ function stake(client, tx, params) {
79
80
  wxaRegistry: tx.object(client.config.packages.waterx_account.account_registry),
80
81
  accountId: params.accountId,
81
82
  accReq: req,
82
- stakeAmount: params.stakeAmount,
83
+ stakeAmount: (0, validate_ts_1.toU64Arg)(params.stakeAmount, "stakeAmount"),
83
84
  },
84
85
  typeArguments: [params.stakeType],
85
86
  })(tx);
@@ -110,7 +111,7 @@ function unstake(client, tx, params) {
110
111
  wxaRegistry: tx.object(client.config.packages.waterx_account.account_registry),
111
112
  accountId: params.accountId,
112
113
  accReq: req,
113
- withdrawalAmount: params.withdrawalAmount,
114
+ withdrawalAmount: (0, validate_ts_1.toU64Arg)(params.withdrawalAmount, "withdrawalAmount"),
114
115
  },
115
116
  typeArguments: [params.stakeType],
116
117
  })(tx);
@@ -60,6 +60,7 @@ exports.openPositionByKeeper = openPositionByKeeper;
60
60
  exports.closePositionByKeeper = closePositionByKeeper;
61
61
  const account_request_ts_1 = require("../../account/account-request.js");
62
62
  const trading = __importStar(require("../../generated/waterx_perp/trading.js"));
63
+ const validate_ts_1 = require("../../utils/validate.js");
63
64
  const constants_ts_1 = require("../constants.js");
64
65
  function typeArgs(client, t) {
65
66
  return [t.collateralType, t.lpType ?? client.wlpType()];
@@ -87,8 +88,8 @@ function closePositionRequest(client, tx, params) {
87
88
  ticker: params.ticker,
88
89
  senderRequest: req,
89
90
  accountId: params.accountId,
90
- positionId: params.positionId,
91
- acceptablePrice: params.acceptablePrice,
91
+ positionId: (0, validate_ts_1.toU64)(params.positionId, "positionId"),
92
+ acceptablePrice: (0, validate_ts_1.toU64)(params.acceptablePrice, "acceptablePrice"),
92
93
  },
93
94
  typeArguments: typeArgs(client, params),
94
95
  })(tx);
@@ -106,11 +107,11 @@ function increasePositionRequest(client, tx, params) {
106
107
  ticker: params.ticker,
107
108
  senderRequest: req,
108
109
  accountId: params.accountId,
109
- orderId: params.orderId ?? null,
110
- positionId: params.positionId,
111
- collateralAmount: params.collateralAmount,
112
- size: params.size,
113
- acceptablePrice: params.acceptablePrice,
110
+ orderId: (0, validate_ts_1.toU64OrNull)(params.orderId, "orderId"),
111
+ positionId: (0, validate_ts_1.toU64)(params.positionId, "positionId"),
112
+ collateralAmount: (0, validate_ts_1.toU64)(params.collateralAmount, "collateralAmount"),
113
+ size: (0, validate_ts_1.toU128)(params.size, "size"),
114
+ acceptablePrice: (0, validate_ts_1.toU64)(params.acceptablePrice, "acceptablePrice"),
114
115
  },
115
116
  typeArguments: typeArgs(client, params),
116
117
  })(tx);
@@ -128,9 +129,9 @@ function decreasePositionRequest(client, tx, params) {
128
129
  ticker: params.ticker,
129
130
  senderRequest: req,
130
131
  accountId: params.accountId,
131
- positionId: params.positionId,
132
- size: params.size,
133
- acceptablePrice: params.acceptablePrice,
132
+ positionId: (0, validate_ts_1.toU64)(params.positionId, "positionId"),
133
+ size: (0, validate_ts_1.toU128)(params.size, "size"),
134
+ acceptablePrice: (0, validate_ts_1.toU64)(params.acceptablePrice, "acceptablePrice"),
134
135
  },
135
136
  typeArguments: typeArgs(client, params),
136
137
  })(tx);
@@ -148,8 +149,8 @@ function depositCollateralRequest(client, tx, params) {
148
149
  ticker: params.ticker,
149
150
  senderRequest: req,
150
151
  accountId: params.accountId,
151
- positionId: params.positionId,
152
- collateralAmount: params.collateralAmount,
152
+ positionId: (0, validate_ts_1.toU64)(params.positionId, "positionId"),
153
+ collateralAmount: (0, validate_ts_1.toU64)(params.collateralAmount, "collateralAmount"),
153
154
  },
154
155
  typeArguments: typeArgs(client, params),
155
156
  })(tx);
@@ -167,8 +168,8 @@ function withdrawCollateralRequest(client, tx, params) {
167
168
  ticker: params.ticker,
168
169
  senderRequest: req,
169
170
  accountId: params.accountId,
170
- positionId: params.positionId,
171
- amount: params.amount,
171
+ positionId: (0, validate_ts_1.toU64)(params.positionId, "positionId"),
172
+ amount: (0, validate_ts_1.toU64)(params.amount, "amount"),
172
173
  },
173
174
  typeArguments: typeArgs(client, params),
174
175
  })(tx);
@@ -202,7 +203,7 @@ function liquidate(client, tx, params) {
202
203
  ticker: params.ticker,
203
204
  pool: tx.object(obj.wlpPool),
204
205
  senderRequest: req,
205
- positionId: params.positionId,
206
+ positionId: (0, validate_ts_1.toU64)(params.positionId, "positionId"),
206
207
  oracle: tx.object(obj.oracle),
207
208
  },
208
209
  typeArguments: typeArgs(client, params),
@@ -221,8 +222,8 @@ function batchLiquidate(client, tx, params) {
221
222
  pool: tx.object(obj.wlpPool),
222
223
  senderRequest: req,
223
224
  oracle: tx.object(obj.oracle),
224
- pageSize: params.pageSize,
225
- pageIndex: params.pageIndex,
225
+ pageSize: (0, validate_ts_1.toU64)(params.pageSize, "pageSize"),
226
+ pageIndex: (0, validate_ts_1.toU64)(params.pageIndex, "pageIndex"),
226
227
  },
227
228
  typeArguments: typeArgs(client, params),
228
229
  })(tx);
@@ -240,9 +241,9 @@ function matchOrders(client, tx, params) {
240
241
  pool: tx.object(obj.wlpPool),
241
242
  senderRequest: req,
242
243
  oracle: tx.object(obj.oracle),
243
- orderTypeTag: params.orderTypeTag,
244
- triggerPrice: params.triggerPrice,
245
- maxFills: params.maxFills,
244
+ orderTypeTag: (0, validate_ts_1.toU8)(params.orderTypeTag, "orderTypeTag"),
245
+ triggerPrice: (0, validate_ts_1.toU128)(params.triggerPrice, "triggerPrice"),
246
+ maxFills: (0, validate_ts_1.toU64)(params.maxFills, "maxFills"),
246
247
  },
247
248
  typeArguments: typeArgs(client, params),
248
249
  })(tx);
@@ -278,8 +279,8 @@ function openPositionByKeeper(client, tx, params) {
278
279
  accountObjectAddress: params.accountObjectAddress,
279
280
  collateralCoin: params.collateralCoin,
280
281
  isLong: params.isLong,
281
- size: params.size,
282
- acceptablePrice: params.acceptablePrice,
282
+ size: (0, validate_ts_1.toU128)(params.size, "size"),
283
+ acceptablePrice: (0, validate_ts_1.toU64)(params.acceptablePrice, "acceptablePrice"),
283
284
  oracle: tx.object(obj.oracle),
284
285
  },
285
286
  typeArguments: typeArgs(client, params),
@@ -297,8 +298,8 @@ function closePositionByKeeper(client, tx, params) {
297
298
  ticker: params.ticker,
298
299
  pool: tx.object(obj.wlpPool),
299
300
  keeperRequest: req,
300
- positionId: params.positionId,
301
- acceptablePrice: params.acceptablePrice,
301
+ positionId: (0, validate_ts_1.toU64)(params.positionId, "positionId"),
302
+ acceptablePrice: (0, validate_ts_1.toU64)(params.acceptablePrice, "acceptablePrice"),
302
303
  oracle: tx.object(obj.oracle),
303
304
  },
304
305
  typeArguments: typeArgs(client, params),
@@ -51,6 +51,7 @@ exports.settleRedeemWlp = settleRedeemWlp;
51
51
  exports.updateTokenValue = updateTokenValue;
52
52
  const account_request_ts_1 = require("../../account/account-request.js");
53
53
  const lp = __importStar(require("../../generated/waterx_perp/lp_pool.js"));
54
+ const validate_ts_1 = require("../../utils/validate.js");
54
55
  /** Returns the minted `lp_amount` so it can be chained into e.g. `stake`. */
55
56
  function mintWlp(client, tx, params) {
56
57
  const req = (0, account_request_ts_1.makeSenderRequest)(client, tx, params.bucketAccount);
@@ -63,8 +64,8 @@ function mintWlp(client, tx, params) {
63
64
  aum: tx.object(requireWlpAum(client)),
64
65
  senderRequest: req,
65
66
  accountId: params.accountId,
66
- depositAmount: params.depositAmount,
67
- minLpAmount: params.minLpAmount,
67
+ depositAmount: (0, validate_ts_1.toU64)(params.depositAmount, "depositAmount"),
68
+ minLpAmount: (0, validate_ts_1.toU64)(params.minLpAmount, "minLpAmount"),
68
69
  oracle: tx.object(client.config.packages.waterx_oracle.oracle),
69
70
  },
70
71
  typeArguments: [params.lpType ?? client.wlpType(), params.depositTokenType],
@@ -88,7 +89,7 @@ function requestRedeemWlp(client, tx, params) {
88
89
  wxaRegistry: tx.object(client.config.packages.waterx_account.account_registry),
89
90
  senderRequest: req,
90
91
  accountId: params.accountId,
91
- lpAmount: params.lpAmount,
92
+ lpAmount: (0, validate_ts_1.toU64)(params.lpAmount, "lpAmount"),
92
93
  },
93
94
  typeArguments: [params.lpType ?? client.wlpType(), params.redeemTokenType],
94
95
  })(tx);
@@ -102,7 +103,7 @@ function cancelRedeemWlp(client, tx, params) {
102
103
  globalConfig: tx.object(client.config.packages.waterx_perp.global_config),
103
104
  wxaRegistry: tx.object(client.config.packages.waterx_account.account_registry),
104
105
  senderRequest: req,
105
- requestId: params.requestId,
106
+ requestId: (0, validate_ts_1.toU64)(params.requestId, "requestId"),
106
107
  },
107
108
  typeArguments: [params.lpType ?? client.wlpType()],
108
109
  })(tx);
@@ -117,7 +118,7 @@ function settleRedeemWlp(client, tx, params) {
117
118
  wxaRegistry: tx.object(client.config.packages.waterx_account.account_registry),
118
119
  operatorRequest: req,
119
120
  aum: tx.object(requireWlpAum(client)),
120
- requestId: params.requestId,
121
+ requestId: (0, validate_ts_1.toU64)(params.requestId, "requestId"),
121
122
  oracle: tx.object(client.config.packages.waterx_oracle.oracle),
122
123
  },
123
124
  typeArguments: [params.lpType ?? client.wlpType(), params.redeemTokenType],
@@ -10,8 +10,17 @@ export declare function resolveSettlementCoinType(client: PredictClient, coinTyp
10
10
  export declare function resolveMarketRegistry(client: PredictClient, marketRegistry?: string): string;
11
11
  export declare function resolveAccountRegistry(client: PredictClient, accountRegistry?: string): string;
12
12
  export declare function resolveGlobalConfig(client: PredictClient, globalConfig?: string): string;
13
- /** Ensures `value` is a valid unsigned 64-bit integer. */
14
- export declare function assertU64(value: bigint, name?: string): bigint;
13
+ /**
14
+ * Ensures `value` is a valid unsigned 64-bit integer.
15
+ *
16
+ * Thin alias for the shared `utils/validate.toU64` — the u64 domain rule
17
+ * (bigint range check; a `number` must be a NON-NEGATIVE SAFE integer, since
18
+ * `2^53 + 2` passes `isInteger` yet has already lost f64 precision and would
19
+ * BCS-encode a silently-wrong value) is authored ONCE there for both product
20
+ * lines. Throws `RangeError` (a subclass of `Error`) naming the parameter.
21
+ */
22
+ export declare function assertU64(value: bigint | number, name?: string): bigint;
23
+ /** Normalize a u64-ish input (bigint / number / decimal string) to a checked `bigint`. */
15
24
  export declare function toBigInt(value: bigint | number | string): bigint;
16
25
  export declare function objectArg(tx: Transaction, value: ObjectArgument): TransactionArgument;
17
26
  export declare function idArg(tx: Transaction, value: IdArgument): TransactionArgument;
@@ -21,10 +21,11 @@ exports.marketIdArg = marketIdArg;
21
21
  exports.bytesToHex = bytesToHex;
22
22
  exports.optionU64 = optionU64;
23
23
  exports.receivingCoinArg = receivingCoinArg;
24
+ const validate_ts_1 = require("../utils/validate.js");
24
25
  const constants_ts_1 = require("./constants.js");
25
26
  const SELECTIONS = ["YES", "NO"];
26
27
  const OUTCOMES = ["YES", "NO", "INVALID"];
27
- const U64_MAX = (1n << 64n) - 1n;
28
+ const DECIMAL_INT_RE = /^\d+$/;
28
29
  function assertSelection(value) {
29
30
  if (SELECTIONS.includes(value))
30
31
  return value;
@@ -68,32 +69,31 @@ function resolveAccountRegistry(client, accountRegistry) {
68
69
  function resolveGlobalConfig(client, globalConfig) {
69
70
  return globalConfig === undefined || globalConfig === "" ? client.globalConfigId() : globalConfig;
70
71
  }
71
- /** Ensures `value` is a valid unsigned 64-bit integer. */
72
+ /**
73
+ * Ensures `value` is a valid unsigned 64-bit integer.
74
+ *
75
+ * Thin alias for the shared `utils/validate.toU64` — the u64 domain rule
76
+ * (bigint range check; a `number` must be a NON-NEGATIVE SAFE integer, since
77
+ * `2^53 + 2` passes `isInteger` yet has already lost f64 precision and would
78
+ * BCS-encode a silently-wrong value) is authored ONCE there for both product
79
+ * lines. Throws `RangeError` (a subclass of `Error`) naming the parameter.
80
+ */
72
81
  function assertU64(value, name = "value") {
73
- if (value < 0n) {
74
- throw new Error(`${name} must be non-negative, got ${value}`);
75
- }
76
- if (value > U64_MAX) {
77
- throw new Error(`${name} exceeds u64 max (${U64_MAX}), got ${value}`);
78
- }
79
- return value;
82
+ return (0, validate_ts_1.toU64)(value, name);
80
83
  }
84
+ /** Normalize a u64-ish input (bigint / number / decimal string) to a checked `bigint`. */
81
85
  function toBigInt(value) {
82
- if (typeof value === "bigint") {
86
+ if (typeof value !== "string")
83
87
  return assertU64(value);
84
- }
85
- if (typeof value === "number") {
86
- if (!Number.isInteger(value)) {
87
- throw new Error(`Invalid integer: ${value}`);
88
- }
89
- return assertU64(BigInt(value));
90
- }
88
+ // String is the one form the shared guard does not cover: digits parse exactly
89
+ // past the 2^53 f64 cliff, so the shape check lives here and the numeric
90
+ // domain still funnels through assertU64.
91
91
  const trimmed = value.trim();
92
92
  if (trimmed === "") {
93
- throw new Error("Invalid integer: empty string");
93
+ throw new RangeError("Invalid integer: empty string");
94
94
  }
95
- if (!/^\d+$/.test(trimmed)) {
96
- throw new Error(`Invalid integer: ${JSON.stringify(value)}`);
95
+ if (!DECIMAL_INT_RE.test(trimmed)) {
96
+ throw new RangeError(`Invalid integer: ${JSON.stringify(value)}`);
97
97
  }
98
98
  return assertU64(BigInt(trimmed));
99
99
  }
@@ -184,8 +184,8 @@ function optionU64(value) {
184
184
  function formatObjectVersion(version) {
185
185
  if (typeof version === "string") {
186
186
  const trimmed = version.trim();
187
- if (trimmed === "" || !/^\d+$/.test(trimmed)) {
188
- throw new Error(`Invalid object version: ${JSON.stringify(version)}`);
187
+ if (trimmed === "" || !DECIMAL_INT_RE.test(trimmed)) {
188
+ throw new RangeError(`Invalid object version: ${JSON.stringify(version)}`);
189
189
  }
190
190
  return trimmed;
191
191
  }