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