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