@gearbox-protocol/sdk 18.3.0 → 18.4.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 (181) hide show
  1. package/dist/cjs/abi/310/iKillSwitchProxy.js +130 -0
  2. package/dist/cjs/onchain/accounts/intents/maxBorrowAndWithdraw.js +3 -1
  3. package/dist/cjs/onchain/index.js +18 -0
  4. package/dist/cjs/onchain/market/adapters/abi/adapters/iTreehouseFastlaneAdapter.js +113 -0
  5. package/dist/cjs/onchain/market/adapters/abi/adapters/iTreehouseRedemptionGatewayAdapter.js +258 -0
  6. package/dist/cjs/onchain/market/adapters/abi/adapters/iTreehouseRouterAdapter.js +161 -0
  7. package/dist/cjs/onchain/market/adapters/abi/adapters/index.js +6 -0
  8. package/dist/cjs/onchain/market/adapters/abi/index.js +13 -0
  9. package/dist/cjs/onchain/market/adapters/abi/treehouse/iTreehouseFastlane.js +39 -0
  10. package/dist/cjs/onchain/market/adapters/abi/treehouse/iTreehouseRedemptionGateway.js +230 -0
  11. package/dist/cjs/onchain/market/adapters/abi/treehouse/iTreehouseRouter.js +29 -0
  12. package/dist/cjs/onchain/market/adapters/abi/treehouse/index.js +7 -0
  13. package/dist/cjs/onchain/market/adapters/contracts/TreehouseFastlaneAdapterContract.js +72 -0
  14. package/dist/cjs/onchain/market/adapters/contracts/TreehouseRedemptionGatewayAdapterContract.js +117 -0
  15. package/dist/cjs/onchain/market/adapters/contracts/TreehouseRouterAdapterContract.js +61 -0
  16. package/dist/cjs/onchain/market/adapters/contracts/index.js +6 -0
  17. package/dist/cjs/onchain/market/adapters/createAdapter.js +6 -0
  18. package/dist/cjs/onchain/market/adapters/index.js +18 -0
  19. package/dist/cjs/onchain/market/index.js +18 -0
  20. package/dist/cjs/permissionless/api/index.js +31 -0
  21. package/dist/cjs/permissionless/api/oracles/PermissionlessOracles.js +637 -65
  22. package/dist/cjs/permissionless/api/oracles/enrich.js +99 -0
  23. package/dist/cjs/permissionless/api/oracles/index.js +33 -0
  24. package/dist/cjs/permissionless/api/oracles/onchain-feed.js +25 -0
  25. package/dist/cjs/permissionless/api/oracles/schemas.js +84 -59
  26. package/dist/cjs/permissionless/api/oracles/score/formula.js +56 -0
  27. package/dist/cjs/permissionless/api/oracles/score/index.js +11 -0
  28. package/dist/cjs/permissionless/api/oracles/score/run.js +252 -0
  29. package/dist/cjs/permissionless/api/oracles/score/types.js +1 -0
  30. package/dist/cjs/permissionless/api/oracles/stress/classify.js +258 -0
  31. package/dist/cjs/permissionless/api/oracles/stress/index.js +24 -0
  32. package/dist/cjs/permissionless/api/oracles/stress/override.js +125 -0
  33. package/dist/cjs/permissionless/api/oracles/stress/run.js +485 -0
  34. package/dist/cjs/permissionless/api/oracles/stress/score.js +98 -0
  35. package/dist/cjs/permissionless/api/oracles/stress/types.js +1 -0
  36. package/dist/cjs/permissionless/api/schemas.js +0 -3
  37. package/dist/cjs/permissionless/api/transactions/InstanceOwnerTransactions.js +14 -3
  38. package/dist/cjs/permissionless/api/transactions/lp-price-feeds.js +10 -4
  39. package/dist/cjs/permissionless/bindings/bytecode-repository.js +8 -0
  40. package/dist/cjs/permissionless/bindings/governance/governor.js +1 -1
  41. package/dist/cjs/permissionless/bindings/index.js +8 -3
  42. package/dist/cjs/permissionless/bindings/kill-switch-proxy.js +188 -0
  43. package/dist/cjs/permissionless/bindings/market-configurator-factory.js +39 -1
  44. package/dist/cjs/permissionless/bindings/market-configurator.js +142 -0
  45. package/dist/cjs/permissionless/index.js +42 -3
  46. package/dist/cjs/permissionless/utils/index.js +3 -0
  47. package/dist/cjs/permissionless/utils/literals.js +2 -0
  48. package/dist/cjs/permissionless/utils/price-update/get-prices.js +64 -0
  49. package/dist/cjs/permissionless/utils/price-update/index.js +2 -0
  50. package/dist/cjs/testnets/TestnetsAPI.js +179 -0
  51. package/dist/cjs/testnets/errors/TestnetNotFoundError.js +15 -0
  52. package/dist/cjs/testnets/errors/TestnetRequestFailedError.js +24 -0
  53. package/dist/cjs/testnets/errors/TestnetStatusError.js +19 -0
  54. package/dist/cjs/testnets/errors/TestnetValidationError.js +24 -0
  55. package/dist/cjs/testnets/errors/TestnetWaitTimeoutError.js +30 -0
  56. package/dist/cjs/testnets/errors/TestnetsError.js +34 -0
  57. package/dist/cjs/testnets/errors/index.js +13 -0
  58. package/dist/cjs/testnets/index.js +32 -0
  59. package/dist/cjs/testnets/package.json +1 -0
  60. package/dist/cjs/testnets/schemas.js +242 -0
  61. package/dist/cjs/testnets/types.js +1 -0
  62. package/dist/esm/abi/310/iKillSwitchProxy.js +129 -0
  63. package/dist/esm/onchain/accounts/intents/maxBorrowAndWithdraw.js +3 -1
  64. package/dist/esm/onchain/index.js +10 -1
  65. package/dist/esm/onchain/market/adapters/abi/adapters/iTreehouseFastlaneAdapter.js +112 -0
  66. package/dist/esm/onchain/market/adapters/abi/adapters/iTreehouseRedemptionGatewayAdapter.js +257 -0
  67. package/dist/esm/onchain/market/adapters/abi/adapters/iTreehouseRouterAdapter.js +160 -0
  68. package/dist/esm/onchain/market/adapters/abi/adapters/index.js +4 -1
  69. package/dist/esm/onchain/market/adapters/abi/index.js +8 -1
  70. package/dist/esm/onchain/market/adapters/abi/treehouse/iTreehouseFastlane.js +38 -0
  71. package/dist/esm/onchain/market/adapters/abi/treehouse/iTreehouseRedemptionGateway.js +229 -0
  72. package/dist/esm/onchain/market/adapters/abi/treehouse/iTreehouseRouter.js +28 -0
  73. package/dist/esm/onchain/market/adapters/abi/treehouse/index.js +4 -0
  74. package/dist/esm/onchain/market/adapters/contracts/TreehouseFastlaneAdapterContract.js +71 -0
  75. package/dist/esm/onchain/market/adapters/contracts/TreehouseRedemptionGatewayAdapterContract.js +116 -0
  76. package/dist/esm/onchain/market/adapters/contracts/TreehouseRouterAdapterContract.js +60 -0
  77. package/dist/esm/onchain/market/adapters/contracts/index.js +4 -1
  78. package/dist/esm/onchain/market/adapters/createAdapter.js +6 -0
  79. package/dist/esm/onchain/market/adapters/index.js +10 -1
  80. package/dist/esm/onchain/market/index.js +10 -1
  81. package/dist/esm/permissionless/api/index.js +7 -1
  82. package/dist/esm/permissionless/api/oracles/PermissionlessOracles.js +639 -67
  83. package/dist/esm/permissionless/api/oracles/enrich.js +94 -0
  84. package/dist/esm/permissionless/api/oracles/index.js +10 -1
  85. package/dist/esm/permissionless/api/oracles/onchain-feed.js +24 -0
  86. package/dist/esm/permissionless/api/oracles/schemas.js +80 -58
  87. package/dist/esm/permissionless/api/oracles/score/formula.js +51 -0
  88. package/dist/esm/permissionless/api/oracles/score/index.js +4 -0
  89. package/dist/esm/permissionless/api/oracles/score/run.js +250 -0
  90. package/dist/esm/permissionless/api/oracles/score/types.js +1 -0
  91. package/dist/esm/permissionless/api/oracles/stress/classify.js +256 -0
  92. package/dist/esm/permissionless/api/oracles/stress/index.js +6 -0
  93. package/dist/esm/permissionless/api/oracles/stress/override.js +119 -0
  94. package/dist/esm/permissionless/api/oracles/stress/run.js +478 -0
  95. package/dist/esm/permissionless/api/oracles/stress/score.js +95 -0
  96. package/dist/esm/permissionless/api/oracles/stress/types.js +1 -0
  97. package/dist/esm/permissionless/api/schemas.js +2 -4
  98. package/dist/esm/permissionless/api/transactions/InstanceOwnerTransactions.js +14 -3
  99. package/dist/esm/permissionless/api/transactions/lp-price-feeds.js +10 -4
  100. package/dist/esm/permissionless/bindings/bytecode-repository.js +8 -0
  101. package/dist/esm/permissionless/bindings/governance/governor.js +1 -1
  102. package/dist/esm/permissionless/bindings/index.js +5 -4
  103. package/dist/esm/permissionless/bindings/kill-switch-proxy.js +184 -0
  104. package/dist/esm/permissionless/bindings/market-configurator-factory.js +37 -0
  105. package/dist/esm/permissionless/bindings/market-configurator.js +143 -1
  106. package/dist/esm/permissionless/index.js +13 -6
  107. package/dist/esm/permissionless/utils/index.js +3 -3
  108. package/dist/esm/permissionless/utils/literals.js +2 -1
  109. package/dist/esm/permissionless/utils/price-update/get-prices.js +63 -1
  110. package/dist/esm/permissionless/utils/price-update/index.js +2 -2
  111. package/dist/esm/testnets/TestnetsAPI.js +178 -0
  112. package/dist/esm/testnets/errors/TestnetNotFoundError.js +14 -0
  113. package/dist/esm/testnets/errors/TestnetRequestFailedError.js +23 -0
  114. package/dist/esm/testnets/errors/TestnetStatusError.js +18 -0
  115. package/dist/esm/testnets/errors/TestnetValidationError.js +23 -0
  116. package/dist/esm/testnets/errors/TestnetWaitTimeoutError.js +29 -0
  117. package/dist/esm/testnets/errors/TestnetsError.js +33 -0
  118. package/dist/esm/testnets/errors/index.js +7 -0
  119. package/dist/esm/testnets/index.js +11 -0
  120. package/dist/esm/testnets/package.json +1 -0
  121. package/dist/esm/testnets/schemas.js +228 -0
  122. package/dist/esm/testnets/types.js +1 -0
  123. package/dist/types/abi/310/iKillSwitchProxy.d.ts +114 -0
  124. package/dist/types/onchain/index.d.ts +10 -1
  125. package/dist/types/onchain/market/adapters/abi/adapters/iTreehouseFastlaneAdapter.d.ts +101 -0
  126. package/dist/types/onchain/market/adapters/abi/adapters/iTreehouseRedemptionGatewayAdapter.d.ts +237 -0
  127. package/dist/types/onchain/market/adapters/abi/adapters/iTreehouseRouterAdapter.d.ts +146 -0
  128. package/dist/types/onchain/market/adapters/abi/adapters/index.d.ts +4 -1
  129. package/dist/types/onchain/market/adapters/abi/index.d.ts +8 -1
  130. package/dist/types/onchain/market/adapters/abi/treehouse/iTreehouseFastlane.d.ts +33 -0
  131. package/dist/types/onchain/market/adapters/abi/treehouse/iTreehouseRedemptionGateway.d.ts +207 -0
  132. package/dist/types/onchain/market/adapters/abi/treehouse/iTreehouseRouter.d.ts +27 -0
  133. package/dist/types/onchain/market/adapters/abi/treehouse/index.d.ts +4 -0
  134. package/dist/types/onchain/market/adapters/contracts/TreehouseFastlaneAdapterContract.d.ts +156 -0
  135. package/dist/types/onchain/market/adapters/contracts/TreehouseRedemptionGatewayAdapterContract.d.ts +481 -0
  136. package/dist/types/onchain/market/adapters/contracts/TreehouseRouterAdapterContract.d.ts +193 -0
  137. package/dist/types/onchain/market/adapters/contracts/index.d.ts +4 -1
  138. package/dist/types/onchain/market/adapters/index.d.ts +10 -1
  139. package/dist/types/onchain/market/index.d.ts +10 -1
  140. package/dist/types/permissionless/api/index.d.ts +10 -2
  141. package/dist/types/permissionless/api/oracles/PermissionlessOracles.d.ts +237 -47
  142. package/dist/types/permissionless/api/oracles/enrich.d.ts +59 -0
  143. package/dist/types/permissionless/api/oracles/index.d.ts +12 -2
  144. package/dist/types/permissionless/api/oracles/onchain-feed.d.ts +15 -0
  145. package/dist/types/permissionless/api/oracles/schemas.d.ts +147 -41
  146. package/dist/types/permissionless/api/oracles/score/formula.d.ts +36 -0
  147. package/dist/types/permissionless/api/oracles/score/index.d.ts +4 -0
  148. package/dist/types/permissionless/api/oracles/score/run.d.ts +42 -0
  149. package/dist/types/permissionless/api/oracles/score/types.d.ts +118 -0
  150. package/dist/types/permissionless/api/oracles/stress/classify.d.ts +57 -0
  151. package/dist/types/permissionless/api/oracles/stress/index.d.ts +6 -0
  152. package/dist/types/permissionless/api/oracles/stress/override.d.ts +59 -0
  153. package/dist/types/permissionless/api/oracles/stress/run.d.ts +100 -0
  154. package/dist/types/permissionless/api/oracles/stress/score.d.ts +57 -0
  155. package/dist/types/permissionless/api/oracles/stress/types.d.ts +272 -0
  156. package/dist/types/permissionless/api/oracles/types.d.ts +320 -125
  157. package/dist/types/permissionless/api/schemas.d.ts +0 -2
  158. package/dist/types/permissionless/api/transactions/lp-price-feeds.d.ts +9 -1
  159. package/dist/types/permissionless/bindings/bytecode-repository.d.ts +10 -1
  160. package/dist/types/permissionless/bindings/index.d.ts +4 -3
  161. package/dist/types/permissionless/bindings/kill-switch-proxy.d.ts +205 -0
  162. package/dist/types/permissionless/bindings/market-configurator-factory.d.ts +30 -0
  163. package/dist/types/permissionless/bindings/market-configurator.d.ts +30 -1
  164. package/dist/types/permissionless/bindings/types.d.ts +27 -0
  165. package/dist/types/permissionless/index.d.ts +15 -6
  166. package/dist/types/permissionless/utils/index.d.ts +3 -3
  167. package/dist/types/permissionless/utils/literals.d.ts +1 -0
  168. package/dist/types/permissionless/utils/price-update/get-prices.d.ts +33 -0
  169. package/dist/types/permissionless/utils/price-update/index.d.ts +2 -2
  170. package/dist/types/testnets/TestnetsAPI.d.ts +108 -0
  171. package/dist/types/testnets/errors/TestnetNotFoundError.d.ts +12 -0
  172. package/dist/types/testnets/errors/TestnetRequestFailedError.d.ts +11 -0
  173. package/dist/types/testnets/errors/TestnetStatusError.d.ts +15 -0
  174. package/dist/types/testnets/errors/TestnetValidationError.d.ts +16 -0
  175. package/dist/types/testnets/errors/TestnetWaitTimeoutError.d.ts +21 -0
  176. package/dist/types/testnets/errors/TestnetsError.d.ts +47 -0
  177. package/dist/types/testnets/errors/index.d.ts +7 -0
  178. package/dist/types/testnets/index.d.ts +11 -0
  179. package/dist/types/testnets/schemas.d.ts +554 -0
  180. package/dist/types/testnets/types.d.ts +717 -0
  181. package/package.json +6 -1
@@ -0,0 +1,256 @@
1
+ //#region src/permissionless/api/oracles/stress/classify.ts
2
+ /**
3
+ * The feed types whose price is a protocol's exchange rate read off a
4
+ * contract, mapped to the group that rate belongs to.
5
+ *
6
+ * These are not leaves: an LP feed also reads the price of whatever the rate
7
+ * is denominated in, and that child is somebody else's source. What belongs to
8
+ * the protocol is the rate alone, which is why these are perturbed at the
9
+ * contract the feed reads it from rather than at the feed.
10
+ *
11
+ * `null` means the protocol is the vault's own and has to come from
12
+ * {@link VAULT_GROUPS} or, failing that, from the feed's description.
13
+ */
14
+ const RATE_TYPE_GROUPS = {
15
+ WSTETH: "Lido",
16
+ CURVE_STABLE: "Curve",
17
+ CURVE_CRYPTO: "Curve",
18
+ CURVE_TWAP: "Curve",
19
+ YEARN: "Yearn",
20
+ MELLOW_LRT: "Mellow",
21
+ BALANCER_STABLE: "Balancer",
22
+ BALANCER_WEIGHTED: "Balancer",
23
+ ERC4626: null
24
+ };
25
+ /**
26
+ * Which protocol an ERC-4626 vault belongs to, by the symbol its feed is
27
+ * described with.
28
+ *
29
+ * Curated, and deliberately short: a vault not named here becomes a group of
30
+ * one under its own symbol, which is still a correct partition — one vault is
31
+ * one source — and is flagged so a reader knows nothing claims it fails
32
+ * alongside anything else.
33
+ */
34
+ const VAULT_GROUPS = {
35
+ sUSDS: "Sky",
36
+ sDAI: "Sky",
37
+ sUSDe: "Ethena",
38
+ sUSDf: "Falcon",
39
+ wstUSR: "Resolv",
40
+ syrupUSDC: "Maple",
41
+ hgETH: "Kelp",
42
+ rstETH: "Mellow",
43
+ Re7LRT: "Mellow",
44
+ pzETH: "Mellow",
45
+ DVstETH: "Mellow",
46
+ tETH: "Treehouse"
47
+ };
48
+ /**
49
+ * Rules over a Chainlink-shaped feed's own description, in order.
50
+ *
51
+ * Only reached for a leaf Gearbox did not deploy, which is where the provider
52
+ * is not already stated. The first match wins, so the specific patterns come
53
+ * before the general shape of a Chainlink pair.
54
+ */
55
+ const DESCRIPTION_RULES = [
56
+ {
57
+ via: "description names Redstone",
58
+ match: /redstone/i,
59
+ group: "Redstone"
60
+ },
61
+ {
62
+ via: "description names Pyth",
63
+ match: /\bpyth\b/i,
64
+ group: "Pyth"
65
+ },
66
+ {
67
+ via: "description names Pendle",
68
+ match: /pendle/i,
69
+ group: "Pendle"
70
+ },
71
+ {
72
+ via: "description names Chronicle",
73
+ match: /chronicle/i,
74
+ group: "Chronicle"
75
+ },
76
+ {
77
+ via: "description names API3",
78
+ match: /\bapi3\b/i,
79
+ group: "API3"
80
+ },
81
+ {
82
+ via: "description names Chaos",
83
+ match: /chaos\s*labs/i,
84
+ group: "Chaos Labs"
85
+ },
86
+ {
87
+ via: "Midas naming convention",
88
+ match: /^m(TBILL|BASIS|EDGE|F-ONE|GLOBAL|Re7YIELD|[A-Z]{2,}[0-9]*)\s*\//,
89
+ group: "Midas"
90
+ },
91
+ {
92
+ via: "exchange-rate adapter, grouped by its token",
93
+ match: /^([A-Za-z0-9+]+)\s*[/-]\s*[A-Za-z0-9+]+\s+exchange\s+rate/i,
94
+ group: (m) => m[1]
95
+ }
96
+ ];
97
+ /**
98
+ * Whether a description is a Chainlink pair: `BASE / QUOTE`, spaced, in the
99
+ * shape their aggregators have published for years.
100
+ *
101
+ * Paired with a version of 6, which is what `EACAggregatorProxy` answers and
102
+ * what the imitators — version 0 or 1, mostly — do not. Either alone would be
103
+ * wrong: plenty of adapters describe themselves as a spaced pair, and plenty
104
+ * of Chainlink feeds are read through a wrapper that reports another version.
105
+ */
106
+ const CHAINLINK_PAIR = /^[A-Za-z0-9+]+ \/ [A-Za-z0-9+]+$/;
107
+ /** Feed types that are their own answer, with no source behind them at all. */
108
+ const SELF_CONTAINED = {
109
+ CONSTANT: "Constant",
110
+ ZERO: "Zero"
111
+ };
112
+ /** The type without the `PRICE_FEED::` the bytecode repository prefixes it with. */
113
+ function bareType(contractType) {
114
+ return contractType.replace(/^PRICE_FEED::/, "");
115
+ }
116
+ /**
117
+ * Which group a leaf price feed belongs to, and how that was decided.
118
+ *
119
+ * The order is what makes it defensible: what Gearbox deployed says who it
120
+ * reads, so those types are taken first; then the description rules, most
121
+ * specific first; then the Chainlink shape; and anything left is its own group
122
+ * under its own description, flagged.
123
+ */
124
+ function classifyLeaf(node) {
125
+ const type = bareType(node.contractType);
126
+ const selfContained = SELF_CONTAINED[type];
127
+ if (selfContained) return {
128
+ group: selfContained,
129
+ identified: true,
130
+ via: "Gearbox feed type"
131
+ };
132
+ if (type === "REDSTONE" || type === "PYTH" || type === "CHAINLINK") return {
133
+ group: type === "PYTH" ? "Pyth" : type === "REDSTONE" ? "Redstone" : "Chainlink",
134
+ identified: true,
135
+ via: "Gearbox feed type"
136
+ };
137
+ for (const rule of DESCRIPTION_RULES) {
138
+ const m = node.description.match(rule.match);
139
+ if (m) return {
140
+ group: typeof rule.group === "function" ? rule.group(m) : rule.group,
141
+ identified: true,
142
+ via: rule.via
143
+ };
144
+ }
145
+ if (CHAINLINK_PAIR.test(node.description) && node.version === 6) return {
146
+ group: "Chainlink",
147
+ identified: true,
148
+ via: "Chainlink pair description at interface version 6"
149
+ };
150
+ return {
151
+ group: node.description || node.address,
152
+ identified: false,
153
+ via: "unrecognised: grouped alone under its own description"
154
+ };
155
+ }
156
+ /**
157
+ * Which group a rate-bearing feed's rate belongs to.
158
+ *
159
+ * The type names the protocol for everything but an ERC-4626 vault, where the
160
+ * protocol is the vault's own and is looked up by the symbol the feed is
161
+ * described with — `"sUSDS / USD LP price feed"` is Sky's.
162
+ */
163
+ function classifyRate(node) {
164
+ const type = bareType(node.contractType);
165
+ const named = RATE_TYPE_GROUPS[type];
166
+ if (named) return {
167
+ group: named,
168
+ identified: true,
169
+ via: "Gearbox feed type"
170
+ };
171
+ const symbol = node.description.match(/^([A-Za-z0-9+.-]+)\s*\//)?.[1];
172
+ const protocol = symbol ? VAULT_GROUPS[symbol] : void 0;
173
+ if (protocol) return {
174
+ group: protocol,
175
+ identified: true,
176
+ via: "curated vault table"
177
+ };
178
+ return {
179
+ group: symbol ?? node.address,
180
+ identified: false,
181
+ via: "vault not in the curated table: grouped alone under its symbol"
182
+ };
183
+ }
184
+ /**
185
+ * Every source behind a chain's feed trees.
186
+ *
187
+ * A source per contract, not per feed: two of an oracle's trees carrying the
188
+ * same aggregator is one source read twice, and the test varies it once.
189
+ *
190
+ * `rateTargets` says which contract each rate-bearing feed reads its rate
191
+ * from — the feed's own `lpContract`, read separately because the compressor
192
+ * does not report it. A feed missing from it is left out with a reason, since
193
+ * perturbing the whole feed instead would charge its protocol with a failure
194
+ * of whatever prices the thing the rate is denominated in.
195
+ **/
196
+ function classifySources(chainId, nodes, rateTargets, baselineOf) {
197
+ const sources = [];
198
+ const unresolved = [];
199
+ const seen = /* @__PURE__ */ new Set();
200
+ for (const node of nodes) {
201
+ const isRate = bareType(node.contractType) in RATE_TYPE_GROUPS;
202
+ if (!isRate && node.underlyingFeeds.length > 0) continue;
203
+ const target = isRate ? rateTargets.get(node.address) : node.address;
204
+ if (!target) {
205
+ unresolved.push(node);
206
+ continue;
207
+ }
208
+ if (seen.has(target)) continue;
209
+ seen.add(target);
210
+ sources.push({
211
+ chainId,
212
+ kind: isRate ? "rate" : "feed",
213
+ target,
214
+ feed: node.address,
215
+ contractType: node.contractType,
216
+ description: node.description,
217
+ perturbable: !(bareType(node.contractType) in SELF_CONTAINED),
218
+ baseline: baselineOf(target),
219
+ ...isRate ? classifyRate(node) : classifyLeaf(node)
220
+ });
221
+ }
222
+ return {
223
+ sources,
224
+ unresolved
225
+ };
226
+ }
227
+ /**
228
+ * The sources each of the given feeds is priced through, following the trees
229
+ * down from every one of them.
230
+ *
231
+ * A feed is reached by a source when the source sits anywhere beneath it, at
232
+ * any depth: a market reading a composite of a composite of a Chainlink
233
+ * aggregator is a market Chainlink can move. Memoised over the whole forest,
234
+ * so a node shared by twenty trees is walked once.
235
+ **/
236
+ function sourcesByFeed(roots, nodes, sources) {
237
+ const sourceOfFeed = /* @__PURE__ */ new Map();
238
+ for (const source of sources) sourceOfFeed.set(source.feed, source.target);
239
+ const memo = /* @__PURE__ */ new Map();
240
+ const walk = (feed, path) => {
241
+ const done = memo.get(feed);
242
+ if (done) return done;
243
+ if (path.has(feed)) return /* @__PURE__ */ new Set();
244
+ const found = /* @__PURE__ */ new Set();
245
+ const own = sourceOfFeed.get(feed);
246
+ if (own) found.add(own);
247
+ path.add(feed);
248
+ for (const child of nodes.get(feed)?.underlyingFeeds ?? []) for (const source of walk(child, path)) found.add(source);
249
+ path.delete(feed);
250
+ memo.set(feed, found);
251
+ return found;
252
+ };
253
+ return new Map(roots.map((root) => [root, walk(root, /* @__PURE__ */ new Set())]));
254
+ }
255
+ //#endregion
256
+ export { classifySources, sourcesByFeed };
@@ -0,0 +1,6 @@
1
+ import { classifySources, sourcesByFeed } from "./classify.js";
2
+ import { UNBOUNDED_DOWN, UNBOUNDED_UP, groupOverride, perturb, rateStub, roundDataStub } from "./override.js";
3
+ import { scoreGroup, slotHarm, verdictOf } from "./score.js";
4
+ import { DEFAULT_SCENARIOS, RATE_FEED_TYPE, readFeedTrees, readForest, readRateTargets, readUnder, runStressTest } from "./run.js";
5
+ import "./types.js";
6
+ export { DEFAULT_SCENARIOS, RATE_FEED_TYPE, UNBOUNDED_DOWN, UNBOUNDED_UP, classifySources, groupOverride, perturb, rateStub, readFeedTrees, readForest, readRateTargets, readUnder, roundDataStub, runStressTest, scoreGroup, slotHarm, sourcesByFeed, verdictOf };
@@ -0,0 +1,119 @@
1
+ //#region src/permissionless/api/oracles/stress/override.ts
2
+ /**
3
+ * Making a contract lie for the length of one `eth_call`.
4
+ *
5
+ * No fork and no simulation harness: an `eth_call` carries a state override,
6
+ * so the aggregator's code is replaced for that call alone and everything
7
+ * above it — the composite feeds, their bounds, the oracle's own checks — runs
8
+ * as deployed, on real state, at the current block. What comes back is what
9
+ * the market would have read.
10
+ **/
11
+ /** A word, as the 32 bytes `PUSH32` wants, two's complement for negatives. */
12
+ function word(value) {
13
+ return BigInt.asUintN(256, value).toString(16).padStart(64, "0");
14
+ }
15
+ /**
16
+ * Runtime code answering every call with the same five words.
17
+ *
18
+ * Deliberately selector-blind. A price feed is read through more than
19
+ * `latestRoundData` — `decimals`, `version`, sometimes `latestAnswer` — and
20
+ * dispatching on each would mean knowing which of them every caller in a tree
21
+ * reaches for. Five words decode as the round data; a caller expecting one
22
+ * word reads the first, which is why `roundId` is set to the 8 decimals every
23
+ * Gearbox feed reports rather than to a round number.
24
+ *
25
+ * `answeredInRound` is the same 8, so the `answeredInRound >= roundId` check
26
+ * the older consumers make passes.
27
+ **/
28
+ function roundDataStub(answer, updatedAt) {
29
+ return `0x${[
30
+ `7f${word(8n)}600052`,
31
+ `7f${word(answer)}602052`,
32
+ `7f${word(updatedAt)}604052`,
33
+ `7f${word(updatedAt)}606052`,
34
+ `7f${word(8n)}608052`,
35
+ "60a06000f3"
36
+ ].join("")}`;
37
+ }
38
+ /**
39
+ * Runtime code answering every call with one word.
40
+ *
41
+ * What an exchange rate getter returns, whichever of them the feed calls:
42
+ * `stEthPerToken`, `convertToAssets`, `get_virtual_price`, `pricePerShare`
43
+ * and `getRate` all answer a single `uint256`, so the stub does not need to
44
+ * know which.
45
+ **/
46
+ function rateStub(rate) {
47
+ return `0x7f${word(rate)}60005260206000f3`;
48
+ }
49
+ /**
50
+ * The two unbounded ends, as far as the types involved can carry them.
51
+ *
52
+ * "Infinitely large" has to stop somewhere: a price is an `int256` and the
53
+ * arithmetic above it multiplies, so a genuinely enormous answer overflows
54
+ * inside the composite feed and reverts — which would be scored as the
55
+ * oracle resisting, when all that happened is that the test asked something
56
+ * the type cannot hold. `1e12` times the current answer is past any bound,
57
+ * any sanity check and any collateral ratio, and still multiplies.
58
+ */
59
+ const UNBOUNDED_UP = 0xe8d4a51000;
60
+ /** The other end: a thousandth of a trillionth, which is as near zero. */
61
+ const UNBOUNDED_DOWN = 1e-12;
62
+ /**
63
+ * A double as an exact integer ratio.
64
+ *
65
+ * Multipliers here span twenty-four orders of magnitude, so the obvious
66
+ * `BigInt(Math.round(m * 1e9))` is not available: at the top of the range the
67
+ * product is past what a double carries exactly and the multiplier quietly
68
+ * stops being the one that was asked for. Going through the decimal expansion
69
+ * keeps all fifteen digits a double has and puts the magnitude in the
70
+ * exponent, where it costs nothing.
71
+ */
72
+ function asRatio(multiplier) {
73
+ const [mantissa = "0", exponent = "0"] = multiplier.toExponential(14).split("e");
74
+ const digits = BigInt(mantissa.replace(".", ""));
75
+ const power = Number(exponent) - 14;
76
+ return power >= 0 ? {
77
+ num: digits * 10n ** BigInt(power),
78
+ den: 1n
79
+ } : {
80
+ num: digits,
81
+ den: 10n ** BigInt(-power)
82
+ };
83
+ }
84
+ /**
85
+ * What a source should answer under a multiplier, given what it answers now.
86
+ *
87
+ * Floored at one rather than at zero: a multiplier small enough to round the
88
+ * baseline away would be testing a feed reporting nothing, which is the revert
89
+ * case and is a different scenario with a different meaning.
90
+ */
91
+ function perturb(baseline, multiplier) {
92
+ const { num, den } = asRatio(Number.isFinite(multiplier) ? Math.min(Math.max(multiplier, UNBOUNDED_DOWN), UNBOUNDED_UP) : multiplier > 0 ? UNBOUNDED_UP : UNBOUNDED_DOWN);
93
+ const out = baseline * num / den;
94
+ return out === 0n ? 1n : out;
95
+ }
96
+ /**
97
+ * The state override that makes every source of a group answer a multiple of
98
+ * what it answers now.
99
+ *
100
+ * One override per contract, so a group whose sources share an aggregator
101
+ * perturbs it once. Sources with nothing behind them and sources that are not
102
+ * answering are both left out, for the reasons on
103
+ * {@link StressSource.perturbable} and {@link StressSource.baseline} — an
104
+ * empty result is a group there is nothing to simulate about.
105
+ **/
106
+ function groupOverride(sources, multiplier, updatedAt) {
107
+ const overrides = [];
108
+ for (const source of sources) {
109
+ if (!source.perturbable || source.baseline === null) continue;
110
+ const value = perturb(source.baseline, multiplier);
111
+ overrides.push({
112
+ address: source.target,
113
+ code: source.kind === "rate" ? rateStub(value) : roundDataStub(value, updatedAt)
114
+ });
115
+ }
116
+ return overrides;
117
+ }
118
+ //#endregion
119
+ export { UNBOUNDED_DOWN, UNBOUNDED_UP, groupOverride, perturb, rateStub, roundDataStub };