@waterx/sdk 4.3.2 → 5.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 (150) hide show
  1. package/.claude/skills/waterx-sdk-integration/SKILL.md +225 -0
  2. package/README.md +216 -45
  3. package/SKILLS.md +34 -0
  4. package/dist/cjs/src/account/config.d.ts +3 -5
  5. package/dist/cjs/src/account/funding/wormhole.d.ts +1 -2
  6. package/dist/cjs/src/account/funding/wormhole.js +1 -2
  7. package/dist/cjs/src/generated/waterx_rule/waterx_rule.d.ts +140 -7
  8. package/dist/cjs/src/generated/waterx_rule/waterx_rule.js +163 -8
  9. package/dist/cjs/src/oracle/aggregate.d.ts +49 -62
  10. package/dist/cjs/src/oracle/aggregate.js +208 -164
  11. package/dist/cjs/src/oracle/config.d.ts +38 -51
  12. package/dist/cjs/src/oracle/config.js +1 -1
  13. package/dist/cjs/src/oracle/host.d.ts +14 -19
  14. package/dist/cjs/src/oracle/host.js +3 -3
  15. package/dist/cjs/src/oracle/index.d.ts +25 -16
  16. package/dist/cjs/src/oracle/index.js +87 -69
  17. package/dist/cjs/src/oracle/price-update-rule.d.ts +104 -66
  18. package/dist/cjs/src/oracle/price-update-rule.js +24 -16
  19. package/dist/cjs/src/oracle/pyth-pro-history.d.ts +36 -0
  20. package/dist/cjs/src/oracle/pyth-pro-history.js +41 -0
  21. package/dist/cjs/src/oracle/read-plane.d.ts +35 -53
  22. package/dist/cjs/src/oracle/read-plane.js +26 -48
  23. package/dist/cjs/src/oracle/read-prices.d.ts +105 -0
  24. package/dist/cjs/src/oracle/read-prices.js +162 -0
  25. package/dist/cjs/src/oracle/rule-registry.d.ts +12 -12
  26. package/dist/cjs/src/oracle/rule-registry.js +12 -14
  27. package/dist/cjs/src/oracle/rules/pyth-lazer-rule.d.ts +24 -3
  28. package/dist/cjs/src/oracle/rules/pyth-lazer-rule.js +40 -20
  29. package/dist/cjs/src/oracle/rules/waterx-rule.d.ts +271 -57
  30. package/dist/cjs/src/oracle/rules/waterx-rule.js +743 -144
  31. package/dist/cjs/src/oracle/schedule.d.ts +109 -0
  32. package/dist/cjs/src/oracle/schedule.js +991 -0
  33. package/dist/cjs/src/oracle/source-list.d.ts +31 -27
  34. package/dist/cjs/src/oracle/source-list.js +43 -45
  35. package/dist/cjs/src/oracle/symbol-catalog.d.ts +54 -0
  36. package/dist/cjs/src/oracle/symbol-catalog.js +69 -0
  37. package/dist/cjs/src/oracle/update-fetch.d.ts +11 -15
  38. package/dist/cjs/src/oracle/update-fetch.js +12 -19
  39. package/dist/cjs/src/oracle/validate.d.ts +105 -0
  40. package/dist/cjs/src/oracle/validate.js +185 -0
  41. package/dist/cjs/src/oracle/weight-coverage.d.ts +79 -0
  42. package/dist/cjs/src/oracle/weight-coverage.js +173 -0
  43. package/dist/cjs/src/perp/client.d.ts +40 -55
  44. package/dist/cjs/src/perp/client.js +44 -17
  45. package/dist/cjs/src/perp/config-view.d.ts +5 -14
  46. package/dist/cjs/src/perp/config-view.js +5 -16
  47. package/dist/cjs/src/perp/config.d.ts +3 -3
  48. package/dist/cjs/src/perp/config.js +11 -13
  49. package/dist/cjs/src/perp/index.d.ts +4 -6
  50. package/dist/cjs/src/perp/index.js +43 -18
  51. package/dist/cjs/src/perp/tx-builders/common.d.ts +86 -48
  52. package/dist/cjs/src/perp/tx-builders/common.js +115 -47
  53. package/dist/cjs/src/perp/tx-builders/wlp.d.ts +3 -14
  54. package/dist/cjs/src/perp/tx-builders/wlp.js +6 -35
  55. package/dist/cjs/src/perp/tx-builders.d.ts +6 -5
  56. package/dist/cjs/src/perp/tx-builders.js +16 -10
  57. package/dist/cjs/src/unified-client.d.ts +9 -35
  58. package/dist/cjs/src/unified-client.js +3 -4
  59. package/dist/cjs/src/utils/config.d.ts +0 -8
  60. package/dist/cjs/src/utils/config.js +5 -13
  61. package/dist/src/account/config.d.ts +3 -5
  62. package/dist/src/account/funding/wormhole.d.ts +1 -2
  63. package/dist/src/account/funding/wormhole.js +1 -2
  64. package/dist/src/generated/waterx_rule/waterx_rule.d.ts +140 -7
  65. package/dist/src/generated/waterx_rule/waterx_rule.js +151 -7
  66. package/dist/src/oracle/aggregate.d.ts +49 -62
  67. package/dist/src/oracle/aggregate.js +209 -164
  68. package/dist/src/oracle/config.d.ts +38 -51
  69. package/dist/src/oracle/config.js +1 -1
  70. package/dist/src/oracle/host.d.ts +14 -19
  71. package/dist/src/oracle/host.js +3 -3
  72. package/dist/src/oracle/index.d.ts +25 -16
  73. package/dist/src/oracle/index.js +61 -54
  74. package/dist/src/oracle/price-update-rule.d.ts +104 -66
  75. package/dist/src/oracle/price-update-rule.js +23 -16
  76. package/dist/src/oracle/pyth-pro-history.d.ts +36 -0
  77. package/dist/src/oracle/pyth-pro-history.js +38 -0
  78. package/dist/src/oracle/read-plane.d.ts +35 -53
  79. package/dist/src/oracle/read-plane.js +25 -47
  80. package/dist/src/oracle/read-prices.d.ts +105 -0
  81. package/dist/src/oracle/read-prices.js +156 -0
  82. package/dist/src/oracle/rule-registry.d.ts +12 -12
  83. package/dist/src/oracle/rule-registry.js +12 -14
  84. package/dist/src/oracle/rules/pyth-lazer-rule.d.ts +24 -3
  85. package/dist/src/oracle/rules/pyth-lazer-rule.js +39 -20
  86. package/dist/src/oracle/rules/waterx-rule.d.ts +271 -57
  87. package/dist/src/oracle/rules/waterx-rule.js +735 -144
  88. package/dist/src/oracle/schedule.d.ts +109 -0
  89. package/dist/src/oracle/schedule.js +985 -0
  90. package/dist/src/oracle/source-list.d.ts +31 -27
  91. package/dist/src/oracle/source-list.js +42 -43
  92. package/dist/src/oracle/symbol-catalog.d.ts +54 -0
  93. package/dist/src/oracle/symbol-catalog.js +65 -0
  94. package/dist/src/oracle/update-fetch.d.ts +11 -15
  95. package/dist/src/oracle/update-fetch.js +12 -18
  96. package/dist/src/oracle/validate.d.ts +105 -0
  97. package/dist/src/oracle/validate.js +177 -0
  98. package/dist/src/oracle/weight-coverage.d.ts +79 -0
  99. package/dist/src/oracle/weight-coverage.js +166 -0
  100. package/dist/src/perp/client.d.ts +40 -55
  101. package/dist/src/perp/client.js +45 -18
  102. package/dist/src/perp/config-view.d.ts +5 -14
  103. package/dist/src/perp/config-view.js +5 -16
  104. package/dist/src/perp/config.d.ts +3 -3
  105. package/dist/src/perp/config.js +11 -13
  106. package/dist/src/perp/index.d.ts +4 -6
  107. package/dist/src/perp/index.js +17 -5
  108. package/dist/src/perp/tx-builders/common.d.ts +86 -48
  109. package/dist/src/perp/tx-builders/common.js +114 -48
  110. package/dist/src/perp/tx-builders/wlp.d.ts +3 -14
  111. package/dist/src/perp/tx-builders/wlp.js +6 -35
  112. package/dist/src/perp/tx-builders.d.ts +6 -5
  113. package/dist/src/perp/tx-builders.js +15 -6
  114. package/dist/src/unified-client.d.ts +9 -35
  115. package/dist/src/unified-client.js +3 -4
  116. package/dist/src/utils/config.d.ts +0 -8
  117. package/dist/src/utils/config.js +5 -12
  118. package/package.json +8 -2
  119. package/dist/cjs/src/generated/pyth_sponsor_rule/deps/sui/balance.d.ts +0 -12
  120. package/dist/cjs/src/generated/pyth_sponsor_rule/deps/sui/balance.js +0 -17
  121. package/dist/cjs/src/generated/pyth_sponsor_rule/pyth_sponsor_rule.d.ts +0 -68
  122. package/dist/cjs/src/generated/pyth_sponsor_rule/pyth_sponsor_rule.js +0 -113
  123. package/dist/cjs/src/generated/waterx_pyth_rule/deps/sui/vec_map.d.ts +0 -36
  124. package/dist/cjs/src/generated/waterx_pyth_rule/deps/sui/vec_map.js +0 -31
  125. package/dist/cjs/src/generated/waterx_pyth_rule/pyth_rule.d.ts +0 -192
  126. package/dist/cjs/src/generated/waterx_pyth_rule/pyth_rule.js +0 -214
  127. package/dist/cjs/src/oracle/pyth.d.ts +0 -227
  128. package/dist/cjs/src/oracle/pyth.js +0 -652
  129. package/dist/cjs/src/oracle/rules/pyth-core-rule.d.ts +0 -15
  130. package/dist/cjs/src/oracle/rules/pyth-core-rule.js +0 -100
  131. package/dist/cjs/src/oracle/rules/pyth-rule.d.ts +0 -11
  132. package/dist/cjs/src/oracle/rules/pyth-rule.js +0 -29
  133. package/dist/cjs/src/oracle/rules/sponsor.d.ts +0 -32
  134. package/dist/cjs/src/oracle/rules/sponsor.js +0 -56
  135. package/dist/src/generated/pyth_sponsor_rule/deps/sui/balance.d.ts +0 -12
  136. package/dist/src/generated/pyth_sponsor_rule/deps/sui/balance.js +0 -14
  137. package/dist/src/generated/pyth_sponsor_rule/pyth_sponsor_rule.d.ts +0 -68
  138. package/dist/src/generated/pyth_sponsor_rule/pyth_sponsor_rule.js +0 -73
  139. package/dist/src/generated/waterx_pyth_rule/deps/sui/vec_map.d.ts +0 -36
  140. package/dist/src/generated/waterx_pyth_rule/deps/sui/vec_map.js +0 -27
  141. package/dist/src/generated/waterx_pyth_rule/pyth_rule.d.ts +0 -192
  142. package/dist/src/generated/waterx_pyth_rule/pyth_rule.js +0 -170
  143. package/dist/src/oracle/pyth.d.ts +0 -227
  144. package/dist/src/oracle/pyth.js +0 -638
  145. package/dist/src/oracle/rules/pyth-core-rule.d.ts +0 -15
  146. package/dist/src/oracle/rules/pyth-core-rule.js +0 -97
  147. package/dist/src/oracle/rules/pyth-rule.d.ts +0 -11
  148. package/dist/src/oracle/rules/pyth-rule.js +0 -26
  149. package/dist/src/oracle/rules/sponsor.d.ts +0 -32
  150. package/dist/src/oracle/rules/sponsor.js +0 -52
@@ -3,12 +3,12 @@
3
3
  * Oracle aggregation — the orchestrator that composes rules into the shared
4
4
  * `Oracle`. This is the ONE file that knows about every rule: it builds a
5
5
  * `PriceCollector`, feeds whichever rules a ticker is configured for
6
- * (Pyth / Lazer / Supra / Constant), then `aggregate`s.
6
+ * (Lazer / Waterx / Supra / Constant), then `aggregate`s.
7
7
  *
8
8
  * Per ticker:
9
9
  * collector = oracle::new_collector(ticker)
10
- * [pyth_rule::feed] when the ticker has a pyth_rule.feeds entry
11
10
  * [pyth_lazer_rule::feed] when the update leg produced a verified lazer Update
11
+ * [waterx_rule::collect_*] when the update leg fetched signed waterx data
12
12
  * [supra_rule::feed] when supra is enabled + wired
13
13
  * [constant_rule::feed] when the ticker is a constant ticker
14
14
  * oracle::aggregate(oracle, collector)
@@ -25,18 +25,16 @@
25
25
  */
26
26
  Object.defineProperty(exports, "__esModule", { value: true });
27
27
  exports.aggregateTicker = aggregateTicker;
28
- exports.aggregateTickerWithPyth = aggregateTickerWithPyth;
29
28
  exports.aggregateTickerWithConstant = aggregateTickerWithConstant;
30
29
  exports.refreshOraclePrices = refreshOraclePrices;
31
30
  const oracle_ts_1 = require("../generated/waterx_oracle/oracle.js");
32
- const record_ts_1 = require("../utils/record.js");
33
- const pyth_ts_1 = require("./pyth.js");
31
+ const price_update_rule_ts_1 = require("./price-update-rule.js");
34
32
  const rule_registry_ts_1 = require("./rule-registry.js");
35
33
  const constant_rule_ts_1 = require("./rules/constant-rule.js");
36
34
  const pyth_lazer_rule_ts_1 = require("./rules/pyth-lazer-rule.js");
37
- const pyth_rule_ts_1 = require("./rules/pyth-rule.js");
38
35
  const supra_rule_ts_1 = require("./rules/supra-rule.js");
39
36
  const waterx_rule_ts_1 = require("./rules/waterx-rule.js");
37
+ const validate_ts_1 = require("./validate.js");
40
38
  /**
41
39
  * Resolve one group's off-chain update payload for {@link refreshOraclePrices}:
42
40
  * try `provider.get(source, tickers)` first (when a provider is configured),
@@ -48,60 +46,53 @@ const waterx_rule_ts_1 = require("./rules/waterx-rule.js");
48
46
  * provider commonly caches one whole-universe payload per source — see
49
47
  * {@link UpdateDataProvider}), so it is narrowed to exactly `group.tickers`
50
48
  * via `rule.narrowUpdateData` before use. This is load-bearing, not
51
- * defensive: without it a Pyth Core hit would emit an
52
- * `update_single_price_feed` and charge its fee for every cached feed
53
- * instead of just this group's, and a payload that cannot cover the group
54
- * (`narrowUpdateData` `null`) would never reach the live-fetch fallback.
55
- * Each rule owns its own subsetting (Core subsets per-feed entries; Lazer's
56
- * indivisible payload passes whole iff fully covered), so the orchestrator
57
- * never branches on `kind` here. A hit whose `kind` doesn't match the
58
- * group's rule is a caller bug (the provider handed back a different rule's
59
- * payload), so that throws — via `narrowUpdateData`'s own
49
+ * defensive: a payload that cannot cover the group (`narrowUpdateData`
50
+ * `null`) would never reach the live-fetch fallback without it, and a
51
+ * divisible payload (waterx leaves) is subset instead of fanned out whole.
52
+ * Each rule owns its own subsetting (waterx subsets per-symbol leaves;
53
+ * Lazer's indivisible payload passes whole iff fully covered), so the
54
+ * orchestrator never branches on `kind` here. A hit whose `kind` doesn't
55
+ * match the group's rule is a caller bug (the provider handed back a
56
+ * different rule's payload), so that throws via `narrowUpdateData`'s own
60
57
  * `assertRuleUpdateData` guard — instead of silently falling back.
61
58
  */
62
- async function resolveGroupUpdateData(host, group, provider) {
63
- if (provider) {
64
- let cached = null;
65
- try {
66
- cached = await provider.get(group.source, group.tickers);
67
- }
68
- catch {
69
- // Provider errors must never break the money path — fall through to
70
- // the live fetch below exactly as a cache miss (`null`) would.
71
- }
72
- if (cached !== null) {
73
- // Wrong-kind hit throws inside narrowUpdateData (assertRuleUpdateData);
74
- // a hit that can't cover the group narrows to null → live-fetch below.
75
- const narrowed = group.rule.narrowUpdateData(host, cached, group.tickers);
76
- if (narrowed !== null)
77
- return narrowed;
78
- }
59
+ async function resolveCachedUpdateData(host, group, provider) {
60
+ if (!provider)
61
+ return null;
62
+ let cached;
63
+ try {
64
+ cached = await provider.get(group.source, group.tickers);
79
65
  }
80
- return group.rule.fetchUpdateData(host, group.tickers);
66
+ catch {
67
+ // Provider errors must never break the money path — report a miss and let
68
+ // the caller live-fetch, exactly as a `null` return would.
69
+ return null;
70
+ }
71
+ if (cached === null)
72
+ return null;
73
+ // Wrong-kind hit throws inside narrowUpdateData (assertRuleUpdateData); a
74
+ // hit that can't cover the group narrows to null, i.e. a miss.
75
+ return group.rule.narrowUpdateData(host, cached, group.tickers);
81
76
  }
82
77
  /**
83
78
  * Aggregate one ticker's price into the shared `Oracle`: build a collector, feed
84
79
  * every rule the ticker is configured for, then `aggregate`.
85
80
  *
86
- * - **Pyth** — fed when `priceInfoObjectId` is supplied (i.e. the ticker has a
87
- * `pyth_rule.feeds` entry). When this PTB's update leg refreshed the
88
- * `PriceInfoObject` it contributes a fresh price; when it did not (a
89
- * lazer-routed ticker), the on-chain rule only READS the object and abstains
90
- * if it is stale — it never aborts — so the call stays mandatory while
91
- * `pyth_rule` remains in the ticker's on-chain weighted set
92
- * (`EMissingPriceSource` requires every weighted rule to appear).
93
81
  * - **Lazer** — fed when `lazerUpdate` is supplied: the verified update this
94
82
  * PTB's lazer update leg produced with the network's verify entry
95
83
  * (`update_v2::Update` on mainnet, `update::Update` on testnet — see
96
84
  * `PythLazerRule.buildUpdateCalls`). If the ticker's aggregator does
97
85
  * not (yet) weight `PythLazerRule`, the contribution is silently dropped
98
86
  * on-chain — feeding ahead of the weight migration is harmless.
99
- * - **Supra** — fed alongside Pyth/Lazer when supra is enabled + wired
87
+ * - **Waterx** — fed when `waterxLeaf` (default shape) or `waterxEnvelope`
88
+ * (fallback shape) is supplied; verify AND feed are bundled into the one
89
+ * collect call per collector.
90
+ * - **Supra** — fed alongside the sources when supra is enabled + wired
100
91
  * (abstains on-chain for symbols it has no pair for).
101
92
  * - **Constant** — fed when the ticker is a constant ticker
102
93
  * ({@link OracleHost.isConstantTicker}).
103
94
  *
104
- * "Dual-feed" (Pyth + Constant, or Pyth + Lazer) and "constant-only" are not
95
+ * "Dual-feed" (Lazer + Constant, or Lazer + Waterx) and "constant-only" are not
105
96
  * special cases — they fall out of which rules the ticker is in. Throws if no
106
97
  * rule applies.
107
98
  */
@@ -112,25 +103,29 @@ function aggregateTicker(tx, host, args) {
112
103
  arguments: { symbol: args.ticker },
113
104
  })(tx);
114
105
  let fed = false;
115
- if (args.priceInfoObjectId) {
116
- (0, pyth_rule_ts_1.feedPythRule)(tx, host, collector, args.priceInfoObjectId);
117
- fed = true;
118
- }
119
106
  if (args.lazerUpdate !== undefined) {
120
107
  (0, pyth_lazer_rule_ts_1.feedLazerRule)(tx, host, collector, args.lazerUpdate);
121
108
  fed = true;
122
109
  }
123
- if (args.waterxEnvelope !== undefined) {
124
- // waterx_rule::collect_batch_latest verifies the batch signature and feeds
125
- // this collector's symbol from the batch. If the ticker's aggregator does
126
- // not (yet) weight `WaterxRule`, the contribution is silently dropped
127
- // on-chain feeding ahead of the weight migration is safe for THIS tx.
128
- // CAVEAT (unlike lazer): the feed call records a per-symbol signed-
129
- // timestamp high-water mark REGARDLESS of weights, and a replayed
130
- // timestamp ABORTS (`EReplayedSignature`, audit F-014) so two PTBs
131
- // carrying the same envelope for the same symbol cannot both land; the
132
- // second aborts even where waterx is unweighted. See WaterxRule's module
133
- // header.
110
+ // One waterx leg at most, and the leaf shape wins: both entries record the
111
+ // same per-symbol signed-timestamp high-water mark and feed the same rule
112
+ // witness into this collector, so emitting both would make the second one
113
+ // abstain on its own predecessor's mark for no gain. `refreshOraclePrices`
114
+ // only ever supplies one; a caller that passes both gets the cheaper leg.
115
+ if (args.waterxLeaf !== undefined) {
116
+ // waterx_rule::collect_single_with_proof re-derives the snapshot root from
117
+ // this leaf + its proof, verifies the enclave signature over that root, and
118
+ // feeds the price. If the ticker's aggregator does not (yet) weight
119
+ // `WaterxRule`, the contribution is silently dropped on-chain feeding
120
+ // ahead of the weight migration is safe. See WaterxRule's module header for
121
+ // the abort-vs-abstain split.
122
+ (0, waterx_rule_ts_1.feedWaterxRuleWithProof)(tx, host, collector, args.waterxLeaf);
123
+ fed = true;
124
+ }
125
+ else if (args.waterxEnvelope !== undefined) {
126
+ // Fallback shape: collect_batch_latest re-verifies the WHOLE batch
127
+ // signature (every item rebuilt in-PTB) and feeds this collector's symbol
128
+ // out of it. Only reached against a quote-center with no leaf route.
134
129
  (0, waterx_rule_ts_1.feedWaterxRule)(tx, host, collector, args.waterxEnvelope);
135
130
  fed = true;
136
131
  }
@@ -143,7 +138,7 @@ function aggregateTicker(tx, host, args) {
143
138
  fed = true;
144
139
  }
145
140
  if (!fed) {
146
- throw new Error(`no oracle rule configured for ticker '${args.ticker}' (no pyth feed, no lazer update, not a constant ticker)`);
141
+ throw new Error(`no oracle rule configured for ticker '${args.ticker}' (no lazer update, no waterx data, not a constant ticker)`);
147
142
  }
148
143
  (0, oracle_ts_1.aggregate)({
149
144
  package: oraclePkg,
@@ -154,33 +149,20 @@ function aggregateTicker(tx, host, args) {
154
149
  })(tx);
155
150
  }
156
151
  /**
157
- * Thin wrapper over {@link aggregateTicker} for a Pyth-fed ticker. Kept for
158
- * back-compat (e.g. WLP mint builds). Caller must run the Pyth update first.
159
- */
160
- function aggregateTickerWithPyth(tx, host, args) {
161
- aggregateTicker(tx, host, args);
162
- }
163
- /**
164
- * {@link aggregateTicker} for a **constant-only** ticker (no Pyth update needed —
165
- * the price comes from the on-chain `constant_rule::Config`).
166
- *
167
- * Throws if the ticker ALSO has a `pyth_rule.feeds` entry (a dual-feed transition
168
- * ticker): feeding only the constant leg would leave the still-weighted Pyth rule
169
- * absent from the collector and abort `aggregate` with `EMissingPriceSource`. Such
170
- * tickers must go through {@link aggregateTicker} with a `priceInfoObjectId` (or
171
- * {@link refreshOraclePrices}), which feeds both.
152
+ * {@link aggregateTicker} for a **constant-only** ticker (no source update
153
+ * needed the price comes from the on-chain `constant_rule::Config`). Kept as
154
+ * a named entry so constant-only call sites (e.g. WLP builders refreshing the
155
+ * USDCUSD pool token) read as what they are; it adds nothing over
156
+ * `aggregateTicker(tx, host, { ticker })`.
172
157
  */
173
158
  function aggregateTickerWithConstant(tx, host, args) {
174
- if ((0, record_ts_1.ownEntry)(host.config.packages.pyth_rule?.feeds, args.ticker) !== undefined) {
175
- throw new Error(`'${args.ticker}' is in pyth_rule.feeds (dual-feed) — feed both via aggregateTicker({ priceInfoObjectId }) / refreshOraclePrices, not aggregateTickerWithConstant`);
176
- }
177
159
  aggregateTicker(tx, host, { ticker: args.ticker });
178
160
  }
179
161
  /**
180
162
  * Refresh multiple tickers in one PTB. For each ticker {@link aggregateTicker}
181
- * feeds whichever rules it is configured for (Pyth if it has a `pyth_rule.feeds`
182
- * entry, Lazer if the lazer update leg served it — see below — Supra when
183
- * enabled, Constant when it's a constant ticker).
163
+ * feeds whichever rules it is configured for (Lazer if the lazer update leg
164
+ * served it, Waterx if the waterx leg fetched signed data for it — see below —
165
+ * Supra when enabled, Constant when it's a constant ticker).
184
166
  *
185
167
  * Before that, the on-chain price *update* leg is routed by the
186
168
  * `host.oracleSources` fed set (see `rule-registry.ts`): EVERY listed source
@@ -194,11 +176,15 @@ function aggregateTickerWithConstant(tx, host, args) {
194
176
  * source's feeds is caught here.
195
177
  *
196
178
  * Each source's fetch + build runs against its own infra, guaranteeing
197
- * per-rule PTB atomicity. A fee-source pre-check runs first (any listed
198
- * source's `requiresFeeSource`) BEFORE any off-chain fetch or PTB mutation
199
- * so a fee-charging source with no `opts.feeSource` throws
200
- * `OracleFeeSourceUnavailable` with zero wasted network calls and zero stray
201
- * moveCalls. Only once that check passes do the off-chain fetches run — in
179
+ * per-rule PTB atomicity. A credential pre-check runs early: any group that
180
+ * still needs to fetch, whose rule declares a `credential` the host does not
181
+ * carry while `host.pyth.api_key` is unset, throws `LazerApiKeyMissing`
182
+ * BEFORE any ORACLE fetch or PTB mutation — zero wasted oracle calls, zero
183
+ * stray moveCalls. (With an `updateDataProvider` configured, its per-source
184
+ * lookups run first so the check can be scoped to the groups the cache did
185
+ * NOT serve; a consumer-implemented provider may do I/O of its own, so the
186
+ * guarantee is about oracle fetches, not about every possible round trip.)
187
+ * Only once that check passes do the off-chain fetches run — in
202
188
  * parallel across sources — and ALL settle before the first PTB mutation;
203
189
  * on-chain reads inside `buildUpdateCalls` can still fail mid-append for
204
190
  * other reasons — callers discard the tx on any throw.
@@ -207,35 +193,24 @@ function aggregateTickerWithConstant(tx, host, args) {
207
193
  * `buildUpdateCalls` returns the verified `Update` PTB value
208
194
  * ({@link RuleUpdateHandle}), and every ticker in that group is aggregated
209
195
  * with `lazerUpdate` set so {@link aggregateTicker} appends
210
- * `pyth_lazer_rule::feed` against it. A lazer-routed ticker that still has a
211
- * `pyth_rule.feeds` entry ALSO keeps its `pyth_rule::feed` leg required
212
- * on-chain while `pyth_rule` stays in the ticker's weighted set
213
- * (`aggregator::remove_outliers` aborts `EMissingPriceSource` unless every
214
- * weighted rule appears in the collector; an abstention counts as
215
- * appearing), and safe: `pyth_rule::feed` only READS the `PriceInfoObject`
216
- * this PTB never refreshed and abstains when it is stale rather than
217
- * aborting. Conversely, a lazer feed call on an aggregator that does not
218
- * (yet) weight `PythLazerRule` is silently dropped on-chain — so
219
- * lazer-routing a ticker ahead of its on-chain weight migration prices it
220
- * from the remaining weighted rules instead of failing.
196
+ * `pyth_lazer_rule::feed` against it. A waterx-served group's signed data is
197
+ * carried straight from its fetched payload to the per-ticker collect call.
198
+ * A feed call on an aggregator that does not (yet) weight that rule is
199
+ * silently dropped on-chain so routing a ticker ahead of its on-chain
200
+ * weight migration prices it from the remaining weighted rules instead of
201
+ * failing.
221
202
  */
222
203
  async function refreshOraclePrices(tx, host, tickers, opts = {}) {
223
204
  if (tickers.length === 0)
224
- return;
205
+ return { refreshed: [], skipped: [] };
225
206
  // Dedupe the caller's list (order-preserving): a repeated ticker would
226
- // otherwise aggregate TWICE in this one PTB — wasted gas for every rule,
227
- // and a hard ABORT under waterx: the second `collect_batch_latest` carries
228
- // the same envelope, and the on-chain per-symbol replay guard rejects an
229
- // already-accepted signed timestamp (`EReplayedSignature`, F-014) even
230
- // inside a single transaction.
207
+ // otherwise aggregate TWICE in this one PTB — wasted gas for every rule, and
208
+ // under waterx the second collect would be dead weight on top of that: the
209
+ // on-chain per-symbol replay guard (F-014) sees its own predecessor's
210
+ // high-water mark from earlier in this same transaction and abstains, so the
211
+ // repeat pays full verification cost to contribute nothing.
231
212
  tickers = [...new Set(tickers)];
232
- // price_info_object lookup for every ticker with a pyth_rule.feeds entry
233
- // needed by aggregateTicker's (unchanged) Pyth feed step below regardless of
234
- // which rule performed the on-chain update for that ticker.
235
- const pythTickers = tickers.filter((t) => (0, record_ts_1.ownEntry)(host.config.packages.pyth_rule?.feeds, t) !== undefined);
236
- const priceInfoByTicker = new Map();
237
- pythTickers.forEach((t) => priceInfoByTicker.set(t, host.getPythFeed(t).price_info_object));
238
- // The fed set is a LIST (`host.oracleSources`, normalized + deduped at
213
+ // The fed set is a LIST (`host.oracleSources`, derived from the config at
239
214
  // client creation): ONE build carries every listed source's data, and the
240
215
  // chain's per-ticker weight tables decide which contributions count —
241
216
  // feeding an unweighted rule's PRICE is dropped on-chain, while starving a
@@ -245,8 +220,8 @@ async function refreshOraclePrices(tx, host, tickers, opts = {}) {
245
220
  // burns a per-symbol signed-timestamp high-water mark regardless of
246
221
  // weights — see aggregateTicker's waterx branch.) Still NO fallback
247
222
  // BETWEEN sources: each group serves only the tickers its own feeds list.
248
- // Zero-ticker groups are dropped here so everything downstream (fee check,
249
- // fetch fan-out, update-leg build) can assume every group has work.
223
+ // Zero-ticker groups are dropped here so everything downstream (credential
224
+ // check, fetch fan-out, update-leg build) can assume every group has work.
250
225
  const groups = host.oracleSources
251
226
  .map((source) => {
252
227
  const rule = (0, rule_registry_ts_1.resolveOracleRule)(source, opts.ruleOverrides);
@@ -254,66 +229,99 @@ async function refreshOraclePrices(tx, host, tickers, opts = {}) {
254
229
  return { source, rule, tickers: tickers.filter((t) => supported.has(t)) };
255
230
  })
256
231
  .filter((group) => group.tickers.length > 0);
257
- // Fail the tx-build (NOT client init, NOT a silent reroute) when NO listed
258
- // source has a feed for a requested ticker that actually needs a price
259
- // update. Only a CONSTANT-ONLY ticker is exempt — priced entirely by
260
- // `constant_rule`, it needs no update leg from any source. A DUAL-FEED ticker
261
- // (constant AND pyth) still needs its Pyth leg refreshed, so `isConstantTicker`
262
- // alone must NOT exempt it: with no source able to serve it, feeding an
263
- // unrefreshed Pyth leg would price it stale (or abort on a missing weighted
264
- // source). `priceInfoByTicker.has(t)` the ticker has a `pyth_rule.feeds`
265
- // entry, so `constant && !hasPyth` is exactly constant-only. This catches a
266
- // MISSING feed; a present-but-WRONG feed id is deliberately not validated
267
- // here (it aborts on-chain at dry-run).
232
+ // A ticker no listed source can price is SKIPPED, not thrown on, and named
233
+ // in the returned summary.
234
+ //
235
+ // This is a broad primitive: callers sweep whole market lists through it, and
236
+ // losing 29 tickers because the 30th is unconfigured is the wrong trade — the
237
+ // 29 still need their prices on chain. Safety lives one level up, where the
238
+ // ACTION is known: the `build*Tx` composers fail closed on the tickers their
239
+ // specific call actually depends on (`assertTickersRefreshed` /
240
+ // `assertWlpPoolRefreshed` in `perp/tx-builders/common.ts`). A bare
241
+ // `refreshOraclePrices` caller composing its own PTB reads `skipped` and
242
+ // decides for itself.
243
+ //
244
+ // Exemption: a CONSTANT-ONLY ticker needs no update leg from any source, so
245
+ // it counts as refreshed with just its constant feed.
246
+ //
247
+ // "Constant-only" is deliberately stricter than "constant-pinned". A ticker
248
+ // that constant_rule pins AND some other rule also has a feed for is NOT
249
+ // exempt, even when that other rule is outside the fed set: the on-chain
250
+ // aggregator very likely weights the rule whose feed the config carries, and
251
+ // aggregating a constant-only collector for it aborts `EMissingPriceSource`.
252
+ // Exempting on `isConstantTicker` alone would be correct only while the fed
253
+ // set covers every ticker's weighted set — an operator-maintained property
254
+ // this function cannot verify without reading chain state. So the strict
255
+ // reading fails SAFE: the ticker is skipped, named, and the composers turn
256
+ // that into a build error instead of an opaque on-chain abort.
257
+ //
258
+ // Catches a MISSING feed only; a present-but-WRONG feed id is deliberately
259
+ // left to abort on-chain at dry-run.
268
260
  const covered = new Set(groups.flatMap((group) => group.tickers));
269
- const isConstantOnly = (t) => host.isConstantTicker(t) && !priceInfoByTicker.has(t);
270
- const unservable = tickers.filter((t) => !covered.has(t) && !isConstantOnly(t));
271
- if (unservable.length > 0) {
272
- const sources = host.oracleSources.join(", ");
273
- throw new Error(`oracleSource [${sources}] has no feed configured for ticker(s): ` +
274
- `${unservable.join(", ")}. Sources are self-contained with no fallback add ` +
275
- `feeds for them under a listed source, or list a source that serves them.`);
276
- }
277
- // Fee-source pre-check, hoisted ABOVE the off-chain fetches and PTB build
278
- // below. It consults only `rule.requiresFeeSource` known before any fetch
279
- // or PTB mutation — so a fee-charging source (Pyth Core) in the fed set with
280
- // no `feeSource` throws with ZERO wasted network calls and zero PTB
281
- // commands, rather than waiting for `buildPythPriceUpdateCalls`'s own
282
- // per-call guard to fire after the off-chain fetches already ran.
283
- if (!opts.feeSource && groups.some((group) => group.rule.requiresFeeSource)) {
284
- throw new pyth_ts_1.OracleFeeSourceUnavailableError();
261
+ const { servable: refreshed, unservable: skipped } = (0, validate_ts_1.partitionServableTickers)(host, tickers, covered);
262
+ // Credential pre-check, hoisted ABOVE the oracle fetches and PTB build below
263
+ // (the position the retired fee-source pre-check held). It consults only
264
+ // `rule.credential` known without fetching anything — so a keyless build
265
+ // against an auth-first source (Lazer) in the fed set throws with ZERO
266
+ // wasted ORACLE calls and zero PTB commands, rather than waiting for that
267
+ // group's own fetch guard to fire after sibling groups' fetches already ran.
268
+ // Fully generic: the kind→value mapping is the port's
269
+ // (`oracleCredentialsFromHost`) and the ERROR is the rule's own, so this
270
+ // loop names neither a credential kind nor a rule.
271
+ //
272
+ // It does NOT run before absolutely everything, and cannot: scoping it to
273
+ // the groups that still need to fetch means knowing which ones the cache
274
+ // served, and `UpdateDataProvider` is consumer-implemented, so its lookup
275
+ // may do I/O. That is the trade — a provider round trip may precede the
276
+ // throw, an oracle fetch never does — and it buys the scoping below.
277
+ //
278
+ // The check is scoped to the groups that will actually FETCH. An
279
+ // `updateDataProvider` is a per-SOURCE cache (`get(source, tickers)`), so a
280
+ // consumer holding the credential out-of-band for one source must not
281
+ // exempt the whole fed set — with a waterx-only cache and a keyless Lazer
282
+ // group, a blanket skip would let the quote-center GET fire before the
283
+ // Lazer group failed. So cache lookups (no network) run first, and only the
284
+ // groups that missed are credential-checked.
285
+ //
286
+ // Phase 0 — resolve cache hits. Cheap and network-free by contract.
287
+ const cachedByGroup = await Promise.all(groups.map((group) => resolveCachedUpdateData(host, group, opts.updateDataProvider)));
288
+ const needsFetch = groups.filter((_, i) => cachedByGroup[i] === null);
289
+ const credentials = (0, price_update_rule_ts_1.oracleCredentialsFromHost)(host);
290
+ for (const { rule } of needsFetch) {
291
+ if (rule.credential && !credentials[rule.credential.kind]) {
292
+ throw rule.credential.missing();
293
+ }
285
294
  }
286
- // Phase 1 — resolve every group's update data IN PARALLEL: the per-source
287
- // fetches (Hermes VAA / Lazer POST / quote-center GET) are independent
288
- // network calls on the tx-build money path, so a multi-source fed set must
289
- // not pay one RTT per source sequentially. ALL fetches settle before the
290
- // first PTB mutation below, so a fetch failure never strands moveCalls in a
295
+ // Phase 1 — live-fetch whatever the cache did not serve, IN PARALLEL: the
296
+ // per-source fetches (Lazer POST / quote-center GET) are independent network
297
+ // calls on the tx-build money path, so a multi-source fed set must not pay
298
+ // one RTT per source sequentially. ALL of them settle before the first PTB
299
+ // mutation below, so a fetch failure never strands moveCalls in a
291
300
  // caller-owned tx — and a failure in ANY group fails the whole build (a
292
301
  // listed source is load-bearing; silently building without it would starve
293
302
  // its weighted tickers on-chain).
294
- const dataByGroup = await Promise.all(groups.map((group) => resolveGroupUpdateData(host, group, opts.updateDataProvider)));
303
+ const fetched = new Map();
304
+ await Promise.all(needsFetch.map(async (group) => {
305
+ fetched.set(group.source, await group.rule.fetchUpdateData(host, group.tickers));
306
+ }));
307
+ const dataByGroup = groups.map((group, i) => cachedByGroup[i] ?? fetched.get(group.source) ?? null);
295
308
  // Phase 2 — build each group's update leg sequentially, in list order, so
296
309
  // PTB command order stays deterministic. The carry step below is an
297
310
  // exhaustive switch over the group's rule kind: a future source whose feed
298
311
  // leg needs per-ticker data from its update leg must decide its carry here
299
312
  // — falling through silently would starve its weighted tickers on-chain.
300
313
  const lazerUpdateByTicker = new Map();
301
- // Signed batch envelope per waterx-served ticker. Unlike Lazer's shared PTB
302
- // handle, waterx's verify+feed is bundled into `collect_batch_latest` in the
303
- // per-ticker feed leg, so its `buildUpdateCalls` emits nothing and the
304
- // envelope is carried straight from the group's fetched data.
314
+ // Signed waterx data per served ticker a per-symbol Merkle leaf normally, a
315
+ // shared batch envelope on the fallback shape. Unlike Lazer's shared PTB
316
+ // handle, waterx's verify+feed is bundled into the per-ticker collect call, so
317
+ // its `buildUpdateCalls` emits nothing and the signed data is carried straight
318
+ // from the group's fetched data.
319
+ const waterxLeafByTicker = new Map();
305
320
  const waterxEnvelopeByTicker = new Map();
306
321
  for (const [i, group] of groups.entries()) {
307
322
  const data = dataByGroup[i] ?? null;
308
- const handle = (await group.rule.buildUpdateCalls(tx, host, data, {
309
- cache: opts.cache,
310
- feeSource: opts.feeSource,
311
- })) ?? undefined;
323
+ const handle = (await group.rule.buildUpdateCalls(tx, host, data)) ?? undefined;
312
324
  switch (group.rule.kind) {
313
- case "pyth_rule":
314
- // Core's update leg wrote the PriceInfoObjects in place — the feed
315
- // leg reads them by id (`priceInfoByTicker`), nothing to carry.
316
- break;
317
325
  case "pyth_lazer_rule":
318
326
  // Route by the handle's kind discriminant — the tag exists so a
319
327
  // non-lazer handle can never be silently fed into
@@ -325,12 +333,45 @@ async function refreshOraclePrices(tx, host, tickers, opts = {}) {
325
333
  break;
326
334
  case "waterx_rule": {
327
335
  // waterx_rule emits no shared handle (verify+feed is bundled into the
328
- // per-ticker `collect_batch_latest`), so the envelope is carried
329
- // straight from this group's fetched data to the feed leg below.
330
- const envelope = (0, waterx_rule_ts_1.waterxEnvelopeOf)(data);
331
- if (envelope) {
332
- for (const ticker of group.tickers)
333
- waterxEnvelopeByTicker.set(ticker, envelope);
336
+ // per-ticker collect call), so the signed data is carried straight from
337
+ // this group's fetched data to the feed leg below. Leaves are per-symbol
338
+ // and indexed BY symbol — never fanned out across the group like the
339
+ // envelope, since each leaf only verifies for its own symbol
340
+ // (`ECollectorSymbolMismatch`). A leaf for a symbol outside this group is
341
+ // dropped rather than carried: the feed leg is keyed by ticker anyway.
342
+ const leaves = (0, waterx_rule_ts_1.waterxLeavesOf)(data);
343
+ // `length > 0` matters: `{ leaves: [] }` is shape-valid (`[].every(...)`
344
+ // is `true`), so a bare truthiness test would TAKE this branch, carry
345
+ // nothing, and `break` past the envelope branch below.
346
+ if (leaves && leaves.length > 0) {
347
+ const served = new Set(group.tickers);
348
+ for (const leaf of leaves) {
349
+ if (served.has(leaf.symbol))
350
+ waterxLeafByTicker.set(leaf.symbol, leaf);
351
+ }
352
+ }
353
+ else {
354
+ const envelope = (0, waterx_rule_ts_1.waterxEnvelopeOf)(data);
355
+ if (envelope) {
356
+ for (const ticker of group.tickers)
357
+ waterxEnvelopeByTicker.set(ticker, envelope);
358
+ }
359
+ }
360
+ // Fail the BUILD, not the chain. Both suppliers of `data` already
361
+ // guarantee full coverage — the live fetch through `assertCoverage`, a
362
+ // cached payload through `narrowUpdateData` (which returns `null`, i.e.
363
+ // "miss → live fetch", rather than a partial) — so reaching here with a
364
+ // ticker uncarried means one of those invariants broke. Left alone it
365
+ // emits a collector with no waterx leg, which surfaces MUCH later as an
366
+ // opaque on-chain `EMissingPriceSource` (or, if the rule is unweighted
367
+ // for that ticker, as a silently thinner weighted set). This names the
368
+ // tickers instead.
369
+ const uncarried = group.tickers.filter((t) => !waterxLeafByTicker.has(t) && !waterxEnvelopeByTicker.has(t));
370
+ if (uncarried.length > 0) {
371
+ throw new Error(`waterx_rule update data carries no signed price for ticker(s): ${uncarried.join(", ")}. ` +
372
+ "Expected one signed leaf per ticker (or a batch envelope covering all of " +
373
+ "them) — a payload that serves none of a group's tickers must be reported " +
374
+ "as a miss, not fed.");
334
375
  }
335
376
  break;
336
377
  }
@@ -340,13 +381,16 @@ async function refreshOraclePrices(tx, host, tickers, opts = {}) {
340
381
  }
341
382
  }
342
383
  }
343
- // Aggregate each ticker, feeding whichever rules it is configured for.
344
- for (const ticker of tickers) {
384
+ // Aggregate each REFRESHED ticker, feeding whichever rules it is configured
385
+ // for. A skipped ticker gets no collector at all — aggregating one with no
386
+ // feed leg would write an empty collector and abort `EMissingPriceSource`.
387
+ for (const ticker of refreshed) {
345
388
  aggregateTicker(tx, host, {
346
389
  ticker,
347
- priceInfoObjectId: priceInfoByTicker.get(ticker),
348
390
  lazerUpdate: lazerUpdateByTicker.get(ticker),
391
+ waterxLeaf: waterxLeafByTicker.get(ticker),
349
392
  waterxEnvelope: waterxEnvelopeByTicker.get(ticker),
350
393
  });
351
394
  }
395
+ return { refreshed, skipped };
352
396
  }