ton-provider-system 0.3.1 → 0.4.0

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
@@ -2,6 +2,7 @@
2
2
 
3
3
  var zod = require('zod');
4
4
  var ton = require('@ton/ton');
5
+ var core = require('@ton/core');
5
6
 
6
7
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
7
8
  // src/types.ts
@@ -3249,6 +3250,74 @@ var NodeAdapter = class {
3249
3250
  };
3250
3251
  }
3251
3252
  // ========================================================================
3253
+ // Failover-aware high-level reads
3254
+ // ========================================================================
3255
+ /**
3256
+ * Get account transactions with provider failover.
3257
+ *
3258
+ * Unlike `getClient().getTransactions(...)`, this loops across the network's
3259
+ * providers: a provider that passes the `getMasterchainInfo` health probe but
3260
+ * fails the v2 `getTransactions` call (e.g. Chainstack/Orbs testnet, which
3261
+ * 403 on transaction reads) is no longer able to permanently break this path.
3262
+ *
3263
+ * Semantics:
3264
+ * 1. Pick the current best provider (the selector's scored top).
3265
+ * 2. Acquire a rate-limit token for THAT provider, bind a short-lived
3266
+ * `TonClient` to its endpoint, and call `getTransactions`.
3267
+ * 3. On success → `reportSuccess()` and return.
3268
+ * 4. On error → `reportError()` (marks the provider success:false, scoring 0
3269
+ * within its cooldown, and clears the selection cache) so the next pick is
3270
+ * a DIFFERENT, still-eligible provider, then retry.
3271
+ * 5. When every candidate has been tried, re-throw the last error so the
3272
+ * caller's retry/dead-letter logic still triggers — but only after a
3273
+ * genuine failover across all providers.
3274
+ *
3275
+ * Returns the same `Transaction[]` `TonClient.getTransactions` returns, so a
3276
+ * consumer can swap `client.getTransactions(addr, opts)` for
3277
+ * `adapter.getTransactions(addr, opts)` with no shape change.
3278
+ */
3279
+ async getTransactions(address, opts = {}, timeoutMs = 15e3) {
3280
+ const network = this.manager.getNetwork();
3281
+ if (!network) {
3282
+ throw new Error("ProviderManager not initialized");
3283
+ }
3284
+ const addr = typeof address === "string" ? core.Address.parse(address) : address;
3285
+ const reqOpts = { limit: opts.limit ?? 20, ...opts };
3286
+ const maxAttempts = Math.max(1, this.manager.getProviders().length);
3287
+ const rateLimiter = this.manager.getRateLimiter();
3288
+ const tried = /* @__PURE__ */ new Set();
3289
+ let lastError;
3290
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
3291
+ const provider = this.manager.getActiveProvider();
3292
+ if (!provider) break;
3293
+ if (tried.has(provider.id)) break;
3294
+ tried.add(provider.id);
3295
+ if (rateLimiter) {
3296
+ await rateLimiter.acquire(provider.id, timeoutMs);
3297
+ }
3298
+ const endpoint = await this.manager.getEndpoint();
3299
+ const apiKey = this.manager.getActiveProvider()?.apiKey;
3300
+ const client = new ton.TonClient({ endpoint, apiKey });
3301
+ try {
3302
+ const result = await withTimeout(
3303
+ client.getTransactions(addr, reqOpts),
3304
+ timeoutMs,
3305
+ `getTransactions(${provider.id})`
3306
+ );
3307
+ this.manager.reportSuccess();
3308
+ return result;
3309
+ } catch (error) {
3310
+ lastError = error;
3311
+ this.logger.warn(
3312
+ `getTransactions failed on ${provider.id}, failing over`,
3313
+ { error: error?.message || String(error) }
3314
+ );
3315
+ this.manager.reportError(error);
3316
+ }
3317
+ }
3318
+ throw lastError || new Error(`getTransactions: no available provider for ${network}`);
3319
+ }
3320
+ // ========================================================================
3252
3321
  // Direct REST API Methods
3253
3322
  // ========================================================================
3254
3323
  /**