@wtflabs/x402 0.0.1-beta.11 → 0.0.1-beta.14

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.
@@ -2955,6 +2955,22 @@ async function createPaymentHeader3(client, x402Version, paymentRequirements) {
2955
2955
  }
2956
2956
 
2957
2957
  // src/schemes/exact/evm/utils/tokenDetection.ts
2958
+ var paymentMethodsCache = /* @__PURE__ */ new Map();
2959
+ var recommendedMethodCache = /* @__PURE__ */ new Map();
2960
+ var PRESET_TOKEN_CAPABILITIES = {
2961
+ // World Liberty Financial USD - 只支持 permit
2962
+ "0x8d0d000ee44948fc98c9b98a4fa4921476f08b0d": {
2963
+ supportedMethods: ["permit"],
2964
+ supportedNetworks: [56],
2965
+ description: "World Liberty Financial USD (WLFI)"
2966
+ }
2967
+ // 可以继续添加更多预设代币
2968
+ // 示例:
2969
+ // "0x其他代币地址": {
2970
+ // supportedMethods: ["eip3009", "permit"],
2971
+ // description: "代币名称",
2972
+ // },
2973
+ };
2958
2974
  var EIP3009_SIGNATURES = ["0xe3ee160e", "0xcf092995"];
2959
2975
  var EIP2612_PERMIT = "0xd505accf";
2960
2976
  var PERMIT2_ADDRESS2 = "0x000000000022D473030F116dDEE9F6B43aC78BA3";
@@ -3019,7 +3035,7 @@ async function getImplementationAddress(client, proxyAddress) {
3019
3035
  }
3020
3036
  async function hasMethod(client, tokenAddress, methodSelector) {
3021
3037
  try {
3022
- const code = await client.getBytecode({ address: tokenAddress });
3038
+ const code = await client.getCode({ address: tokenAddress });
3023
3039
  if (!code) return false;
3024
3040
  const hasMethodInProxy = code.toLowerCase().includes(methodSelector.slice(2).toLowerCase());
3025
3041
  if (hasMethodInProxy) {
@@ -3027,7 +3043,7 @@ async function hasMethod(client, tokenAddress, methodSelector) {
3027
3043
  }
3028
3044
  const implAddress = await getImplementationAddress(client, tokenAddress);
3029
3045
  if (implAddress) {
3030
- const implCode = await client.getBytecode({ address: implAddress });
3046
+ const implCode = await client.getCode({ address: implAddress });
3031
3047
  if (implCode) {
3032
3048
  const hasMethodInImpl = implCode.toLowerCase().includes(methodSelector.slice(2).toLowerCase());
3033
3049
  if (hasMethodInImpl) {
@@ -3044,7 +3060,7 @@ async function hasMethod(client, tokenAddress, methodSelector) {
3044
3060
  }
3045
3061
  async function hasAnyMethod(client, tokenAddress, methodSelectors) {
3046
3062
  try {
3047
- const code = await client.getBytecode({ address: tokenAddress });
3063
+ const code = await client.getCode({ address: tokenAddress });
3048
3064
  if (!code) return false;
3049
3065
  const codeLower = code.toLowerCase();
3050
3066
  const hasMethodInProxy = methodSelectors.some(
@@ -3055,7 +3071,7 @@ async function hasAnyMethod(client, tokenAddress, methodSelectors) {
3055
3071
  }
3056
3072
  const implAddress = await getImplementationAddress(client, tokenAddress);
3057
3073
  if (implAddress) {
3058
- const implCode = await client.getBytecode({ address: implAddress });
3074
+ const implCode = await client.getCode({ address: implAddress });
3059
3075
  if (implCode) {
3060
3076
  const implCodeLower = implCode.toLowerCase();
3061
3077
  const hasMethodInImpl = methodSelectors.some(
@@ -3075,7 +3091,7 @@ async function hasAnyMethod(client, tokenAddress, methodSelectors) {
3075
3091
  }
3076
3092
  async function checkPermit2Support(client) {
3077
3093
  try {
3078
- const permit2Code = await client.getBytecode({ address: PERMIT2_ADDRESS2 });
3094
+ const permit2Code = await client.getCode({ address: PERMIT2_ADDRESS2 });
3079
3095
  if (!permit2Code) return false;
3080
3096
  return true;
3081
3097
  } catch (error) {
@@ -3085,6 +3101,48 @@ async function checkPermit2Support(client) {
3085
3101
  }
3086
3102
  async function detectTokenPaymentMethods(tokenAddress, client) {
3087
3103
  const address = tokenAddress.toLowerCase();
3104
+ const chainId = await client.getChainId();
3105
+ const cacheKey = `${chainId}:${address}`;
3106
+ const cached = paymentMethodsCache.get(cacheKey);
3107
+ if (cached) {
3108
+ console.log(`\u{1F4BE} Using cached payment methods for token ${address}`);
3109
+ return cached;
3110
+ }
3111
+ const presetCapabilities = PRESET_TOKEN_CAPABILITIES[address];
3112
+ if (presetCapabilities) {
3113
+ if (!chainId || !presetCapabilities.supportedNetworks.includes(chainId)) {
3114
+ return {
3115
+ address,
3116
+ supportedMethods: [],
3117
+ details: {
3118
+ hasEIP3009: false,
3119
+ hasPermit: false,
3120
+ hasPermit2Approval: false
3121
+ }
3122
+ };
3123
+ }
3124
+ const hasEIP30092 = presetCapabilities.supportedMethods.includes("eip3009");
3125
+ const hasPermit2 = presetCapabilities.supportedMethods.includes("permit");
3126
+ const hasPermit2Approval2 = presetCapabilities.supportedMethods.includes("permit2") || presetCapabilities.supportedMethods.includes("permit2-witness");
3127
+ if (hasEIP30092) {
3128
+ console.log(" \u2705 EIP-3009 (transferWithAuthorization) - from preset");
3129
+ }
3130
+ if (hasPermit2) {
3131
+ console.log(" \u2705 EIP-2612 (permit) - from preset");
3132
+ }
3133
+ if (hasPermit2Approval2) {
3134
+ console.log(" \u2705 Permit2 support - from preset");
3135
+ }
3136
+ return {
3137
+ address,
3138
+ supportedMethods: presetCapabilities.supportedMethods,
3139
+ details: {
3140
+ hasEIP3009: hasEIP30092,
3141
+ hasPermit: hasPermit2,
3142
+ hasPermit2Approval: hasPermit2Approval2
3143
+ }
3144
+ };
3145
+ }
3088
3146
  console.log(`\u{1F50D} Detecting payment methods for token ${address}...`);
3089
3147
  const [hasEIP3009, hasPermit, hasPermit2Approval] = await Promise.all([
3090
3148
  hasAnyMethod(client, address, EIP3009_SIGNATURES),
@@ -3108,7 +3166,7 @@ async function detectTokenPaymentMethods(tokenAddress, client) {
3108
3166
  if (supportedMethods.length === 0) {
3109
3167
  console.log(" \u26A0\uFE0F No advanced payment methods detected (standard ERC-20 only)");
3110
3168
  }
3111
- return {
3169
+ const result = {
3112
3170
  address,
3113
3171
  supportedMethods,
3114
3172
  details: {
@@ -3117,16 +3175,30 @@ async function detectTokenPaymentMethods(tokenAddress, client) {
3117
3175
  hasPermit2Approval
3118
3176
  }
3119
3177
  };
3178
+ paymentMethodsCache.set(cacheKey, result);
3179
+ return result;
3120
3180
  }
3121
3181
  async function getRecommendedPaymentMethod(tokenAddress, client) {
3182
+ const address = tokenAddress.toLowerCase();
3183
+ const chainId = await client.getChainId();
3184
+ const cacheKey = `${chainId}:${address}`;
3185
+ const cached = recommendedMethodCache.get(cacheKey);
3186
+ if (cached !== void 0) {
3187
+ console.log(`\u{1F4BE} Using cached recommended method for token ${address}`);
3188
+ return cached;
3189
+ }
3122
3190
  const capabilities = await detectTokenPaymentMethods(tokenAddress, client);
3123
3191
  const { supportedMethods } = capabilities;
3124
- if (supportedMethods.includes("eip3009")) return "eip3009";
3125
- if (supportedMethods.includes("permit")) return "permit";
3126
- if (supportedMethods.includes("permit2") || supportedMethods.includes("permit2-witness")) {
3127
- return "permit2";
3128
- }
3129
- return null;
3192
+ let result = null;
3193
+ if (supportedMethods.includes("eip3009")) {
3194
+ result = "eip3009";
3195
+ } else if (supportedMethods.includes("permit")) {
3196
+ result = "permit";
3197
+ } else if (supportedMethods.includes("permit2") || supportedMethods.includes("permit2-witness")) {
3198
+ result = "permit2";
3199
+ }
3200
+ recommendedMethodCache.set(cacheKey, result);
3201
+ return result;
3130
3202
  }
3131
3203
  async function getTokenInfo(tokenAddress, client) {
3132
3204
  const address = tokenAddress.toLowerCase();