ton-provider-system 0.1.6 → 0.1.7

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.
package/dist/index.d.cts CHANGED
@@ -1474,7 +1474,7 @@ declare function getTonClient(manager: ProviderManager): Promise<TonClient>;
1474
1474
  */
1475
1475
  declare function getTonClientWithRateLimit(manager: ProviderManager): Promise<{
1476
1476
  client: TonClient;
1477
- withRateLimit: <T>(fn: () => Promise<T>) => Promise<T>;
1477
+ withRateLimit: <T>(fn: () => Promise<T>, maxRetries?: number) => Promise<T>;
1478
1478
  }>;
1479
1479
  /**
1480
1480
  * Get TonClient for network (one-shot convenience)
package/dist/index.d.ts CHANGED
@@ -1474,7 +1474,7 @@ declare function getTonClient(manager: ProviderManager): Promise<TonClient>;
1474
1474
  */
1475
1475
  declare function getTonClientWithRateLimit(manager: ProviderManager): Promise<{
1476
1476
  client: TonClient;
1477
- withRateLimit: <T>(fn: () => Promise<T>) => Promise<T>;
1477
+ withRateLimit: <T>(fn: () => Promise<T>, maxRetries?: number) => Promise<T>;
1478
1478
  }>;
1479
1479
  /**
1480
1480
  * Get TonClient for network (one-shot convenience)
package/dist/index.js CHANGED
@@ -2556,16 +2556,42 @@ async function getTonClient(manager) {
2556
2556
  async function getTonClientWithRateLimit(manager) {
2557
2557
  const adapter = new NodeAdapter(manager);
2558
2558
  const client = await adapter.getClient();
2559
- const withRateLimit = async (fn) => {
2560
- await manager.getEndpointWithRateLimit();
2561
- try {
2562
- const result = await fn();
2563
- manager.reportSuccess();
2564
- return result;
2565
- } catch (error) {
2566
- manager.reportError(error);
2567
- throw error;
2559
+ const withRateLimit = async (fn, maxRetries = 3) => {
2560
+ let lastError;
2561
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
2562
+ try {
2563
+ await manager.getEndpointWithRateLimit(6e4);
2564
+ const result = await fn();
2565
+ manager.reportSuccess();
2566
+ return result;
2567
+ } catch (error) {
2568
+ lastError = error;
2569
+ const errorMsg = error?.message || String(error) || "";
2570
+ const is429 = errorMsg.includes("429") || errorMsg.includes("rate limit") || error?.status === 429 || error?.response?.status === 429;
2571
+ if (is429 && attempt < maxRetries) {
2572
+ manager.reportError(error);
2573
+ const managerState = manager.getState();
2574
+ const provider = manager.getActiveProvider();
2575
+ let backoff = 1e3;
2576
+ if (provider && managerState.providers) {
2577
+ const providerState = managerState.providers.get(provider.id);
2578
+ if (providerState?.rateLimit?.currentBackoff) {
2579
+ backoff = providerState.rateLimit.currentBackoff;
2580
+ }
2581
+ }
2582
+ const additionalDelay = Math.min(attempt * 500, 2e3);
2583
+ const waitTime = backoff + additionalDelay;
2584
+ console.warn(
2585
+ `[ProviderSystem] Rate limit error (429), retrying in ${waitTime}ms (attempt ${attempt + 1}/${maxRetries + 1})`
2586
+ );
2587
+ await sleep(waitTime);
2588
+ continue;
2589
+ }
2590
+ manager.reportError(error);
2591
+ throw error;
2592
+ }
2568
2593
  }
2594
+ throw lastError || new Error("Rate limit retries exhausted");
2569
2595
  };
2570
2596
  return { client, withRateLimit };
2571
2597
  }