@t402/wdk 2.0.0 → 2.3.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/README.md +117 -112
- package/dist/cjs/index.d.ts +384 -11
- package/dist/cjs/index.js +803 -173
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.d.mts +384 -11
- package/dist/esm/index.mjs +786 -178
- package/dist/esm/index.mjs.map +1 -1
- package/package.json +48 -22
package/README.md
CHANGED
|
@@ -36,47 +36,47 @@ npm install @tetherto/wdk-protocol-bridge-usdt0-evm
|
|
|
36
36
|
Before using T402WDK, register the Tether WDK modules once at app startup:
|
|
37
37
|
|
|
38
38
|
```typescript
|
|
39
|
-
import WDK from '@tetherto/wdk'
|
|
40
|
-
import WalletManagerEvm from '@tetherto/wdk-wallet-evm'
|
|
41
|
-
import BridgeUsdt0Evm from '@tetherto/wdk-protocol-bridge-usdt0-evm'
|
|
42
|
-
import { T402WDK } from '@t402/wdk'
|
|
39
|
+
import WDK from '@tetherto/wdk'
|
|
40
|
+
import WalletManagerEvm from '@tetherto/wdk-wallet-evm'
|
|
41
|
+
import BridgeUsdt0Evm from '@tetherto/wdk-protocol-bridge-usdt0-evm' // Optional
|
|
42
|
+
import { T402WDK } from '@t402/wdk'
|
|
43
43
|
|
|
44
44
|
// Register WDK modules (once at app startup)
|
|
45
|
-
T402WDK.registerWDK(WDK, WalletManagerEvm, BridgeUsdt0Evm)
|
|
45
|
+
T402WDK.registerWDK(WDK, WalletManagerEvm, BridgeUsdt0Evm)
|
|
46
46
|
```
|
|
47
47
|
|
|
48
48
|
### 2. Create a Wallet
|
|
49
49
|
|
|
50
50
|
```typescript
|
|
51
51
|
// Generate a new seed phrase
|
|
52
|
-
const seedPhrase = T402WDK.generateSeedPhrase()
|
|
52
|
+
const seedPhrase = T402WDK.generateSeedPhrase()
|
|
53
53
|
|
|
54
54
|
// Or use an existing seed phrase
|
|
55
|
-
const seedPhrase = 'your twelve word seed phrase here ...'
|
|
55
|
+
const seedPhrase = 'your twelve word seed phrase here ...'
|
|
56
56
|
|
|
57
57
|
// Create wallet with chain configurations
|
|
58
58
|
const wallet = new T402WDK(seedPhrase, {
|
|
59
59
|
arbitrum: 'https://arb1.arbitrum.io/rpc',
|
|
60
60
|
ethereum: 'https://eth.llamarpc.com',
|
|
61
61
|
base: 'https://mainnet.base.org',
|
|
62
|
-
})
|
|
62
|
+
})
|
|
63
63
|
```
|
|
64
64
|
|
|
65
65
|
### 3. Get a Signer for T402 Payments
|
|
66
66
|
|
|
67
67
|
```typescript
|
|
68
|
-
import { createT402HTTPClient } from '@t402/core'
|
|
68
|
+
import { createT402HTTPClient } from '@t402/core'
|
|
69
69
|
|
|
70
70
|
// Get signer for Arbitrum
|
|
71
|
-
const signer = await wallet.getSigner('arbitrum')
|
|
71
|
+
const signer = await wallet.getSigner('arbitrum')
|
|
72
72
|
|
|
73
73
|
// Use with T402 HTTP client
|
|
74
74
|
const client = createT402HTTPClient({
|
|
75
75
|
signers: [{ scheme: 'exact', signer }],
|
|
76
|
-
})
|
|
76
|
+
})
|
|
77
77
|
|
|
78
78
|
// Make paid requests
|
|
79
|
-
const response = await client.fetch('https://api.example.com/premium')
|
|
79
|
+
const response = await client.fetch('https://api.example.com/premium')
|
|
80
80
|
```
|
|
81
81
|
|
|
82
82
|
## API Reference
|
|
@@ -111,25 +111,29 @@ const wallet = new T402WDK(
|
|
|
111
111
|
**Config Example:**
|
|
112
112
|
|
|
113
113
|
```typescript
|
|
114
|
-
const wallet = new T402WDK(
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
114
|
+
const wallet = new T402WDK(
|
|
115
|
+
seedPhrase,
|
|
116
|
+
{
|
|
117
|
+
// String shorthand (uses default chain settings)
|
|
118
|
+
arbitrum: 'https://arb1.arbitrum.io/rpc',
|
|
119
|
+
|
|
120
|
+
// Full config object
|
|
121
|
+
ethereum: {
|
|
122
|
+
provider: 'https://eth.llamarpc.com',
|
|
123
|
+
chainId: 1,
|
|
124
|
+
network: 'eip155:1',
|
|
125
|
+
},
|
|
123
126
|
},
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
127
|
+
{
|
|
128
|
+
// Optional cache configuration
|
|
129
|
+
cache: {
|
|
130
|
+
enabled: true,
|
|
131
|
+
tokenBalanceTTL: 30000, // 30 seconds
|
|
132
|
+
nativeBalanceTTL: 15000, // 15 seconds
|
|
133
|
+
aggregatedBalanceTTL: 60000, // 60 seconds
|
|
134
|
+
},
|
|
131
135
|
},
|
|
132
|
-
|
|
136
|
+
)
|
|
133
137
|
```
|
|
134
138
|
|
|
135
139
|
#### Instance Properties
|
|
@@ -177,21 +181,21 @@ wallet.clearSignerCache();
|
|
|
177
181
|
|
|
178
182
|
```typescript
|
|
179
183
|
// Get USDT0 balance on a chain
|
|
180
|
-
const usdt0Balance = await wallet.getUsdt0Balance('arbitrum')
|
|
184
|
+
const usdt0Balance = await wallet.getUsdt0Balance('arbitrum')
|
|
181
185
|
|
|
182
186
|
// Get USDC balance on a chain
|
|
183
|
-
const usdcBalance = await wallet.getUsdcBalance('base')
|
|
187
|
+
const usdcBalance = await wallet.getUsdcBalance('base')
|
|
184
188
|
|
|
185
189
|
// Get all balances for a chain
|
|
186
|
-
const chainBalance = await wallet.getChainBalances('arbitrum')
|
|
190
|
+
const chainBalance = await wallet.getChainBalances('arbitrum')
|
|
187
191
|
// Returns: { chain, network, native, tokens: [...] }
|
|
188
192
|
|
|
189
193
|
// Get aggregated balances across all chains
|
|
190
|
-
const allBalances = await wallet.getAggregatedBalances()
|
|
194
|
+
const allBalances = await wallet.getAggregatedBalances()
|
|
191
195
|
// Returns: { totalUsdt0, totalUsdc, chains: [...] }
|
|
192
196
|
|
|
193
197
|
// Find best chain for a payment amount
|
|
194
|
-
const best = await wallet.findBestChainForPayment(1000000n, 'USDT0')
|
|
198
|
+
const best = await wallet.findBestChainForPayment(1000000n, 'USDT0')
|
|
195
199
|
// Returns: { chain: 'arbitrum', token: 'USDT0', balance: 5000000n } | null
|
|
196
200
|
```
|
|
197
201
|
|
|
@@ -242,36 +246,40 @@ The signer returned by `wallet.getSigner()` implements the T402 `ClientEvmSigner
|
|
|
242
246
|
|
|
243
247
|
```typescript
|
|
244
248
|
interface WDKSigner {
|
|
245
|
-
readonly address: Address
|
|
249
|
+
readonly address: Address
|
|
246
250
|
|
|
247
251
|
// Sign EIP-712 typed data (used by T402 for EIP-3009)
|
|
248
252
|
signTypedData(message: {
|
|
249
|
-
domain: Record<string, unknown
|
|
250
|
-
types: Record<string, unknown
|
|
251
|
-
primaryType: string
|
|
252
|
-
message: Record<string, unknown
|
|
253
|
-
}): Promise<`0x${string}
|
|
253
|
+
domain: Record<string, unknown>
|
|
254
|
+
types: Record<string, unknown>
|
|
255
|
+
primaryType: string
|
|
256
|
+
message: Record<string, unknown>
|
|
257
|
+
}): Promise<`0x${string}`>
|
|
254
258
|
|
|
255
259
|
// Sign a personal message
|
|
256
|
-
signMessage(message: string | Uint8Array): Promise<`0x${string}
|
|
260
|
+
signMessage(message: string | Uint8Array): Promise<`0x${string}`>
|
|
257
261
|
|
|
258
262
|
// Get native token balance
|
|
259
|
-
getBalance(): Promise<bigint
|
|
263
|
+
getBalance(): Promise<bigint>
|
|
260
264
|
|
|
261
265
|
// Get ERC20 token balance
|
|
262
|
-
getTokenBalance(tokenAddress: Address): Promise<bigint
|
|
266
|
+
getTokenBalance(tokenAddress: Address): Promise<bigint>
|
|
263
267
|
|
|
264
268
|
// Estimate gas for a transaction
|
|
265
|
-
estimateGas(params: { to: Address; value?: bigint; data?: string }): Promise<bigint
|
|
269
|
+
estimateGas(params: { to: Address; value?: bigint; data?: string }): Promise<bigint>
|
|
266
270
|
|
|
267
271
|
// Send a transaction
|
|
268
|
-
sendTransaction(params: {
|
|
272
|
+
sendTransaction(params: {
|
|
273
|
+
to: Address
|
|
274
|
+
value?: bigint
|
|
275
|
+
data?: string
|
|
276
|
+
}): Promise<{ hash: `0x${string}` }>
|
|
269
277
|
|
|
270
278
|
// Utility methods
|
|
271
|
-
getChain(): string
|
|
272
|
-
getChainId(): number
|
|
273
|
-
getAccountIndex(): number
|
|
274
|
-
isInitialized: boolean
|
|
279
|
+
getChain(): string
|
|
280
|
+
getChainId(): number
|
|
281
|
+
getAccountIndex(): number
|
|
282
|
+
isInitialized: boolean
|
|
275
283
|
}
|
|
276
284
|
```
|
|
277
285
|
|
|
@@ -296,18 +304,18 @@ import {
|
|
|
296
304
|
wrapError,
|
|
297
305
|
withRetry,
|
|
298
306
|
withTimeout,
|
|
299
|
-
} from '@t402/wdk'
|
|
307
|
+
} from '@t402/wdk'
|
|
300
308
|
|
|
301
309
|
try {
|
|
302
|
-
const signer = await wallet.getSigner('polygon')
|
|
310
|
+
const signer = await wallet.getSigner('polygon')
|
|
303
311
|
} catch (error) {
|
|
304
312
|
if (isWDKError(error)) {
|
|
305
|
-
console.error(`Error code: ${error.code}`)
|
|
306
|
-
console.error(`Message: ${error.message}`)
|
|
307
|
-
console.error(`Context:`, error.context)
|
|
313
|
+
console.error(`Error code: ${error.code}`)
|
|
314
|
+
console.error(`Message: ${error.message}`)
|
|
315
|
+
console.error(`Context:`, error.context)
|
|
308
316
|
|
|
309
317
|
if (hasErrorCode(error, WDKErrorCode.CHAIN_NOT_CONFIGURED)) {
|
|
310
|
-
console.error('Chain is not configured')
|
|
318
|
+
console.error('Chain is not configured')
|
|
311
319
|
}
|
|
312
320
|
|
|
313
321
|
if (error.isRetryable()) {
|
|
@@ -319,80 +327,77 @@ try {
|
|
|
319
327
|
|
|
320
328
|
### Error Codes
|
|
321
329
|
|
|
322
|
-
| Code Range | Category
|
|
323
|
-
|
|
324
|
-
| 1xxx
|
|
325
|
-
| 2xxx
|
|
326
|
-
| 3xxx
|
|
327
|
-
| 4xxx
|
|
328
|
-
| 5xxx
|
|
329
|
-
| 6xxx
|
|
330
|
-
| 7xxx
|
|
331
|
-
| 8xxx
|
|
330
|
+
| Code Range | Category |
|
|
331
|
+
| ---------- | -------------------------- |
|
|
332
|
+
| 1xxx | Initialization errors |
|
|
333
|
+
| 2xxx | Chain configuration errors |
|
|
334
|
+
| 3xxx | Signer errors |
|
|
335
|
+
| 4xxx | Signing errors |
|
|
336
|
+
| 5xxx | Balance errors |
|
|
337
|
+
| 6xxx | Transaction errors |
|
|
338
|
+
| 7xxx | Bridge errors |
|
|
339
|
+
| 8xxx | RPC errors |
|
|
332
340
|
|
|
333
341
|
### Retry Utilities
|
|
334
342
|
|
|
335
343
|
```typescript
|
|
336
|
-
import { withRetry, withTimeout } from '@t402/wdk'
|
|
344
|
+
import { withRetry, withTimeout } from '@t402/wdk'
|
|
337
345
|
|
|
338
346
|
// Retry an operation with exponential backoff
|
|
339
|
-
const balance = await withRetry(
|
|
340
|
-
() => signer.getBalance(),
|
|
341
|
-
{ maxRetries: 3, baseDelay: 500 }
|
|
342
|
-
);
|
|
347
|
+
const balance = await withRetry(() => signer.getBalance(), { maxRetries: 3, baseDelay: 500 })
|
|
343
348
|
|
|
344
349
|
// Add timeout to a promise
|
|
345
350
|
const result = await withTimeout(
|
|
346
351
|
someAsyncOperation(),
|
|
347
|
-
30000,
|
|
348
|
-
'Operation description'
|
|
349
|
-
)
|
|
352
|
+
30000, // 30 second timeout
|
|
353
|
+
'Operation description',
|
|
354
|
+
)
|
|
350
355
|
```
|
|
351
356
|
|
|
352
357
|
## Supported Chains
|
|
353
358
|
|
|
354
|
-
| Chain
|
|
355
|
-
|
|
356
|
-
| Ethereum
|
|
357
|
-
| Arbitrum
|
|
358
|
-
| Base
|
|
359
|
-
| Ink
|
|
360
|
-
| Berachain | 80094
|
|
361
|
-
| Unichain
|
|
359
|
+
| Chain | Chain ID | USDT0 | Bridging |
|
|
360
|
+
| --------- | -------- | ----- | -------- |
|
|
361
|
+
| Ethereum | 1 | ✅ | ✅ |
|
|
362
|
+
| Arbitrum | 42161 | ✅ | ✅ |
|
|
363
|
+
| Base | 8453 | ✅ | ✅ |
|
|
364
|
+
| Ink | 57073 | ✅ | ✅ |
|
|
365
|
+
| Berachain | 80094 | ✅ | ✅ |
|
|
366
|
+
| Unichain | 130 | ✅ | ✅ |
|
|
362
367
|
|
|
363
368
|
## Examples
|
|
364
369
|
|
|
365
370
|
### Complete Payment Flow
|
|
366
371
|
|
|
367
372
|
```typescript
|
|
368
|
-
import WDK from '@tetherto/wdk'
|
|
369
|
-
import WalletManagerEvm from '@tetherto/wdk-wallet-evm'
|
|
370
|
-
import { T402WDK } from '@t402/wdk'
|
|
371
|
-
import { createT402HTTPClient } from '@t402/core'
|
|
373
|
+
import WDK from '@tetherto/wdk'
|
|
374
|
+
import WalletManagerEvm from '@tetherto/wdk-wallet-evm'
|
|
375
|
+
import { T402WDK } from '@t402/wdk'
|
|
376
|
+
import { createT402HTTPClient } from '@t402/core'
|
|
372
377
|
|
|
373
378
|
// 1. Setup (once at app startup)
|
|
374
|
-
T402WDK.registerWDK(WDK, WalletManagerEvm)
|
|
379
|
+
T402WDK.registerWDK(WDK, WalletManagerEvm)
|
|
375
380
|
|
|
376
381
|
// 2. Create wallet
|
|
377
382
|
const wallet = new T402WDK(seedPhrase, {
|
|
378
383
|
arbitrum: 'https://arb1.arbitrum.io/rpc',
|
|
379
|
-
})
|
|
384
|
+
})
|
|
380
385
|
|
|
381
386
|
// 3. Check balance before payment
|
|
382
|
-
const balance = await wallet.getUsdt0Balance('arbitrum')
|
|
383
|
-
console.log(`USDT0 Balance: ${balance / 1000000n} USDT0`)
|
|
387
|
+
const balance = await wallet.getUsdt0Balance('arbitrum')
|
|
388
|
+
console.log(`USDT0 Balance: ${balance / 1000000n} USDT0`)
|
|
384
389
|
|
|
385
390
|
// 4. Get signer and create client
|
|
386
|
-
const signer = await wallet.getSigner('arbitrum')
|
|
391
|
+
const signer = await wallet.getSigner('arbitrum')
|
|
387
392
|
const client = createT402HTTPClient({
|
|
388
393
|
signers: [{ scheme: 'exact', signer }],
|
|
389
|
-
})
|
|
394
|
+
})
|
|
390
395
|
|
|
391
396
|
// 5. Make paid request
|
|
392
|
-
const response = await client.fetch('https://api.example.com/premium')
|
|
397
|
+
const response = await client.fetch('https://api.example.com/premium')
|
|
393
398
|
|
|
394
399
|
// 6. Invalidate cache after payment
|
|
395
|
-
wallet.invalidateChainCache('arbitrum')
|
|
400
|
+
wallet.invalidateChainCache('arbitrum')
|
|
396
401
|
```
|
|
397
402
|
|
|
398
403
|
### Multi-Chain Balance Check
|
|
@@ -402,19 +407,19 @@ const wallet = new T402WDK(seedPhrase, {
|
|
|
402
407
|
arbitrum: 'https://arb1.arbitrum.io/rpc',
|
|
403
408
|
ethereum: 'https://eth.llamarpc.com',
|
|
404
409
|
base: 'https://mainnet.base.org',
|
|
405
|
-
})
|
|
410
|
+
})
|
|
406
411
|
|
|
407
412
|
// Get all balances
|
|
408
|
-
const balances = await wallet.getAggregatedBalances()
|
|
413
|
+
const balances = await wallet.getAggregatedBalances()
|
|
409
414
|
|
|
410
|
-
console.log(`Total USDT0: ${balances.totalUsdt0 / 1000000n}`)
|
|
411
|
-
console.log(`Total USDC: ${balances.totalUsdc / 1000000n}`)
|
|
415
|
+
console.log(`Total USDT0: ${balances.totalUsdt0 / 1000000n}`)
|
|
416
|
+
console.log(`Total USDC: ${balances.totalUsdc / 1000000n}`)
|
|
412
417
|
|
|
413
418
|
for (const chain of balances.chains) {
|
|
414
|
-
console.log(`\n${chain.chain}:`)
|
|
415
|
-
console.log(` Native: ${chain.native}`)
|
|
419
|
+
console.log(`\n${chain.chain}:`)
|
|
420
|
+
console.log(` Native: ${chain.native}`)
|
|
416
421
|
for (const token of chain.tokens) {
|
|
417
|
-
console.log(` ${token.symbol}: ${token.formatted}`)
|
|
422
|
+
console.log(` ${token.symbol}: ${token.formatted}`)
|
|
418
423
|
}
|
|
419
424
|
}
|
|
420
425
|
```
|
|
@@ -422,32 +427,32 @@ for (const chain of balances.chains) {
|
|
|
422
427
|
### Auto-Select Best Chain
|
|
423
428
|
|
|
424
429
|
```typescript
|
|
425
|
-
const amount = 50_000000n
|
|
430
|
+
const amount = 50_000000n // 50 USDT0
|
|
426
431
|
|
|
427
432
|
// Find the best chain with sufficient balance
|
|
428
|
-
const best = await wallet.findBestChainForPayment(amount)
|
|
433
|
+
const best = await wallet.findBestChainForPayment(amount)
|
|
429
434
|
|
|
430
435
|
if (best) {
|
|
431
|
-
console.log(`Use ${best.token} on ${best.chain}`)
|
|
432
|
-
const signer = await wallet.getSigner(best.chain)
|
|
436
|
+
console.log(`Use ${best.token} on ${best.chain}`)
|
|
437
|
+
const signer = await wallet.getSigner(best.chain)
|
|
433
438
|
// Use signer for payment...
|
|
434
439
|
} else {
|
|
435
|
-
console.log('Insufficient balance on all chains')
|
|
440
|
+
console.log('Insufficient balance on all chains')
|
|
436
441
|
}
|
|
437
442
|
```
|
|
438
443
|
|
|
439
444
|
### Cross-Chain Bridge
|
|
440
445
|
|
|
441
446
|
```typescript
|
|
442
|
-
import BridgeUsdt0Evm from '@tetherto/wdk-protocol-bridge-usdt0-evm'
|
|
447
|
+
import BridgeUsdt0Evm from '@tetherto/wdk-protocol-bridge-usdt0-evm'
|
|
443
448
|
|
|
444
449
|
// Register with bridge support
|
|
445
|
-
T402WDK.registerWDK(WDK, WalletManagerEvm, BridgeUsdt0Evm)
|
|
450
|
+
T402WDK.registerWDK(WDK, WalletManagerEvm, BridgeUsdt0Evm)
|
|
446
451
|
|
|
447
452
|
const wallet = new T402WDK(seedPhrase, {
|
|
448
453
|
ethereum: 'https://eth.llamarpc.com',
|
|
449
454
|
arbitrum: 'https://arb1.arbitrum.io/rpc',
|
|
450
|
-
})
|
|
455
|
+
})
|
|
451
456
|
|
|
452
457
|
// Check if bridging is possible
|
|
453
458
|
if (wallet.canBridge('ethereum', 'arbitrum')) {
|
|
@@ -456,10 +461,10 @@ if (wallet.canBridge('ethereum', 'arbitrum')) {
|
|
|
456
461
|
fromChain: 'ethereum',
|
|
457
462
|
toChain: 'arbitrum',
|
|
458
463
|
amount: 100_000000n,
|
|
459
|
-
})
|
|
464
|
+
})
|
|
460
465
|
|
|
461
|
-
console.log(`Bridge tx: ${result.txHash}`)
|
|
462
|
-
console.log(`Estimated time: ${result.estimatedTime}s`)
|
|
466
|
+
console.log(`Bridge tx: ${result.txHash}`)
|
|
467
|
+
console.log(`Estimated time: ${result.estimatedTime}s`)
|
|
463
468
|
}
|
|
464
469
|
```
|
|
465
470
|
|
|
@@ -503,7 +508,7 @@ import type {
|
|
|
503
508
|
BalanceCacheConfig,
|
|
504
509
|
BalanceCacheStats,
|
|
505
510
|
RetryConfig,
|
|
506
|
-
} from '@t402/wdk'
|
|
511
|
+
} from '@t402/wdk'
|
|
507
512
|
```
|
|
508
513
|
|
|
509
514
|
## License
|