@gearbox-protocol/sdk 18.2.0-next.1 → 18.2.0-next.11

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 (135) hide show
  1. package/dist/cjs/abi/compressors/subcompressors/liquidation/treehouseLiquidationSubcompressor.js +179 -0
  2. package/dist/cjs/abi/compressors/subcompressors/withdrawal/treehouseWithdrawalSubcompressor.js +534 -0
  3. package/dist/cjs/abi/router/treehouseWorker.js +885 -0
  4. package/dist/cjs/dev/AccountOpener.js +1 -1
  5. package/dist/cjs/dev/withdrawalUtils.js +1 -1
  6. package/dist/cjs/history/trace/extractTransfers.js +1 -1
  7. package/dist/cjs/onchain/accounts/CreditAccountsServiceV310.js +2 -2
  8. package/dist/cjs/onchain/accounts/liquidations/LiquidationsService.js +1 -1
  9. package/dist/cjs/onchain/accounts/withdrawal-compressor/RedemptionLoggerV310Contract.js +1 -2
  10. package/dist/cjs/onchain/accounts/withdrawal-compressor/WithdrawalCompressorV310Contract.js +1 -2
  11. package/dist/cjs/onchain/accounts/withdrawal-compressor/WithdrawalCompressorV311Contract.js +1 -2
  12. package/dist/cjs/onchain/accounts/withdrawal-compressor/WithdrawalCompressorV313Contract.js +1 -1
  13. package/dist/cjs/onchain/base/TokensMeta.js +3 -3
  14. package/dist/cjs/onchain/core/createAddressProvider.js +1 -1
  15. package/dist/cjs/onchain/market/adapters/abi/actionAbi.js +1 -0
  16. package/dist/cjs/onchain/market/adapters/abi/conctructorAbi.js +3 -0
  17. package/dist/cjs/onchain/market/adapters/contracts/AccountMigratorAdapterContract.js +1 -1
  18. package/dist/cjs/onchain/market/adapters/contracts/ERC4626AdapterContract.js +1 -1
  19. package/dist/cjs/onchain/market/adapters/types.js +3 -0
  20. package/dist/cjs/onchain/market/credit/CreditFacadeV310BaseContract.js +1 -1
  21. package/dist/cjs/onchain/market/pool/PoolV310Contract.js +1 -1
  22. package/dist/cjs/onchain/market/rwa/createDegenNFT.js +1 -1
  23. package/dist/cjs/onchain/market/zapper/IETHZapperContract.js +1 -2
  24. package/dist/cjs/onchain/market/zapper/ZapperContract.js +1 -1
  25. package/dist/cjs/onchain/preview/preview/previewPoolPositionOperation.js +2 -2
  26. package/dist/cjs/onchain/utils/viem/simulateWithPriceUpdates.js +1 -1
  27. package/dist/cjs/permissionless/api/index.js +26 -0
  28. package/dist/cjs/permissionless/api/oracles/PermissionlessOracles.js +585 -47
  29. package/dist/cjs/permissionless/api/oracles/enrich.js +99 -0
  30. package/dist/cjs/permissionless/api/oracles/index.js +28 -0
  31. package/dist/cjs/permissionless/api/oracles/schemas.js +102 -35
  32. package/dist/cjs/permissionless/api/oracles/score/formula.js +99 -0
  33. package/dist/cjs/permissionless/api/oracles/score/index.js +8 -0
  34. package/dist/cjs/permissionless/api/oracles/score/run.js +88 -0
  35. package/dist/cjs/permissionless/api/oracles/score/types.js +1 -0
  36. package/dist/cjs/permissionless/api/oracles/stress/classify.js +258 -0
  37. package/dist/cjs/permissionless/api/oracles/stress/index.js +22 -0
  38. package/dist/cjs/permissionless/api/oracles/stress/override.js +125 -0
  39. package/dist/cjs/permissionless/api/oracles/stress/run.js +432 -0
  40. package/dist/cjs/permissionless/api/oracles/stress/score.js +98 -0
  41. package/dist/cjs/permissionless/api/oracles/stress/types.js +1 -0
  42. package/dist/cjs/permissionless/api/schemas.js +0 -3
  43. package/dist/cjs/permissionless/bindings/index.js +1 -1
  44. package/dist/cjs/permissionless/index.js +29 -1
  45. package/dist/cjs/permissionless/utils/index.js +2 -0
  46. package/dist/cjs/permissionless/utils/price-update/get-prices.js +64 -0
  47. package/dist/cjs/permissionless/utils/price-update/index.js +2 -0
  48. package/dist/cjs/testnets/TestnetsAPI.js +179 -0
  49. package/dist/cjs/testnets/errors/TestnetNotFoundError.js +15 -0
  50. package/dist/cjs/testnets/errors/TestnetRequestFailedError.js +24 -0
  51. package/dist/cjs/testnets/errors/TestnetStatusError.js +19 -0
  52. package/dist/cjs/testnets/errors/TestnetValidationError.js +24 -0
  53. package/dist/cjs/testnets/errors/TestnetWaitTimeoutError.js +30 -0
  54. package/dist/cjs/testnets/errors/TestnetsError.js +34 -0
  55. package/dist/cjs/testnets/errors/index.js +13 -0
  56. package/dist/cjs/testnets/index.js +32 -0
  57. package/dist/cjs/testnets/package.json +1 -0
  58. package/dist/cjs/testnets/schemas.js +242 -0
  59. package/dist/cjs/testnets/types.js +1 -0
  60. package/dist/esm/abi/compressors/subcompressors/liquidation/treehouseLiquidationSubcompressor.js +178 -0
  61. package/dist/esm/abi/compressors/subcompressors/withdrawal/treehouseWithdrawalSubcompressor.js +533 -0
  62. package/dist/esm/abi/router/treehouseWorker.js +884 -0
  63. package/dist/esm/onchain/market/adapters/abi/actionAbi.js +1 -0
  64. package/dist/esm/onchain/market/adapters/abi/conctructorAbi.js +3 -0
  65. package/dist/esm/onchain/market/adapters/types.js +3 -0
  66. package/dist/esm/permissionless/api/index.js +7 -1
  67. package/dist/esm/permissionless/api/oracles/PermissionlessOracles.js +587 -49
  68. package/dist/esm/permissionless/api/oracles/enrich.js +94 -0
  69. package/dist/esm/permissionless/api/oracles/index.js +10 -1
  70. package/dist/esm/permissionless/api/oracles/schemas.js +95 -35
  71. package/dist/esm/permissionless/api/oracles/score/formula.js +96 -0
  72. package/dist/esm/permissionless/api/oracles/score/index.js +4 -0
  73. package/dist/esm/permissionless/api/oracles/score/run.js +87 -0
  74. package/dist/esm/permissionless/api/oracles/score/types.js +1 -0
  75. package/dist/esm/permissionless/api/oracles/stress/classify.js +256 -0
  76. package/dist/esm/permissionless/api/oracles/stress/index.js +6 -0
  77. package/dist/esm/permissionless/api/oracles/stress/override.js +119 -0
  78. package/dist/esm/permissionless/api/oracles/stress/run.js +427 -0
  79. package/dist/esm/permissionless/api/oracles/stress/score.js +95 -0
  80. package/dist/esm/permissionless/api/oracles/stress/types.js +1 -0
  81. package/dist/esm/permissionless/api/schemas.js +2 -4
  82. package/dist/esm/permissionless/bindings/index.js +1 -1
  83. package/dist/esm/permissionless/index.js +9 -3
  84. package/dist/esm/permissionless/utils/index.js +2 -2
  85. package/dist/esm/permissionless/utils/price-update/get-prices.js +63 -1
  86. package/dist/esm/permissionless/utils/price-update/index.js +2 -2
  87. package/dist/esm/testnets/TestnetsAPI.js +178 -0
  88. package/dist/esm/testnets/errors/TestnetNotFoundError.js +14 -0
  89. package/dist/esm/testnets/errors/TestnetRequestFailedError.js +23 -0
  90. package/dist/esm/testnets/errors/TestnetStatusError.js +18 -0
  91. package/dist/esm/testnets/errors/TestnetValidationError.js +23 -0
  92. package/dist/esm/testnets/errors/TestnetWaitTimeoutError.js +29 -0
  93. package/dist/esm/testnets/errors/TestnetsError.js +33 -0
  94. package/dist/esm/testnets/errors/index.js +7 -0
  95. package/dist/esm/testnets/index.js +11 -0
  96. package/dist/esm/testnets/package.json +1 -0
  97. package/dist/esm/testnets/schemas.js +228 -0
  98. package/dist/esm/testnets/types.js +1 -0
  99. package/dist/types/abi/compressors/subcompressors/liquidation/treehouseLiquidationSubcompressor.d.ts +148 -0
  100. package/dist/types/abi/compressors/subcompressors/withdrawal/treehouseWithdrawalSubcompressor.d.ts +440 -0
  101. package/dist/types/abi/router/treehouseWorker.d.ts +728 -0
  102. package/dist/types/onchain/market/adapters/types.d.ts +4 -1
  103. package/dist/types/permissionless/api/index.d.ts +10 -2
  104. package/dist/types/permissionless/api/oracles/PermissionlessOracles.d.ts +232 -35
  105. package/dist/types/permissionless/api/oracles/enrich.d.ts +59 -0
  106. package/dist/types/permissionless/api/oracles/index.d.ts +12 -2
  107. package/dist/types/permissionless/api/oracles/schemas.d.ts +209 -22
  108. package/dist/types/permissionless/api/oracles/score/formula.d.ts +46 -0
  109. package/dist/types/permissionless/api/oracles/score/index.d.ts +4 -0
  110. package/dist/types/permissionless/api/oracles/score/run.d.ts +21 -0
  111. package/dist/types/permissionless/api/oracles/score/types.d.ts +85 -0
  112. package/dist/types/permissionless/api/oracles/stress/classify.d.ts +57 -0
  113. package/dist/types/permissionless/api/oracles/stress/index.d.ts +6 -0
  114. package/dist/types/permissionless/api/oracles/stress/override.d.ts +59 -0
  115. package/dist/types/permissionless/api/oracles/stress/run.d.ts +73 -0
  116. package/dist/types/permissionless/api/oracles/stress/score.d.ts +57 -0
  117. package/dist/types/permissionless/api/oracles/stress/types.d.ts +272 -0
  118. package/dist/types/permissionless/api/oracles/types.d.ts +408 -63
  119. package/dist/types/permissionless/api/schemas.d.ts +0 -2
  120. package/dist/types/permissionless/index.d.ts +11 -3
  121. package/dist/types/permissionless/utils/index.d.ts +2 -2
  122. package/dist/types/permissionless/utils/price-update/get-prices.d.ts +33 -0
  123. package/dist/types/permissionless/utils/price-update/index.d.ts +2 -2
  124. package/dist/types/testnets/TestnetsAPI.d.ts +108 -0
  125. package/dist/types/testnets/errors/TestnetNotFoundError.d.ts +12 -0
  126. package/dist/types/testnets/errors/TestnetRequestFailedError.d.ts +11 -0
  127. package/dist/types/testnets/errors/TestnetStatusError.d.ts +15 -0
  128. package/dist/types/testnets/errors/TestnetValidationError.d.ts +16 -0
  129. package/dist/types/testnets/errors/TestnetWaitTimeoutError.d.ts +21 -0
  130. package/dist/types/testnets/errors/TestnetsError.d.ts +47 -0
  131. package/dist/types/testnets/errors/index.d.ts +7 -0
  132. package/dist/types/testnets/index.d.ts +11 -0
  133. package/dist/types/testnets/schemas.d.ts +554 -0
  134. package/dist/types/testnets/types.d.ts +717 -0
  135. package/package.json +9 -3
@@ -31,6 +31,16 @@ const latestRoundDataAbi = [{
31
31
  stateMutability: "view",
32
32
  type: "function"
33
33
  }];
34
+ const decimalsAbi = [{
35
+ inputs: [],
36
+ name: "decimals",
37
+ outputs: [{
38
+ name: "",
39
+ type: "uint8"
40
+ }],
41
+ stateMutability: "view",
42
+ type: "function"
43
+ }];
34
44
  function getLatestRoundDataCall(priceFeed) {
35
45
  return {
36
46
  address: priceFeed,
@@ -39,6 +49,58 @@ function getLatestRoundDataCall(priceFeed) {
39
49
  args: []
40
50
  };
41
51
  }
52
+ /**
53
+ * The scale each feed's answer is in, in one multicall.
54
+ *
55
+ * Part of the Chainlink interface every feed in a Gearbox tree implements,
56
+ * and the only way to read an answer that is not an oracle's own: a feed an
57
+ * oracle holds reports 8 decimals by construction, while the feeds beneath a
58
+ * composite are whatever was deployed — an ETH/USD aggregator at 8 and a
59
+ * rate leg at 18 sit under the same parent.
60
+ *
61
+ * `null` is a feed that would not answer, which a caller should treat as
62
+ * unknown rather than defaulting it to 8.
63
+ */
64
+ async function getFeedDecimals({ client, priceFeeds }) {
65
+ if (priceFeeds.length === 0) return {};
66
+ const results = await client.multicall({
67
+ contracts: priceFeeds.map((priceFeed) => ({
68
+ address: priceFeed,
69
+ abi: decimalsAbi,
70
+ functionName: "decimals",
71
+ args: []
72
+ })),
73
+ allowFailure: true
74
+ });
75
+ return Object.fromEntries(priceFeeds.map((priceFeed, index) => {
76
+ const result = results[index];
77
+ return [priceFeed, result?.status === "success" ? Number(result.result) : null];
78
+ }));
79
+ }
80
+ /**
81
+ * What the feeds report as they stand, in one multicall.
82
+ *
83
+ * What {@link getPrices} does without the part that makes it expensive: no
84
+ * `OnchainSDK` to attach and no update transactions to assemble, which is the
85
+ * whole cost of that call and is wasted on a feed that answers already. A feed
86
+ * that needs a fresh answer before it will be read comes back `null` here, so
87
+ * a caller that must price those falls back to {@link getPrices} for them and
88
+ * pays once for the few rather than once for all.
89
+ *
90
+ * `null` is also a feed whose read reverted for any other reason, which is not
91
+ * a zero.
92
+ */
93
+ async function getLatestPrices({ client, priceFeeds }) {
94
+ if (priceFeeds.length === 0) return {};
95
+ const results = await client.multicall({
96
+ contracts: priceFeeds.map(getLatestRoundDataCall),
97
+ allowFailure: true
98
+ });
99
+ return Object.fromEntries(priceFeeds.map((priceFeed, index) => {
100
+ const result = results[index];
101
+ return [priceFeed, result?.status === "success" ? result.result[1] : null];
102
+ }));
103
+ }
42
104
  async function getPricesChunk({ client, priceFeeds, sdk }) {
43
105
  const updateCall = priceFeeds.map((priceFeed) => getLatestRoundDataCall(priceFeed));
44
106
  const multicallCalls = (await sdk.priceFeeds.generateExternalPriceFeedsUpdateTxs(priceFeeds)).txs.map((tx) => ({
@@ -82,4 +144,4 @@ async function getPrices({ client, priceFeeds, chunkSize = 10, gasLimit }) {
82
144
  }), {});
83
145
  }
84
146
  //#endregion
85
- export { getPrices };
147
+ export { getFeedDecimals, getLatestPrices, getPrices };
@@ -1,4 +1,4 @@
1
- import { getPrices } from "./get-prices.js";
1
+ import { getFeedDecimals, getLatestPrices, getPrices } from "./get-prices.js";
2
2
  import { getUpdatablePriceFeeds } from "./get-updatable-feeds.js";
3
3
  import { getPriceUpdateTx } from "./get-price-update-tx.js";
4
- export { getPriceUpdateTx, getPrices, getUpdatablePriceFeeds };
4
+ export { getFeedDecimals, getLatestPrices, getPriceUpdateTx, getPrices, getUpdatablePriceFeeds };
@@ -0,0 +1,178 @@
1
+ import { TestnetStatusError } from "./errors/TestnetStatusError.js";
2
+ import { TestnetNotFoundError } from "./errors/TestnetNotFoundError.js";
3
+ import { TestnetRequestFailedError } from "./errors/TestnetRequestFailedError.js";
4
+ import { TestnetValidationError } from "./errors/TestnetValidationError.js";
5
+ import { TestnetWaitTimeoutError } from "./errors/TestnetWaitTimeoutError.js";
6
+ import "./errors/index.js";
7
+ import { testnetSchema, testnetStatusSchema } from "./schemas.js";
8
+ import { z } from "zod/v4";
9
+ //#region src/testnets/TestnetsAPI.ts
10
+ const DEFAULT_BASE_URL = "https://anvil.gearbox.foundation";
11
+ const DEFAULT_TIMEOUT = 3e4;
12
+ const DEFAULT_WAIT_TIMEOUT = 6e5;
13
+ const DEFAULT_WAIT_INTERVAL = 5e3;
14
+ const errorBodySchema = z.object({ error: z.string() });
15
+ /**
16
+ * Client for anvil-manager, which runs the anvil forks Gearbox calls
17
+ * testnets. It manages non-persistent testnets only, addressed by their
18
+ * execution id.
19
+ *
20
+ * ```ts
21
+ * const testnets = new TestnetsAPI({ apiKey: process.env.ANVIL_MANAGER_API_KEY });
22
+ * const { executionId } = await testnets.create({ network: "Mainnet", timeout: "2h" });
23
+ * const status = await testnets.waitUntilReady(executionId);
24
+ * // ... use status.rpcUrl ...
25
+ * await testnets.kill(executionId);
26
+ * ```
27
+ *
28
+ * Every method throws a {@link TestnetsError} subclass when it fails, except
29
+ * when a response body is not JSON: then it rejects with the error of
30
+ * `response.json()`.
31
+ **/
32
+ var TestnetsAPI = class {
33
+ logger;
34
+ #baseUrl;
35
+ #apiKey;
36
+ #timeout;
37
+ constructor(options = {}) {
38
+ this.#baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
39
+ this.#apiKey = options.apiKey;
40
+ this.#timeout = options.timeout ?? DEFAULT_TIMEOUT;
41
+ this.logger = options.logger?.child?.({ name: "TestnetsAPI" }) ?? options.logger;
42
+ }
43
+ /**
44
+ * Base URL every request of this client is sent to.
45
+ **/
46
+ get baseUrl() {
47
+ return this.#baseUrl;
48
+ }
49
+ /**
50
+ * Every testnet anvil-manager knows of, persistent ones included.
51
+ **/
52
+ async list() {
53
+ const url = this.#url("/api/v1/forks");
54
+ const response = await this.#fetch(url, { method: "GET" });
55
+ return this.#decode(url, response, z.array(testnetSchema));
56
+ }
57
+ /**
58
+ * Status of one testnet.
59
+ *
60
+ * @param id - Execution id of the testnet.
61
+ * @throws {@link TestnetNotFoundError} when there is no such testnet.
62
+ **/
63
+ async get(id) {
64
+ const url = this.#forkUrl(id);
65
+ const response = await this.#fetch(url, { method: "GET" });
66
+ return this.#decode(url, response, testnetStatusSchema);
67
+ }
68
+ /**
69
+ * Starts a testnet. It answers as soon as the testnet is created, long
70
+ * before it is {@link TestnetStatus.ready}.
71
+ *
72
+ * @param files - Saved in the testnet's shared dir, where scripts and tasks
73
+ * can read them.
74
+ **/
75
+ async create(request, files) {
76
+ const url = this.#url("/api/v1/forks");
77
+ const response = await this.#fetch(url, {
78
+ method: "POST",
79
+ ...createBody(request, files)
80
+ });
81
+ return this.#decode(url, response, testnetStatusSchema);
82
+ }
83
+ /**
84
+ * Stops a testnet. Stopping one that is already stopped succeeds.
85
+ *
86
+ * @param id - Execution id of the testnet.
87
+ * @throws {@link TestnetNotFoundError} when there is no such testnet.
88
+ * @throws {@link TestnetStatusError} with status 400 when the testnet is
89
+ * persistent.
90
+ **/
91
+ async kill(id) {
92
+ await this.#fetch(this.#forkUrl(id), { method: "DELETE" });
93
+ }
94
+ /**
95
+ * Polls a testnet's status until it is {@link TestnetStatus.ready}.
96
+ *
97
+ * @param id - Execution id of the testnet.
98
+ * @returns The first status that is ready.
99
+ * @throws {@link TestnetWaitTimeoutError} when the testnet is not ready in
100
+ * time. Any error of a poll is thrown as is, without retrying.
101
+ **/
102
+ async waitUntilReady(id, options = {}) {
103
+ const { timeout = DEFAULT_WAIT_TIMEOUT, interval = DEFAULT_WAIT_INTERVAL } = options;
104
+ const deadline = Date.now() + timeout;
105
+ while (true) {
106
+ const status = await this.get(id);
107
+ if (status.ready) return status;
108
+ const remaining = deadline - Date.now();
109
+ if (remaining <= 0) throw new TestnetWaitTimeoutError(this.#forkUrl(id), id, timeout);
110
+ this.logger?.debug(`testnet ${id} is not ready yet`);
111
+ await new Promise((resolve) => setTimeout(resolve, Math.min(interval, remaining)));
112
+ }
113
+ }
114
+ #url(path) {
115
+ return `${this.#baseUrl}${path}`;
116
+ }
117
+ #forkUrl(id) {
118
+ return this.#url(`/api/v1/forks/${encodeURIComponent(id)}`);
119
+ }
120
+ /**
121
+ * Sends one request and returns its response once it is known to be 2xx.
122
+ **/
123
+ async #fetch(url, init) {
124
+ const headers = new Headers(init.headers);
125
+ headers.set("accept", "application/json");
126
+ if (this.#apiKey) headers.set("authorization", `Bearer ${this.#apiKey}`);
127
+ let response;
128
+ try {
129
+ response = await fetch(url, {
130
+ ...init,
131
+ headers,
132
+ signal: AbortSignal.timeout(this.#timeout)
133
+ });
134
+ } catch (error) {
135
+ throw new TestnetRequestFailedError(url, error);
136
+ }
137
+ if (!response.ok) {
138
+ const responseBody = await response.text();
139
+ const details = errorBodySchema.safeParse(responseBody).data?.error;
140
+ throw response.status === 404 ? new TestnetNotFoundError(url, details) : new TestnetStatusError(url, response.status, details);
141
+ }
142
+ return response;
143
+ }
144
+ /**
145
+ * Body of a 2xx response, decoded.
146
+ **/
147
+ async #decode(url, response, schema) {
148
+ const payload = await response.json();
149
+ const parsed = schema.safeParse(payload);
150
+ if (!parsed.success) {
151
+ const error = new TestnetValidationError(url, parsed.error);
152
+ this.logger?.error(error, "anvil-manager response does not match the testnet schemas");
153
+ throw error;
154
+ }
155
+ return parsed.data;
156
+ }
157
+ };
158
+ /**
159
+ * Body and content type of a create request: JSON alone, or a multipart form
160
+ * with the JSON in its `json` field when there are files to upload.
161
+ **/
162
+ function createBody(request, files) {
163
+ const json = JSON.stringify(request);
164
+ if (!files || Object.keys(files).length === 0) return {
165
+ body: json,
166
+ headers: { "content-type": "application/json" }
167
+ };
168
+ const form = new FormData();
169
+ form.append("json", json);
170
+ for (const [name, contents] of Object.entries(files)) form.append("file", toBlob(contents), name);
171
+ return { body: form };
172
+ }
173
+ function toBlob(contents) {
174
+ if (contents instanceof Blob) return contents;
175
+ return new Blob([typeof contents === "string" ? contents : new Uint8Array(contents)]);
176
+ }
177
+ //#endregion
178
+ export { TestnetsAPI };
@@ -0,0 +1,14 @@
1
+ import { TestnetStatusError } from "./TestnetStatusError.js";
2
+ //#region src/testnets/errors/TestnetNotFoundError.ts
3
+ /**
4
+ * Thrown when anvil-manager answered 404: there is no testnet with that
5
+ * execution id.
6
+ **/
7
+ var TestnetNotFoundError = class extends TestnetStatusError {
8
+ name = "TestnetNotFoundError";
9
+ constructor(url, details) {
10
+ super(url, 404, details);
11
+ }
12
+ };
13
+ //#endregion
14
+ export { TestnetNotFoundError };
@@ -0,0 +1,23 @@
1
+ import { TestnetsError } from "./TestnetsError.js";
2
+ //#region src/testnets/errors/TestnetRequestFailedError.ts
3
+ /**
4
+ * Thrown when a request to anvil-manager never completed: the network was
5
+ * down, or it took longer than the client's timeout.
6
+ **/
7
+ var TestnetRequestFailedError = class extends TestnetsError {
8
+ name = "TestnetRequestFailedError";
9
+ constructor(url, cause) {
10
+ super(timedOut(cause) ? "The request to anvil-manager timed out." : "The request to anvil-manager failed.", {
11
+ url,
12
+ cause
13
+ });
14
+ }
15
+ };
16
+ /**
17
+ * Whether a caught value is the rejection `AbortSignal.timeout` produces.
18
+ **/
19
+ function timedOut(error) {
20
+ return error instanceof Error && error.name === "TimeoutError";
21
+ }
22
+ //#endregion
23
+ export { TestnetRequestFailedError };
@@ -0,0 +1,18 @@
1
+ import { TestnetsError } from "./TestnetsError.js";
2
+ //#region src/testnets/errors/TestnetStatusError.ts
3
+ /**
4
+ * Thrown when anvil-manager answered with a status outside the 2xx range. What
5
+ * it said about it, when it said anything, is the `details` of the error.
6
+ **/
7
+ var TestnetStatusError = class extends TestnetsError {
8
+ name = "TestnetStatusError";
9
+ constructor(url, status, details) {
10
+ super(`anvil-manager answered ${status}.`, {
11
+ url,
12
+ status,
13
+ details
14
+ });
15
+ }
16
+ };
17
+ //#endregion
18
+ export { TestnetStatusError };
@@ -0,0 +1,23 @@
1
+ import { TestnetsError } from "./TestnetsError.js";
2
+ import { z } from "zod/v4";
3
+ //#region src/testnets/errors/TestnetValidationError.ts
4
+ /**
5
+ * Thrown when anvil-manager answered, but with a payload the testnet schemas
6
+ * do not describe, i.e. the two are of different versions.
7
+ **/
8
+ var TestnetValidationError = class extends TestnetsError {
9
+ name = "TestnetValidationError";
10
+ /**
11
+ * The zod failure itself, e.g. for `z.treeifyError(error.zodError)`.
12
+ **/
13
+ zodError;
14
+ constructor(url, zodError) {
15
+ super("The anvil-manager response does not match the testnet schemas.", {
16
+ url,
17
+ metaMessages: z.prettifyError(zodError).split("\n")
18
+ });
19
+ this.zodError = zodError;
20
+ }
21
+ };
22
+ //#endregion
23
+ export { TestnetValidationError };
@@ -0,0 +1,29 @@
1
+ import { TestnetsError } from "./TestnetsError.js";
2
+ //#region src/testnets/errors/TestnetWaitTimeoutError.ts
3
+ /**
4
+ * Thrown when a testnet did not become ready before the wait for it ran out.
5
+ **/
6
+ var TestnetWaitTimeoutError = class extends TestnetsError {
7
+ name = "TestnetWaitTimeoutError";
8
+ /**
9
+ * Execution id of the testnet that was waited for.
10
+ **/
11
+ id;
12
+ /**
13
+ * How long the wait lasted, in milliseconds.
14
+ **/
15
+ timeout;
16
+ /**
17
+ * @param url - Status URL that was polled.
18
+ **/
19
+ constructor(url, id, timeout) {
20
+ super(`Testnet ${id} did not become ready within ${timeout} ms.`, {
21
+ url,
22
+ metaMessages: [`Execution id: ${id}`, `Timeout: ${timeout} ms`]
23
+ });
24
+ this.id = id;
25
+ this.timeout = timeout;
26
+ }
27
+ };
28
+ //#endregion
29
+ export { TestnetWaitTimeoutError };
@@ -0,0 +1,33 @@
1
+ import { BaseError } from "viem";
2
+ //#region src/testnets/errors/TestnetsError.ts
3
+ /**
4
+ * Thrown by every testnet API call that fails. Its subclasses say how it
5
+ * failed; catch this one to catch all of them.
6
+ **/
7
+ var TestnetsError = class extends BaseError {
8
+ /**
9
+ * URL that was requested.
10
+ **/
11
+ url;
12
+ /**
13
+ * Status anvil-manager answered with, absent when the request never
14
+ * completed.
15
+ **/
16
+ status;
17
+ constructor(summary, params) {
18
+ const { cause, details } = params;
19
+ super(summary, {
20
+ cause: cause instanceof Error ? cause : void 0,
21
+ details: cause === void 0 || cause instanceof Error ? details : String(cause),
22
+ metaMessages: [
23
+ `URL: ${params.url}`,
24
+ ...params.status === void 0 ? [] : [`Status: ${params.status}`],
25
+ ...params.metaMessages ?? []
26
+ ]
27
+ });
28
+ this.url = params.url;
29
+ this.status = params.status;
30
+ }
31
+ };
32
+ //#endregion
33
+ export { TestnetsError };
@@ -0,0 +1,7 @@
1
+ import { TestnetsError } from "./TestnetsError.js";
2
+ import { TestnetStatusError } from "./TestnetStatusError.js";
3
+ import { TestnetNotFoundError } from "./TestnetNotFoundError.js";
4
+ import { TestnetRequestFailedError } from "./TestnetRequestFailedError.js";
5
+ import { TestnetValidationError } from "./TestnetValidationError.js";
6
+ import { TestnetWaitTimeoutError } from "./TestnetWaitTimeoutError.js";
7
+ export { TestnetNotFoundError, TestnetRequestFailedError, TestnetStatusError, TestnetValidationError, TestnetWaitTimeoutError, TestnetsError };
@@ -0,0 +1,11 @@
1
+ import { TestnetsError } from "./errors/TestnetsError.js";
2
+ import { TestnetStatusError } from "./errors/TestnetStatusError.js";
3
+ import { TestnetNotFoundError } from "./errors/TestnetNotFoundError.js";
4
+ import { TestnetRequestFailedError } from "./errors/TestnetRequestFailedError.js";
5
+ import { TestnetValidationError } from "./errors/TestnetValidationError.js";
6
+ import { TestnetWaitTimeoutError } from "./errors/TestnetWaitTimeoutError.js";
7
+ import "./errors/index.js";
8
+ import { testnetBackendOptionsSchema, testnetBackendStateSchema, testnetBackendStatusSchema, testnetBlockSchema, testnetOptionsSchema, testnetSchema, testnetScriptBlockSchema, testnetScriptOptionsSchema, testnetScriptSchema, testnetStageStatusSchema, testnetStatusSchema, testnetTaskDefinitionSchema, testnetTaskProgressSchema, testnetTaskSchema } from "./schemas.js";
9
+ import { TestnetsAPI } from "./TestnetsAPI.js";
10
+ import "./types.js";
11
+ export { TestnetNotFoundError, TestnetRequestFailedError, TestnetStatusError, TestnetValidationError, TestnetWaitTimeoutError, TestnetsAPI, TestnetsError, testnetBackendOptionsSchema, testnetBackendStateSchema, testnetBackendStatusSchema, testnetBlockSchema, testnetOptionsSchema, testnetSchema, testnetScriptBlockSchema, testnetScriptOptionsSchema, testnetScriptSchema, testnetStageStatusSchema, testnetStatusSchema, testnetTaskDefinitionSchema, testnetTaskProgressSchema, testnetTaskSchema };
@@ -0,0 +1 @@
1
+ {"type": "module","sideEffects":false}
@@ -0,0 +1,228 @@
1
+ import { NetworkType } from "../onchain/chain/chains.js";
2
+ import { ZodAddress } from "../onchain/utils/zod.js";
3
+ import { z } from "zod/v4";
4
+ //#region src/testnets/schemas.ts
5
+ /**
6
+ * Go marshals a nil slice as `null`; this reads it as an empty array.
7
+ **/
8
+ function nullableArray(item) {
9
+ return z.array(item).nullable().transform((value) => value ?? []);
10
+ }
11
+ /**
12
+ * Go marshals a nil map as `null`; this reads it as an empty record.
13
+ **/
14
+ function nullableRecord(value) {
15
+ return z.record(z.string(), value).nullable().transform((record) => record ?? {});
16
+ }
17
+ const timestamp = z.iso.datetime({ offset: true });
18
+ /**
19
+ * {@link TestnetStageStatus}
20
+ **/
21
+ const testnetStageStatusSchema = z.enum([
22
+ "queued",
23
+ "started",
24
+ "error",
25
+ "failed",
26
+ "success",
27
+ "terminated"
28
+ ]);
29
+ /**
30
+ * {@link TestnetBackendState}
31
+ **/
32
+ const testnetBackendStateSchema = z.enum([
33
+ "provisioning",
34
+ "starting",
35
+ "active",
36
+ "error"
37
+ ]);
38
+ /**
39
+ * {@link Testnet}
40
+ **/
41
+ const testnetSchema = z.object({
42
+ alias: z.string(),
43
+ executionId: z.string(),
44
+ isActive: z.boolean(),
45
+ description: z.string(),
46
+ persistent: z.boolean(),
47
+ paused: z.boolean(),
48
+ network: NetworkType,
49
+ createdAt: timestamp
50
+ });
51
+ /**
52
+ * {@link TestnetBackendOptions}
53
+ **/
54
+ const testnetBackendOptionsSchema = z.object({
55
+ version: z.string().optional(),
56
+ env: z.record(z.string(), z.string()).optional(),
57
+ parentBranchId: z.string().optional()
58
+ });
59
+ /**
60
+ * {@link TestnetBackendStatus}
61
+ **/
62
+ const testnetBackendStatusSchema = z.object({
63
+ state: testnetBackendStateSchema,
64
+ error: z.string().optional(),
65
+ version: z.string().optional()
66
+ });
67
+ /**
68
+ * {@link TestnetBlock}
69
+ **/
70
+ const testnetBlockSchema = z.object({
71
+ number: z.number(),
72
+ time: timestamp
73
+ });
74
+ /**
75
+ * {@link TestnetScriptBlock}
76
+ **/
77
+ const testnetScriptBlockSchema = testnetBlockSchema.extend({
78
+ timeDrift: z.number(),
79
+ originDrift: z.string(),
80
+ automine: z.boolean(),
81
+ mineInterval: z.number()
82
+ });
83
+ /**
84
+ * {@link TestnetScriptOptions}
85
+ **/
86
+ const testnetScriptOptionsSchema = z.object({
87
+ id: z.string(),
88
+ description: z.string(),
89
+ npx: z.string().optional(),
90
+ docker: z.string().optional(),
91
+ args: nullableArray(z.string()),
92
+ env: nullableRecord(z.string()),
93
+ flags: nullableArray(z.string()),
94
+ timeout: z.string(),
95
+ packageManager: z.string().optional(),
96
+ input: z.unknown().optional()
97
+ });
98
+ /**
99
+ * {@link TestnetScript}
100
+ **/
101
+ const testnetScriptSchema = z.object({
102
+ script: testnetScriptOptionsSchema,
103
+ duration: z.string(),
104
+ blockBefore: testnetScriptBlockSchema.optional(),
105
+ blockAfter: testnetScriptBlockSchema.optional(),
106
+ status: testnetStageStatusSchema,
107
+ error: z.string().optional(),
108
+ exitCode: z.number(),
109
+ source: z.string().optional(),
110
+ revision: z.string().optional()
111
+ });
112
+ /**
113
+ * {@link TestnetTaskDefinition}
114
+ **/
115
+ const testnetTaskDefinitionSchema = z.object({
116
+ id: z.string().optional(),
117
+ family: z.string().optional(),
118
+ name: z.string().optional(),
119
+ description: z.string().optional(),
120
+ image: z.string(),
121
+ version: z.string().optional(),
122
+ entrypoint: z.array(z.string()).optional(),
123
+ command: z.array(z.string()).optional(),
124
+ env: z.record(z.string(), z.string()).optional(),
125
+ secrets: z.record(z.string(), z.string()).optional(),
126
+ input: z.unknown().optional(),
127
+ timeout: z.number().optional(),
128
+ concurrent: z.boolean(),
129
+ multicontainer: z.boolean(),
130
+ schemaVersion: z.number().optional()
131
+ });
132
+ /**
133
+ * {@link TestnetTaskProgress}
134
+ **/
135
+ const testnetTaskProgressSchema = z.object({
136
+ completed: z.number(),
137
+ total: z.number(),
138
+ details: z.unknown().optional()
139
+ });
140
+ /**
141
+ * {@link TestnetTask}
142
+ **/
143
+ const testnetTaskSchema = z.object({
144
+ id: z.string(),
145
+ definitionId: z.string(),
146
+ family: z.string(),
147
+ executionId: z.string(),
148
+ name: z.string().optional(),
149
+ description: z.string().optional(),
150
+ image: z.string(),
151
+ source: z.string().optional(),
152
+ revision: z.string().optional(),
153
+ entrypoint: z.array(z.string()).optional(),
154
+ command: z.array(z.string()).optional(),
155
+ env: z.record(z.string(), z.string()).optional(),
156
+ secrets: z.record(z.string(), z.string()).optional(),
157
+ input: z.unknown().optional(),
158
+ multicontainer: z.boolean(),
159
+ concurrent: z.boolean(),
160
+ outputDir: z.string().optional(),
161
+ status: testnetStageStatusSchema,
162
+ error: z.string().optional(),
163
+ createdAt: timestamp,
164
+ updatedAt: timestamp,
165
+ progress: testnetTaskProgressSchema
166
+ });
167
+ /**
168
+ * {@link TestnetOptions}
169
+ **/
170
+ const testnetOptionsSchema = z.object({
171
+ description: z.string().optional(),
172
+ chainId: z.number().optional(),
173
+ network: NetworkType,
174
+ cid: z.string().optional(),
175
+ anvilFlags: z.array(z.string()).optional(),
176
+ anvilImage: z.string().optional(),
177
+ rpcProvider: z.string().optional(),
178
+ addressProvider: ZodAddress().optional(),
179
+ marketConfigurators: z.array(ZodAddress()).optional(),
180
+ rwaFactories: z.array(ZodAddress()).optional(),
181
+ blockNumber: z.string(),
182
+ owner: z.string(),
183
+ timeout: z.string(),
184
+ synctronLogLevel: z.string().optional(),
185
+ scripts: nullableArray(testnetScriptOptionsSchema),
186
+ disableSync: z.boolean(),
187
+ disableIntervalMining: z.boolean(),
188
+ disableRPCProtection: z.boolean(),
189
+ debug: z.boolean().optional(),
190
+ webhooks: nullableArray(z.string()),
191
+ backend: testnetBackendOptionsSchema.optional(),
192
+ tasks: z.array(testnetTaskDefinitionSchema).optional()
193
+ });
194
+ /**
195
+ * {@link TestnetStatus}
196
+ **/
197
+ const testnetStatusSchema = z.object({
198
+ alias: z.string().optional(),
199
+ network: NetworkType,
200
+ nextRestart: timestamp.optional(),
201
+ isActive: z.boolean(),
202
+ ready: z.boolean(),
203
+ paused: z.boolean(),
204
+ rpcUrl: z.string().optional(),
205
+ url: z.string(),
206
+ logsUrl: z.string(),
207
+ persistent: z.boolean(),
208
+ latestBlock: testnetScriptBlockSchema.optional(),
209
+ tasks: z.array(testnetTaskSchema).optional(),
210
+ backend: testnetBackendStatusSchema.optional(),
211
+ executionId: z.string(),
212
+ options: testnetOptionsSchema,
213
+ forkBlock: testnetBlockSchema,
214
+ syncBlock: z.number(),
215
+ syncError: z.string().optional(),
216
+ originChainId: z.number(),
217
+ anvilChainId: z.number(),
218
+ createdAt: timestamp,
219
+ addressProvider: ZodAddress(),
220
+ marketConfigurators: z.array(ZodAddress()).optional(),
221
+ rwaFactories: z.array(ZodAddress()).optional(),
222
+ onchainExecutionId: ZodAddress(),
223
+ scripts: z.array(testnetScriptSchema).optional(),
224
+ cid: z.string().optional(),
225
+ files: z.array(z.string()).optional()
226
+ });
227
+ //#endregion
228
+ export { testnetBackendOptionsSchema, testnetBackendStateSchema, testnetBackendStatusSchema, testnetBlockSchema, testnetOptionsSchema, testnetSchema, testnetScriptBlockSchema, testnetScriptOptionsSchema, testnetScriptSchema, testnetStageStatusSchema, testnetStatusSchema, testnetTaskDefinitionSchema, testnetTaskProgressSchema, testnetTaskSchema };
@@ -0,0 +1 @@
1
+ export {};