ton-provider-system 0.2.1 → 0.2.4
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/README.md +111 -14
- package/dist/index.cjs +222 -71
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +37 -2
- package/dist/index.d.ts +37 -2
- package/dist/index.js +222 -71
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/rpc.json +1 -1
package/README.md
CHANGED
|
@@ -53,17 +53,23 @@ Or add to your `package.json`:
|
|
|
53
53
|
### Node.js (Scripts, Telegram Bot)
|
|
54
54
|
|
|
55
55
|
```typescript
|
|
56
|
-
import { ProviderManager, getTonClient } from 'ton-provider-system';
|
|
56
|
+
import { ProviderManager, getTonClient, NodeAdapter } from 'ton-provider-system';
|
|
57
57
|
|
|
58
58
|
// Initialize
|
|
59
59
|
const pm = ProviderManager.getInstance();
|
|
60
60
|
await pm.init('testnet');
|
|
61
61
|
|
|
62
|
-
//
|
|
63
|
-
const
|
|
62
|
+
// Option 1: Use adapter (RECOMMENDED - handles rate limiting automatically)
|
|
63
|
+
const adapter = new NodeAdapter(pm);
|
|
64
|
+
const balance = await adapter.getAddressBalance(address);
|
|
65
|
+
const state = await adapter.getAddressState(address);
|
|
64
66
|
|
|
65
|
-
// Use
|
|
66
|
-
const
|
|
67
|
+
// Option 2: Use TonClient with rate limiting
|
|
68
|
+
const client = await getTonClient(pm);
|
|
69
|
+
// Always use getEndpointWithRateLimit() before operations
|
|
70
|
+
const endpoint = await pm.getEndpointWithRateLimit();
|
|
71
|
+
// Note: TonClient doesn't automatically respect rate limits
|
|
72
|
+
// Consider using adapter methods instead
|
|
67
73
|
```
|
|
68
74
|
|
|
69
75
|
### Browser (React/Next.js)
|
|
@@ -72,12 +78,16 @@ const balance = await client.getBalance(address);
|
|
|
72
78
|
import { ProviderManager, BrowserAdapter } from 'ton-provider-system';
|
|
73
79
|
|
|
74
80
|
// Create instance (not singleton for React)
|
|
81
|
+
// IMPORTANT: Use 'browser' adapter to filter CORS-incompatible providers
|
|
75
82
|
const pm = new ProviderManager({ adapter: 'browser' });
|
|
76
83
|
await pm.init(network);
|
|
77
84
|
|
|
78
85
|
// Use browser adapter for fetch-based operations
|
|
86
|
+
// Adapter methods automatically handle rate limiting
|
|
79
87
|
const adapter = new BrowserAdapter(pm);
|
|
80
88
|
const balance = await adapter.getAddressBalance(address);
|
|
89
|
+
const state = await adapter.getAddressState(address);
|
|
90
|
+
const result = await adapter.runGetMethod(address, 'method', []);
|
|
81
91
|
```
|
|
82
92
|
|
|
83
93
|
## Configuration
|
|
@@ -140,10 +150,11 @@ const pm = new ProviderManager({ adapter: 'browser' });
|
|
|
140
150
|
await pm.init('testnet');
|
|
141
151
|
await pm.init('mainnet');
|
|
142
152
|
|
|
143
|
-
// Get endpoint URL
|
|
153
|
+
// Get endpoint URL (no rate limiting - use for one-off requests)
|
|
144
154
|
const endpoint = await pm.getEndpoint();
|
|
145
155
|
|
|
146
|
-
// Get endpoint with rate limiting
|
|
156
|
+
// Get endpoint with rate limiting (RECOMMENDED for production)
|
|
157
|
+
// Waits for rate limit token before returning endpoint
|
|
147
158
|
const endpoint = await pm.getEndpointWithRateLimit(5000);
|
|
148
159
|
|
|
149
160
|
// Test all providers
|
|
@@ -394,18 +405,104 @@ pnpm test:verbose
|
|
|
394
405
|
|
|
395
406
|
### No providers available
|
|
396
407
|
|
|
397
|
-
|
|
398
|
-
|
|
408
|
+
**Symptoms**: `No providers available, using fallback` warning
|
|
409
|
+
|
|
410
|
+
**Solutions**:
|
|
411
|
+
1. Check `.env` file has API keys configured for at least one provider
|
|
412
|
+
2. Verify environment variables are loaded (use `dotenv` or similar)
|
|
413
|
+
3. Run `pnpm test` to test all providers
|
|
414
|
+
4. Check provider health: `const results = await pm.testAllProviders()`
|
|
399
415
|
|
|
400
|
-
### Rate limit errors
|
|
416
|
+
### Rate limit errors (429)
|
|
401
417
|
|
|
402
|
-
|
|
403
|
-
|
|
418
|
+
**Symptoms**: Frequent 429 errors, requests failing
|
|
419
|
+
|
|
420
|
+
**Solutions**:
|
|
421
|
+
1. **Use `getEndpointWithRateLimit()`** instead of `getEndpoint()` - this is the recommended approach
|
|
422
|
+
2. Use adapter methods (`adapter.getAddressState()`) which automatically handle rate limiting
|
|
423
|
+
3. The system automatically switches to next provider on 429 errors
|
|
424
|
+
4. Configure more providers in `.env` for redundancy
|
|
425
|
+
5. Check RPS limits in `rpc.json` - some providers have very low limits (e.g., Tatum: 3 RPS)
|
|
426
|
+
|
|
427
|
+
**Example**:
|
|
428
|
+
```typescript
|
|
429
|
+
// ❌ BAD - bypasses rate limiting
|
|
430
|
+
const endpoint = await pm.getEndpoint();
|
|
431
|
+
const client = new TonClient({ endpoint });
|
|
432
|
+
await client.getBalance(address); // May hit rate limit
|
|
433
|
+
|
|
434
|
+
// ✅ GOOD - respects rate limiting
|
|
435
|
+
const endpoint = await pm.getEndpointWithRateLimit();
|
|
436
|
+
const client = new TonClient({ endpoint });
|
|
437
|
+
await client.getBalance(address);
|
|
438
|
+
|
|
439
|
+
// ✅ BEST - adapter handles everything
|
|
440
|
+
const adapter = new NodeAdapter(pm);
|
|
441
|
+
const balance = await adapter.getAddressBalance(address);
|
|
442
|
+
```
|
|
404
443
|
|
|
405
444
|
### Block height mismatch (stale provider)
|
|
406
445
|
|
|
407
|
-
|
|
408
|
-
|
|
446
|
+
**Symptoms**: Provider returns old block data
|
|
447
|
+
|
|
448
|
+
**Solutions**:
|
|
449
|
+
1. System automatically marks stale providers and prefers fresh ones
|
|
450
|
+
2. Stale providers are still used if no fresh providers available
|
|
451
|
+
3. Check provider health: `const health = pm.getHealthChecker()?.getResult(providerId, network)`
|
|
452
|
+
|
|
453
|
+
### Provider failures (503, 502, timeout)
|
|
454
|
+
|
|
455
|
+
**Symptoms**: Providers marked as offline, frequent failovers
|
|
456
|
+
|
|
457
|
+
**Solutions**:
|
|
458
|
+
1. These are usually temporary infrastructure issues
|
|
459
|
+
2. System automatically fails over to next provider
|
|
460
|
+
3. Failed providers are retried after cooldown period (default: 30 seconds)
|
|
461
|
+
4. Check provider status: `const results = await pm.testAllProviders()`
|
|
462
|
+
5. Permanent errors (404, 401) are not retried - check API keys
|
|
463
|
+
|
|
464
|
+
### Browser compatibility (CORS errors)
|
|
465
|
+
|
|
466
|
+
**Symptoms**: CORS errors in browser, providers not working
|
|
467
|
+
|
|
468
|
+
**Solutions**:
|
|
469
|
+
1. Use `BrowserAdapter` instead of direct `TonClient` in browser
|
|
470
|
+
2. Some providers are not browser-compatible (e.g., Tatum) - they're automatically filtered
|
|
471
|
+
3. Check browser compatibility: `const providers = pm.getProviders()` (already filtered)
|
|
472
|
+
4. Use `adapter: 'browser'` when creating `ProviderManager` in browser environment
|
|
473
|
+
|
|
474
|
+
**Example**:
|
|
475
|
+
```typescript
|
|
476
|
+
// ✅ Correct for browser
|
|
477
|
+
const pm = new ProviderManager({ adapter: 'browser' });
|
|
478
|
+
await pm.init('testnet');
|
|
479
|
+
const adapter = new BrowserAdapter(pm);
|
|
480
|
+
const balance = await adapter.getAddressBalance(address);
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
### Error handling and failover
|
|
484
|
+
|
|
485
|
+
**Symptoms**: Need to handle provider failures gracefully
|
|
486
|
+
|
|
487
|
+
**Solutions**:
|
|
488
|
+
1. Always wrap operations in try-catch
|
|
489
|
+
2. Call `pm.reportError(error)` on failures to trigger failover
|
|
490
|
+
3. Call `pm.reportSuccess()` on success to update rate limiter
|
|
491
|
+
4. System automatically fails over, but manual reporting improves accuracy
|
|
492
|
+
|
|
493
|
+
**Example**:
|
|
494
|
+
```typescript
|
|
495
|
+
try {
|
|
496
|
+
const endpoint = await pm.getEndpointWithRateLimit();
|
|
497
|
+
const client = new TonClient({ endpoint });
|
|
498
|
+
const result = await client.someMethod();
|
|
499
|
+
pm.reportSuccess(); // Update rate limiter
|
|
500
|
+
return result;
|
|
501
|
+
} catch (error) {
|
|
502
|
+
pm.reportError(error); // Trigger failover
|
|
503
|
+
throw error;
|
|
504
|
+
}
|
|
505
|
+
```
|
|
409
506
|
|
|
410
507
|
## Publishing
|
|
411
508
|
|