@faremeter/info 0.15.0 → 0.17.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 CHANGED
@@ -24,6 +24,33 @@ pnpm install @faremeter/info
24
24
 
25
25
  <!-- TSDOC_START -->
26
26
 
27
+ ## Functions
28
+
29
+ - [normalizeNetworkId](#normalizenetworkid)
30
+ - [translateNetworkToLegacy](#translatenetworktolegacy)
31
+
32
+ ### normalizeNetworkId
33
+
34
+ Normalize a legacy network identifier to CAIP-2 format.
35
+ Handles both EVM and Solana networks.
36
+ Returns the input unchanged if no mapping exists (may already be CAIP-2
37
+ or an unknown network).
38
+
39
+ | Function | Type |
40
+ | -------------------- | ----------------------------- |
41
+ | `normalizeNetworkId` | `(network: string) => string` |
42
+
43
+ ### translateNetworkToLegacy
44
+
45
+ Translate a CAIP-2 network identifier to legacy format.
46
+ Handles both EVM and Solana networks.
47
+ Returns the input unchanged if no mapping exists (may not be a known
48
+ CAIP-2 network, or may already be legacy).
49
+
50
+ | Function | Type |
51
+ | -------------------------- | ----------------------------- |
52
+ | `translateNetworkToLegacy` | `(network: string) => string` |
53
+
27
54
  <!-- TSDOC_END -->
28
55
 
29
56
  ## Related Packages
@@ -6,10 +6,10 @@ export declare function addX402PaymentRequirementDefaults(req: Partial<x402Payme
6
6
  maxAmountRequired: string;
7
7
  resource: string;
8
8
  description: string;
9
- mimeType: string;
10
9
  payTo: string;
11
10
  maxTimeoutSeconds: number;
12
11
  asset: string;
12
+ mimeType?: string;
13
13
  outputSchema?: object;
14
14
  extra?: object;
15
15
  }>;
package/dist/src/evm.d.ts CHANGED
@@ -1,31 +1,141 @@
1
1
  import { type UnitInput } from "./common.js";
2
2
  import { Address } from "@faremeter/types/evm";
3
3
  declare const knownX402Networks: {
4
- readonly base: {
4
+ readonly "eip155:8453": {
5
+ readonly legacyName: "base";
5
6
  readonly chainId: 8453;
6
7
  };
7
- readonly "base-sepolia": {
8
+ readonly "eip155:84532": {
9
+ readonly legacyName: "base-sepolia";
8
10
  readonly chainId: 84532;
9
11
  };
10
- readonly "skale-europa-testnet": {
12
+ readonly "eip155:1444673419": {
13
+ readonly legacyName: "skale-europa-testnet";
11
14
  readonly chainId: 1444673419;
12
15
  };
16
+ readonly "eip155:137": {
17
+ readonly legacyName: "polygon";
18
+ readonly chainId: 137;
19
+ };
20
+ readonly "eip155:80002": {
21
+ readonly legacyName: "polygon-amoy";
22
+ readonly chainId: 80002;
23
+ };
24
+ readonly "eip155:143": {
25
+ readonly legacyName: "monad";
26
+ readonly chainId: 143;
27
+ };
28
+ readonly "eip155:10143": {
29
+ readonly legacyName: "monad-testnet";
30
+ readonly chainId: 10143;
31
+ };
32
+ readonly "eip155:324705682": {
33
+ readonly legacyName: "skale-base-sepolia";
34
+ readonly chainId: 324705682;
35
+ };
36
+ readonly "eip155:1187947933": {
37
+ readonly legacyName: "skale-base";
38
+ readonly chainId: 1187947933;
39
+ };
13
40
  };
14
- type knownX402Networks = typeof knownX402Networks;
15
- export type KnownX402Network = keyof knownX402Networks;
16
- export declare function isKnownX402Network(n: string): n is KnownX402Network;
17
- export declare function lookupKnownX402Network(n: KnownX402Network): {
18
- name: "base" | "base-sepolia" | "skale-europa-testnet";
41
+ type KnownX402Networks = typeof knownX402Networks;
42
+ export type CAIP2Network = keyof KnownX402Networks;
43
+ export type LegacyNetworkName = KnownX402Networks[CAIP2Network]["legacyName"];
44
+ /**
45
+ * Converts an EVM chain ID to CAIP-2 network identifier.
46
+ *
47
+ * @param chainId - The EVM chain ID
48
+ * @returns The CAIP-2 network identifier (eip155:chainId)
49
+ */
50
+ export declare function chainIdToCAIP2(chainId: number): string;
51
+ /**
52
+ * Extracts the EVM chain ID from a CAIP-2 network identifier.
53
+ *
54
+ * @param caip2 - The CAIP-2 network identifier
55
+ * @returns The chain ID, or null if not a valid eip155 identifier
56
+ */
57
+ export declare function caip2ToChainId(caip2: string): number | null;
58
+ /**
59
+ * Converts a legacy EVM network name to CAIP-2 format.
60
+ *
61
+ * @param legacy - Legacy network name (e.g., "base", "polygon")
62
+ * @returns The CAIP-2 network identifier, or null if unknown
63
+ */
64
+ export declare function legacyNameToCAIP2(legacy: string): string | null;
65
+ /**
66
+ * Converts a CAIP-2 network identifier to legacy EVM network name.
67
+ *
68
+ * @param caip2 - The CAIP-2 network identifier
69
+ * @returns The legacy network name, or null if unknown
70
+ */
71
+ export declare function caip2ToLegacyName(caip2: string): string | null;
72
+ /**
73
+ * Normalizes an EVM network identifier to CAIP-2 format.
74
+ *
75
+ * Accepts chain IDs (number or string), legacy names, or CAIP-2 identifiers.
76
+ * Returns the input unchanged if no mapping exists.
77
+ *
78
+ * @param network - The network identifier in any supported format
79
+ * @returns The CAIP-2 network identifier
80
+ */
81
+ export declare function normalizeNetworkId(network: string | number): string;
82
+ /**
83
+ * Type guard that checks if a string is a known EVM CAIP-2 network.
84
+ *
85
+ * @param n - The string to check
86
+ * @returns True if the string is a known EVM CAIP-2 network identifier
87
+ */
88
+ export declare function isKnownCAIP2Network(n: string): n is CAIP2Network;
89
+ /**
90
+ * Looks up network information by CAIP-2 identifier.
91
+ *
92
+ * @param n - The CAIP-2 network identifier
93
+ * @returns Network information including legacy name and chain ID
94
+ */
95
+ export declare function lookupKnownCAIP2Network(n: CAIP2Network): {
96
+ caip2: "eip155:8453" | "eip155:84532" | "eip155:1444673419" | "eip155:137" | "eip155:80002" | "eip155:143" | "eip155:10143" | "eip155:324705682" | "eip155:1187947933";
97
+ legacyName: "base";
19
98
  chainId: 8453;
20
99
  } | {
21
- name: "base" | "base-sepolia" | "skale-europa-testnet";
100
+ caip2: "eip155:8453" | "eip155:84532" | "eip155:1444673419" | "eip155:137" | "eip155:80002" | "eip155:143" | "eip155:10143" | "eip155:324705682" | "eip155:1187947933";
101
+ legacyName: "base-sepolia";
22
102
  chainId: 84532;
23
103
  } | {
24
- name: "base" | "base-sepolia" | "skale-europa-testnet";
104
+ caip2: "eip155:8453" | "eip155:84532" | "eip155:1444673419" | "eip155:137" | "eip155:80002" | "eip155:143" | "eip155:10143" | "eip155:324705682" | "eip155:1187947933";
105
+ legacyName: "skale-europa-testnet";
25
106
  chainId: 1444673419;
107
+ } | {
108
+ caip2: "eip155:8453" | "eip155:84532" | "eip155:1444673419" | "eip155:137" | "eip155:80002" | "eip155:143" | "eip155:10143" | "eip155:324705682" | "eip155:1187947933";
109
+ legacyName: "polygon";
110
+ chainId: 137;
111
+ } | {
112
+ caip2: "eip155:8453" | "eip155:84532" | "eip155:1444673419" | "eip155:137" | "eip155:80002" | "eip155:143" | "eip155:10143" | "eip155:324705682" | "eip155:1187947933";
113
+ legacyName: "polygon-amoy";
114
+ chainId: 80002;
115
+ } | {
116
+ caip2: "eip155:8453" | "eip155:84532" | "eip155:1444673419" | "eip155:137" | "eip155:80002" | "eip155:143" | "eip155:10143" | "eip155:324705682" | "eip155:1187947933";
117
+ legacyName: "monad";
118
+ chainId: 143;
119
+ } | {
120
+ caip2: "eip155:8453" | "eip155:84532" | "eip155:1444673419" | "eip155:137" | "eip155:80002" | "eip155:143" | "eip155:10143" | "eip155:324705682" | "eip155:1187947933";
121
+ legacyName: "monad-testnet";
122
+ chainId: 10143;
123
+ } | {
124
+ caip2: "eip155:8453" | "eip155:84532" | "eip155:1444673419" | "eip155:137" | "eip155:80002" | "eip155:143" | "eip155:10143" | "eip155:324705682" | "eip155:1187947933";
125
+ legacyName: "skale-base-sepolia";
126
+ chainId: 324705682;
127
+ } | {
128
+ caip2: "eip155:8453" | "eip155:84532" | "eip155:1444673419" | "eip155:137" | "eip155:80002" | "eip155:143" | "eip155:10143" | "eip155:324705682" | "eip155:1187947933";
129
+ legacyName: "skale-base";
130
+ chainId: 1187947933;
26
131
  };
27
- export type x402Network = KnownX402Network | `eip155:${number}`;
28
- export declare function lookupX402Network(chainId: number): x402Network;
132
+ /**
133
+ * Looks up the x402 network identifier for an EVM chain ID.
134
+ *
135
+ * @param chainId - The EVM chain ID
136
+ * @returns The CAIP-2 network identifier
137
+ */
138
+ export declare function lookupX402Network(chainId: number): string;
29
139
  export type ContractInfo = {
30
140
  address: Address;
31
141
  contractName: string;
@@ -36,15 +146,15 @@ export type ContractInfo = {
36
146
  declare const knownAssets: {
37
147
  readonly USDC: {
38
148
  readonly network: {
39
- readonly "base-sepolia": {
149
+ readonly "eip155:84532": {
40
150
  readonly address: "0x036cbd53842c5426634e7929541ec2318f3dcf7e";
41
151
  readonly contractName: "USDC";
42
152
  };
43
- readonly base: {
153
+ readonly "eip155:8453": {
44
154
  readonly address: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913";
45
155
  readonly contractName: "USD Coin";
46
156
  };
47
- readonly "skale-europa-testnet": {
157
+ readonly "eip155:1444673419": {
48
158
  readonly address: "0x9eAb55199f4481eCD7659540A17Af618766b07C4";
49
159
  readonly contractName: "USDC";
50
160
  readonly forwarder: "0x7779B0d1766e6305E5f8081E3C0CDF58FcA24330";
@@ -67,14 +177,29 @@ declare const knownAssets: {
67
177
  readonly address: "0x534b2f3A21130d7a60830c2Df862319e593943A3";
68
178
  readonly contractName: "USDC";
69
179
  };
180
+ readonly "eip155:324705682": {
181
+ readonly address: "0x2e08028E3C4c2356572E096d8EF835cD5C6030bD";
182
+ readonly contractName: "Bridged USDC (SKALE Bridge)";
183
+ };
184
+ readonly "eip155:1187947933": {
185
+ readonly address: "0x85889c8c714505E0c94b30fcfcF64fE3Ac8FCb20";
186
+ readonly contractName: "Bridged USDC (SKALE Bridge)";
187
+ };
70
188
  };
71
189
  readonly toUnit: (v: UnitInput) => string;
72
190
  };
73
191
  };
74
192
  export type KnownAsset = keyof typeof knownAssets;
75
- export declare function lookupKnownAsset(network: x402Network | number, name: KnownAsset): {
193
+ /**
194
+ * Looks up asset information by network and asset name.
195
+ *
196
+ * @param network - The network identifier (chain ID, CAIP-2, or legacy name)
197
+ * @param name - The known asset name (e.g., "USDC")
198
+ * @returns Asset information including contract address, or undefined if not found
199
+ */
200
+ export declare function lookupKnownAsset(network: string | number, name: KnownAsset): {
76
201
  name: "USDC";
77
- network: x402Network;
202
+ network: string;
78
203
  toUnit: (v: UnitInput) => string;
79
204
  address: Address;
80
205
  contractName: string;
@@ -82,25 +207,45 @@ export declare function lookupKnownAsset(network: x402Network | number, name: Kn
82
207
  forwarderName?: string;
83
208
  forwarderVersion?: string;
84
209
  } | undefined;
210
+ /**
211
+ * Type guard that checks if a string is a known EVM asset name.
212
+ *
213
+ * @param asset - The string to check
214
+ * @returns True if the string is a known asset name
215
+ */
85
216
  export declare function isKnownAsset(asset: string): asset is KnownAsset;
86
217
  export type AssetNameOrContractInfo = string | ContractInfo;
87
- export declare function findAssetInfo(network: x402Network, assetNameOrInfo: AssetNameOrContractInfo): ContractInfo;
218
+ /**
219
+ * Finds asset information by network and asset name or contract info.
220
+ *
221
+ * @param network - The network identifier
222
+ * @param assetNameOrInfo - Asset name (e.g., "USDC") or direct ContractInfo
223
+ * @returns Contract information for the asset
224
+ * @throws Error if the asset is unknown or not found on the network
225
+ */
226
+ export declare function findAssetInfo(network: string | number, assetNameOrInfo: AssetNameOrContractInfo): ContractInfo;
88
227
  export type x402ExactArgs = {
89
- network: x402Network | number;
228
+ network: string | number;
90
229
  asset: KnownAsset;
91
230
  amount: UnitInput;
92
231
  payTo: Address;
93
232
  };
233
+ /**
234
+ * Creates x402 exact payment requirements for EVM.
235
+ *
236
+ * @param args - Payment configuration including network, asset, amount, and payTo
237
+ * @returns x402 payment requirement for the exact scheme
238
+ */
94
239
  export declare function x402Exact(args: x402ExactArgs): Partial<{
95
240
  scheme: string;
96
241
  network: string;
97
242
  maxAmountRequired: string;
98
243
  resource: string;
99
244
  description: string;
100
- mimeType: string;
101
245
  payTo: string;
102
246
  maxTimeoutSeconds: number;
103
247
  asset: string;
248
+ mimeType?: string;
104
249
  outputSchema?: object;
105
250
  extra?: object;
106
251
  }>;
@@ -1 +1 @@
1
- {"version":3,"file":"evm.d.ts","sourceRoot":"","sources":["../../src/evm.ts"],"names":[],"mappings":"AAAA,OAAO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AAEF;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,aAAa;;;;;;;;;;;;GAiB5C"}
package/dist/src/evm.js CHANGED
@@ -1,48 +1,134 @@
1
1
  import { addX402PaymentRequirementDefaults } from "./common.js";
2
2
  import { Address } from "@faremeter/types/evm";
3
3
  const knownX402Networks = {
4
- base: {
5
- chainId: 8453,
6
- },
7
- "base-sepolia": {
8
- chainId: 84532,
9
- },
10
- "skale-europa-testnet": {
4
+ "eip155:8453": { legacyName: "base", chainId: 8453 },
5
+ "eip155:84532": { legacyName: "base-sepolia", chainId: 84532 },
6
+ "eip155:1444673419": {
7
+ legacyName: "skale-europa-testnet",
11
8
  chainId: 1444673419,
12
9
  },
10
+ "eip155:137": { legacyName: "polygon", chainId: 137 },
11
+ "eip155:80002": { legacyName: "polygon-amoy", chainId: 80002 },
12
+ "eip155:143": { legacyName: "monad", chainId: 143 },
13
+ "eip155:10143": { legacyName: "monad-testnet", chainId: 10143 },
14
+ "eip155:324705682": { legacyName: "skale-base-sepolia", chainId: 324705682 },
15
+ "eip155:1187947933": { legacyName: "skale-base", chainId: 1187947933 },
13
16
  };
14
- export function isKnownX402Network(n) {
17
+ const legacyNameToCAIP2Map = new Map(Object.entries(knownX402Networks).map(([caip2, info]) => [
18
+ info.legacyName,
19
+ caip2,
20
+ ]));
21
+ /**
22
+ * Converts an EVM chain ID to CAIP-2 network identifier.
23
+ *
24
+ * @param chainId - The EVM chain ID
25
+ * @returns The CAIP-2 network identifier (eip155:chainId)
26
+ */
27
+ export function chainIdToCAIP2(chainId) {
28
+ return `eip155:${chainId}`;
29
+ }
30
+ /**
31
+ * Extracts the EVM chain ID from a CAIP-2 network identifier.
32
+ *
33
+ * @param caip2 - The CAIP-2 network identifier
34
+ * @returns The chain ID, or null if not a valid eip155 identifier
35
+ */
36
+ export function caip2ToChainId(caip2) {
37
+ const match = /^eip155:(\d+)$/.exec(caip2);
38
+ if (!match?.[1]) {
39
+ return null;
40
+ }
41
+ return parseInt(match[1], 10);
42
+ }
43
+ /**
44
+ * Converts a legacy EVM network name to CAIP-2 format.
45
+ *
46
+ * @param legacy - Legacy network name (e.g., "base", "polygon")
47
+ * @returns The CAIP-2 network identifier, or null if unknown
48
+ */
49
+ export function legacyNameToCAIP2(legacy) {
50
+ return legacyNameToCAIP2Map.get(legacy) ?? null;
51
+ }
52
+ /**
53
+ * Converts a CAIP-2 network identifier to legacy EVM network name.
54
+ *
55
+ * @param caip2 - The CAIP-2 network identifier
56
+ * @returns The legacy network name, or null if unknown
57
+ */
58
+ export function caip2ToLegacyName(caip2) {
59
+ const network = knownX402Networks[caip2];
60
+ return network?.legacyName ?? null;
61
+ }
62
+ /**
63
+ * Normalizes an EVM network identifier to CAIP-2 format.
64
+ *
65
+ * Accepts chain IDs (number or string), legacy names, or CAIP-2 identifiers.
66
+ * Returns the input unchanged if no mapping exists.
67
+ *
68
+ * @param network - The network identifier in any supported format
69
+ * @returns The CAIP-2 network identifier
70
+ */
71
+ export function normalizeNetworkId(network) {
72
+ if (typeof network === "number") {
73
+ return chainIdToCAIP2(network);
74
+ }
75
+ if (network.startsWith("eip155:")) {
76
+ return network;
77
+ }
78
+ const caip2 = legacyNameToCAIP2(network);
79
+ if (caip2) {
80
+ return caip2;
81
+ }
82
+ const chainId = parseInt(network, 10);
83
+ if (!isNaN(chainId)) {
84
+ return chainIdToCAIP2(chainId);
85
+ }
86
+ return network;
87
+ }
88
+ /**
89
+ * Type guard that checks if a string is a known EVM CAIP-2 network.
90
+ *
91
+ * @param n - The string to check
92
+ * @returns True if the string is a known EVM CAIP-2 network identifier
93
+ */
94
+ export function isKnownCAIP2Network(n) {
15
95
  return n in knownX402Networks;
16
96
  }
17
- export function lookupKnownX402Network(n) {
97
+ /**
98
+ * Looks up network information by CAIP-2 identifier.
99
+ *
100
+ * @param n - The CAIP-2 network identifier
101
+ * @returns Network information including legacy name and chain ID
102
+ */
103
+ export function lookupKnownCAIP2Network(n) {
18
104
  return {
19
105
  ...knownX402Networks[n],
20
- name: n,
106
+ caip2: n,
21
107
  };
22
108
  }
109
+ /**
110
+ * Looks up the x402 network identifier for an EVM chain ID.
111
+ *
112
+ * @param chainId - The EVM chain ID
113
+ * @returns The CAIP-2 network identifier
114
+ */
23
115
  export function lookupX402Network(chainId) {
24
- let k;
25
- for (k in knownX402Networks) {
26
- if (knownX402Networks[k].chainId == chainId) {
27
- return k;
28
- }
29
- }
30
- return ("eip155:" + chainId.toString());
116
+ return chainIdToCAIP2(chainId);
31
117
  }
32
118
  const knownAssets = {
33
119
  USDC: {
34
120
  network: {
35
- "base-sepolia": {
121
+ "eip155:84532": {
36
122
  address: "0x036cbd53842c5426634e7929541ec2318f3dcf7e",
37
123
  contractName: "USDC",
38
124
  },
39
- base: {
125
+ "eip155:8453": {
40
126
  address: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
41
127
  contractName: "USD Coin",
42
128
  },
43
- "skale-europa-testnet": {
129
+ "eip155:1444673419": {
44
130
  address: "0x9eAb55199f4481eCD7659540A17Af618766b07C4",
45
- contractName: "USDC", // EIP-3009 Forwarder,
131
+ contractName: "USDC",
46
132
  forwarder: "0x7779B0d1766e6305E5f8081E3C0CDF58FcA24330",
47
133
  forwarderName: "USDC Forwarder",
48
134
  forwarderVersion: "1",
@@ -67,41 +153,69 @@ const knownAssets = {
67
153
  address: "0x534b2f3A21130d7a60830c2Df862319e593943A3",
68
154
  contractName: "USDC",
69
155
  },
156
+ "eip155:324705682": {
157
+ address: "0x2e08028E3C4c2356572E096d8EF835cD5C6030bD",
158
+ contractName: "Bridged USDC (SKALE Bridge)",
159
+ },
160
+ "eip155:1187947933": {
161
+ address: "0x85889c8c714505E0c94b30fcfcF64fE3Ac8FCb20",
162
+ contractName: "Bridged USDC (SKALE Bridge)",
163
+ },
70
164
  },
71
165
  toUnit: (v) => v.toString(),
72
166
  },
73
167
  };
168
+ /**
169
+ * Looks up asset information by network and asset name.
170
+ *
171
+ * @param network - The network identifier (chain ID, CAIP-2, or legacy name)
172
+ * @param name - The known asset name (e.g., "USDC")
173
+ * @returns Asset information including contract address, or undefined if not found
174
+ */
74
175
  export function lookupKnownAsset(network, name) {
75
176
  const assetInfo = knownAssets[name];
76
177
  if (!assetInfo) {
77
178
  return;
78
179
  }
79
- if (typeof network === "number") {
80
- network = lookupX402Network(network);
81
- }
82
- const contractInfo = assetInfo.network[network];
180
+ const caip2Network = normalizeNetworkId(network);
181
+ const contractInfo = assetInfo.network[caip2Network];
83
182
  if (!contractInfo) {
84
183
  return;
85
184
  }
86
185
  return {
87
186
  ...contractInfo,
88
187
  name,
89
- network,
188
+ network: caip2Network,
90
189
  toUnit: assetInfo.toUnit,
91
190
  };
92
191
  }
192
+ /**
193
+ * Type guard that checks if a string is a known EVM asset name.
194
+ *
195
+ * @param asset - The string to check
196
+ * @returns True if the string is a known asset name
197
+ */
93
198
  export function isKnownAsset(asset) {
94
199
  return asset in knownAssets;
95
200
  }
201
+ /**
202
+ * Finds asset information by network and asset name or contract info.
203
+ *
204
+ * @param network - The network identifier
205
+ * @param assetNameOrInfo - Asset name (e.g., "USDC") or direct ContractInfo
206
+ * @returns Contract information for the asset
207
+ * @throws Error if the asset is unknown or not found on the network
208
+ */
96
209
  export function findAssetInfo(network, assetNameOrInfo) {
97
210
  let assetInfo;
211
+ const caip2Network = normalizeNetworkId(network);
98
212
  if (typeof assetNameOrInfo == "string") {
99
213
  if (!isKnownAsset(assetNameOrInfo)) {
100
214
  throw new Error(`Unknown asset: ${assetNameOrInfo}`);
101
215
  }
102
- const t = lookupKnownAsset(network, assetNameOrInfo);
216
+ const t = lookupKnownAsset(caip2Network, assetNameOrInfo);
103
217
  if (!t) {
104
- throw new Error(`Couldn't look up asset ${assetNameOrInfo} on ${network}`);
218
+ throw new Error(`Couldn't look up asset ${assetNameOrInfo} on ${caip2Network}`);
105
219
  }
106
220
  assetInfo = t;
107
221
  }
@@ -110,6 +224,12 @@ export function findAssetInfo(network, assetNameOrInfo) {
110
224
  }
111
225
  return assetInfo;
112
226
  }
227
+ /**
228
+ * Creates x402 exact payment requirements for EVM.
229
+ *
230
+ * @param args - Payment configuration including network, asset, amount, and payTo
231
+ * @returns x402 payment requirement for the exact scheme
232
+ */
113
233
  export function x402Exact(args) {
114
234
  const tokenInfo = lookupKnownAsset(args.network, args.asset);
115
235
  if (!tokenInfo) {
@@ -121,7 +241,7 @@ export function x402Exact(args) {
121
241
  maxAmountRequired: tokenInfo.toUnit(args.amount),
122
242
  payTo: args.payTo,
123
243
  asset: tokenInfo.address,
124
- maxTimeoutSeconds: 300, // from coinbase/x402's middleware defaults
244
+ maxTimeoutSeconds: 300,
125
245
  });
126
246
  return req;
127
247
  }