@paxoslabs/amplify-sdk 0.5.2 → 0.5.3

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 (58) hide show
  1. package/dist/{chain-utils-BdJecHBA.d.mts → chain-utils-5r2UnCDS.d.mts} +47 -1
  2. package/dist/{chain-utils-BdJecHBA.d.ts → chain-utils-5r2UnCDS.d.ts} +47 -1
  3. package/dist/{chunk-5OK753GA.js → chunk-4NQPS3JC.js} +470 -358
  4. package/dist/chunk-4NQPS3JC.js.map +1 -0
  5. package/dist/chunk-6JLKHV6O.js +128 -0
  6. package/dist/chunk-6JLKHV6O.js.map +1 -0
  7. package/dist/{chunk-NNDY5TID.js → chunk-FHE43NKY.js} +12 -12
  8. package/dist/{chunk-NNDY5TID.js.map → chunk-FHE43NKY.js.map} +1 -1
  9. package/dist/chunk-GOJQYEJQ.js +3212 -0
  10. package/dist/chunk-GOJQYEJQ.js.map +1 -0
  11. package/dist/{chunk-HU5CTL4C.mjs → chunk-LMNADWTH.mjs} +364 -253
  12. package/dist/chunk-LMNADWTH.mjs.map +1 -0
  13. package/dist/chunk-ODXJYXUH.mjs +3168 -0
  14. package/dist/chunk-ODXJYXUH.mjs.map +1 -0
  15. package/dist/{chunk-QZHI2T7O.mjs → chunk-QMFYPHX5.mjs} +280 -47
  16. package/dist/chunk-QMFYPHX5.mjs.map +1 -0
  17. package/dist/{chunk-Q5FXJU5Y.mjs → chunk-UY2WD7MF.mjs} +3 -3
  18. package/dist/{chunk-Q5FXJU5Y.mjs.map → chunk-UY2WD7MF.mjs.map} +1 -1
  19. package/dist/chunk-WD6QFSXZ.js +701 -0
  20. package/dist/chunk-WD6QFSXZ.js.map +1 -0
  21. package/dist/chunk-Y5LBT2WT.mjs +118 -0
  22. package/dist/chunk-Y5LBT2WT.mjs.map +1 -0
  23. package/dist/core.d.mts +50 -7
  24. package/dist/core.d.ts +50 -7
  25. package/dist/core.js +32 -16
  26. package/dist/core.mjs +4 -4
  27. package/dist/display.d.mts +211 -2
  28. package/dist/display.d.ts +211 -2
  29. package/dist/display.js +26 -10
  30. package/dist/display.mjs +4 -4
  31. package/dist/{index-DXXA8gEA.d.mts → index-D8RtV9cB.d.mts} +498 -410
  32. package/dist/{index-aE5lTOUH.d.ts → index-ev_V5sjt.d.ts} +498 -410
  33. package/dist/index.d.mts +40 -5
  34. package/dist/index.d.ts +40 -5
  35. package/dist/index.js +80 -60
  36. package/dist/index.mjs +5 -5
  37. package/dist/utils.d.mts +1 -1
  38. package/dist/utils.d.ts +1 -1
  39. package/dist/utils.js +7 -7
  40. package/dist/utils.mjs +2 -2
  41. package/dist/vaults.d.mts +2 -2
  42. package/dist/vaults.d.ts +2 -2
  43. package/dist/vaults.js +25 -25
  44. package/dist/vaults.mjs +4 -4
  45. package/package.json +1 -1
  46. package/dist/chunk-5OK753GA.js.map +0 -1
  47. package/dist/chunk-7JQQ2TH4.mjs +0 -1231
  48. package/dist/chunk-7JQQ2TH4.mjs.map +0 -1
  49. package/dist/chunk-HU5CTL4C.mjs.map +0 -1
  50. package/dist/chunk-QZHI2T7O.mjs.map +0 -1
  51. package/dist/chunk-SWUG4PTB.js +0 -464
  52. package/dist/chunk-SWUG4PTB.js.map +0 -1
  53. package/dist/chunk-WK7EJRBB.mjs +0 -1482
  54. package/dist/chunk-WK7EJRBB.mjs.map +0 -1
  55. package/dist/chunk-WYBYBPX5.js +0 -1518
  56. package/dist/chunk-WYBYBPX5.js.map +0 -1
  57. package/dist/chunk-ZKDXRGI5.js +0 -1239
  58. package/dist/chunk-ZKDXRGI5.js.map +0 -1
@@ -1,1482 +0,0 @@
1
- import { defineChain, createPublicClient, http } from 'viem';
2
- import { base, sepolia, mainnet } from 'viem/chains';
3
-
4
- var __defProp = Object.defineProperty;
5
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
-
8
- // src/utils/fetch.ts
9
- var DEFAULT_TIMEOUT = 1e4;
10
- function createTimeoutSignal(timeoutMs) {
11
- const controller = new AbortController();
12
- setTimeout(() => controller.abort(), timeoutMs);
13
- return controller.signal;
14
- }
15
-
16
- // src/types/telemetry.ts
17
- var LogLevel = {
18
- DEBUG: 0,
19
- INFO: 1,
20
- WARN: 2,
21
- ERROR: 3,
22
- NONE: 4
23
- };
24
-
25
- // src/lib/logger.ts
26
- var ConsoleLogger = class {
27
- constructor(level = LogLevel.ERROR) {
28
- __publicField(this, "level");
29
- this.level = level;
30
- }
31
- /**
32
- * Update log level
33
- */
34
- setLevel(level) {
35
- this.level = level;
36
- }
37
- /**
38
- * Get current log level
39
- */
40
- getLevel() {
41
- return this.level;
42
- }
43
- debug(message, context) {
44
- if (this.level <= LogLevel.DEBUG) {
45
- if (context) {
46
- console.debug("[Amplify SDK]", message, context);
47
- } else {
48
- console.debug("[Amplify SDK]", message);
49
- }
50
- }
51
- }
52
- info(message, context) {
53
- if (this.level <= LogLevel.INFO) {
54
- if (context) {
55
- console.info("[Amplify SDK]", message, context);
56
- } else {
57
- console.info("[Amplify SDK]", message);
58
- }
59
- }
60
- }
61
- warn(message, context) {
62
- if (this.level <= LogLevel.WARN) {
63
- if (context) {
64
- console.warn("[Amplify SDK]", message, context);
65
- } else {
66
- console.warn("[Amplify SDK]", message);
67
- }
68
- }
69
- }
70
- error(message, context) {
71
- if (this.level <= LogLevel.ERROR) {
72
- if (context) {
73
- console.error("[Amplify SDK]", message, context);
74
- } else {
75
- console.error("[Amplify SDK]", message);
76
- }
77
- }
78
- }
79
- };
80
- var currentLogger = new ConsoleLogger(LogLevel.ERROR);
81
- var defaultLogger = currentLogger;
82
- function setLogger(logger) {
83
- currentLogger = logger;
84
- }
85
- function getLogger() {
86
- return currentLogger;
87
- }
88
- function setLogLevel(level) {
89
- if (defaultLogger && typeof defaultLogger.setLevel === "function") {
90
- defaultLogger.setLevel(level);
91
- }
92
- }
93
-
94
- // src/constants/environment.ts
95
- var BASE_URL = "https://api.paxoslabs.com";
96
- function getGraphQLEndpoint() {
97
- return `${BASE_URL}/graphql`;
98
- }
99
- function getRestV2BaseURL() {
100
- return `${BASE_URL}/v2`;
101
- }
102
-
103
- // src/lib/vault-cache.ts
104
- var DEFAULT_TTL = 6e5;
105
- var VaultCache = class {
106
- /**
107
- * Creates a new VaultCache instance
108
- *
109
- * @param ttl - Time-to-live in milliseconds (default: 600000 = 10 minutes)
110
- */
111
- constructor(ttl = DEFAULT_TTL) {
112
- /**
113
- * Vaults keyed by baseTokenAddress
114
- * One key can map to multiple vaults (different chains/yield types)
115
- */
116
- __publicField(this, "vaults");
117
- /**
118
- * Assets keyed by token address
119
- */
120
- __publicField(this, "assets");
121
- /**
122
- * Vaults keyed by boringVaultAddress (lowercase, for case-insensitive lookup)
123
- */
124
- __publicField(this, "vaultsByAddress");
125
- /**
126
- * Vaults keyed by vault name (lowercase, for case-insensitive lookup)
127
- */
128
- __publicField(this, "vaultsByName");
129
- /**
130
- * Unix timestamp (milliseconds) of last successful fetch
131
- */
132
- __publicField(this, "lastFetch");
133
- /**
134
- * Time-to-live in milliseconds
135
- */
136
- __publicField(this, "ttl");
137
- /**
138
- * Current refresh promise (prevents duplicate concurrent refreshes)
139
- * When non-null, a refresh is in progress and callers should await this promise
140
- */
141
- __publicField(this, "refreshPromise");
142
- this.vaults = /* @__PURE__ */ new Map();
143
- this.assets = /* @__PURE__ */ new Map();
144
- this.vaultsByAddress = /* @__PURE__ */ new Map();
145
- this.vaultsByName = /* @__PURE__ */ new Map();
146
- this.lastFetch = 0;
147
- this.ttl = ttl;
148
- this.refreshPromise = null;
149
- }
150
- /**
151
- * Gets vaults by deposit token address
152
- *
153
- * Returns undefined if no vaults found for the given token address.
154
- * Does NOT automatically refresh if cache is expired; use refresh() for that.
155
- *
156
- * @param tokenAddress - Deposit token address (baseTokenAddress)
157
- * @returns Array of AmplifyVault objects, or undefined if not found
158
- */
159
- getVault(tokenAddress) {
160
- return this.vaults.get(tokenAddress);
161
- }
162
- /**
163
- * Gets a vault by its boringVaultAddress (on-chain vault contract).
164
- * Case-insensitive lookup.
165
- */
166
- getVaultByAddress(address) {
167
- return this.vaultsByAddress.get(address.toLowerCase());
168
- }
169
- /**
170
- * Gets a vault by its human-readable name.
171
- * Case-insensitive lookup.
172
- */
173
- getVaultByName(name) {
174
- return this.vaultsByName.get(name.toLowerCase());
175
- }
176
- /**
177
- * Gets asset by token address
178
- *
179
- * Returns undefined if asset not found.
180
- * Does NOT automatically refresh if cache is expired; use refresh() for that.
181
- *
182
- * @param tokenAddress - Token address
183
- * @returns SupportedAsset object, or undefined if not found
184
- */
185
- getAsset(tokenAddress) {
186
- return this.assets.get(tokenAddress);
187
- }
188
- /**
189
- * Gets all cached vaults
190
- *
191
- * Returns an array of all vaults across all deposit tokens.
192
- * Does NOT automatically refresh if cache is expired; use refresh() for that.
193
- *
194
- * @returns Array of all AmplifyVault objects
195
- */
196
- getAllVaults() {
197
- const seen = /* @__PURE__ */ new Set();
198
- const allVaults = [];
199
- for (const vaultArray of this.vaults.values()) {
200
- for (const vault of vaultArray) {
201
- if (!seen.has(vault.id)) {
202
- seen.add(vault.id);
203
- allVaults.push(vault);
204
- }
205
- }
206
- }
207
- return allVaults;
208
- }
209
- /**
210
- * Gets all cached assets
211
- *
212
- * Returns an array of all assets.
213
- * Does NOT automatically refresh if cache is expired; use refresh() for that.
214
- *
215
- * @returns Array of all SupportedAsset objects
216
- */
217
- getAllAssets() {
218
- return Array.from(this.assets.values());
219
- }
220
- /**
221
- * Checks if cache is expired
222
- *
223
- * Cache is considered expired if current time exceeds lastFetch + ttl.
224
- *
225
- * @returns true if cache is expired, false otherwise
226
- */
227
- isExpired() {
228
- return Date.now() > this.lastFetch + this.ttl;
229
- }
230
- /**
231
- * Gets the time until cache expires
232
- *
233
- * @returns Milliseconds until expiry, or 0 if already expired
234
- */
235
- getTimeUntilExpiry() {
236
- const expiryTime = this.lastFetch + this.ttl;
237
- const now = Date.now();
238
- return Math.max(0, expiryTime - now);
239
- }
240
- /**
241
- * Checks if cache is empty (never populated)
242
- *
243
- * @returns true if cache has never been populated, false otherwise
244
- */
245
- isEmpty() {
246
- return this.lastFetch === 0;
247
- }
248
- /**
249
- * Manually refreshes the cache
250
- *
251
- * Fetches fresh data from the API and updates both vaults and assets maps.
252
- * Updates lastFetch timestamp on success.
253
- *
254
- * If a refresh is already in progress, this method returns the existing
255
- * promise instead of starting a concurrent refresh. This is more efficient
256
- * than polling and ensures all callers wait for the same refresh operation.
257
- *
258
- * @throws {APIError} If the API request fails
259
- */
260
- async refresh() {
261
- if (this.refreshPromise) {
262
- return this.refreshPromise;
263
- }
264
- this.refreshPromise = this._doRefresh();
265
- try {
266
- await this.refreshPromise;
267
- } finally {
268
- this.refreshPromise = null;
269
- }
270
- }
271
- /**
272
- * Internal refresh implementation
273
- * Separated to allow promise reuse in refresh()
274
- *
275
- * Fetches data via GraphQL and derives both vaults and assets
276
- * from the same response.
277
- */
278
- async _doRefresh() {
279
- const { vaults, assets } = await fetchVaultDataFromGraphQL();
280
- this.vaults.clear();
281
- this.assets.clear();
282
- this.vaultsByAddress.clear();
283
- this.vaultsByName.clear();
284
- for (const vault of vaults) {
285
- for (const asset of vault.supportedAssets) {
286
- const tokenAddress = asset.address;
287
- const existing = this.vaults.get(tokenAddress);
288
- if (existing) {
289
- if (!existing.includes(vault)) {
290
- existing.push(vault);
291
- }
292
- } else {
293
- this.vaults.set(tokenAddress, [vault]);
294
- }
295
- }
296
- this.vaultsByAddress.set(
297
- vault.vault.boringVaultAddress.toLowerCase(),
298
- vault
299
- );
300
- if (vault.name) {
301
- this.vaultsByName.set(vault.name.toLowerCase(), vault);
302
- }
303
- }
304
- for (const asset of assets) {
305
- this.assets.set(asset.address, asset);
306
- }
307
- this.lastFetch = Date.now();
308
- }
309
- /**
310
- * Check if a refresh is currently in progress
311
- *
312
- * @returns true if cache is being refreshed, false otherwise
313
- */
314
- isRefreshing() {
315
- return this.refreshPromise !== null;
316
- }
317
- /**
318
- * Clears the cache
319
- *
320
- * Removes all cached data and resets lastFetch timestamp.
321
- * Does not affect TTL setting.
322
- */
323
- clear() {
324
- this.vaults.clear();
325
- this.assets.clear();
326
- this.vaultsByAddress.clear();
327
- this.vaultsByName.clear();
328
- this.lastFetch = 0;
329
- }
330
- /**
331
- * Gets cache statistics
332
- *
333
- * @returns Object with cache statistics
334
- */
335
- getStats() {
336
- return {
337
- vaultCount: this.getAllVaults().length,
338
- assetCount: this.assets.size,
339
- tokenCount: this.vaults.size,
340
- lastFetch: this.lastFetch,
341
- ttl: this.ttl,
342
- isExpired: this.isExpired(),
343
- isEmpty: this.isEmpty(),
344
- timeUntilExpiry: this.getTimeUntilExpiry()
345
- };
346
- }
347
- };
348
-
349
- // src/constants/index.ts
350
- var DEFAULT_DEADLINE = 3;
351
- var CHAINLINK_ADDRESS = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419";
352
- var DEFAULT_APPROVAL_AMOUNT = BigInt(2) ** BigInt(256) - BigInt(1);
353
- var API_BASE_URL = "https://api.paxoslabs.com";
354
-
355
- // src/lib/telemetry.ts
356
- var SDK_VERSION = "0.1.1-beta.1";
357
- var MAX_ERROR_BUFFER_SIZE = 10;
358
- var CONFIG_FETCH_TIMEOUT = 5e3;
359
- var telemetryState = {
360
- enabled: false,
361
- isInitialized: false,
362
- isInitializing: false,
363
- initPromise: null,
364
- config: null,
365
- apiKey: null
366
- };
367
- var posthogClient = null;
368
- var errorBuffer = [];
369
- var bufferFullWarningLogged = false;
370
- function hashApiKey(apiKey) {
371
- const FNV_OFFSET = BigInt("0xcbf29ce484222325");
372
- const FNV_PRIME = BigInt("0x100000001b3");
373
- let hash = FNV_OFFSET;
374
- for (let i = 0; i < apiKey.length; i++) {
375
- hash ^= BigInt(apiKey.charCodeAt(i));
376
- hash = hash * FNV_PRIME & BigInt("0xffffffffffffffff");
377
- }
378
- return `sdk_${hash.toString(16).slice(0, 12)}`;
379
- }
380
- async function fetchTelemetryConfig(apiKey) {
381
- const endpoint = "/v1/earn-sdk/config";
382
- const url = `${API_BASE_URL}${endpoint}`;
383
- try {
384
- const headers = getRequestHeaders();
385
- if (!headers["x-api-key"]) {
386
- headers["x-api-key"] = apiKey;
387
- }
388
- const response = await fetch(url, {
389
- method: "GET",
390
- headers,
391
- signal: createTimeoutSignal(CONFIG_FETCH_TIMEOUT)
392
- });
393
- if (!response.ok) {
394
- return null;
395
- }
396
- const data = await response.json();
397
- if (!data || typeof data.posthogApiKey !== "string") {
398
- return null;
399
- }
400
- return {
401
- posthogApiKey: data.posthogApiKey,
402
- posthogHost: data.posthogHost || "https://us.i.posthog.com"
403
- };
404
- } catch {
405
- return null;
406
- }
407
- }
408
- async function flushErrorBuffer() {
409
- if (errorBuffer.length === 0 || !posthogClient) {
410
- return;
411
- }
412
- for (const bufferedError of errorBuffer) {
413
- const payload = {
414
- error_type: bufferedError.name,
415
- error_message: bufferedError.message,
416
- error_code: bufferedError.code,
417
- sdk_version: SDK_VERSION,
418
- buffered: true,
419
- ...bufferedError.context
420
- };
421
- posthogClient.capture("sdk_error", payload);
422
- }
423
- errorBuffer.length = 0;
424
- }
425
- async function initTelemetry(apiKey) {
426
- if (telemetryState.isInitialized) {
427
- return;
428
- }
429
- if (telemetryState.isInitializing && telemetryState.initPromise) {
430
- await telemetryState.initPromise;
431
- return;
432
- }
433
- telemetryState.isInitializing = true;
434
- telemetryState.apiKey = apiKey;
435
- telemetryState.initPromise = (async () => {
436
- try {
437
- const config = await fetchTelemetryConfig(apiKey);
438
- if (!config) {
439
- telemetryState.enabled = false;
440
- telemetryState.isInitialized = true;
441
- return;
442
- }
443
- telemetryState.config = config;
444
- const posthogModule = await import('posthog-js');
445
- const posthog = posthogModule.default;
446
- posthog.init(config.posthogApiKey, {
447
- api_host: config.posthogHost,
448
- autocapture: false,
449
- capture_pageview: false,
450
- capture_pageleave: false,
451
- disable_session_recording: true,
452
- persistence: "memory",
453
- // Don't persist to localStorage
454
- loaded: (ph) => {
455
- ph.identify(hashApiKey(apiKey));
456
- }
457
- });
458
- posthogClient = posthog;
459
- telemetryState.enabled = true;
460
- telemetryState.isInitialized = true;
461
- await flushErrorBuffer();
462
- posthog.capture("sdk_initialized", {
463
- sdk_version: SDK_VERSION
464
- });
465
- } catch (error) {
466
- const logger = getLogger();
467
- logger.warn("Telemetry initialization failed", {
468
- error: error instanceof Error ? error.message : String(error)
469
- });
470
- telemetryState.enabled = false;
471
- telemetryState.isInitialized = true;
472
- } finally {
473
- telemetryState.isInitializing = false;
474
- telemetryState.initPromise = null;
475
- }
476
- })();
477
- await telemetryState.initPromise;
478
- }
479
- function trackError(error, context) {
480
- const logger = getLogger();
481
- logger.error(error.message, {
482
- name: error.name,
483
- stack: error.stack,
484
- ...context
485
- });
486
- const errorDetails = {
487
- name: error.name,
488
- message: error.message,
489
- code: error.code,
490
- context: context || {},
491
- timestamp: Date.now()
492
- };
493
- if (!posthogClient || !telemetryState.enabled) {
494
- if (errorBuffer.length < MAX_ERROR_BUFFER_SIZE) {
495
- errorBuffer.push(errorDetails);
496
- } else if (!bufferFullWarningLogged) {
497
- logger.warn(
498
- `Telemetry error buffer full (${MAX_ERROR_BUFFER_SIZE} errors). Additional errors will not be tracked until telemetry initializes.`
499
- );
500
- bufferFullWarningLogged = true;
501
- }
502
- return;
503
- }
504
- const payload = {
505
- error_type: error.name,
506
- error_message: error.message,
507
- error_code: error.code,
508
- endpoint: error.endpoint,
509
- status_code: error.statusCode,
510
- sdk_version: SDK_VERSION,
511
- ...context
512
- };
513
- posthogClient.capture("sdk_error", payload);
514
- }
515
- function resetTelemetry() {
516
- telemetryState.enabled = false;
517
- telemetryState.isInitialized = false;
518
- telemetryState.isInitializing = false;
519
- telemetryState.initPromise = null;
520
- telemetryState.config = null;
521
- telemetryState.apiKey = null;
522
- posthogClient = null;
523
- errorBuffer.length = 0;
524
- bufferFullWarningLogged = false;
525
- }
526
-
527
- // src/types/amplify-sdk-api.ts
528
- var APIError = class _APIError extends Error {
529
- constructor(message, options) {
530
- super(message);
531
- /**
532
- * HTTP status code (if available)
533
- */
534
- __publicField(this, "statusCode");
535
- /**
536
- * API endpoint that failed
537
- */
538
- __publicField(this, "endpoint");
539
- /**
540
- * Original error cause
541
- */
542
- __publicField(this, "cause");
543
- this.name = "APIError";
544
- this.statusCode = options?.statusCode;
545
- this.endpoint = options?.endpoint;
546
- this.cause = options?.cause;
547
- Object.setPrototypeOf(this, _APIError.prototype);
548
- if (Error.captureStackTrace) {
549
- Error.captureStackTrace(this, _APIError);
550
- }
551
- try {
552
- trackError(this, {
553
- endpoint: this.endpoint,
554
- statusCode: this.statusCode
555
- });
556
- } catch {
557
- }
558
- }
559
- };
560
- function isValidYieldType(value) {
561
- return typeof value === "string" && (value === "CORE" || value === "TREASURY" || value === "FRONTIER");
562
- }
563
- function isValidAddress(value) {
564
- return typeof value === "string" && /^0x[a-fA-F0-9]{40}$/.test(value);
565
- }
566
- function isValidChainId(value) {
567
- return typeof value === "number" && value > 0 && Number.isInteger(value);
568
- }
569
-
570
- // src/client/graphql-client.ts
571
- var VAULT_FIELDS = `
572
- id
573
- name
574
- chainId
575
- isDeleted
576
- inDeprecation
577
- boringVaultModuleId
578
- boringVaultAddress
579
- rolesAuthorityModuleId
580
- tellerModuleId
581
- accountantModuleId
582
- managerModuleId
583
- withdrawQueueModuleId
584
- communityCodeDepositorModuleId
585
- enterpriseConfig {
586
- predicatePolicyId
587
- }
588
- baseTokenAddressId
589
- baseTokenStandInId
590
- baseTokenStandIn {
591
- decimals
592
- symbol
593
- coinGeckoApiId
594
- }
595
- withdrawalSla {
596
- withdrawAssetAddress
597
- externalWithdrawalQueueDelaySLA
598
- externalAccountantRateUpdateSLA
599
- }
600
- supportedAssets {
601
- address
602
- chainId
603
- depositable
604
- withdrawable
605
- symbol
606
- tokenName
607
- decimals
608
- }
609
- `;
610
- var DEPOSIT_TOKEN_FIELDS = `
611
- id
612
- address
613
- chainId
614
- name
615
- `;
616
- var TOKEN_METADATA_FIELDS = `
617
- address
618
- chainId
619
- symbol
620
- tokenName
621
- decimals
622
- coinGeckoTokenId
623
- tokenStandard
624
- source
625
- firstSeenAt
626
- metadataRefreshedAt
627
- `;
628
- var VAULT_ASSET_FLAGS_FIELDS = `
629
- depositable
630
- withdrawable
631
- `;
632
- var SDK_CONFIG_CORE_FIELDS = `
633
- id
634
- chainId
635
- environment
636
- yieldType
637
- vaultId
638
- depositTokenAddressId
639
- `;
640
- function buildSdkConfigsQuery(options) {
641
- const nestedFields = [];
642
- if (options.vault) {
643
- nestedFields.push(`vault { ${options.vault} }`);
644
- }
645
- if (options.depositTokenAddress) {
646
- nestedFields.push(`depositTokenAddress { ${options.depositTokenAddress} }`);
647
- }
648
- if (options.tokenMetadata) {
649
- nestedFields.push(`tokenMetadata { ${options.tokenMetadata} }`);
650
- }
651
- if (options.vaultAssetFlags) {
652
- nestedFields.push(`vaultAssetFlags { ${options.vaultAssetFlags} }`);
653
- }
654
- const allFields = [...options.fields, ...nestedFields].join("\n ");
655
- return `
656
- query AmplifySdkConfigs(
657
- $environment: Environment
658
- $chainId: Int
659
- $yieldType: YieldType
660
- ) {
661
- amplifySdkConfigs(
662
- environment: $environment
663
- chainId: $chainId
664
- yieldType: $yieldType
665
- ) {
666
- ${allFields}
667
- }
668
- }
669
- `;
670
- }
671
- async function executeGraphQLQuery(endpoint, query, variables) {
672
- const headers = getRequestHeaders();
673
- let response;
674
- try {
675
- response = await fetch(endpoint, {
676
- method: "POST",
677
- headers,
678
- body: JSON.stringify({ query, variables }),
679
- signal: createTimeoutSignal(DEFAULT_TIMEOUT)
680
- });
681
- } catch (error) {
682
- if (error instanceof Error && error.name === "AbortError") {
683
- throw new APIError("GraphQL request timed out", {
684
- endpoint,
685
- cause: error
686
- });
687
- }
688
- throw new APIError(
689
- `GraphQL network error: ${error instanceof Error ? error.message : String(error)}`,
690
- { endpoint, cause: error }
691
- );
692
- }
693
- if (!response.ok) {
694
- throw new APIError(
695
- `GraphQL HTTP error: ${response.status} ${response.statusText}`,
696
- { endpoint, statusCode: response.status }
697
- );
698
- }
699
- let body;
700
- try {
701
- body = await response.json();
702
- } catch (error) {
703
- throw new APIError("GraphQL response is not valid JSON", {
704
- endpoint,
705
- cause: error
706
- });
707
- }
708
- if (body.errors && body.errors.length > 0) {
709
- const messages = body.errors.map((e) => e.message).join("; ");
710
- throw new APIError(`GraphQL errors: ${messages}`, {
711
- endpoint,
712
- cause: body.errors
713
- });
714
- }
715
- if (!body.data) {
716
- throw new APIError("GraphQL response missing data field", { endpoint });
717
- }
718
- return body.data;
719
- }
720
- async function fetchAmplifySdkConfigs(endpoint, variables) {
721
- const query = buildSdkConfigsQuery({
722
- fields: SDK_CONFIG_CORE_FIELDS.trim().split(/\s+/),
723
- vault: VAULT_FIELDS,
724
- depositTokenAddress: DEPOSIT_TOKEN_FIELDS,
725
- tokenMetadata: TOKEN_METADATA_FIELDS,
726
- vaultAssetFlags: VAULT_ASSET_FLAGS_FIELDS
727
- });
728
- return executeGraphQLQuery(
729
- endpoint,
730
- query,
731
- variables
732
- );
733
- }
734
-
735
- // src/constants/config.ts
736
- var YieldType = {
737
- CORE: "CORE",
738
- TREASURY: "TREASURY",
739
- FRONTIER: "FRONTIER"
740
- };
741
- var DEFAULT_SLIPPAGE_BPS = 50;
742
-
743
- // src/client/graphql-mapper.ts
744
- function mapYieldType(graphqlYieldType) {
745
- switch (graphqlYieldType) {
746
- case "CORE":
747
- case "PRIME":
748
- return YieldType.CORE;
749
- case "TREASURY":
750
- case "TBILL":
751
- return YieldType.TREASURY;
752
- case "FRONTIER":
753
- case "LENDING":
754
- return YieldType.FRONTIER;
755
- default: {
756
- const logger = getLogger();
757
- logger.warn(`Unknown GraphQL yield type: ${graphqlYieldType}, skipping`);
758
- return null;
759
- }
760
- }
761
- }
762
- function mapGraphQLConfigToVault(config) {
763
- if (!config.vault || !config.depositTokenAddress) {
764
- return null;
765
- }
766
- const yieldType = mapYieldType(config.yieldType);
767
- if (!yieldType) {
768
- return null;
769
- }
770
- const { vault, depositTokenAddress } = config;
771
- const indexerAssets = vault.supportedAssets && vault.supportedAssets.length > 0 ? vault.supportedAssets.map((a) => ({
772
- address: a.address,
773
- chainId: a.chainId,
774
- symbol: a.symbol ?? a.address,
775
- name: a.tokenName ?? a.symbol ?? a.address,
776
- decimals: a.decimals ?? 0,
777
- depositable: a.depositable,
778
- withdrawable: a.withdrawable
779
- })) : [
780
- {
781
- address: depositTokenAddress.address,
782
- symbol: config.tokenMetadata?.symbol ?? depositTokenAddress.name,
783
- name: config.tokenMetadata?.tokenName ?? depositTokenAddress.name,
784
- decimals: config.tokenMetadata?.decimals ?? 0,
785
- ...config.vaultAssetFlags && {
786
- depositable: config.vaultAssetFlags.depositable,
787
- withdrawable: config.vaultAssetFlags.withdrawable
788
- }
789
- }
790
- ];
791
- return {
792
- id: config.id,
793
- chainId: config.chainId,
794
- yieldType,
795
- name: vault.name,
796
- inDeprecation: vault.inDeprecation,
797
- vault: {
798
- boringVaultAddress: vault.boringVaultAddress,
799
- tellerAddress: vault.tellerModuleId,
800
- accountantAddress: vault.accountantModuleId,
801
- managerAddress: vault.managerModuleId,
802
- rolesAuthorityAddress: vault.rolesAuthorityModuleId,
803
- baseTokenAddress: vault.baseTokenAddressId,
804
- baseTokenStandIn: vault.baseTokenStandInId ? vault.baseTokenStandInId : void 0,
805
- withdrawQueueAddress: vault.withdrawQueueModuleId ? vault.withdrawQueueModuleId : void 0,
806
- communityCodeDepositorAddress: vault.communityCodeDepositorModuleId ? vault.communityCodeDepositorModuleId : void 0
807
- },
808
- enterpriseConfig: vault.enterpriseConfig ? { predicatePolicyId: vault.enterpriseConfig.predicatePolicyId } : void 0,
809
- supportedAssets: indexerAssets,
810
- sla: vault.withdrawalSla && vault.withdrawalSla.length > 0 ? {
811
- externalWithdrawalQueueDelaySLA: vault.withdrawalSla[0].externalWithdrawalQueueDelaySLA,
812
- externalAccountantRateUpdateSLA: vault.withdrawalSla[0].externalAccountantRateUpdateSLA
813
- } : void 0
814
- };
815
- }
816
- function aggregateVaultConfigs(configs) {
817
- const vaultMap = /* @__PURE__ */ new Map();
818
- for (const config of configs) {
819
- if (!config.vault || !config.depositTokenAddress) continue;
820
- if (vaultMap.has(config.vaultId)) continue;
821
- const mapped = mapGraphQLConfigToVault(config);
822
- if (mapped) {
823
- vaultMap.set(config.vaultId, mapped);
824
- }
825
- }
826
- return Array.from(vaultMap.values());
827
- }
828
- function extractSupportedAssetsFromConfigs(configs) {
829
- const assetMap = /* @__PURE__ */ new Map();
830
- for (const config of configs) {
831
- if (!config.depositTokenAddress) continue;
832
- const key = config.depositTokenAddress.address.toLowerCase();
833
- const existing = assetMap.get(key);
834
- if (existing) {
835
- existing.chains.add(config.chainId);
836
- if (existing.decimals === 0 && config.tokenMetadata?.decimals) {
837
- existing.decimals = config.tokenMetadata.decimals;
838
- existing.symbol = config.tokenMetadata.symbol ?? existing.symbol;
839
- existing.name = config.tokenMetadata.tokenName ?? existing.name;
840
- }
841
- } else {
842
- assetMap.set(key, {
843
- address: config.depositTokenAddress.address,
844
- symbol: config.tokenMetadata?.symbol ?? config.depositTokenAddress.name,
845
- name: config.tokenMetadata?.tokenName ?? config.depositTokenAddress.name,
846
- decimals: config.tokenMetadata?.decimals ?? 0,
847
- chains: /* @__PURE__ */ new Set([config.chainId])
848
- });
849
- }
850
- }
851
- return Array.from(assetMap.values()).map((asset) => ({
852
- address: asset.address,
853
- symbol: asset.symbol,
854
- name: asset.name,
855
- decimals: asset.decimals,
856
- chains: Array.from(asset.chains)
857
- }));
858
- }
859
-
860
- // src/client/amplify-sdk-client.ts
861
- function validateVaultFilterOptions(options) {
862
- if (!options) return;
863
- if (options.chainId !== void 0 && !isValidChainId(options.chainId)) {
864
- throw new APIError(
865
- `Invalid chainId: ${options.chainId}. Must be a positive integer.`,
866
- { endpoint: "/v1/earn-sdk/vaults" }
867
- );
868
- }
869
- if (options.yieldType !== void 0 && !isValidYieldType(options.yieldType)) {
870
- throw new APIError(
871
- `Invalid yieldType: ${options.yieldType}. Must be one of: CORE, TREASURY, FRONTIER.`,
872
- { endpoint: "/v1/earn-sdk/vaults" }
873
- );
874
- }
875
- if (options.depositTokenAddress !== void 0 && !isValidAddress(options.depositTokenAddress)) {
876
- throw new APIError(
877
- `Invalid depositTokenAddress: ${options.depositTokenAddress}. Must be a valid Ethereum address.`,
878
- { endpoint: "/v1/earn-sdk/vaults" }
879
- );
880
- }
881
- }
882
- function validateAssetFilterOptions(options) {
883
- if (!options) return;
884
- if (options.chains !== void 0) {
885
- if (!Array.isArray(options.chains) || options.chains.length === 0) {
886
- throw new APIError(
887
- "Invalid chains: Must be a non-empty array of chain IDs.",
888
- { endpoint: "/v1/earn-sdk/supported-assets-by-chains" }
889
- );
890
- }
891
- for (const chainId of options.chains) {
892
- if (!isValidChainId(chainId)) {
893
- throw new APIError(
894
- `Invalid chainId in chains array: ${chainId}. Must be a positive integer.`,
895
- { endpoint: "/v1/earn-sdk/supported-assets-by-chains" }
896
- );
897
- }
898
- }
899
- }
900
- if (options.yieldType !== void 0 && !isValidYieldType(options.yieldType)) {
901
- throw new APIError(
902
- `Invalid yieldType: ${options.yieldType}. Must be one of: CORE, TREASURY, FRONTIER.`,
903
- { endpoint: "/v1/earn-sdk/supported-assets-by-chains" }
904
- );
905
- }
906
- if (options.address !== void 0 && !isValidAddress(options.address)) {
907
- throw new APIError(
908
- `Invalid address: ${options.address}. Must be a valid Ethereum address.`,
909
- { endpoint: "/v1/earn-sdk/supported-assets-by-chains" }
910
- );
911
- }
912
- if (options.symbol !== void 0 && typeof options.symbol !== "string") {
913
- throw new APIError(`Invalid symbol: ${options.symbol}. Must be a string.`, {
914
- endpoint: "/v1/earn-sdk/supported-assets-by-chains"
915
- });
916
- }
917
- }
918
- async function fetchVaultDataFromGraphQL() {
919
- const endpoint = getGraphQLEndpoint();
920
- const response = await fetchAmplifySdkConfigs(endpoint);
921
- const configs = response.amplifySdkConfigs;
922
- const vaults = aggregateVaultConfigs(configs);
923
- const assets = extractSupportedAssetsFromConfigs(configs);
924
- return { vaults, assets };
925
- }
926
- function applyVaultFilters(vaults, options) {
927
- if (!options) return vaults;
928
- let filtered = vaults;
929
- if (options.chainId !== void 0) {
930
- filtered = filtered.filter((v) => v.chainId === options.chainId);
931
- }
932
- if (options.yieldType) {
933
- filtered = filtered.filter((v) => v.yieldType === options.yieldType);
934
- }
935
- if (options.depositTokenAddress) {
936
- const normalized = options.depositTokenAddress.toLowerCase();
937
- filtered = filtered.filter(
938
- (v) => v.supportedAssets.some((a) => a.address.toLowerCase() === normalized)
939
- );
940
- }
941
- return filtered;
942
- }
943
- function applyAssetFilters(assets, options) {
944
- if (!options) return assets;
945
- let filtered = assets;
946
- if (options.chains !== void 0 && options.chains.length > 0) {
947
- filtered = filtered.filter(
948
- (asset) => options.chains?.some((chainId) => asset.chains.includes(chainId))
949
- );
950
- }
951
- if (options.address) {
952
- const normalizedAddress = options.address.toLowerCase();
953
- filtered = filtered.filter(
954
- (asset) => asset.address.toLowerCase() === normalizedAddress
955
- );
956
- }
957
- if (options.symbol) {
958
- filtered = filtered.filter((asset) => asset.symbol === options.symbol);
959
- }
960
- return filtered;
961
- }
962
- async function getVaults(options) {
963
- validateVaultFilterOptions(options);
964
- const cache = getCache();
965
- if (cache.isEmpty() || cache.isExpired()) {
966
- await cache.refresh();
967
- }
968
- return applyVaultFilters(cache.getAllVaults(), options);
969
- }
970
- async function getSupportedAssets(options) {
971
- validateAssetFilterOptions(options);
972
- const cache = getCache();
973
- if (cache.isEmpty() || cache.isExpired()) {
974
- await cache.refresh();
975
- }
976
- return applyAssetFilters(cache.getAllAssets(), options);
977
- }
978
- var globalCache = null;
979
- function initializeCache(ttl) {
980
- globalCache = new VaultCache(ttl);
981
- return globalCache;
982
- }
983
- function getCache() {
984
- if (!globalCache) {
985
- globalCache = new VaultCache();
986
- }
987
- return globalCache;
988
- }
989
- async function refreshVaultCache() {
990
- const cache = getCache();
991
- await cache.refresh();
992
- }
993
- function clearCache() {
994
- const cache = getCache();
995
- cache.clear();
996
- }
997
- async function getAssetsFromCache(options) {
998
- return getSupportedAssets(options);
999
- }
1000
- async function findVaultByConfig(params) {
1001
- if (!isValidAddress(params.assetAddress)) {
1002
- throw new APIError(
1003
- `Invalid assetAddress: ${params.assetAddress}. Must be a valid Ethereum address.`,
1004
- { endpoint: "findVaultByConfig" }
1005
- );
1006
- }
1007
- if (!isValidYieldType(params.yieldType)) {
1008
- throw new APIError(
1009
- `Invalid yieldType: ${params.yieldType}. Must be one of: CORE, TREASURY, FRONTIER.`,
1010
- { endpoint: "findVaultByConfig" }
1011
- );
1012
- }
1013
- if (!isValidChainId(params.chainId)) {
1014
- throw new APIError(
1015
- `Invalid chainId: ${params.chainId}. Must be a positive integer.`,
1016
- { endpoint: "findVaultByConfig" }
1017
- );
1018
- }
1019
- const cache = getCache();
1020
- if (cache.isEmpty() || cache.isExpired()) {
1021
- await cache.refresh();
1022
- }
1023
- const normalizedAddress = params.assetAddress.toLowerCase();
1024
- let vaultsByToken = cache.getVault(params.assetAddress);
1025
- if (!vaultsByToken) {
1026
- vaultsByToken = cache.getVault(normalizedAddress);
1027
- }
1028
- if (!vaultsByToken) {
1029
- const allVaults = cache.getAllVaults();
1030
- const matchingVaults = allVaults.filter(
1031
- (vault) => vault.supportedAssets.some(
1032
- (a) => a.address.toLowerCase() === normalizedAddress
1033
- )
1034
- );
1035
- vaultsByToken = matchingVaults.length > 0 ? matchingVaults : void 0;
1036
- }
1037
- if (!vaultsByToken || vaultsByToken.length === 0) {
1038
- return null;
1039
- }
1040
- const matchingVault = vaultsByToken.find(
1041
- (vault) => vault.yieldType === params.yieldType && vault.chainId === params.chainId
1042
- );
1043
- return matchingVault || null;
1044
- }
1045
- async function getVaultsByConfig(params) {
1046
- const cache = getCache();
1047
- if (cache.isEmpty() || cache.isExpired()) {
1048
- await cache.refresh();
1049
- }
1050
- let vaults = cache.getAllVaults();
1051
- if (params?.yieldType) {
1052
- if (!isValidYieldType(params.yieldType)) {
1053
- throw new APIError(
1054
- `Invalid yieldType: ${params.yieldType}. Must be one of: CORE, TREASURY, FRONTIER.`,
1055
- { endpoint: "getVaultsByConfig" }
1056
- );
1057
- }
1058
- vaults = vaults.filter((v) => v.yieldType === params.yieldType);
1059
- }
1060
- if (params?.chainId !== void 0) {
1061
- if (!isValidChainId(params.chainId)) {
1062
- throw new APIError(
1063
- `Invalid chainId: ${params.chainId}. Must be a positive integer.`,
1064
- { endpoint: "getVaultsByConfig" }
1065
- );
1066
- }
1067
- vaults = vaults.filter((v) => v.chainId === params.chainId);
1068
- }
1069
- if (params?.depositAssetAddress) {
1070
- if (!isValidAddress(params.depositAssetAddress)) {
1071
- throw new APIError(
1072
- `Invalid depositAssetAddress: ${params.depositAssetAddress}.`,
1073
- { endpoint: "getVaultsByConfig" }
1074
- );
1075
- }
1076
- const normalized = params.depositAssetAddress.toLowerCase();
1077
- vaults = vaults.filter(
1078
- (v) => v.supportedAssets.some((a) => a.address.toLowerCase() === normalized)
1079
- );
1080
- }
1081
- if (params?.withdrawAssetAddress) {
1082
- if (!isValidAddress(params.withdrawAssetAddress)) {
1083
- throw new APIError(
1084
- `Invalid withdrawAssetAddress: ${params.withdrawAssetAddress}.`,
1085
- { endpoint: "getVaultsByConfig" }
1086
- );
1087
- }
1088
- const normalized = params.withdrawAssetAddress.toLowerCase();
1089
- vaults = vaults.filter(
1090
- (v) => v.supportedAssets.some(
1091
- (a) => a.withdrawable !== false && a.address.toLowerCase() === normalized
1092
- )
1093
- );
1094
- }
1095
- if (params?.settlementAssetAddress) {
1096
- if (!isValidAddress(params.settlementAssetAddress)) {
1097
- throw new APIError(
1098
- `Invalid settlementAssetAddress: ${params.settlementAssetAddress}.`,
1099
- { endpoint: "getVaultsByConfig" }
1100
- );
1101
- }
1102
- const normalized = params.settlementAssetAddress.toLowerCase();
1103
- vaults = vaults.filter(
1104
- (v) => v.supportedAssets.some((a) => a.address.toLowerCase() === normalized)
1105
- );
1106
- }
1107
- return vaults;
1108
- }
1109
- async function getWithdrawSupportedAssets() {
1110
- const cache = getCache();
1111
- if (cache.isEmpty() || cache.isExpired()) {
1112
- await cache.refresh();
1113
- }
1114
- const vaults = cache.getAllVaults();
1115
- const assets = cache.getAllAssets();
1116
- const result = [];
1117
- const assetMap = /* @__PURE__ */ new Map();
1118
- for (const asset of assets) {
1119
- assetMap.set(asset.address.toLowerCase(), asset);
1120
- }
1121
- const assetVaultMap = /* @__PURE__ */ new Map();
1122
- for (const vault of vaults) {
1123
- for (const asset of vault.supportedAssets) {
1124
- const assetAddress = asset.address.toLowerCase();
1125
- if (!assetVaultMap.has(assetAddress)) {
1126
- assetVaultMap.set(assetAddress, []);
1127
- }
1128
- assetVaultMap.get(assetAddress)?.push({
1129
- id: vault.id,
1130
- yieldType: vault.yieldType,
1131
- chainId: vault.chainId,
1132
- vaultId: vault.id
1133
- });
1134
- }
1135
- }
1136
- for (const [assetAddress, vaultsData] of assetVaultMap.entries()) {
1137
- const asset = assetMap.get(assetAddress);
1138
- if (asset) {
1139
- result.push({
1140
- address: asset.address,
1141
- symbol: asset.symbol,
1142
- decimals: asset.decimals,
1143
- vaults: vaultsData
1144
- });
1145
- }
1146
- }
1147
- return result;
1148
- }
1149
- var hyperEvm = defineChain({
1150
- id: 999,
1151
- name: "HyperEVM",
1152
- nativeCurrency: {
1153
- decimals: 18,
1154
- name: "Hyperliquid",
1155
- symbol: "HYPE"
1156
- },
1157
- rpcUrls: {
1158
- default: {
1159
- http: ["https://rpc.hyperliquid.xyz/evm"],
1160
- webSocket: ["wss://hyperliquid.drpc.org"]
1161
- }
1162
- },
1163
- blockExplorers: {
1164
- default: { name: "Explorer", url: "https://purrsec.com/" }
1165
- },
1166
- contracts: {
1167
- multicall3: {
1168
- address: "0xcA11bde05977b3631167028862bE2a173976CA11",
1169
- blockCreated: 13051
1170
- }
1171
- }
1172
- });
1173
- var stableTestnet = defineChain({
1174
- id: 2201,
1175
- name: "Stable Testnet",
1176
- nativeCurrency: {
1177
- decimals: 18,
1178
- name: "Ether",
1179
- symbol: "ETH"
1180
- },
1181
- rpcUrls: {
1182
- default: {
1183
- http: ["https://rpc.testnet.stable.xyz"]
1184
- }
1185
- },
1186
- blockExplorers: {
1187
- default: { name: "Stablescan", url: "https://testnet.stablescan.xyz" }
1188
- },
1189
- contracts: {
1190
- multicall3: {
1191
- address: "0xcA11bde05977b3631167028862bE2a173976CA11",
1192
- blockCreated: 0
1193
- }
1194
- },
1195
- testnet: true
1196
- });
1197
- var CHAIN_ID_TO_CHAIN_MAP = {
1198
- [mainnet.id]: mainnet,
1199
- [sepolia.id]: sepolia,
1200
- [base.id]: base,
1201
- [hyperEvm.id]: hyperEvm,
1202
- [stableTestnet.id]: stableTestnet
1203
- };
1204
-
1205
- // src/utils/chain-utils.ts
1206
- function toChainId(value) {
1207
- return typeof value === "number" ? value : Number(value);
1208
- }
1209
- var chainsCache = null;
1210
- async function getChainFromConfig(chainId, config) {
1211
- if (chainsCache && !config) {
1212
- const chain2 = chainsCache.get(Number(chainId));
1213
- if (chain2) {
1214
- return chain2;
1215
- }
1216
- }
1217
- const vaults = config ?? await getVaults();
1218
- const numericChainId = Number(chainId);
1219
- const knownChain = CHAIN_ID_TO_CHAIN_MAP[numericChainId];
1220
- const vaultForChain = vaults.find((v) => v.chainId === numericChainId);
1221
- if (!knownChain && !vaultForChain) {
1222
- throw new Error(`Vault not found for ID: ${chainId}`);
1223
- }
1224
- if (config && !chainsCache) {
1225
- const cache = /* @__PURE__ */ new Map();
1226
- for (const v of vaults) {
1227
- const chainConfig = CHAIN_ID_TO_CHAIN_MAP[v.chainId];
1228
- if (!chainConfig) {
1229
- throw new Error(`Chain config not found for ID: ${v.chainId}`);
1230
- }
1231
- cache.set(v.chainId, {
1232
- id: v.chainId,
1233
- name: chainConfig.name,
1234
- nativeCurrency: chainConfig.nativeCurrency,
1235
- rpcUrls: chainConfig.rpcUrls,
1236
- contracts: chainConfig.contracts
1237
- });
1238
- }
1239
- if (knownChain && !cache.has(numericChainId)) {
1240
- cache.set(numericChainId, {
1241
- id: numericChainId,
1242
- name: knownChain.name,
1243
- nativeCurrency: knownChain.nativeCurrency,
1244
- rpcUrls: knownChain.rpcUrls,
1245
- contracts: knownChain.contracts
1246
- });
1247
- }
1248
- chainsCache = cache;
1249
- }
1250
- const chain = chainsCache ? chainsCache.get(numericChainId) : void 0;
1251
- if (!chain) {
1252
- throw new Error(`Chain not found for ID: ${chainId}`);
1253
- }
1254
- return chain;
1255
- }
1256
- function getChainNameById(chainId) {
1257
- return CHAIN_ID_TO_CHAIN_MAP[chainId]?.name || null;
1258
- }
1259
- function clearChainsCache() {
1260
- chainsCache = null;
1261
- }
1262
-
1263
- // src/lib/viem/client.ts
1264
- var clients = /* @__PURE__ */ new Map();
1265
- var warnedChains = /* @__PURE__ */ new Set();
1266
- var getClient = async (chainId) => {
1267
- if (!chainId) {
1268
- throw new Error("Chain ID is required");
1269
- }
1270
- const numericChainId = toChainId(chainId);
1271
- if (!clients.has(numericChainId)) {
1272
- const chain = await getChainFromConfig(numericChainId, await getVaults());
1273
- const customRpcUrl = getRpcUrls()[numericChainId];
1274
- if (!customRpcUrl && !warnedChains.has(numericChainId)) {
1275
- warnedChains.add(numericChainId);
1276
- getLogger().warn(
1277
- `No custom RPC URL provided for chain ${numericChainId} (${chain.name}). Using default public RPC. Public RPCs may be unreliable \u2014 pass rpcUrls in initAmplifySDK() options for better reliability.`
1278
- );
1279
- }
1280
- const rpcUrl = customRpcUrl ?? chain.rpcUrls.default.http[0];
1281
- const client2 = createPublicClient({
1282
- chain,
1283
- transport: http(rpcUrl)
1284
- });
1285
- clients.set(numericChainId, client2);
1286
- }
1287
- const client = clients.get(numericChainId);
1288
- if (!client) {
1289
- throw new Error(`Client not found for chain ID ${chainId}`);
1290
- }
1291
- return client;
1292
- };
1293
- function clearClients() {
1294
- clients.clear();
1295
- warnedChains.clear();
1296
- }
1297
-
1298
- // src/lib/sdk-config.ts
1299
- var MAX_CACHE_RETRIES = 3;
1300
- var RETRY_DELAYS = [1e3, 2e3, 4e3];
1301
- var sdkConfig = {
1302
- apiKey: null,
1303
- isInitialized: false,
1304
- initializedAt: null,
1305
- isInitializing: false,
1306
- initPromise: null,
1307
- telemetryEnabled: true,
1308
- cachePopulationPromise: null,
1309
- isCacheReady: false,
1310
- rpcUrls: {}
1311
- };
1312
- var ERROR_MESSAGES = {
1313
- INVALID_API_KEY: "Invalid API key format. Expected format: pxl_<type>_<string>",
1314
- EMPTY_API_KEY: "API key cannot be empty. Provide a valid API key from Paxos Labs.",
1315
- UNKNOWN_ERROR: "Unexpected initialization error. Contact support if issue persists."};
1316
- function validateAPIKey(apiKey) {
1317
- if (!apiKey || apiKey.trim().length === 0) {
1318
- throw new APIError(ERROR_MESSAGES.EMPTY_API_KEY, {
1319
- endpoint: "initAmplifySDK",
1320
- statusCode: void 0
1321
- });
1322
- }
1323
- if (apiKey.length < 10) {
1324
- throw new APIError(ERROR_MESSAGES.INVALID_API_KEY, {
1325
- endpoint: "initAmplifySDK",
1326
- statusCode: void 0
1327
- });
1328
- }
1329
- if (!apiKey.startsWith("pxl_")) {
1330
- getLogger().warn(
1331
- 'API key does not start with expected prefix "pxl_". It may not be valid.'
1332
- );
1333
- }
1334
- }
1335
- function setAPIKey(apiKey) {
1336
- sdkConfig.apiKey = apiKey;
1337
- }
1338
- function getAPIKey() {
1339
- return sdkConfig.apiKey;
1340
- }
1341
- function getRpcUrls() {
1342
- return { ...sdkConfig.rpcUrls };
1343
- }
1344
- function clearConfig() {
1345
- sdkConfig.apiKey = null;
1346
- sdkConfig.isInitialized = false;
1347
- sdkConfig.initializedAt = null;
1348
- sdkConfig.isInitializing = false;
1349
- sdkConfig.initPromise = null;
1350
- sdkConfig.telemetryEnabled = true;
1351
- sdkConfig.cachePopulationPromise = null;
1352
- sdkConfig.isCacheReady = false;
1353
- sdkConfig.rpcUrls = {};
1354
- clearClients();
1355
- resetTelemetry();
1356
- }
1357
- function getRequestHeaders() {
1358
- const apiKey = getAPIKey();
1359
- const headers = {
1360
- "Content-Type": "application/json"
1361
- };
1362
- if (apiKey) {
1363
- headers["x-api-key"] = apiKey;
1364
- }
1365
- return headers;
1366
- }
1367
- async function populateCacheInBackground(cache) {
1368
- const logger = getLogger();
1369
- for (let attempt = 0; attempt < MAX_CACHE_RETRIES; attempt++) {
1370
- try {
1371
- await cache.refresh();
1372
- sdkConfig.isCacheReady = true;
1373
- logger.debug("Background cache population completed", {
1374
- attempt: attempt + 1
1375
- });
1376
- return;
1377
- } catch (error) {
1378
- const isLastAttempt = attempt === MAX_CACHE_RETRIES - 1;
1379
- if (isLastAttempt) {
1380
- logger.warn(
1381
- "Background cache population failed after all retries, falling back to on-demand fetching",
1382
- {
1383
- error: error instanceof Error ? error.message : String(error),
1384
- attempts: MAX_CACHE_RETRIES
1385
- }
1386
- );
1387
- return;
1388
- }
1389
- logger.debug(
1390
- `Cache population attempt ${attempt + 1} failed, retrying...`,
1391
- {
1392
- error: error instanceof Error ? error.message : String(error),
1393
- nextRetryIn: RETRY_DELAYS[attempt]
1394
- }
1395
- );
1396
- await new Promise((resolve) => setTimeout(resolve, RETRY_DELAYS[attempt]));
1397
- }
1398
- }
1399
- }
1400
- function isCacheReady() {
1401
- return sdkConfig.isCacheReady;
1402
- }
1403
- async function waitForCacheReady() {
1404
- if (sdkConfig.isCacheReady) {
1405
- return;
1406
- }
1407
- if (sdkConfig.cachePopulationPromise) {
1408
- await sdkConfig.cachePopulationPromise;
1409
- if (sdkConfig.isCacheReady) {
1410
- return;
1411
- }
1412
- }
1413
- const cache = getCache();
1414
- await cache.refresh();
1415
- sdkConfig.isCacheReady = true;
1416
- }
1417
- async function initAmplifySDK(apiKey, options) {
1418
- validateAPIKey(apiKey);
1419
- if (sdkConfig.isInitialized && sdkConfig.apiKey === apiKey) {
1420
- const nextRpcUrls = options?.rpcUrls ?? {};
1421
- const hasRpcUrlChange = Object.keys(nextRpcUrls).length !== Object.keys(sdkConfig.rpcUrls).length || Object.entries(nextRpcUrls).some(
1422
- ([chainId, url]) => sdkConfig.rpcUrls[Number(chainId)] !== url
1423
- );
1424
- if (hasRpcUrlChange) {
1425
- sdkConfig.rpcUrls = { ...nextRpcUrls };
1426
- clearClients();
1427
- }
1428
- return;
1429
- }
1430
- if (sdkConfig.isInitializing && sdkConfig.initPromise) {
1431
- await sdkConfig.initPromise;
1432
- return;
1433
- }
1434
- if (sdkConfig.isInitialized && sdkConfig.apiKey !== apiKey) {
1435
- clearConfig();
1436
- clearCache();
1437
- }
1438
- if (options?.logger) {
1439
- setLogger(options.logger);
1440
- }
1441
- if (options?.logLevel !== void 0) {
1442
- setLogLevel(options.logLevel);
1443
- }
1444
- sdkConfig.telemetryEnabled = options?.telemetry !== false;
1445
- sdkConfig.rpcUrls = options?.rpcUrls ?? {};
1446
- sdkConfig.isInitializing = true;
1447
- sdkConfig.initPromise = (async () => {
1448
- try {
1449
- setAPIKey(apiKey);
1450
- const cache = initializeCache();
1451
- sdkConfig.isInitialized = true;
1452
- sdkConfig.initializedAt = /* @__PURE__ */ new Date();
1453
- sdkConfig.isCacheReady = false;
1454
- sdkConfig.cachePopulationPromise = populateCacheInBackground(cache);
1455
- if (sdkConfig.telemetryEnabled) {
1456
- initTelemetry(apiKey).catch(() => {
1457
- });
1458
- }
1459
- getLogger().info(
1460
- "Amplify SDK initialized (cache populating in background)"
1461
- );
1462
- } catch (error) {
1463
- clearConfig();
1464
- clearCache();
1465
- if (error instanceof APIError) {
1466
- throw error;
1467
- }
1468
- throw new APIError(ERROR_MESSAGES.UNKNOWN_ERROR, {
1469
- endpoint: "initAmplifySDK",
1470
- cause: error
1471
- });
1472
- } finally {
1473
- sdkConfig.isInitializing = false;
1474
- sdkConfig.initPromise = null;
1475
- }
1476
- })();
1477
- await sdkConfig.initPromise;
1478
- }
1479
-
1480
- export { APIError, API_BASE_URL, CHAINLINK_ADDRESS, DEFAULT_APPROVAL_AMOUNT, DEFAULT_DEADLINE, DEFAULT_SLIPPAGE_BPS, DEFAULT_TIMEOUT, LogLevel, YieldType, __publicField, clearChainsCache, createTimeoutSignal, findVaultByConfig, getAssetsFromCache, getCache, getChainFromConfig, getChainNameById, getClient, getLogger, getRequestHeaders, getRestV2BaseURL, getSupportedAssets, getVaults, getVaultsByConfig, getWithdrawSupportedAssets, initAmplifySDK, initializeCache, isCacheReady, isValidAddress, isValidChainId, refreshVaultCache, setLogLevel, setLogger, toChainId, waitForCacheReady };
1481
- //# sourceMappingURL=chunk-WK7EJRBB.mjs.map
1482
- //# sourceMappingURL=chunk-WK7EJRBB.mjs.map