@pafi-dev/issuer 0.5.40 → 0.5.41

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.js CHANGED
@@ -2593,6 +2593,393 @@ function createSdkErrorMapper(factories) {
2593
2593
  };
2594
2594
  }
2595
2595
 
2596
+ // src/api/issuerApiAdapter.ts
2597
+ import { getAddress as getAddress10 } from "viem";
2598
+ import {
2599
+ buildAndSignSponsorAuth,
2600
+ computeAuthorizationHash,
2601
+ decodeBatchExecuteCalls as decodeBatchExecuteCalls4,
2602
+ encodeBatchExecute as encodeBatchExecute2,
2603
+ ENTRY_POINT_V08 as ENTRY_POINT_V083,
2604
+ getContractAddresses as getContractAddresses8,
2605
+ parseEip7702DelegatedAddress as parseEip7702DelegatedAddress2
2606
+ } from "@pafi-dev/core";
2607
+ var IssuerApiAdapter = class {
2608
+ cfg;
2609
+ constructor(config) {
2610
+ this.cfg = config;
2611
+ }
2612
+ // ------------------------------ Read endpoints ---------------------------
2613
+ async config(chainId) {
2614
+ const result = await this.cfg.issuerService.api.handleConfig(chainId);
2615
+ return {
2616
+ chainId: result.chainId,
2617
+ contracts: result.contracts
2618
+ };
2619
+ }
2620
+ async gasFee() {
2621
+ const result = await this.cfg.issuerService.api.handleGasFee();
2622
+ return { gasFeeUsdt: result.gasFeeUsdt.toString() };
2623
+ }
2624
+ async pools(authenticatedAddress, chainId, pointTokenAddress) {
2625
+ const result = await this.cfg.issuerService.api.handlePools(
2626
+ authenticatedAddress,
2627
+ { chainId, pointTokenAddress: getAddress10(pointTokenAddress) }
2628
+ );
2629
+ return { pools: result.pools };
2630
+ }
2631
+ async user(authenticatedAddress, chainId, userAddress, pointTokenAddress) {
2632
+ const result = await this.cfg.issuerService.api.handleUser(
2633
+ authenticatedAddress,
2634
+ {
2635
+ chainId,
2636
+ userAddress: getAddress10(userAddress),
2637
+ pointTokenAddress: getAddress10(pointTokenAddress)
2638
+ }
2639
+ );
2640
+ return {
2641
+ mintRequestNonce: result.mintRequestNonce.toString(),
2642
+ receiverConsentNonce: result.receiverConsentNonce.toString(),
2643
+ offChainBalance: result.offChainBalance.toString(),
2644
+ onChainBalance: result.onChainBalance.toString(),
2645
+ totalBalance: result.totalBalance.toString(),
2646
+ balance: result.offChainBalance.toString(),
2647
+ isMinter: result.isMinter
2648
+ };
2649
+ }
2650
+ async quote(authenticatedAddress, chainId, pointTokenAddress, pointAmount) {
2651
+ const [gasFeeResult, poolsResult] = await Promise.all([
2652
+ this.cfg.issuerService.api.handleGasFee(),
2653
+ this.cfg.issuerService.api.handlePools(authenticatedAddress, {
2654
+ chainId,
2655
+ pointTokenAddress: getAddress10(pointTokenAddress)
2656
+ })
2657
+ ]);
2658
+ const quote = await quotePointTokenToUsdt({
2659
+ provider: this.cfg.provider,
2660
+ chainId,
2661
+ pointTokenAddress: getAddress10(pointTokenAddress),
2662
+ pointAmount,
2663
+ pools: poolsResult.pools,
2664
+ gasFeeUsdt: gasFeeResult.gasFeeUsdt
2665
+ });
2666
+ const dto = {
2667
+ pointAmount: pointAmount.toString(),
2668
+ estimatedUsdtOut: quote.estimatedUsdtOut.toString(),
2669
+ gasFeeUsdt: gasFeeResult.gasFeeUsdt.toString(),
2670
+ netUsdtOut: quote.netUsdtOut.toString(),
2671
+ exchangeRate: quote.exchangeRate,
2672
+ suggestedDeadline: quote.suggestedDeadline.toString(),
2673
+ gasEstimate: quote.gasEstimate.toString()
2674
+ };
2675
+ if (quote.quoteError) dto.quoteError = quote.quoteError;
2676
+ return dto;
2677
+ }
2678
+ // ------------------------------ Action endpoints -------------------------
2679
+ async claim(input) {
2680
+ const pointTokenAddress = getAddress10(input.pointTokenAddress);
2681
+ const result = await this.cfg.ptClaimHandler.handle({
2682
+ authenticatedAddress: input.authenticatedAddress,
2683
+ userAddress: input.authenticatedAddress,
2684
+ amount: input.amount,
2685
+ pointTokenAddress,
2686
+ chainId: input.chainId,
2687
+ aaNonce: input.aaNonce,
2688
+ mintRequestNonce: input.mintRequestNonce
2689
+ });
2690
+ const sponsorAuth = await this.buildSponsorAuth(
2691
+ input.authenticatedAddress,
2692
+ result.userOp.callData,
2693
+ input.chainId,
2694
+ "mint"
2695
+ );
2696
+ return {
2697
+ calls: result.calls,
2698
+ callsFallback: result.callsFallback,
2699
+ feeAmount: result.feeAmount.toString(),
2700
+ lockId: result.lockId,
2701
+ signatureDeadline: result.signatureDeadline.toString(),
2702
+ sponsorAuth
2703
+ };
2704
+ }
2705
+ async redeem(input) {
2706
+ this.assertRedeemHandler();
2707
+ const response = await this.cfg.ptRedeemHandler.handle({
2708
+ userAddress: input.authenticatedAddress,
2709
+ authenticatedAddress: input.authenticatedAddress,
2710
+ amount: input.amount,
2711
+ aaNonce: input.aaNonce,
2712
+ chainId: input.chainId
2713
+ });
2714
+ const sponsorAuth = await this.buildSponsorAuth(
2715
+ input.authenticatedAddress,
2716
+ response.userOp.callData,
2717
+ input.chainId,
2718
+ "burn"
2719
+ );
2720
+ return {
2721
+ calls: decodeBatchExecuteCalls4(response.userOp.callData),
2722
+ callsFallback: response.fallback ? decodeBatchExecuteCalls4(response.fallback.userOp.callData) : void 0,
2723
+ feeAmount: response.feeAmount.toString(),
2724
+ lockId: response.lockId,
2725
+ lockIdFallback: response.fallback?.lockId,
2726
+ netCreditAmount: response.netCreditAmount.toString(),
2727
+ netCreditAmountFallback: response.fallback?.netCreditAmount.toString(),
2728
+ expiresInSeconds: response.expiresInSeconds,
2729
+ signatureDeadline: response.signatureDeadline.toString(),
2730
+ sponsorAuth
2731
+ };
2732
+ }
2733
+ async swap(input) {
2734
+ const result = await this.cfg.swapHandler.handle({
2735
+ userAddress: input.authenticatedAddress,
2736
+ chainId: input.chainId,
2737
+ pointTokenAddress: getAddress10(input.pointTokenAddress),
2738
+ amountIn: input.amountIn,
2739
+ aaNonce: input.aaNonce,
2740
+ slippageBps: input.slippageBps
2741
+ });
2742
+ const sponsorAuth = await this.buildSponsorAuth(
2743
+ input.authenticatedAddress,
2744
+ result.userOp.callData,
2745
+ input.chainId,
2746
+ "swap"
2747
+ );
2748
+ return {
2749
+ calls: result.calls,
2750
+ callsFallback: result.callsFallback,
2751
+ feeAmount: result.feeAmount.toString(),
2752
+ estimatedUsdtOut: result.estimatedUsdtOut.toString(),
2753
+ minAmountOut: result.minAmountOut.toString(),
2754
+ estimatedUsdtOutFallback: result.estimatedUsdtOutFallback?.toString(),
2755
+ minAmountOutFallback: result.minAmountOutFallback?.toString(),
2756
+ deadline: result.deadline.toString(),
2757
+ sponsorAuth
2758
+ };
2759
+ }
2760
+ async perpDeposit(input) {
2761
+ const result = await this.cfg.perpHandler.handle({
2762
+ userAddress: input.authenticatedAddress,
2763
+ chainId: input.chainId,
2764
+ amount: input.amount,
2765
+ brokerId: input.brokerId,
2766
+ aaNonce: input.aaNonce
2767
+ });
2768
+ const sponsorAuth = await this.buildSponsorAuth(
2769
+ input.authenticatedAddress,
2770
+ result.userOp.callData,
2771
+ input.chainId,
2772
+ "perp-deposit"
2773
+ );
2774
+ return {
2775
+ calls: result.calls,
2776
+ callsFallback: result.callsFallback,
2777
+ relayTokenFee: result.relayTokenFee.toString(),
2778
+ maxFee: result.maxFee.toString(),
2779
+ netDeposit: result.netDeposit.toString(),
2780
+ ptGasFee: result.feeAmount.toString(),
2781
+ accountId: result.accountId,
2782
+ brokerHash: result.brokerHash,
2783
+ usdcAddress: result.usdcAddress,
2784
+ relayAddress: result.relayAddress,
2785
+ sponsorAuth
2786
+ };
2787
+ }
2788
+ // ------------------------------ Mobile endpoints -------------------------
2789
+ async claimPrepare(input) {
2790
+ const pointTokenAddress = getAddress10(input.pointTokenAddress);
2791
+ const claimResult = await this.cfg.ptClaimHandler.handle({
2792
+ authenticatedAddress: input.authenticatedAddress,
2793
+ userAddress: input.authenticatedAddress,
2794
+ amount: input.amount,
2795
+ pointTokenAddress,
2796
+ chainId: input.chainId,
2797
+ aaNonce: input.aaNonce,
2798
+ mintRequestNonce: input.mintRequestNonce
2799
+ });
2800
+ const prepared = await this.runMobilePrepare(
2801
+ input.authenticatedAddress,
2802
+ input.chainId,
2803
+ claimResult.lockId,
2804
+ claimResult.userOp,
2805
+ claimResult.fallback,
2806
+ "mint",
2807
+ pointTokenAddress,
2808
+ claimResult.expiresInSeconds
2809
+ );
2810
+ return {
2811
+ lockId: claimResult.lockId,
2812
+ userOpHash: prepared.sponsored.userOpHash,
2813
+ typedData: prepared.sponsored.typedData,
2814
+ userOpHashFallback: prepared.fallback?.userOpHash,
2815
+ typedDataFallback: prepared.fallback?.typedData,
2816
+ feeAmount: claimResult.feeAmount.toString(),
2817
+ signatureDeadline: claimResult.signatureDeadline.toString(),
2818
+ expiresInSeconds: claimResult.expiresInSeconds,
2819
+ sponsored: prepared.isSponsored,
2820
+ needsDelegation: prepared.needsDelegation
2821
+ };
2822
+ }
2823
+ async claimSubmit(input) {
2824
+ return await handleMobileSubmit({
2825
+ lockId: input.lockId,
2826
+ authenticatedAddress: input.authenticatedAddress,
2827
+ signature: input.signature,
2828
+ variant: input.variant,
2829
+ store: this.cfg.pendingUserOpStore,
2830
+ bindUserOpHash: (lockId, hash) => this.cfg.ledger.bindMintUserOpHash(lockId, hash),
2831
+ pafiBackendClient: this.cfg.pafiBackendClient
2832
+ });
2833
+ }
2834
+ async redeemPrepare(input) {
2835
+ this.assertRedeemHandler();
2836
+ const pointTokenAddress = getAddress10(input.pointTokenAddress);
2837
+ const redeemResponse = await this.cfg.ptRedeemHandler.handle({
2838
+ userAddress: input.authenticatedAddress,
2839
+ authenticatedAddress: input.authenticatedAddress,
2840
+ amount: input.amount,
2841
+ aaNonce: input.aaNonce,
2842
+ chainId: input.chainId
2843
+ });
2844
+ const prepared = await this.runMobilePrepare(
2845
+ input.authenticatedAddress,
2846
+ input.chainId,
2847
+ redeemResponse.lockId,
2848
+ redeemResponse.userOp,
2849
+ redeemResponse.fallback?.userOp,
2850
+ "burn",
2851
+ pointTokenAddress,
2852
+ redeemResponse.expiresInSeconds
2853
+ );
2854
+ return {
2855
+ lockId: redeemResponse.lockId,
2856
+ userOpHash: prepared.sponsored.userOpHash,
2857
+ typedData: prepared.sponsored.typedData,
2858
+ userOpHashFallback: prepared.fallback?.userOpHash,
2859
+ typedDataFallback: prepared.fallback?.typedData,
2860
+ netCreditAmount: redeemResponse.netCreditAmount.toString(),
2861
+ netCreditAmountFallback: redeemResponse.fallback?.netCreditAmount.toString(),
2862
+ feeAmount: redeemResponse.feeAmount.toString(),
2863
+ signatureDeadline: redeemResponse.signatureDeadline.toString(),
2864
+ expiresInSeconds: redeemResponse.expiresInSeconds,
2865
+ sponsored: prepared.isSponsored,
2866
+ needsDelegation: prepared.needsDelegation
2867
+ };
2868
+ }
2869
+ async redeemSubmit(input) {
2870
+ return await handleMobileSubmit({
2871
+ lockId: input.lockId,
2872
+ authenticatedAddress: input.authenticatedAddress,
2873
+ signature: input.signature,
2874
+ variant: input.variant,
2875
+ store: this.cfg.pendingUserOpStore,
2876
+ bindUserOpHash: (lockId, hash) => this.cfg.ledger.bindCreditUserOpHash(lockId, hash),
2877
+ pafiBackendClient: this.cfg.pafiBackendClient
2878
+ });
2879
+ }
2880
+ async claimStatus(authenticatedAddress, lockId) {
2881
+ return await handleClaimStatus({
2882
+ lockId,
2883
+ userAddress: authenticatedAddress,
2884
+ ledger: this.cfg.ledger,
2885
+ pafiBackendClient: this.cfg.pafiBackendClient,
2886
+ onWarning: this.cfg.onWarning
2887
+ });
2888
+ }
2889
+ async redeemStatus(authenticatedAddress, lockId) {
2890
+ return await handleRedeemStatus({
2891
+ lockId,
2892
+ userAddress: authenticatedAddress,
2893
+ ledger: this.cfg.ledger,
2894
+ pafiBackendClient: this.cfg.pafiBackendClient,
2895
+ onWarning: this.cfg.onWarning
2896
+ });
2897
+ }
2898
+ // ------------------------------ Delegate endpoints -----------------------
2899
+ async delegateStatus(authenticatedAddress, chainId) {
2900
+ const { batchExecutor } = getContractAddresses8(chainId);
2901
+ const code = await this.cfg.provider.getCode({
2902
+ address: authenticatedAddress
2903
+ });
2904
+ return {
2905
+ isDelegated: parseEip7702DelegatedAddress2(code) !== null,
2906
+ batchExecutorAddress: batchExecutor
2907
+ };
2908
+ }
2909
+ async delegatePrepare(authenticatedAddress, chainId) {
2910
+ const { batchExecutor } = getContractAddresses8(chainId);
2911
+ const accountNonce = BigInt(
2912
+ await this.cfg.provider.getTransactionCount({
2913
+ address: authenticatedAddress
2914
+ })
2915
+ );
2916
+ const authorizationHash = computeAuthorizationHash(
2917
+ chainId,
2918
+ batchExecutor,
2919
+ accountNonce
2920
+ );
2921
+ return {
2922
+ authorizationHash,
2923
+ delegationNonce: accountNonce.toString(),
2924
+ batchExecutorAddress: batchExecutor,
2925
+ chainId
2926
+ };
2927
+ }
2928
+ async delegateSubmit(input) {
2929
+ const fees = await this.cfg.provider.estimateFeesPerGas();
2930
+ const result = await handleDelegateSubmit({
2931
+ userAddress: input.authenticatedAddress,
2932
+ chainId: input.chainId,
2933
+ delegationNonce: input.delegationNonce,
2934
+ aaNonce: input.aaNonce,
2935
+ authSig: input.authSig,
2936
+ fees,
2937
+ pafiBackendClient: this.cfg.pafiBackendClient,
2938
+ onWarning: this.cfg.onWarning
2939
+ });
2940
+ return { userOpHash: result.userOpHash };
2941
+ }
2942
+ // ------------------------------ Internal helpers -------------------------
2943
+ /**
2944
+ * Build + sign a SponsorAuth payload. Returns `undefined` when no
2945
+ * issuer id is configured, so the controller can skip the field.
2946
+ */
2947
+ async buildSponsorAuth(authenticatedAddress, callData, chainId, scenario) {
2948
+ if (!this.cfg.pafiIssuerId) return void 0;
2949
+ return buildAndSignSponsorAuth({
2950
+ userAddress: authenticatedAddress,
2951
+ callData,
2952
+ chainId,
2953
+ scenario,
2954
+ issuerId: this.cfg.pafiIssuerId,
2955
+ issuerSignerWallet: this.cfg.issuerSignerWallet
2956
+ });
2957
+ }
2958
+ async runMobilePrepare(authenticatedAddress, chainId, lockId, partialUserOp, partialUserOpFallback, scenario, pointTokenAddress, ttlSeconds) {
2959
+ return await handleMobilePrepare({
2960
+ userAddress: authenticatedAddress,
2961
+ chainId,
2962
+ lockId,
2963
+ partialUserOp,
2964
+ partialUserOpFallback,
2965
+ scenario,
2966
+ pointTokenAddress,
2967
+ ttlSeconds,
2968
+ store: this.cfg.pendingUserOpStore,
2969
+ provider: this.cfg.provider,
2970
+ pafiBackendClient: this.cfg.pafiBackendClient,
2971
+ onWarning: this.cfg.onWarning
2972
+ });
2973
+ }
2974
+ assertRedeemHandler() {
2975
+ if (!this.cfg.ptRedeemHandler) {
2976
+ throw new Error(
2977
+ "PTRedeemHandler not wired \u2014 IssuerApiAdapter.redeem* require a configured ptRedeemHandler."
2978
+ );
2979
+ }
2980
+ }
2981
+ };
2982
+
2596
2983
  // src/pools/subgraphPoolsProvider.ts
2597
2984
  import { isAddress } from "viem";
2598
2985
  import { PAFI_SUBGRAPH_URL } from "@pafi-dev/core";
@@ -3211,8 +3598,8 @@ var PafiBackendClient = class {
3211
3598
  };
3212
3599
 
3213
3600
  // src/config.ts
3214
- import { getAddress as getAddress10 } from "viem";
3215
- import { getContractAddresses as getContractAddresses8 } from "@pafi-dev/core";
3601
+ import { getAddress as getAddress11 } from "viem";
3602
+ import { getContractAddresses as getContractAddresses9 } from "@pafi-dev/core";
3216
3603
  function createIssuerService(config) {
3217
3604
  if (!config.provider) {
3218
3605
  throw new Error("createIssuerService: provider is required");
@@ -3232,7 +3619,7 @@ function createIssuerService(config) {
3232
3619
  "createIssuerService: at least one of pointTokenAddress / pointTokenAddresses is required"
3233
3620
  );
3234
3621
  }
3235
- const tokenAddresses = rawAddresses.map((a) => getAddress10(a));
3622
+ const tokenAddresses = rawAddresses.map((a) => getAddress11(a));
3236
3623
  const ledger = config.ledger;
3237
3624
  const sessionStore = config.sessionStore ?? new MemorySessionStore();
3238
3625
  const policy = config.policy ?? new DefaultPolicyEngine({ ledger });
@@ -3282,7 +3669,7 @@ function createIssuerService(config) {
3282
3669
  indexers.set(tokenAddress, new PointIndexer(indexerConfig));
3283
3670
  }
3284
3671
  const firstIndexer = indexers.get(tokenAddresses[0]);
3285
- const chainAddresses = getContractAddresses8(config.chainId);
3672
+ const chainAddresses = getContractAddresses9(config.chainId);
3286
3673
  const resolvedContracts = {
3287
3674
  batchExecutor: chainAddresses.batchExecutor,
3288
3675
  usdt: chainAddresses.usdt,
@@ -3330,11 +3717,11 @@ function createIssuerService(config) {
3330
3717
  }
3331
3718
 
3332
3719
  // src/issuer-state/validator.ts
3333
- import { getAddress as getAddress11 } from "viem";
3720
+ import { getAddress as getAddress12 } from "viem";
3334
3721
  import {
3335
3722
  POINT_TOKEN_V2_ABI as POINT_TOKEN_V2_ABI3,
3336
3723
  issuerRegistryGetIssuerFlatAbi,
3337
- getContractAddresses as getContractAddresses9
3724
+ getContractAddresses as getContractAddresses10
3338
3725
  } from "@pafi-dev/core";
3339
3726
  var ISSUER_RECORD_TTL_MS = 3e4;
3340
3727
  var IssuerStateValidator = class _IssuerStateValidator {
@@ -3352,7 +3739,7 @@ var IssuerStateValidator = class _IssuerStateValidator {
3352
3739
  * `CONTRACT_ADDRESSES` map for the given chain.
3353
3740
  */
3354
3741
  static forChain(provider, chainId) {
3355
- const { issuerRegistry } = getContractAddresses9(chainId);
3742
+ const { issuerRegistry } = getContractAddresses10(chainId);
3356
3743
  return new _IssuerStateValidator(provider, issuerRegistry);
3357
3744
  }
3358
3745
  /**
@@ -3361,7 +3748,7 @@ var IssuerStateValidator = class _IssuerStateValidator {
3361
3748
  */
3362
3749
  invalidate(pointToken) {
3363
3750
  if (pointToken) {
3364
- const key = getAddress11(pointToken);
3751
+ const key = getAddress12(pointToken);
3365
3752
  this.pointTokenIssuerCache.delete(key);
3366
3753
  this.stateCache.delete(key);
3367
3754
  this.inflight.delete(key);
@@ -3376,7 +3763,7 @@ var IssuerStateValidator = class _IssuerStateValidator {
3376
3763
  * The issuer field is set at `initialize()` and never changes.
3377
3764
  */
3378
3765
  async getIssuerAddressForPointToken(pointToken) {
3379
- const key = getAddress11(pointToken);
3766
+ const key = getAddress12(pointToken);
3380
3767
  const cached = this.pointTokenIssuerCache.get(key);
3381
3768
  if (cached) return cached;
3382
3769
  const issuer = await this.provider.readContract({
@@ -3384,15 +3771,15 @@ var IssuerStateValidator = class _IssuerStateValidator {
3384
3771
  abi: POINT_TOKEN_V2_ABI3,
3385
3772
  functionName: "issuer"
3386
3773
  });
3387
- this.pointTokenIssuerCache.set(key, getAddress11(issuer));
3388
- return getAddress11(issuer);
3774
+ this.pointTokenIssuerCache.set(key, getAddress12(issuer));
3775
+ return getAddress12(issuer);
3389
3776
  }
3390
3777
  /**
3391
3778
  * Read registry record + totalSupply, with 30s cache and in-flight
3392
3779
  * deduplication. Does NOT throw on inactive/missing — returns raw state.
3393
3780
  */
3394
3781
  async getIssuerState(pointToken) {
3395
- const tokenAddr = getAddress11(pointToken);
3782
+ const tokenAddr = getAddress12(pointToken);
3396
3783
  const now = Date.now();
3397
3784
  const cached = this.stateCache.get(tokenAddr);
3398
3785
  if (cached && cached.expiresAt > now) return cached.value;
@@ -3501,6 +3888,7 @@ export {
3501
3888
  DefaultPolicyEngine,
3502
3889
  FeeManager,
3503
3890
  InMemoryCursorStore,
3891
+ IssuerApiAdapter,
3504
3892
  IssuerApiHandlers,
3505
3893
  IssuerStateError,
3506
3894
  IssuerStateValidator,