@dexterai/x402 1.9.4 → 2.1.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.
Files changed (40) hide show
  1. package/README.md +27 -0
  2. package/dist/adapters/index.cjs +375 -3
  3. package/dist/adapters/index.d.cts +4 -5
  4. package/dist/adapters/index.d.ts +4 -5
  5. package/dist/adapters/index.js +369 -3
  6. package/dist/client/index.cjs +570 -10
  7. package/dist/client/index.d.cts +200 -36
  8. package/dist/client/index.d.ts +200 -36
  9. package/dist/client/index.js +568 -10
  10. package/dist/react/index.cjs +404 -8
  11. package/dist/react/index.d.cts +5 -5
  12. package/dist/react/index.d.ts +5 -5
  13. package/dist/react/index.js +404 -8
  14. package/dist/server/index.cjs +3 -4
  15. package/dist/server/index.d.cts +2 -2
  16. package/dist/server/index.d.ts +2 -2
  17. package/dist/server/index.js +3 -4
  18. package/dist/{sponsored-access-D1_mINs4.d.ts → sponsored-access-DAVzu4x6.d.cts} +13 -2
  19. package/dist/{sponsored-access-Br6YPA-m.d.cts → sponsored-access-Lxa11w_X.d.ts} +13 -2
  20. package/dist/types-D1u7iu8n.d.cts +304 -0
  21. package/dist/types-YQlJI5E3.d.ts +304 -0
  22. package/dist/{types-CjLMR7qs.d.cts → types-_iT11DL0.d.cts} +2 -2
  23. package/dist/{types-CjLMR7qs.d.ts → types-_iT11DL0.d.ts} +2 -2
  24. package/dist/utils/index.cjs +0 -1
  25. package/dist/utils/index.js +0 -1
  26. package/package.json +1 -1
  27. package/dist/adapters/index.cjs.map +0 -1
  28. package/dist/adapters/index.js.map +0 -1
  29. package/dist/client/index.cjs.map +0 -1
  30. package/dist/client/index.js.map +0 -1
  31. package/dist/react/index.cjs.map +0 -1
  32. package/dist/react/index.js.map +0 -1
  33. package/dist/server/index.cjs.map +0 -1
  34. package/dist/server/index.js.map +0 -1
  35. package/dist/solana-BcOfK6Eq.d.cts +0 -132
  36. package/dist/solana-Cxr5byPa.d.ts +0 -132
  37. package/dist/types-BIHhO2-I.d.ts +0 -123
  38. package/dist/types-CfKflCZO.d.cts +0 -123
  39. package/dist/utils/index.cjs.map +0 -1
  40. package/dist/utils/index.js.map +0 -1
package/README.md CHANGED
@@ -700,6 +700,33 @@ interface SponsoredRecommendation {
700
700
  5. Agent's LLM sees the recommendation and can call the suggested resource
701
701
  6. If the agent calls it, the facilitator records a conversion with both tx hashes as proof
702
702
 
703
+ ### Advertise Your API
704
+
705
+ Want your API recommended to agents across the x402 network? Create and fund campaigns through the Agent API — no signup, no accounts. Your wallet is your identity.
706
+
707
+ ```typescript
708
+ // All campaign management is x402-gated at x402ads.io
709
+ const x402Fetch = wrapFetch(fetch, { walletPrivateKey: key });
710
+
711
+ // Create a campaign ($0.10 USDC)
712
+ const res = await x402Fetch('https://x402ads.io/v1/agent/campaigns', {
713
+ method: 'POST',
714
+ headers: { 'Content-Type': 'application/json' },
715
+ body: JSON.stringify({
716
+ name: 'Promote My API',
717
+ rec_resource_url: 'https://api.example.com/data',
718
+ rec_description: 'Real-time market data',
719
+ rec_sponsor_name: 'Example Corp',
720
+ target_categories: ['defi', 'data'],
721
+ bid_strategy: 'cpa',
722
+ max_bid_amount: '50000',
723
+ budget_daily: '5000000',
724
+ }),
725
+ });
726
+ ```
727
+
728
+ Full advertiser guide: [docs.dexter.cash/docs/sponsored-access/for-advertisers](https://docs.dexter.cash/docs/sponsored-access/for-advertisers)
729
+
703
730
  ---
704
731
 
705
732
  ## API Reference
@@ -34,9 +34,14 @@ __export(adapters_exports, {
34
34
  AVALANCHE: () => AVALANCHE,
35
35
  BASE_MAINNET: () => BASE_MAINNET,
36
36
  BASE_SEPOLIA: () => BASE_SEPOLIA,
37
+ BSC_MAINNET: () => BSC_MAINNET,
38
+ BSC_STABLECOIN_ADDRESSES: () => BSC_STABLECOIN_ADDRESSES,
39
+ BSC_USDC: () => BSC_USDC,
40
+ BSC_USDT: () => BSC_USDT,
37
41
  ETHEREUM_MAINNET: () => ETHEREUM_MAINNET,
38
42
  EvmAdapter: () => EvmAdapter,
39
43
  OPTIMISM: () => OPTIMISM,
44
+ PERMIT2_ADDRESS: () => PERMIT2_ADDRESS,
40
45
  POLYGON: () => POLYGON,
41
46
  SKALE_BASE: () => SKALE_BASE,
42
47
  SKALE_BASE_SEPOLIA: () => SKALE_BASE_SEPOLIA,
@@ -45,6 +50,7 @@ __export(adapters_exports, {
45
50
  SOLANA_TESTNET: () => SOLANA_TESTNET,
46
51
  SolanaAdapter: () => SolanaAdapter,
47
52
  USDC_ADDRESSES: () => USDC_ADDRESSES,
53
+ X402_EXACT_PERMIT2_PROXY: () => X402_EXACT_PERMIT2_PROXY,
48
54
  createDefaultAdapters: () => createDefaultAdapters,
49
55
  createEvmAdapter: () => createEvmAdapter,
50
56
  createSolanaAdapter: () => createSolanaAdapter,
@@ -244,16 +250,40 @@ function createSolanaAdapter(config) {
244
250
  }
245
251
 
246
252
  // src/adapters/evm.ts
253
+ var PERMIT2_ADDRESS = "0x000000000022D473030F116dDEE9F6B43aC78BA3";
254
+ var X402_EXACT_PERMIT2_PROXY = "0x402085c248EeA27D92E8b30b2C58ed07f9E20001";
255
+ var PERMIT2_WITNESS_TYPES = {
256
+ PermitWitnessTransferFrom: [
257
+ { name: "permitted", type: "TokenPermissions" },
258
+ { name: "spender", type: "address" },
259
+ { name: "nonce", type: "uint256" },
260
+ { name: "deadline", type: "uint256" },
261
+ { name: "witness", type: "Witness" }
262
+ ],
263
+ TokenPermissions: [
264
+ { name: "token", type: "address" },
265
+ { name: "amount", type: "uint256" }
266
+ ],
267
+ Witness: [
268
+ { name: "to", type: "address" },
269
+ { name: "validAfter", type: "uint256" }
270
+ ]
271
+ };
272
+ var MAX_UINT256 = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
247
273
  var BASE_MAINNET = "eip155:8453";
248
274
  var BASE_SEPOLIA = "eip155:84532";
249
275
  var ARBITRUM_ONE = "eip155:42161";
250
276
  var POLYGON = "eip155:137";
251
277
  var OPTIMISM = "eip155:10";
252
278
  var AVALANCHE = "eip155:43114";
279
+ var BSC_MAINNET = "eip155:56";
253
280
  var SKALE_BASE = "eip155:1187947933";
254
281
  var SKALE_BASE_SEPOLIA = "eip155:324705682";
255
282
  var ETHEREUM_MAINNET = "eip155:1";
283
+ var BSC_USDT = "0x55d398326f99059fF775485246999027B3197955";
284
+ var BSC_USDC = "0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d";
256
285
  var CHAIN_IDS = {
286
+ [BSC_MAINNET]: 56,
257
287
  [BASE_MAINNET]: 8453,
258
288
  [BASE_SEPOLIA]: 84532,
259
289
  [ARBITRUM_ONE]: 42161,
@@ -265,6 +295,7 @@ var CHAIN_IDS = {
265
295
  [ETHEREUM_MAINNET]: 1
266
296
  };
267
297
  var DEFAULT_RPC_URLS2 = {
298
+ [BSC_MAINNET]: "https://bsc-dataseed1.binance.org",
268
299
  [BASE_MAINNET]: "https://api.dexter.cash/api/base/rpc",
269
300
  [BASE_SEPOLIA]: "https://sepolia.base.org",
270
301
  [ARBITRUM_ONE]: "https://arb1.arbitrum.io/rpc",
@@ -276,6 +307,7 @@ var DEFAULT_RPC_URLS2 = {
276
307
  [ETHEREUM_MAINNET]: "https://eth.llamarpc.com"
277
308
  };
278
309
  var USDC_ADDRESSES = {
310
+ [BSC_MAINNET]: BSC_USDC,
279
311
  [BASE_MAINNET]: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
280
312
  [BASE_SEPOLIA]: "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
281
313
  [ARBITRUM_ONE]: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
@@ -286,6 +318,10 @@ var USDC_ADDRESSES = {
286
318
  [SKALE_BASE_SEPOLIA]: "0x2e08028E3C4c2356572E096d8EF835cD5C6030bD",
287
319
  [ETHEREUM_MAINNET]: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
288
320
  };
321
+ var BSC_STABLECOIN_ADDRESSES = {
322
+ [BSC_USDT]: { symbol: "USDT", decimals: 18 },
323
+ [BSC_USDC]: { symbol: "USDC", decimals: 18 }
324
+ };
289
325
  function isEvmWallet(wallet) {
290
326
  if (!wallet || typeof wallet !== "object") return false;
291
327
  const w = wallet;
@@ -293,7 +329,7 @@ function isEvmWallet(wallet) {
293
329
  }
294
330
  var EvmAdapter = class {
295
331
  name = "EVM";
296
- networks = [BASE_MAINNET, BASE_SEPOLIA, ETHEREUM_MAINNET, ARBITRUM_ONE];
332
+ networks = [BSC_MAINNET, BASE_MAINNET, BASE_SEPOLIA, ETHEREUM_MAINNET, ARBITRUM_ONE];
297
333
  config;
298
334
  log;
299
335
  constructor(config = {}) {
@@ -304,6 +340,7 @@ var EvmAdapter = class {
304
340
  canHandle(network) {
305
341
  if (this.networks.includes(network)) return true;
306
342
  if (network === "base") return true;
343
+ if (network === "bsc") return true;
307
344
  if (network === "ethereum") return true;
308
345
  if (network === "arbitrum") return true;
309
346
  if (network.startsWith("eip155:")) return true;
@@ -317,6 +354,7 @@ var EvmAdapter = class {
317
354
  return DEFAULT_RPC_URLS2[network];
318
355
  }
319
356
  if (network === "base") return DEFAULT_RPC_URLS2[BASE_MAINNET];
357
+ if (network === "bsc") return DEFAULT_RPC_URLS2[BSC_MAINNET];
320
358
  if (network === "ethereum") return DEFAULT_RPC_URLS2[ETHEREUM_MAINNET];
321
359
  if (network === "arbitrum") return DEFAULT_RPC_URLS2[ARBITRUM_ONE];
322
360
  return DEFAULT_RPC_URLS2[BASE_MAINNET];
@@ -336,6 +374,7 @@ var EvmAdapter = class {
336
374
  return parseInt(chainIdStr, 10);
337
375
  }
338
376
  if (network === "base") return 8453;
377
+ if (network === "bsc") return 56;
339
378
  if (network === "ethereum") return 1;
340
379
  if (network === "arbitrum") return 42161;
341
380
  return 8453;
@@ -385,13 +424,19 @@ var EvmAdapter = class {
385
424
  const paddedAddress = address.slice(2).toLowerCase().padStart(64, "0");
386
425
  return selector + paddedAddress;
387
426
  }
388
- async buildTransaction(accept, wallet, _rpcUrl) {
427
+ async buildTransaction(accept, wallet, rpcUrl) {
389
428
  if (!isEvmWallet(wallet)) {
390
429
  throw new Error("Invalid EVM wallet");
391
430
  }
392
431
  if (!wallet.address) {
393
432
  throw new Error("Wallet not connected");
394
433
  }
434
+ if (accept.scheme === "exact-approval") {
435
+ return this.buildApprovalTransaction(accept, wallet, rpcUrl);
436
+ }
437
+ if (accept.extra?.assetTransferMethod === "permit2") {
438
+ return this.buildPermit2Transaction(accept, wallet, rpcUrl);
439
+ }
395
440
  const { payTo, asset, extra } = accept;
396
441
  const amount = accept.amount ?? accept.maxAmountRequired;
397
442
  if (!amount) {
@@ -462,6 +507,325 @@ var EvmAdapter = class {
462
507
  signature
463
508
  };
464
509
  }
510
+ // ===========================================================================
511
+ // exact-approval: BSC and other chains without EIP-3009
512
+ // ===========================================================================
513
+ /**
514
+ * Build a payment transaction for chains that use the approval-based scheme.
515
+ * The facilitator's /supported response provides the EIP-712 domain and types
516
+ * in accept.extra, so the client doesn't hardcode any contract addresses.
517
+ */
518
+ async buildApprovalTransaction(accept, wallet, rpcUrl) {
519
+ const { payTo, asset, extra } = accept;
520
+ const amount = accept.amount ?? accept.maxAmountRequired;
521
+ if (!amount) {
522
+ throw new Error("Missing amount in payment requirements");
523
+ }
524
+ const facilitatorContract = extra?.facilitatorContract;
525
+ if (!facilitatorContract) {
526
+ throw new Error(
527
+ "exact-approval scheme requires extra.facilitatorContract from the facilitator. The /supported endpoint should provide this."
528
+ );
529
+ }
530
+ if (!wallet.signTypedData) {
531
+ throw new Error("Wallet does not support signTypedData (EIP-712)");
532
+ }
533
+ this.log("Building approval-based transaction:", {
534
+ from: wallet.address,
535
+ to: payTo,
536
+ amount,
537
+ asset,
538
+ network: accept.network,
539
+ facilitatorContract
540
+ });
541
+ const url = rpcUrl || this.getDefaultRpcUrl(accept.network);
542
+ const fee = extra?.fee ?? "0";
543
+ const totalNeeded = BigInt(amount) + BigInt(fee);
544
+ const currentAllowance = await this.readAllowance(url, asset, wallet.address, facilitatorContract);
545
+ if (currentAllowance < totalNeeded) {
546
+ if (!wallet.sendTransaction) {
547
+ throw new Error(
548
+ "BSC payments require a wallet that supports sendTransaction for the one-time token approval. Use createEvmKeypairWallet() or a browser wallet with transaction support."
549
+ );
550
+ }
551
+ const approvalAmount = this.calculateApprovalAmount(amount, fee, extra?.approvalStrategy);
552
+ this.log(`Approving ${approvalAmount} for ${facilitatorContract} (current allowance: ${currentAllowance})`);
553
+ const approveTxHash = await wallet.sendTransaction({
554
+ to: asset,
555
+ data: this.encodeApprove(facilitatorContract, approvalAmount),
556
+ value: 0n
557
+ });
558
+ this.log(`Approval tx sent: ${approveTxHash}`);
559
+ await this.waitForReceipt(url, approveTxHash);
560
+ this.log("Approval confirmed");
561
+ } else {
562
+ this.log("Sufficient allowance, skipping approval");
563
+ }
564
+ const nonceBytes = new Uint8Array(16);
565
+ (globalThis.crypto ?? (await import("crypto")).webcrypto).getRandomValues(nonceBytes);
566
+ const nonce = [...nonceBytes].reduce((acc, b) => acc * 256n + BigInt(b), 0n).toString();
567
+ const paymentIdBytes = new Uint8Array(32);
568
+ (globalThis.crypto ?? (await import("crypto")).webcrypto).getRandomValues(paymentIdBytes);
569
+ const paymentId = "0x" + [...paymentIdBytes].map((b) => b.toString(16).padStart(2, "0")).join("");
570
+ const now = Math.floor(Date.now() / 1e3);
571
+ const deadline = now + (accept.maxTimeoutSeconds || 300);
572
+ const eip712Domain = extra?.eip712Domain;
573
+ const domain = eip712Domain ? {
574
+ name: eip712Domain.name,
575
+ version: eip712Domain.version,
576
+ chainId: BigInt(eip712Domain.chainId),
577
+ verifyingContract: eip712Domain.verifyingContract
578
+ } : {
579
+ name: "DexterBSCFacilitator",
580
+ version: "1",
581
+ chainId: BigInt(this.getChainId(accept.network)),
582
+ verifyingContract: facilitatorContract
583
+ };
584
+ const types = extra?.eip712Types ?? {
585
+ Payment: [
586
+ { name: "from", type: "address" },
587
+ { name: "to", type: "address" },
588
+ { name: "token", type: "address" },
589
+ { name: "amount", type: "uint256" },
590
+ { name: "fee", type: "uint256" },
591
+ { name: "nonce", type: "uint256" },
592
+ { name: "deadline", type: "uint256" },
593
+ { name: "paymentId", type: "bytes32" }
594
+ ]
595
+ };
596
+ const message = {
597
+ from: wallet.address,
598
+ to: payTo,
599
+ token: asset,
600
+ amount: BigInt(amount),
601
+ fee: BigInt(fee),
602
+ nonce: BigInt(nonce),
603
+ deadline: BigInt(deadline),
604
+ paymentId
605
+ };
606
+ const signature = await wallet.signTypedData({
607
+ domain,
608
+ types,
609
+ primaryType: "Payment",
610
+ message
611
+ });
612
+ this.log("EIP-712 Payment signature obtained");
613
+ const payload = {
614
+ from: wallet.address,
615
+ to: payTo,
616
+ token: asset,
617
+ amount,
618
+ fee,
619
+ nonce,
620
+ deadline,
621
+ paymentId,
622
+ signature
623
+ };
624
+ return {
625
+ serialized: JSON.stringify(payload),
626
+ signature
627
+ };
628
+ }
629
+ // ===========================================================================
630
+ // Permit2: Universal ERC-20 payments via Uniswap's Permit2 contract
631
+ // ===========================================================================
632
+ /**
633
+ * Build a Permit2 payment transaction. Used when the facilitator signals
634
+ * assetTransferMethod: "permit2" in extra (e.g., BSC where EIP-3009 is unavailable).
635
+ *
636
+ * Flow:
637
+ * 1. Check if token has approved the Permit2 contract. If not, approve(Permit2, maxUint256).
638
+ * 2. Sign EIP-712 PermitWitnessTransferFrom against the Permit2 contract.
639
+ * 3. Return { permit2Authorization, signature } payload for the facilitator.
640
+ */
641
+ async buildPermit2Transaction(accept, wallet, rpcUrl) {
642
+ const { payTo, asset } = accept;
643
+ const amount = accept.amount ?? accept.maxAmountRequired;
644
+ if (!amount) {
645
+ throw new Error("Missing amount in payment requirements");
646
+ }
647
+ if (!wallet.signTypedData) {
648
+ throw new Error("Wallet does not support signTypedData (EIP-712)");
649
+ }
650
+ this.log("Building Permit2 transaction:", {
651
+ from: wallet.address,
652
+ to: payTo,
653
+ amount,
654
+ asset,
655
+ network: accept.network
656
+ });
657
+ const url = rpcUrl || this.getDefaultRpcUrl(accept.network);
658
+ const currentAllowance = await this.readAllowance(url, asset, wallet.address, PERMIT2_ADDRESS);
659
+ if (currentAllowance < BigInt(amount)) {
660
+ if (!wallet.sendTransaction) {
661
+ throw new Error(
662
+ "Permit2 payments require a wallet that supports sendTransaction for the one-time Permit2 approval. Use createEvmKeypairWallet() or a browser wallet with transaction support."
663
+ );
664
+ }
665
+ this.log(`Approving Permit2 for ${asset} (current allowance: ${currentAllowance})`);
666
+ const approveTxHash = await wallet.sendTransaction({
667
+ to: asset,
668
+ data: this.encodeApprove(PERMIT2_ADDRESS, MAX_UINT256),
669
+ value: 0n
670
+ });
671
+ this.log(`Permit2 approval tx sent: ${approveTxHash}`);
672
+ await this.waitForReceipt(url, approveTxHash);
673
+ this.log("Permit2 approval confirmed");
674
+ } else {
675
+ this.log("Sufficient Permit2 allowance, skipping approval");
676
+ }
677
+ const nonceBytes = new Uint8Array(32);
678
+ (globalThis.crypto ?? (await import("crypto")).webcrypto).getRandomValues(nonceBytes);
679
+ const nonce = [...nonceBytes].reduce((acc, b) => acc * 256n + BigInt(b), 0n);
680
+ const now = Math.floor(Date.now() / 1e3);
681
+ const validAfter = now - 600;
682
+ const deadline = now + (accept.maxTimeoutSeconds || 300);
683
+ const chainId = this.getChainId(accept.network);
684
+ const domain = {
685
+ name: "Permit2",
686
+ chainId: BigInt(chainId),
687
+ verifyingContract: PERMIT2_ADDRESS
688
+ };
689
+ const message = {
690
+ permitted: {
691
+ token: asset,
692
+ amount: BigInt(amount)
693
+ },
694
+ spender: X402_EXACT_PERMIT2_PROXY,
695
+ nonce,
696
+ deadline: BigInt(deadline),
697
+ witness: {
698
+ to: payTo,
699
+ validAfter: BigInt(validAfter)
700
+ }
701
+ };
702
+ const signature = await wallet.signTypedData({
703
+ domain,
704
+ types: PERMIT2_WITNESS_TYPES,
705
+ primaryType: "PermitWitnessTransferFrom",
706
+ message
707
+ });
708
+ this.log("Permit2 PermitWitnessTransferFrom signature obtained");
709
+ const payload = {
710
+ signature,
711
+ permit2Authorization: {
712
+ from: wallet.address,
713
+ permitted: {
714
+ token: asset,
715
+ amount
716
+ },
717
+ spender: X402_EXACT_PERMIT2_PROXY,
718
+ nonce: nonce.toString(),
719
+ deadline: String(deadline),
720
+ witness: {
721
+ to: payTo,
722
+ validAfter: String(validAfter)
723
+ }
724
+ }
725
+ };
726
+ return {
727
+ serialized: JSON.stringify(payload),
728
+ signature
729
+ };
730
+ }
731
+ /**
732
+ * Read ERC-20 allowance via raw eth_call (no viem dependency needed).
733
+ */
734
+ async readAllowance(rpcUrl, token, owner, spender) {
735
+ const selector = "0xdd62ed3e";
736
+ const paddedOwner = owner.slice(2).toLowerCase().padStart(64, "0");
737
+ const paddedSpender = spender.slice(2).toLowerCase().padStart(64, "0");
738
+ const data = selector + paddedOwner + paddedSpender;
739
+ try {
740
+ const response = await fetch(rpcUrl, {
741
+ method: "POST",
742
+ headers: { "Content-Type": "application/json" },
743
+ body: JSON.stringify({
744
+ jsonrpc: "2.0",
745
+ id: 1,
746
+ method: "eth_call",
747
+ params: [{ to: token, data }, "latest"]
748
+ })
749
+ });
750
+ const result = await response.json();
751
+ if (result.error || !result.result || result.result === "0x") return 0n;
752
+ return BigInt(result.result);
753
+ } catch {
754
+ return 0n;
755
+ }
756
+ }
757
+ /**
758
+ * Encode ERC-20 approve(address,uint256) calldata.
759
+ */
760
+ encodeApprove(spender, amount) {
761
+ const selector = "0x095ea7b3";
762
+ const paddedSpender = spender.slice(2).toLowerCase().padStart(64, "0");
763
+ const paddedAmount = amount.toString(16).padStart(64, "0");
764
+ return selector + paddedSpender + paddedAmount;
765
+ }
766
+ /**
767
+ * Wait for a transaction receipt by polling eth_getTransactionReceipt.
768
+ */
769
+ async waitForReceipt(rpcUrl, txHash, timeoutMs = 3e4) {
770
+ const start = Date.now();
771
+ while (Date.now() - start < timeoutMs) {
772
+ try {
773
+ const response = await fetch(rpcUrl, {
774
+ method: "POST",
775
+ headers: { "Content-Type": "application/json" },
776
+ body: JSON.stringify({
777
+ jsonrpc: "2.0",
778
+ id: 1,
779
+ method: "eth_getTransactionReceipt",
780
+ params: [txHash]
781
+ })
782
+ });
783
+ const result = await response.json();
784
+ if (result.result) {
785
+ if (result.result.status === "0x0") {
786
+ throw new Error(`Approval transaction reverted: ${txHash}`);
787
+ }
788
+ return;
789
+ }
790
+ } catch (err) {
791
+ if (err instanceof Error && err.message.includes("reverted")) throw err;
792
+ }
793
+ await new Promise((r) => setTimeout(r, 2e3));
794
+ }
795
+ throw new Error(`Approval transaction receipt timeout after ${timeoutMs}ms: ${txHash}`);
796
+ }
797
+ /**
798
+ * Calculate how much to approve based on the facilitator's approval strategy.
799
+ * Buffered approvals reduce the number of on-chain approval txs for micropayments.
800
+ */
801
+ calculateApprovalAmount(paymentAmount, fee, strategy) {
802
+ const total = BigInt(paymentAmount) + BigInt(fee);
803
+ if (!strategy || strategy.mode === "exact") {
804
+ return total;
805
+ }
806
+ const multiple = BigInt(strategy.defaultMultiple ?? 10);
807
+ const buffered = total * multiple;
808
+ if (strategy.maxCapUsd) {
809
+ const decimals = this.inferDecimals(paymentAmount);
810
+ const maxCap = BigInt(Math.floor(strategy.maxCapUsd * Math.pow(10, decimals)));
811
+ if (buffered > maxCap) return maxCap;
812
+ }
813
+ if (strategy.exactAboveUsd) {
814
+ const decimals = this.inferDecimals(paymentAmount);
815
+ const threshold = BigInt(Math.floor(strategy.exactAboveUsd * Math.pow(10, decimals)));
816
+ if (BigInt(paymentAmount) > threshold) return total;
817
+ }
818
+ return buffered;
819
+ }
820
+ /**
821
+ * Infer token decimals from payment amount magnitude.
822
+ * BSC stablecoins use 18 decimals, all others use 6.
823
+ * A $1 payment is 1000000 (6 dec) or 1000000000000000000 (18 dec).
824
+ * If the amount has > 12 digits, it's almost certainly 18 decimals.
825
+ */
826
+ inferDecimals(amount) {
827
+ return amount.length > 12 ? 18 : 6;
828
+ }
465
829
  };
466
830
  function createEvmAdapter(config) {
467
831
  return new EvmAdapter(config);
@@ -475,6 +839,9 @@ function isKnownUSDC(asset) {
475
839
  for (const addr of Object.values(USDC_ADDRESSES)) {
476
840
  if (addr.toLowerCase() === lc) return true;
477
841
  }
842
+ for (const addr of Object.keys(BSC_STABLECOIN_ADDRESSES)) {
843
+ if (addr.toLowerCase() === lc) return true;
844
+ }
478
845
  return false;
479
846
  }
480
847
  function createDefaultAdapters(verbose = false) {
@@ -492,9 +859,14 @@ function findAdapter(adapters, network) {
492
859
  AVALANCHE,
493
860
  BASE_MAINNET,
494
861
  BASE_SEPOLIA,
862
+ BSC_MAINNET,
863
+ BSC_STABLECOIN_ADDRESSES,
864
+ BSC_USDC,
865
+ BSC_USDT,
495
866
  ETHEREUM_MAINNET,
496
867
  EvmAdapter,
497
868
  OPTIMISM,
869
+ PERMIT2_ADDRESS,
498
870
  POLYGON,
499
871
  SKALE_BASE,
500
872
  SKALE_BASE_SEPOLIA,
@@ -503,6 +875,7 @@ function findAdapter(adapters, network) {
503
875
  SOLANA_TESTNET,
504
876
  SolanaAdapter,
505
877
  USDC_ADDRESSES,
878
+ X402_EXACT_PERMIT2_PROXY,
506
879
  createDefaultAdapters,
507
880
  createEvmAdapter,
508
881
  createSolanaAdapter,
@@ -511,4 +884,3 @@ function findAdapter(adapters, network) {
511
884
  isKnownUSDC,
512
885
  isSolanaWallet
513
886
  });
514
- //# sourceMappingURL=index.cjs.map
@@ -1,12 +1,11 @@
1
- import { C as ChainAdapter } from '../types-CfKflCZO.cjs';
2
- export { A as AdapterConfig, B as BalanceInfo, G as GenericWallet, S as SignedTransaction, W as WalletSet } from '../types-CfKflCZO.cjs';
3
- import { b as SolanaAdapter, d as EvmAdapter } from '../solana-BcOfK6Eq.cjs';
4
- export { A as ARBITRUM_ONE, k as AVALANCHE, B as BASE_MAINNET, j as BASE_SEPOLIA, n as ETHEREUM_MAINNET, E as EvmWallet, O as OPTIMISM, P as POLYGON, l as SKALE_BASE, m as SKALE_BASE_SEPOLIA, e as SOLANA_DEVNET, S as SOLANA_MAINNET, f as SOLANA_TESTNET, g as SolanaWallet, U as USDC_ADDRESSES, a as createEvmAdapter, c as createSolanaAdapter, h as isEvmWallet, i as isSolanaWallet } from '../solana-BcOfK6Eq.cjs';
5
- import '../types-CjLMR7qs.cjs';
1
+ import { e as SolanaAdapter, f as EvmAdapter, C as ChainAdapter } from '../types-D1u7iu8n.cjs';
2
+ export { q as ARBITRUM_ONE, s as AVALANCHE, A as AdapterConfig, B as BASE_MAINNET, p as BASE_SEPOLIA, l as BSC_MAINNET, o as BSC_STABLECOIN_ADDRESSES, n as BSC_USDC, m as BSC_USDT, d as BalanceInfo, v as ETHEREUM_MAINNET, E as EvmWallet, G as GenericWallet, O as OPTIMISM, P as PERMIT2_ADDRESS, r as POLYGON, t as SKALE_BASE, u as SKALE_BASE_SEPOLIA, h as SOLANA_DEVNET, b as SOLANA_MAINNET, j as SOLANA_TESTNET, g as SignedTransaction, S as SolanaWallet, U as USDC_ADDRESSES, W as WalletSet, X as X402_EXACT_PERMIT2_PROXY, a as createEvmAdapter, c as createSolanaAdapter, k as isEvmWallet, i as isSolanaWallet } from '../types-D1u7iu8n.cjs';
3
+ import '../types-_iT11DL0.cjs';
6
4
 
7
5
  /**
8
6
  * Check if an asset address is a known USDC contract (any chain).
9
7
  * Single source of truth for decimal inference in the client.
8
+ * Also recognizes BSC stablecoins (USDT + USDC, both 18 decimals).
10
9
  */
11
10
  declare function isKnownUSDC(asset: string): boolean;
12
11
  /**
@@ -1,12 +1,11 @@
1
- import { C as ChainAdapter } from '../types-BIHhO2-I.js';
2
- export { A as AdapterConfig, B as BalanceInfo, G as GenericWallet, S as SignedTransaction, W as WalletSet } from '../types-BIHhO2-I.js';
3
- import { b as SolanaAdapter, d as EvmAdapter } from '../solana-Cxr5byPa.js';
4
- export { A as ARBITRUM_ONE, k as AVALANCHE, B as BASE_MAINNET, j as BASE_SEPOLIA, n as ETHEREUM_MAINNET, E as EvmWallet, O as OPTIMISM, P as POLYGON, l as SKALE_BASE, m as SKALE_BASE_SEPOLIA, e as SOLANA_DEVNET, S as SOLANA_MAINNET, f as SOLANA_TESTNET, g as SolanaWallet, U as USDC_ADDRESSES, a as createEvmAdapter, c as createSolanaAdapter, h as isEvmWallet, i as isSolanaWallet } from '../solana-Cxr5byPa.js';
5
- import '../types-CjLMR7qs.js';
1
+ import { e as SolanaAdapter, f as EvmAdapter, C as ChainAdapter } from '../types-YQlJI5E3.js';
2
+ export { q as ARBITRUM_ONE, s as AVALANCHE, A as AdapterConfig, B as BASE_MAINNET, p as BASE_SEPOLIA, l as BSC_MAINNET, o as BSC_STABLECOIN_ADDRESSES, n as BSC_USDC, m as BSC_USDT, d as BalanceInfo, v as ETHEREUM_MAINNET, E as EvmWallet, G as GenericWallet, O as OPTIMISM, P as PERMIT2_ADDRESS, r as POLYGON, t as SKALE_BASE, u as SKALE_BASE_SEPOLIA, h as SOLANA_DEVNET, b as SOLANA_MAINNET, j as SOLANA_TESTNET, g as SignedTransaction, S as SolanaWallet, U as USDC_ADDRESSES, W as WalletSet, X as X402_EXACT_PERMIT2_PROXY, a as createEvmAdapter, c as createSolanaAdapter, k as isEvmWallet, i as isSolanaWallet } from '../types-YQlJI5E3.js';
3
+ import '../types-_iT11DL0.js';
6
4
 
7
5
  /**
8
6
  * Check if an asset address is a known USDC contract (any chain).
9
7
  * Single source of truth for decimal inference in the client.
8
+ * Also recognizes BSC stablecoins (USDT + USDC, both 18 decimals).
10
9
  */
11
10
  declare function isKnownUSDC(asset: string): boolean;
12
11
  /**