@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.d.mts CHANGED
@@ -512,13 +512,16 @@ declare class UniswapService {
512
512
  private publicClient;
513
513
  constructor();
514
514
  /**
515
- * Get amount of USDC needed to buy exact amount of ETH
516
- * @param amountETHWei Amount of ETH (Wei) needed
515
+ * Get PoolKey for USDC/WETH on Base V4
516
+ * Assuming standard fee tier 0.05% (500) and tickSpacing 10
517
517
  */
518
- quoteUSDCForETH(amountETHWei: bigint): Promise<bigint>;
518
+ private getPoolKey;
519
519
  /**
520
- * Build tx data for swapping USDC -> ETH
521
- * Uses SwapRouter02.exactOutputSingle + unwrapWETH9
520
+ * Quote USDC needed for exact ETH output using Uniswap V4
521
+ */
522
+ quoteUSDCForETH(amountOutETH: bigint): Promise<bigint>;
523
+ /**
524
+ * Build tx data for swapping USDC -> ETH using Universal Router (V4)
522
525
  */
523
526
  buildSwapData(recipient: Address, amountOutETH: bigint, maxAmountInUSDC: bigint): Hex;
524
527
  getRouterAddress(): Address;
package/dist/index.d.ts CHANGED
@@ -512,13 +512,16 @@ declare class UniswapService {
512
512
  private publicClient;
513
513
  constructor();
514
514
  /**
515
- * Get amount of USDC needed to buy exact amount of ETH
516
- * @param amountETHWei Amount of ETH (Wei) needed
515
+ * Get PoolKey for USDC/WETH on Base V4
516
+ * Assuming standard fee tier 0.05% (500) and tickSpacing 10
517
517
  */
518
- quoteUSDCForETH(amountETHWei: bigint): Promise<bigint>;
518
+ private getPoolKey;
519
519
  /**
520
- * Build tx data for swapping USDC -> ETH
521
- * Uses SwapRouter02.exactOutputSingle + unwrapWETH9
520
+ * Quote USDC needed for exact ETH output using Uniswap V4
521
+ */
522
+ quoteUSDCForETH(amountOutETH: bigint): Promise<bigint>;
523
+ /**
524
+ * Build tx data for swapping USDC -> ETH using Universal Router (V4)
522
525
  */
523
526
  buildSwapData(recipient: Address, amountOutETH: bigint, maxAmountInUSDC: bigint): Hex;
524
527
  getRouterAddress(): Address;
package/dist/index.js CHANGED
@@ -2851,15 +2851,17 @@ init_TransferManager();
2851
2851
  init_near();
2852
2852
  init_cctp2();
2853
2853
  init_stargate();
2854
- var UNISWAP_V3_ROUTER = "0x2626664c2603336E57B271c5C0b26F421741e481";
2855
- var UNISWAP_V3_QUOTER = "0x3d4e44Eb1374240CE5F1B871ab261CD16335B76a";
2854
+ var UNIVERSAL_ROUTER = "0x95273d871c8156636e114b63797d78D7E1720d81";
2855
+ var QUOTER_V4 = "0x0d5e0f97300c3b313360b376b539d48b121532048d";
2856
2856
  var USDC_ADDRESS = "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913";
2857
2857
  var WETH_ADDRESS = "0x4200000000000000000000000000000000000006";
2858
- var QUOTER_ABI = viem.parseAbi([
2859
- "function quoteExactOutputSingle((address tokenIn, address tokenOut, uint256 amount, uint24 fee, uint160 sqrtPriceLimitX96) params) external returns (uint256 amountIn, uint160 sqrtPriceX96After, uint32 initializedTicksCrossed, uint256 gasEstimate)"
2858
+ var V4_SWAP_COMMAND = "0x10";
2859
+ var UNWRAP_WETH_COMMAND = "0x0c";
2860
+ var QUOTER_V4_ABI = viem.parseAbi([
2861
+ "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)"
2860
2862
  ]);
2861
- var ROUTER_ABI = viem.parseAbi([
2862
- "function exactOutputSingle((address tokenIn, address tokenOut, uint24 fee, address recipient, uint256 amountOut, uint256 amountInMaximum, uint160 sqrtPriceLimitX96) params) external payable returns (uint256 amountIn)"
2863
+ var UNIVERSAL_ROUTER_ABI = viem.parseAbi([
2864
+ "function execute(bytes calldata commands, bytes[] calldata inputs, uint256 deadline) external payable"
2863
2865
  ]);
2864
2866
  var UniswapService = class {
2865
2867
  constructor() {
@@ -2870,65 +2872,90 @@ var UniswapService = class {
2870
2872
  });
2871
2873
  }
2872
2874
  /**
2873
- * Get amount of USDC needed to buy exact amount of ETH
2874
- * @param amountETHWei Amount of ETH (Wei) needed
2875
+ * Get PoolKey for USDC/WETH on Base V4
2876
+ * Assuming standard fee tier 0.05% (500) and tickSpacing 10
2875
2877
  */
2876
- async quoteUSDCForETH(amountETHWei) {
2878
+ getPoolKey() {
2879
+ const isUSDC0 = USDC_ADDRESS.toLowerCase() < WETH_ADDRESS.toLowerCase();
2880
+ const currency0 = isUSDC0 ? USDC_ADDRESS : WETH_ADDRESS;
2881
+ const currency1 = isUSDC0 ? WETH_ADDRESS : USDC_ADDRESS;
2882
+ return {
2883
+ currency0,
2884
+ currency1,
2885
+ fee: 500,
2886
+ // 0.05%
2887
+ tickSpacing: 10,
2888
+ hooks: "0x0000000000000000000000000000000000000000"
2889
+ };
2890
+ }
2891
+ /**
2892
+ * Quote USDC needed for exact ETH output using Uniswap V4
2893
+ */
2894
+ async quoteUSDCForETH(amountOutETH) {
2877
2895
  try {
2878
- const params = {
2879
- tokenIn: USDC_ADDRESS,
2880
- tokenOut: WETH_ADDRESS,
2881
- amount: amountETHWei,
2882
- fee: 500,
2883
- sqrtPriceLimitX96: 0n
2884
- };
2896
+ const key = this.getPoolKey();
2897
+ const isUSDC0 = USDC_ADDRESS.toLowerCase() < WETH_ADDRESS.toLowerCase();
2898
+ const zeroForOne = isUSDC0;
2899
+ const amountParam = -amountOutETH;
2885
2900
  const result = await this.publicClient.readContract({
2886
- address: UNISWAP_V3_QUOTER,
2887
- abi: QUOTER_ABI,
2901
+ address: QUOTER_V4,
2902
+ abi: QUOTER_V4_ABI,
2888
2903
  functionName: "quoteExactOutputSingle",
2889
- args: [params]
2904
+ args: [
2905
+ key,
2906
+ {
2907
+ zeroForOne,
2908
+ amount: amountParam,
2909
+ // Negative for Exact Output
2910
+ sqrtPriceLimitX96: 0n
2911
+ },
2912
+ "0x"
2913
+ // No hook data
2914
+ ]
2890
2915
  });
2891
2916
  const amountIn = result[0];
2892
2917
  return amountIn * 105n / 100n;
2893
2918
  } catch (e) {
2894
- console.error("Quote failed", e);
2895
- throw new Error("Failed to quote USDC for ETH swap");
2919
+ console.error("V4 Quote failed", e);
2920
+ throw new Error("Failed to quote USDC for ETH swap on Uniswap V4");
2896
2921
  }
2897
2922
  }
2898
2923
  /**
2899
- * Build tx data for swapping USDC -> ETH
2900
- * Uses SwapRouter02.exactOutputSingle + unwrapWETH9
2924
+ * Build tx data for swapping USDC -> ETH using Universal Router (V4)
2901
2925
  */
2902
2926
  buildSwapData(recipient, amountOutETH, maxAmountInUSDC) {
2903
- const swapParams = {
2904
- tokenIn: USDC_ADDRESS,
2905
- tokenOut: WETH_ADDRESS,
2906
- fee: 500,
2907
- recipient: UNISWAP_V3_ROUTER,
2908
- // Router must hold WETH to unwrap it
2909
- amountOut: amountOutETH,
2910
- amountInMaximum: maxAmountInUSDC,
2911
- sqrtPriceLimitX96: 0n
2912
- };
2913
- const swapCalldata = viem.encodeFunctionData({
2914
- abi: ROUTER_ABI,
2915
- functionName: "exactOutputSingle",
2916
- args: [swapParams]
2917
- });
2918
- const unwrapCalldata = viem.encodeFunctionData({
2919
- abi: viem.parseAbi(["function unwrapWETH9(uint256 amountMinimum, address recipient) payable"]),
2920
- functionName: "unwrapWETH9",
2921
- args: [amountOutETH, recipient]
2922
- });
2923
- const multicallCalldata = viem.encodeFunctionData({
2924
- abi: viem.parseAbi(["function multicall(bytes[] data) payable returns (bytes[])"]),
2925
- functionName: "multicall",
2926
- args: [[swapCalldata, unwrapCalldata]]
2927
+ const key = this.getPoolKey();
2928
+ const isUSDC0 = USDC_ADDRESS.toLowerCase() < WETH_ADDRESS.toLowerCase();
2929
+ const zeroForOne = isUSDC0;
2930
+ const swapInput = viem.encodeAbiParameters(
2931
+ viem.parseAbiParameters("tuple(address currency0, address currency1, uint24 fee, int24 tickSpacing, address hooks) key, bool zeroForOne, int128 amountSpecified, uint160 sqrtPriceLimitX96, bytes hookData"),
2932
+ [
2933
+ key,
2934
+ zeroForOne,
2935
+ -amountOutETH,
2936
+ // exact output
2937
+ 0n,
2938
+ // no price limit
2939
+ "0x"
2940
+ // no hook data
2941
+ ]
2942
+ );
2943
+ const unwrapInput = viem.encodeAbiParameters(
2944
+ viem.parseAbiParameters("address, uint256"),
2945
+ [recipient, amountOutETH]
2946
+ // Unwrap at least the output amount.
2947
+ );
2948
+ const commands = V4_SWAP_COMMAND + UNWRAP_WETH_COMMAND.substring(2);
2949
+ const inputs = [swapInput, unwrapInput];
2950
+ const deadline = BigInt(Math.floor(Date.now() / 1e3) + 300);
2951
+ return viem.encodeFunctionData({
2952
+ abi: UNIVERSAL_ROUTER_ABI,
2953
+ functionName: "execute",
2954
+ args: [commands, inputs, deadline]
2927
2955
  });
2928
- return multicallCalldata;
2929
2956
  }
2930
2957
  getRouterAddress() {
2931
- return UNISWAP_V3_ROUTER;
2958
+ return UNIVERSAL_ROUTER;
2932
2959
  }
2933
2960
  getUSDCAddress() {
2934
2961
  return USDC_ADDRESS;