ton-provider-system 0.1.5 → 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
@@ -1315,6 +1315,8 @@ var TokenBucketRateLimiter = class {
1315
1315
  if (this.currentBackoff > 0) {
1316
1316
  this.logger.debug(`Applying backoff: ${this.currentBackoff}ms`);
1317
1317
  await sleep(this.currentBackoff);
1318
+ this.lastRefill = Date.now();
1319
+ this.currentBackoff = 0;
1318
1320
  }
1319
1321
  while (this.tokens <= 0) {
1320
1322
  if (Date.now() - startTime > timeoutMs) {
@@ -2554,16 +2556,42 @@ async function getTonClient(manager) {
2554
2556
  async function getTonClientWithRateLimit(manager) {
2555
2557
  const adapter = new NodeAdapter(manager);
2556
2558
  const client = await adapter.getClient();
2557
- const withRateLimit = async (fn) => {
2558
- await manager.getEndpointWithRateLimit();
2559
- try {
2560
- const result = await fn();
2561
- manager.reportSuccess();
2562
- return result;
2563
- } catch (error) {
2564
- manager.reportError(error);
2565
- 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
+ }
2566
2593
  }
2594
+ throw lastError || new Error("Rate limit retries exhausted");
2567
2595
  };
2568
2596
  return { client, withRateLimit };
2569
2597
  }