@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,167 @@
1
+ // Numeric-domain guards. One place for the question "is this value inside the
2
+ // domain the callee can honestly compute over?", asked at the three points
3
+ // where garbage enters:
4
+ //
5
+ // 1. INTEGER params (`toU64` / `toU128` / `toU8` / `toU16` + the `*OrNull` /
6
+ // `toU64Arg` variants) on the fetch AND write (tx-build) surfaces. What the
7
+ // generated BCS layer does with a bad `number` depends on the width, and at
8
+ // no width is the answer "throw naming the parameter" (verified against
9
+ // `@mysten/sui`'s `bcs`):
10
+ // - u64 / u128 throw on negative and on fractional — but a `number` past
11
+ // 2^53 has already lost precision in JS BEFORE BCS sees it. `2**53 + 1`
12
+ // collapses to `2**53` at parse time, so BCS faithfully encodes a value
13
+ // the caller never wrote. (`2**53 + 2` IS exactly representable and
14
+ // encodes correctly — the hazard is the odd side of the f64 cliff, not
15
+ // BCS.) Hence the `Number.isSafeInteger` floor here.
16
+ // - u8 / u16 throw when the value is out of range but SILENTLY TRUNCATE a
17
+ // fractional one (`bcs.u8().serialize(2.7)` encodes `2`), so the
18
+ // fractional case is the one only a guard can catch.
19
+ // Every integer param funnels through these so garbage throws with the
20
+ // parameter's name before a transaction is built.
21
+ // 2. FLOAT / BIGINT domains (`assertFinite` / `assertFiniteNonNegative` /
22
+ // `assertUnitFraction` / `assertTokenDecimal` / `assertUnsignedBigInt`) for
23
+ // the money-path math in `utils/math.ts`.
24
+ // 3. The WHOLE-DOLLAR USD price domain (`WholeDollarUsdPrice` /
25
+ // `parseWholeDollarU64`) the `waterx_perp_view` read params live in.
26
+ //
27
+ // Families 2 and 3 live HERE rather than privately inside `math.ts` /
28
+ // `perp/fetch/positions.ts` so the whole numeric-domain vocabulary is
29
+ // discoverable in one file: hiding it is why `parseWholeDollarU64` once
30
+ // re-derived `toU64`'s entire chain (finite → integer → safe integer →
31
+ // non-negative → <= u64::MAX) by hand.
32
+ //
33
+ // Everything throws `RangeError` naming the offending parameter.
34
+ //
35
+ // Visibility: this module is internal EXCEPT for the whole-dollar pair, which
36
+ // is public and re-exported from `perp/fetch/positions.ts` (→ the `perp/fetch`
37
+ // barrel → `@waterx/sdk`) — that is the published import path, not this file.
38
+ export const U64_MAX = 18446744073709551615n; // 2^64 − 1
39
+ const U128_MAX = 340282366920938463463374607431768211455n; // 2^128 − 1
40
+ function toUint(value, label, max, width) {
41
+ let v;
42
+ if (typeof value === "bigint") {
43
+ v = value;
44
+ }
45
+ else {
46
+ if (!Number.isSafeInteger(value) || value < 0) {
47
+ throw new RangeError(`${label} must be a non-negative safe integer (< 2^53) or a bigint, got ${value}`);
48
+ }
49
+ v = BigInt(value);
50
+ }
51
+ if (v < 0n || v > max) {
52
+ throw new RangeError(`${label} out of ${width} range, got ${v}`);
53
+ }
54
+ return v;
55
+ }
56
+ /** Validate a u64 param (bigint passthrough with range check; number must be a safe integer >= 0). */
57
+ export function toU64(value, label) {
58
+ return toUint(value, label, U64_MAX, "u64");
59
+ }
60
+ /** Validate a u128 param (bigint passthrough with range check; number must be a safe integer >= 0). */
61
+ export function toU128(value, label) {
62
+ return toUint(value, label, U128_MAX, "u128");
63
+ }
64
+ function toSmallUint(value, label, max, width) {
65
+ if (!Number.isInteger(value) || value < 0 || value > max) {
66
+ throw new RangeError(`${label} must be an integer in [0, ${max}] (${width}), got ${value}`);
67
+ }
68
+ return value;
69
+ }
70
+ /**
71
+ * Validate a u8 param. Returns a `number` (not a bigint) because that is the
72
+ * shape the generated BCS layer takes at this width.
73
+ *
74
+ * Separate from `toU64` on purpose: at u8/u16 the BCS writer already throws on
75
+ * an out-of-range value, but SILENTLY TRUNCATES a fractional one — so the
76
+ * fractional case is what this guard exists to catch (see the header note).
77
+ */
78
+ export function toU8(value, label) {
79
+ return toSmallUint(value, label, 0xff, "u8");
80
+ }
81
+ /** Validate a u16 param. Same width caveat as {@link toU8}. */
82
+ export function toU16(value, label) {
83
+ return toSmallUint(value, label, 0xffff, "u16");
84
+ }
85
+ /** `Option<u64>` param: `null` / `undefined` pass through as `null`, anything else is validated. */
86
+ export function toU64OrNull(value, label) {
87
+ return value == null ? null : toU64(value, label);
88
+ }
89
+ /** `Option<u128>` param: `null` / `undefined` pass through as `null`, anything else is validated. */
90
+ export function toU128OrNull(value, label) {
91
+ return value == null ? null : toU128(value, label);
92
+ }
93
+ /**
94
+ * u64 param that may instead be a PTB result chained from an earlier command
95
+ * (e.g. the `lp_amount` returned by `mintWlp`). A `TransactionArgument` passes
96
+ * through untouched — its value only exists on chain, where Move types it.
97
+ */
98
+ export function toU64Arg(value, label) {
99
+ return typeof value === "bigint" || typeof value === "number" ? toU64(value, label) : value;
100
+ }
101
+ // NOTE: there is deliberately no `toU128Arg`. No write builder exposes a u128
102
+ // param that can take a chained `TransactionArgument` (every u128 site — size /
103
+ // trigger price / acceptable price — is a caller-supplied literal), so a u128
104
+ // half of `toU64Arg` would be an export with no consumer. Add it back the day a
105
+ // builder actually chains a u128 PTB result.
106
+ /** Reject NaN / ±Infinity for a `number` money-path input. */
107
+ export function assertFinite(label, value) {
108
+ if (!Number.isFinite(value)) {
109
+ throw new RangeError(`${label} must be a finite number, got ${value}`);
110
+ }
111
+ }
112
+ /** Reject NaN / ±Infinity / negative for a `number` money-path input. */
113
+ export function assertFiniteNonNegative(label, value) {
114
+ assertFinite(label, value);
115
+ if (value < 0) {
116
+ throw new RangeError(`${label} must be >= 0, got ${value}`);
117
+ }
118
+ }
119
+ /**
120
+ * Reject anything outside `[0, 1]` for a `number` input that is a FRACTION of a
121
+ * whole — a maintenance-margin rate above 1 means "maintenance exceeds the
122
+ * entire notional", which is not a rate the caller can have meant.
123
+ */
124
+ export function assertUnitFraction(label, value) {
125
+ assertFinite(label, value);
126
+ if (value < 0 || value > 1) {
127
+ throw new RangeError(`${label} must be within [0, 1], got ${value}`);
128
+ }
129
+ }
130
+ /**
131
+ * Reject a token-decimal outside `[0, 19]` — the domain of the contract's
132
+ * `10u64.pow(decimal)` (`10^19 < 2^64 <= 10^20`), and the same bound the raw
133
+ * `POW10` table in `utils/math.ts` is built over.
134
+ */
135
+ export function assertTokenDecimal(label, value) {
136
+ if (!Number.isInteger(value) || value < 0 || value > 19) {
137
+ throw new RangeError(`${label} must be an integer in [0, 19], got ${value}`);
138
+ }
139
+ }
140
+ /** Reject a negative `bigint` where the on-chain type is unsigned. */
141
+ export function assertUnsignedBigInt(label, value) {
142
+ if (value < 0n) {
143
+ throw new RangeError(`${label} must be >= 0, got ${value}`);
144
+ }
145
+ }
146
+ const PLAIN_INT_RE = /^-?\d+$/;
147
+ /**
148
+ * Parse a user/env-supplied value into a whole-dollar u64 price
149
+ * (`WholeDollarUsdPrice`) with NO silent rounding: throws `RangeError` on
150
+ * fractional, negative, non-finite, non-numeric, or `> u64::MAX` input. If
151
+ * rounding is ever wanted it must be explicit at the call site
152
+ * (e.g. `parseWholeDollarU64(Math.round(x))`) — never baked in here.
153
+ *
154
+ * The numeric domain (finite → safe integer → non-negative → `<= u64::MAX`) is
155
+ * `toU64`'s and is NOT restated here; this function only adds the string form,
156
+ * whose digits parse exactly past the 2^53 f64 cliff.
157
+ */
158
+ export function parseWholeDollarU64(value) {
159
+ const label = "whole-dollar USD price";
160
+ if (typeof value !== "string")
161
+ return toU64(value, label);
162
+ const trimmed = value.trim();
163
+ if (!PLAIN_INT_RE.test(trimmed)) {
164
+ throw new RangeError(`${label} must be a plain integer string, got "${value}"`);
165
+ }
166
+ return toU64(BigInt(trimmed), label);
167
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@waterx/sdk",
3
- "version": "4.0.1",
3
+ "version": "4.3.0",
4
4
  "description": "WaterX SDK — perpetuals and prediction markets on Sui",
5
5
  "license": "MIT",
6
6
  "author": "WaterX",
@@ -170,6 +170,9 @@
170
170
  "lint:fix": "eslint \"src/**/*.ts\" \"test/**/*.ts\" \"scripts/**/*.ts\" \"examples/**/*.ts\" \"vitest.config.ts\" --fix",
171
171
  "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\" \"scripts/**/*.ts\" \"examples/**/*.ts\" \"vitest.config.ts\" \"eslint.config.mjs\" \"sui-codegen.config.mjs\" \"packages/predict-stress/**/*.{ts,mjs,json,md}\"",
172
172
  "env:init": "tsx scripts/init-local-env.ts",
173
+ "oracle:aggregates": "tsx scripts/print-oracle-aggregates.ts",
174
+ "oracle:aggregates:testnet": "tsx scripts/print-oracle-aggregates.ts --testnet",
175
+ "oracle:aggregates:mainnet": "tsx scripts/print-oracle-aggregates.ts --mainnet",
173
176
  "typecheck": "tsc --noEmit",
174
177
  "check": "pnpm lint && pnpm typecheck && pnpm test:unit",
175
178
  "test": "vitest run --project unit --project predict-unit",