nansen-cli 1.41.1 → 1.43.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/src/x402.js CHANGED
@@ -13,6 +13,9 @@ import {
13
13
  } from './x402-svm.js';
14
14
  import { resolvePassword } from './keychain.js';
15
15
  import { CHAIN_RPCS } from './rpc-urls.js';
16
+ import { evaluatePaymentRequirement, resolvePaymentAmount } from './x402-policy.js';
17
+ import { EVM_X402_TOKENS } from './x402-tokens.js';
18
+ export { EVM_X402_TOKENS } from './x402-tokens.js';
16
19
 
17
20
  /**
18
21
  * Parse PaymentRequirements from a 402 response.
@@ -94,19 +97,26 @@ async function hasPermit2Allowance(network, token, owner, amount) {
94
97
  * @returns {string|null} Base64 payment signature, or null on failure
95
98
  */
96
99
  async function buildPaymentForRequirement(requirement, exported, url) {
100
+ const decision = evaluatePaymentRequirement(requirement);
101
+ if (!decision.ok) {
102
+ console.error(`[x402] ${decision.reason}`);
103
+ return null;
104
+ }
105
+
97
106
  if (isEvmNetwork(requirement.network)) {
98
107
  if ((requirement.extra || {}).assetTransferMethod === 'permit2-exact') {
108
+ const resolvedAmount = resolvePaymentAmount(requirement);
99
109
  const approved = await hasPermit2Allowance(
100
110
  requirement.network,
101
111
  requirement.asset,
102
112
  exported.evm.address,
103
- requirement.amount,
113
+ resolvedAmount,
104
114
  );
105
115
  if (!approved) {
106
116
  console.error(
107
117
  `[x402] Skipping ${requirement.network} permit2 option: Permit2 ` +
108
118
  `(${PERMIT2_ADDRESS}) allowance for token ${requirement.asset} is ` +
109
- `missing or below the payment amount (${requirement.amount}). ` +
119
+ `missing or below the payment amount (${resolvedAmount}). ` +
110
120
  `Send approve(${PERMIT2_ADDRESS}, <amount>) from the wallet to enable it.`,
111
121
  );
112
122
  return null;
@@ -207,11 +217,6 @@ export async function createPaymentSignature(response, url, options = {}) {
207
217
  return null;
208
218
  }
209
219
 
210
- /**
211
- * x402 payment tokens per EVM network, matching the stablecoins the API
212
- * advertises in 402 `accepts` entries. `decimals` matters: USDT on BNB Smart
213
- * Chain uses 18 decimals, unlike the 6-decimal tokens on Base and X Layer.
214
- */
215
220
  // RPC endpoint per supported x402 EVM network.
216
221
  export const EVM_X402_RPCS = {
217
222
  'eip155:8453': CHAIN_RPCS.base,
@@ -223,24 +228,6 @@ function getEvmRpcUrl(network) {
223
228
  return EVM_X402_RPCS[network] || null;
224
229
  }
225
230
 
226
- // Known payment tokens per network, in the order servers typically advertise
227
- // them. A network can accept several stablecoins (BSC accepts four); `decimals`
228
- // is per token — every BSC stablecoin is an 18-decimal BEP-20 deployment,
229
- // unlike the 6-decimal tokens on Base and X Layer.
230
- export const EVM_X402_TOKENS = {
231
- 'eip155:8453': [
232
- { token: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', symbol: 'USDC', decimals: 6 }, // Base USDC
233
- ],
234
- 'eip155:196': [
235
- { token: '0x779Ded0c9e1022225f8E0630b35a9b54bE713736', symbol: 'USDT0', decimals: 6 }, // X Layer USDT0
236
- ],
237
- 'eip155:56': [
238
- { token: '0xcE24439F2D9C6a2289F741120FE202248B666666', symbol: 'U', decimals: 18 }, // United Stables
239
- { token: '0x8d0d000ee44948fc98c9b98a4fa4921476f08b0d', symbol: 'USD1', decimals: 18 }, // World Liberty Financial USD
240
- { token: '0x55d398326f99059fF775485246999027B3197955', symbol: 'USDT', decimals: 18 }, // Tether USD
241
- { token: '0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d', symbol: 'USDC', decimals: 18 }, // Binance-Peg USD Coin
242
- ],
243
- };
244
231
 
245
232
  /**
246
233
  * Check stablecoin balance for x402 payment wallet on the given network.