@faremeter/payment-evm 0.16.0 → 0.17.1

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.
@@ -1,10 +1,25 @@
1
1
  import { type PublicClient } from "viem";
2
2
  import { type Address } from "@faremeter/types/evm";
3
+ /**
4
+ * Arguments for retrieving an ERC-20 token balance.
5
+ */
3
6
  export interface GetTokenBalanceArgs {
7
+ /** The wallet address to check the balance for */
4
8
  account: Address;
9
+ /** The ERC-20 token contract address */
5
10
  asset: Address;
11
+ /** Viem public client for querying the chain */
6
12
  client: PublicClient;
7
13
  }
14
+ /**
15
+ * Retrieves the ERC-20 token balance and decimals for an account.
16
+ *
17
+ * Uses multicall to fetch both values in a single RPC request.
18
+ *
19
+ * @param args - The account, asset, and client configuration
20
+ * @returns The balance amount and token decimals
21
+ * @throws Error if the balance query fails
22
+ */
8
23
  export declare function getTokenBalance(args: GetTokenBalanceArgs): Promise<{
9
24
  amount: bigint;
10
25
  decimals: number;
@@ -1 +1 @@
1
- {"version":3,"file":"erc20.d.ts","sourceRoot":"","sources":["../../src/erc20.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,MAAM,CAAC;AACzC,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAEpD,MAAM,WAAW,mBAAmB;IAClC,OAAO,EAAE,OAAO,CAAC;IACjB,KAAK,EAAE,OAAO,CAAC;IACf,MAAM,EAAE,YAAY,CAAC;CACtB;AAED,wBAAsB,eAAe,CAAC,IAAI,EAAE,mBAAmB;;;GA4C9D"}
1
+ {"version":3,"file":"erc20.d.ts","sourceRoot":"","sources":["../../src/erc20.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,MAAM,CAAC;AACzC,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAEpD;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,kDAAkD;IAClD,OAAO,EAAE,OAAO,CAAC;IACjB,wCAAwC;IACxC,KAAK,EAAE,OAAO,CAAC;IACf,gDAAgD;IAChD,MAAM,EAAE,YAAY,CAAC;CACtB;AAED;;;;;;;;GAQG;AACH,wBAAsB,eAAe,CAAC,IAAI,EAAE,mBAAmB;;;GA4C9D"}
package/dist/src/erc20.js CHANGED
@@ -1,5 +1,14 @@
1
1
  import {} from "viem";
2
2
  import {} from "@faremeter/types/evm";
3
+ /**
4
+ * Retrieves the ERC-20 token balance and decimals for an account.
5
+ *
6
+ * Uses multicall to fetch both values in a single RPC request.
7
+ *
8
+ * @param args - The account, asset, and client configuration
9
+ * @returns The balance amount and token decimals
10
+ * @throws Error if the balance query fails
11
+ */
3
12
  export async function getTokenBalance(args) {
4
13
  const { account, asset, client } = args;
5
14
  const [balance, decimals] = await client.multicall({
@@ -16,9 +16,23 @@ interface WalletForPayment {
16
16
  }) => Promise<Hex>;
17
17
  };
18
18
  }
19
+ /**
20
+ * Options for creating an EVM exact payment handler.
21
+ */
19
22
  export type CreatePaymentHandlerOpts = {
23
+ /** The asset to use for payments (defaults to USDC) */
20
24
  asset?: AssetNameOrContractInfo;
21
25
  };
26
+ /**
27
+ * Creates a payment handler for the EVM exact payment scheme.
28
+ *
29
+ * The handler generates EIP-3009 transferWithAuthorization signatures
30
+ * that can be executed by the facilitator to complete payments.
31
+ *
32
+ * @param wallet - Wallet providing chain context and signing capabilities
33
+ * @param opts - Optional configuration for the asset
34
+ * @returns A PaymentHandler function for use with the x402 client
35
+ */
22
36
  export declare function createPaymentHandler(wallet: WalletForPayment, opts?: CreatePaymentHandlerOpts): PaymentHandler;
23
37
  export {};
24
38
  //# sourceMappingURL=client.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../src/exact/client.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,cAAc,EAGf,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAGL,KAAK,uBA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AE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;YAC/B,WAAW,EAAE,MAAM,CAAC;YACpB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;SAClC,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;KACpB,CAAC;CACH;AAED;;GAEG;AACH,MAAM,MAAM,wBAAwB,GAAG;IACrC,uDAAuD;IACvD,KAAK,CAAC,EAAE,uBAAuB,CAAC;CACjC,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,gBAAgB,EACxB,IAAI,GAAE,wBAA6B,GAClC,cAAc,CAqGhB"}
@@ -4,6 +4,16 @@ import { isAddress } from "viem";
4
4
  import { type } from "arktype";
5
5
  import { EIP712_TYPES, eip712Domain, } from "./constants.js";
6
6
  import { generateMatcher } from "./common.js";
7
+ /**
8
+ * Creates a payment handler for the EVM exact payment scheme.
9
+ *
10
+ * The handler generates EIP-3009 transferWithAuthorization signatures
11
+ * that can be executed by the facilitator to complete payments.
12
+ *
13
+ * @param wallet - Wallet providing chain context and signing capabilities
14
+ * @param opts - Optional configuration for the asset
15
+ * @returns A PaymentHandler function for use with the x402 client
16
+ */
7
17
  export function createPaymentHandler(wallet, opts = {}) {
8
18
  const x402Network = lookupX402Network(wallet.chain.id);
9
19
  const assetInfo = findAssetInfo(x402Network, opts.asset ?? "USDC");
@@ -29,7 +39,7 @@ export function createPaymentHandler(wallet, opts = {}) {
29
39
  const authorization = {
30
40
  from: wallet.address,
31
41
  to: payToAddress,
32
- value: requirements.maxAmountRequired, // String value of amount
42
+ value: requirements.amount, // String value of amount
33
43
  validAfter: validAfter.toString(),
34
44
  validBefore: validBefore.toString(),
35
45
  nonce: nonce,
@@ -56,7 +66,7 @@ export function createPaymentHandler(wallet, opts = {}) {
56
66
  const message = {
57
67
  from: wallet.address,
58
68
  to: payToAddress,
59
- value: BigInt(requirements.maxAmountRequired),
69
+ value: BigInt(requirements.amount),
60
70
  validAfter: BigInt(validAfter),
61
71
  validBefore: BigInt(validBefore),
62
72
  nonce: nonce,
@@ -96,6 +96,10 @@ export declare const EIP712_TYPES: {
96
96
  readonly type: "bytes32";
97
97
  }];
98
98
  };
99
+ /**
100
+ * Validator for the x402 exact payment payload containing
101
+ * an EIP-3009 authorization and signature.
102
+ */
99
103
  export declare const x402ExactPayload: import("arktype/internal/methods/object.ts").ObjectType<{
100
104
  signature: (In: string) => import("arktype").Out<`0x${string}`>;
101
105
  authorization: {
@@ -109,6 +113,9 @@ export declare const x402ExactPayload: import("arktype/internal/methods/object.t
109
113
  }, {}>;
110
114
  export type x402ExactPayload = typeof x402ExactPayload.infer;
111
115
  export type eip3009Authorization = x402ExactPayload["authorization"];
116
+ /**
117
+ * Validator for EIP-712 domain parameters provided in payment requirements.
118
+ */
112
119
  export declare const eip712Domain: import("arktype/internal/methods/object.ts").ObjectType<{
113
120
  name?: string;
114
121
  version?: string;
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../src/exact/constants.ts"],"names":[],"mappings":"AAUA,eAAO,MAAM,iBAAiB,UAAU,CAAC;AAEzC,eAAO,MAAM,+BAA+B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAiDlC,CAAC;AAEX,eAAO,MAAM,YAAY;;;;;;;;;;;;;;;;;;;;CASf,CAAC;AAEX,eAAO,MAAM,gBAAgB;;;;;;;;;;MAU3B,CAAC;AAEH,MAAM,MAAM,gBAAgB,GAAG,OAAO,gBAAgB,CAAC,KAAK,CAAC;AAC7D,MAAM,MAAM,oBAAoB,GAAG,gBAAgB,CAAC,eAAe,CAAC,CAAC;AAErE,eAAO,MAAM,YAAY;;;;;MAKvB,CAAC;AAEH,MAAM,MAAM,YAAY,GAAG,OAAO,YAAY,CAAC,KAAK,CAAC"}
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../src/exact/constants.ts"],"names":[],"mappings":"AAUA,eAAO,MAAM,iBAAiB,UAAU,CAAC;AAEzC,eAAO,MAAM,+BAA+B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAiDlC,CAAC;AAEX,eAAO,MAAM,YAAY;;;;;;;;;;;;;;;;;;;;CASf,CAAC;AAEX;;;GAGG;AACH,eAAO,MAAM,gBAAgB;;;;;;;;;;MAU3B,CAAC;AAEH,MAAM,MAAM,gBAAgB,GAAG,OAAO,gBAAgB,CAAC,KAAK,CAAC;AAC7D,MAAM,MAAM,oBAAoB,GAAG,gBAAgB,CAAC,eAAe,CAAC,CAAC;AAErE;;GAEG;AACH,eAAO,MAAM,YAAY;;;;;MAKvB,CAAC;AAEH,MAAM,MAAM,YAAY,GAAG,OAAO,YAAY,CAAC,KAAK,CAAC"}
@@ -67,6 +67,10 @@ export const EIP712_TYPES = {
67
67
  { name: "nonce", type: "bytes32" },
68
68
  ],
69
69
  };
70
+ /**
71
+ * Validator for the x402 exact payment payload containing
72
+ * an EIP-3009 authorization and signature.
73
+ */
70
74
  export const x402ExactPayload = type({
71
75
  signature: prefixedHexString,
72
76
  authorization: {
@@ -78,6 +82,9 @@ export const x402ExactPayload = type({
78
82
  nonce: prefixedHexString,
79
83
  },
80
84
  });
85
+ /**
86
+ * Validator for EIP-712 domain parameters provided in payment requirements.
87
+ */
81
88
  export const eip712Domain = type({
82
89
  "name?": "string",
83
90
  "version?": "string",
@@ -1,11 +1,23 @@
1
1
  import { type ChainInfoWithRPC } from "@faremeter/types/evm";
2
2
  import { type FacilitatorHandler } from "@faremeter/types/facilitator";
3
3
  import type { Transport } from "viem";
4
- import { type KnownX402Network, type AssetNameOrContractInfo } from "@faremeter/info/evm";
4
+ import { type AssetNameOrContractInfo } from "@faremeter/info/evm";
5
5
  type CreateFacilitatorHandlerOpts = {
6
- network?: KnownX402Network;
6
+ network?: string;
7
7
  transport?: Transport;
8
8
  };
9
+ /**
10
+ * Creates a facilitator handler for the EVM exact payment scheme.
11
+ *
12
+ * The handler verifies EIP-3009 authorization signatures and executes
13
+ * the transferWithAuthorization call on the token contract.
14
+ *
15
+ * @param chain - Chain configuration including RPC URLs
16
+ * @param privateKey - Private key for signing settlement transactions
17
+ * @param assetNameOrInfo - Asset name or contract info for the token
18
+ * @param opts - Optional configuration for network and transport
19
+ * @returns A FacilitatorHandler for processing EVM exact payments
20
+ */
9
21
  export declare function createFacilitatorHandler(chain: ChainInfoWithRPC, privateKey: string, assetNameOrInfo: AssetNameOrContractInfo, opts?: CreateFacilitatorHandlerOpts): Promise<FacilitatorHandler>;
10
22
  export {};
11
23
  //# sourceMappingURL=facilitator.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"facilitator.d.ts","sourceRoot":"","sources":["../../../src/exact/facilitator.ts"],"names":[],"mappings":"AAQA,OAAO,EAAgB,KAAK,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAC3E,OAAO,EAAE,KAAK,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAGvE,OAAO,KAAK,EAAgB,SAAS,EAAE,MAAM,MAAM,CAAC;AAapD,OAAO,EAEL,KAAK,gBAAgB,EAErB,KAAK,uBAAuB,EAC7B,MAAM,qBAAqB,CAAC;AAuB7B,KAAK,4BAA4B,GAAG;IAClC,OAAO,CAAC,EAAE,gBAAgB,CAAC;IAC3B,SAAS,CAAC,EAAE,SAAS,CAAC;CACvB,CAAC;AACF,wBAAsB,wBAAwB,CAC5C,KAAK,EAAE,gBAAgB,EACvB,UAAU,EAAE,MAAM,EAClB,eAAe,EAAE,uBAAuB,EACxC,IAAI,GAAE,4BAAiC,GACtC,OAAO,CAAC,kBAAkB,CAAC,CAqV7B"}
1
+ {"version":3,"file":"facilitator.d.ts","sourceRoot":"","sources":["../../../src/exact/facilitator.ts"],"names":[],"mappings":"AAQA,OAAO,EAAgB,KAAK,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAC3E,OAAO,EAAE,KAAK,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAGvE,OAAO,KAAK,EAAgB,SAAS,EAAE,MAAM,MAAM,CAAC;AAapD,OAAO,EAGL,KAAK,uBAAuB,EAC7B,MAAM,qBAAqB,CAAC;AAuB7B,KAAK,4BAA4B,GAAG;IAClC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,SAAS,CAAC;CACvB,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,wBAAsB,wBAAwB,CAC5C,KAAK,EAAE,gBAAgB,EACvB,UAAU,EAAE,MAAM,EAClB,eAAe,EAAE,uBAAuB,EACxC,IAAI,GAAE,4BAAiC,GACtC,OAAO,CAAC,kBAAkB,CAAC,CAsV7B"}
@@ -13,6 +13,18 @@ function parseSignature(signature) {
13
13
  const v = parseInt(sig.slice(128, 130), 16);
14
14
  return { v, r, s };
15
15
  }
16
+ /**
17
+ * Creates a facilitator handler for the EVM exact payment scheme.
18
+ *
19
+ * The handler verifies EIP-3009 authorization signatures and executes
20
+ * the transferWithAuthorization call on the token contract.
21
+ *
22
+ * @param chain - Chain configuration including RPC URLs
23
+ * @param privateKey - Private key for signing settlement transactions
24
+ * @param assetNameOrInfo - Asset name or contract info for the token
25
+ * @param opts - Optional configuration for network and transport
26
+ * @returns A FacilitatorHandler for processing EVM exact payments
27
+ */
16
28
  export async function createFacilitatorHandler(chain, privateKey, assetNameOrInfo, opts = {}) {
17
29
  if (!isPrivateKey(privateKey)) {
18
30
  throw new Error(`Invalid private key: ${privateKey}`);
@@ -71,13 +83,13 @@ export async function createFacilitatorHandler(chain, privateKey, assetNameOrInf
71
83
  const getSupported = () => {
72
84
  return [
73
85
  Promise.resolve({
74
- x402Version: 1,
86
+ x402Version: 2,
75
87
  network,
76
88
  scheme: X402_EXACT_SCHEME,
77
89
  }),
78
90
  ];
79
91
  };
80
- const getRequirements = async (req) => {
92
+ const getRequirements = async ({ accepts: req, }) => {
81
93
  return req.filter(isMatchingRequirement).map((x) => ({
82
94
  ...x,
83
95
  asset,
@@ -104,7 +116,7 @@ export async function createFacilitatorHandler(chain, privateKey, assetNameOrInf
104
116
  return errorResponse("Payment authorized to wrong address");
105
117
  }
106
118
  // Check if the amount matches
107
- if (authorization.value !== requirements.maxAmountRequired) {
119
+ if (authorization.value !== requirements.amount) {
108
120
  return errorResponse("Incorrect payment amount");
109
121
  }
110
122
  // Check if the authorization is still valid (time-wise)
@@ -204,9 +216,9 @@ export async function createFacilitatorHandler(chain, privateKey, assetNameOrInf
204
216
  const errorResponse = (msg) => {
205
217
  return {
206
218
  success: false,
207
- error: msg,
208
- txHash: null,
209
- networkId: null,
219
+ errorReason: msg,
220
+ transaction: "",
221
+ network: requirements.network,
210
222
  };
211
223
  };
212
224
  const verifyResult = await verifyTransaction(requirements, payment);
@@ -264,9 +276,8 @@ export async function createFacilitatorHandler(chain, privateKey, assetNameOrInf
264
276
  }
265
277
  return {
266
278
  success: true,
267
- error: null,
268
- txHash,
269
- networkId: chainId.toString(),
279
+ transaction: txHash,
280
+ network: payment.accepted.network,
270
281
  };
271
282
  }
272
283
  catch (cause) {
@@ -1,3 +1,19 @@
1
+ /**
2
+ * @title EVM Payment Package
3
+ * @sidebarTitle Payment EVM
4
+ * @description EVM payment handlers for ERC-20 and exact payment schemes
5
+ * @packageDocumentation
6
+ */
7
+ /**
8
+ * @title EVM Exact Payment Scheme
9
+ * @sidebarTitle Payment EVM / Exact
10
+ * @description EIP-3009 transferWithAuthorization payment handlers
11
+ */
1
12
  export * as exact from "./exact/index.js";
13
+ /**
14
+ * @title ERC-20 Utilities
15
+ * @sidebarTitle Payment EVM / ERC-20
16
+ * @description ERC-20 token balance and transfer utilities
17
+ */
2
18
  export * as erc20 from "./erc20.js";
3
19
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH;;;;GAIG;AACH,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC;;;;GAIG;AACH,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC"}
package/dist/src/index.js CHANGED
@@ -1,2 +1,18 @@
1
+ /**
2
+ * @title EVM Payment Package
3
+ * @sidebarTitle Payment EVM
4
+ * @description EVM payment handlers for ERC-20 and exact payment schemes
5
+ * @packageDocumentation
6
+ */
7
+ /**
8
+ * @title EVM Exact Payment Scheme
9
+ * @sidebarTitle Payment EVM / Exact
10
+ * @description EIP-3009 transferWithAuthorization payment handlers
11
+ */
1
12
  export * as exact from "./exact/index.js";
13
+ /**
14
+ * @title ERC-20 Utilities
15
+ * @sidebarTitle Payment EVM / ERC-20
16
+ * @description ERC-20 token balance and transfer utilities
17
+ */
2
18
  export * as erc20 from "./erc20.js";