@1llet.xyz/erc4337-gasless-sdk 0.4.62 → 0.4.63

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.mjs CHANGED
@@ -2,7 +2,7 @@ import { monad, unichain, arbitrum, polygon, bsc, avalanche, optimism, gnosis, b
2
2
  import * as StellarSdk from 'stellar-sdk';
3
3
  import { Networks } from 'stellar-sdk';
4
4
  import axios from 'axios';
5
- import { parseAbi, createPublicClient, http, encodeFunctionData, createWalletClient, decodeErrorResult, encodeAbiParameters, keccak256, maxUint256, padHex } from 'viem';
5
+ import { parseAbi, createPublicClient, http, encodeAbiParameters, parseAbiParameters, encodeFunctionData, createWalletClient, decodeErrorResult, keccak256, maxUint256, padHex } from 'viem';
6
6
  import { privateKeyToAccount } from 'viem/accounts';
7
7
  import { OpenAPI, OneClickService, QuoteRequest } from '@defuse-protocol/one-click-sdk-typescript';
8
8
 
@@ -2827,15 +2827,17 @@ init_TransferManager();
2827
2827
  init_near();
2828
2828
  init_cctp2();
2829
2829
  init_stargate();
2830
- var UNISWAP_V3_ROUTER = "0x2626664c2603336E57B271c5C0b26F421741e481";
2831
- var UNISWAP_V3_QUOTER = "0x3d4e44Eb1374240CE5F1B871ab261CD16335B76a";
2830
+ var UNIVERSAL_ROUTER = "0x95273d871c8156636e114b63797d78D7E1720d81";
2831
+ var QUOTER_V4 = "0x0d5e0f97300c3b313360b376b539d48b121532048d";
2832
2832
  var USDC_ADDRESS = "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913";
2833
2833
  var WETH_ADDRESS = "0x4200000000000000000000000000000000000006";
2834
- var QUOTER_ABI = parseAbi([
2835
- "function quoteExactOutputSingle((address tokenIn, address tokenOut, uint256 amount, uint24 fee, uint160 sqrtPriceLimitX96) params) external returns (uint256 amountIn, uint160 sqrtPriceX96After, uint32 initializedTicksCrossed, uint256 gasEstimate)"
2834
+ var V4_SWAP_COMMAND = "0x10";
2835
+ var UNWRAP_WETH_COMMAND = "0x0c";
2836
+ var QUOTER_V4_ABI = parseAbi([
2837
+ "function quoteExactOutputSingle(tuple(address currency0, address currency1, uint24 fee, int24 tickSpacing, address hooks) key, tuple(bool zeroForOne, int128 amount, uint160 sqrtPriceLimitX96) params, bytes hookData) external returns (uint256 amountIn, uint256 gasEstimate)"
2836
2838
  ]);
2837
- var ROUTER_ABI = parseAbi([
2838
- "function exactOutputSingle((address tokenIn, address tokenOut, uint24 fee, address recipient, uint256 amountOut, uint256 amountInMaximum, uint160 sqrtPriceLimitX96) params) external payable returns (uint256 amountIn)"
2839
+ var UNIVERSAL_ROUTER_ABI = parseAbi([
2840
+ "function execute(bytes calldata commands, bytes[] calldata inputs, uint256 deadline) external payable"
2839
2841
  ]);
2840
2842
  var UniswapService = class {
2841
2843
  constructor() {
@@ -2846,65 +2848,90 @@ var UniswapService = class {
2846
2848
  });
2847
2849
  }
2848
2850
  /**
2849
- * Get amount of USDC needed to buy exact amount of ETH
2850
- * @param amountETHWei Amount of ETH (Wei) needed
2851
+ * Get PoolKey for USDC/WETH on Base V4
2852
+ * Assuming standard fee tier 0.05% (500) and tickSpacing 10
2851
2853
  */
2852
- async quoteUSDCForETH(amountETHWei) {
2854
+ getPoolKey() {
2855
+ const isUSDC0 = USDC_ADDRESS.toLowerCase() < WETH_ADDRESS.toLowerCase();
2856
+ const currency0 = isUSDC0 ? USDC_ADDRESS : WETH_ADDRESS;
2857
+ const currency1 = isUSDC0 ? WETH_ADDRESS : USDC_ADDRESS;
2858
+ return {
2859
+ currency0,
2860
+ currency1,
2861
+ fee: 500,
2862
+ // 0.05%
2863
+ tickSpacing: 10,
2864
+ hooks: "0x0000000000000000000000000000000000000000"
2865
+ };
2866
+ }
2867
+ /**
2868
+ * Quote USDC needed for exact ETH output using Uniswap V4
2869
+ */
2870
+ async quoteUSDCForETH(amountOutETH) {
2853
2871
  try {
2854
- const params = {
2855
- tokenIn: USDC_ADDRESS,
2856
- tokenOut: WETH_ADDRESS,
2857
- amount: amountETHWei,
2858
- fee: 500,
2859
- sqrtPriceLimitX96: 0n
2860
- };
2872
+ const key = this.getPoolKey();
2873
+ const isUSDC0 = USDC_ADDRESS.toLowerCase() < WETH_ADDRESS.toLowerCase();
2874
+ const zeroForOne = isUSDC0;
2875
+ const amountParam = -amountOutETH;
2861
2876
  const result = await this.publicClient.readContract({
2862
- address: UNISWAP_V3_QUOTER,
2863
- abi: QUOTER_ABI,
2877
+ address: QUOTER_V4,
2878
+ abi: QUOTER_V4_ABI,
2864
2879
  functionName: "quoteExactOutputSingle",
2865
- args: [params]
2880
+ args: [
2881
+ key,
2882
+ {
2883
+ zeroForOne,
2884
+ amount: amountParam,
2885
+ // Negative for Exact Output
2886
+ sqrtPriceLimitX96: 0n
2887
+ },
2888
+ "0x"
2889
+ // No hook data
2890
+ ]
2866
2891
  });
2867
2892
  const amountIn = result[0];
2868
2893
  return amountIn * 105n / 100n;
2869
2894
  } catch (e) {
2870
- console.error("Quote failed", e);
2871
- throw new Error("Failed to quote USDC for ETH swap");
2895
+ console.error("V4 Quote failed", e);
2896
+ throw new Error("Failed to quote USDC for ETH swap on Uniswap V4");
2872
2897
  }
2873
2898
  }
2874
2899
  /**
2875
- * Build tx data for swapping USDC -> ETH
2876
- * Uses SwapRouter02.exactOutputSingle + unwrapWETH9
2900
+ * Build tx data for swapping USDC -> ETH using Universal Router (V4)
2877
2901
  */
2878
2902
  buildSwapData(recipient, amountOutETH, maxAmountInUSDC) {
2879
- const swapParams = {
2880
- tokenIn: USDC_ADDRESS,
2881
- tokenOut: WETH_ADDRESS,
2882
- fee: 500,
2883
- recipient: UNISWAP_V3_ROUTER,
2884
- // Router must hold WETH to unwrap it
2885
- amountOut: amountOutETH,
2886
- amountInMaximum: maxAmountInUSDC,
2887
- sqrtPriceLimitX96: 0n
2888
- };
2889
- const swapCalldata = encodeFunctionData({
2890
- abi: ROUTER_ABI,
2891
- functionName: "exactOutputSingle",
2892
- args: [swapParams]
2893
- });
2894
- const unwrapCalldata = encodeFunctionData({
2895
- abi: parseAbi(["function unwrapWETH9(uint256 amountMinimum, address recipient) payable"]),
2896
- functionName: "unwrapWETH9",
2897
- args: [amountOutETH, recipient]
2898
- });
2899
- const multicallCalldata = encodeFunctionData({
2900
- abi: parseAbi(["function multicall(bytes[] data) payable returns (bytes[])"]),
2901
- functionName: "multicall",
2902
- args: [[swapCalldata, unwrapCalldata]]
2903
+ const key = this.getPoolKey();
2904
+ const isUSDC0 = USDC_ADDRESS.toLowerCase() < WETH_ADDRESS.toLowerCase();
2905
+ const zeroForOne = isUSDC0;
2906
+ const swapInput = encodeAbiParameters(
2907
+ parseAbiParameters("tuple(address currency0, address currency1, uint24 fee, int24 tickSpacing, address hooks) key, bool zeroForOne, int128 amountSpecified, uint160 sqrtPriceLimitX96, bytes hookData"),
2908
+ [
2909
+ key,
2910
+ zeroForOne,
2911
+ -amountOutETH,
2912
+ // exact output
2913
+ 0n,
2914
+ // no price limit
2915
+ "0x"
2916
+ // no hook data
2917
+ ]
2918
+ );
2919
+ const unwrapInput = encodeAbiParameters(
2920
+ parseAbiParameters("address, uint256"),
2921
+ [recipient, amountOutETH]
2922
+ // Unwrap at least the output amount.
2923
+ );
2924
+ const commands = V4_SWAP_COMMAND + UNWRAP_WETH_COMMAND.substring(2);
2925
+ const inputs = [swapInput, unwrapInput];
2926
+ const deadline = BigInt(Math.floor(Date.now() / 1e3) + 300);
2927
+ return encodeFunctionData({
2928
+ abi: UNIVERSAL_ROUTER_ABI,
2929
+ functionName: "execute",
2930
+ args: [commands, inputs, deadline]
2903
2931
  });
2904
- return multicallCalldata;
2905
2932
  }
2906
2933
  getRouterAddress() {
2907
- return UNISWAP_V3_ROUTER;
2934
+ return UNIVERSAL_ROUTER;
2908
2935
  }
2909
2936
  getUSDCAddress() {
2910
2937
  return USDC_ADDRESS;