11import type { BuildSwapCallDataParams } from "@/types" ;
2- import {
3- COMMANDS ,
4- FeeTier ,
5- TICK_SPACING_BY_FEE ,
6- type UniDevKitV4Instance ,
7- } from "@/types" ;
8- import { DEFAULT_HOOKS , getPool } from "@/utils/getPool" ;
9- import { getTokens } from "@/utils/getTokens" ;
2+ import { COMMANDS } from "@/types" ;
103import { ethers } from "ethers" ;
114import type { Hex } from "viem" ;
125
@@ -27,51 +20,29 @@ import type { Hex } from "viem";
2720 * ```typescript
2821 * const swapParams = {
2922 * tokenIn: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
30- * tokenOut: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
3123 * amountIn: parseUnits("100", 6), // 100 USDC
3224 * amountOutMin: parseUnits("0.05", 18), // Min 0.05 WETH
25+ * pool: pool,
3326 * };
3427 *
35- * const calldata = await buildSwapCallData(swapParams, instance );
28+ * const calldata = await buildSwapCallData(swapParams);
3629 *
3730 * // Send transaction
3831 * const tx = await sendTransaction({
39- * to: instance.contracts.universalRouter ,
32+ * to: universalRouterAddress ,
4033 * data: calldata,
4134 * value: 0,
4235 * });
4336 * ```
4437 */
4538export async function buildSwapCallData (
4639 params : BuildSwapCallDataParams ,
47- instance : UniDevKitV4Instance ,
4840) : Promise < Hex > {
4941 // Extract and set default parameters
50- const {
51- tokenIn,
52- tokenOut,
53- amountIn,
54- amountOutMin = 0n ,
55- fee = FeeTier . MEDIUM ,
56- tickSpacing = TICK_SPACING_BY_FEE [ fee ] ,
57- hooks = DEFAULT_HOOKS ,
58- } = params ;
42+ const { tokenIn, amountIn, amountOutMin = 0n , pool } = params ;
5943
60- // Get pool information
61- const pool = await getPool (
62- { tokens : [ tokenIn , tokenOut ] , fee, tickSpacing, hooks } ,
63- instance ,
64- ) ;
65- if ( ! pool ) {
66- throw new Error ( "No swapable pool found" ) ;
67- }
68-
69- // Get token instances for address comparison
70- const tokenInstances = await getTokens (
71- { addresses : [ tokenIn , tokenOut ] } ,
72- instance ,
73- ) ;
74- const zeroForOne = tokenInstances [ 0 ] . address === tokenIn ;
44+ const zeroForOne =
45+ tokenIn . toLowerCase ( ) === pool . poolKey . currency0 . toLowerCase ( ) ;
7546
7647 // Encode Universal Router commands
7748 const commands = ethers . utils . solidityPack (
@@ -94,8 +65,8 @@ export async function buildSwapCallData(
9465 {
9566 poolKey : pool . poolKey ,
9667 zeroForOne,
97- amountIn : ethers . BigNumber . from ( amountIn ) ,
98- amountOutMinimum : ethers . BigNumber . from ( amountOutMin ) ,
68+ amountIn : ethers . BigNumber . from ( amountIn . toString ( ) ) ,
69+ amountOutMinimum : ethers . BigNumber . from ( amountOutMin . toString ( ) ) ,
9970 hookData : "0x" ,
10071 } ,
10172 ] ,
@@ -106,11 +77,13 @@ export async function buildSwapCallData(
10677 exactInputSingleParams ,
10778 ethers . utils . defaultAbiCoder . encode (
10879 [ "address" , "uint128" ] ,
109- [ pool . poolKey . currency0 , amountIn ] ,
80+ zeroForOne
81+ ? [ pool . poolKey . currency0 , amountIn ]
82+ : [ pool . poolKey . currency1 , amountIn ] ,
11083 ) ,
11184 ethers . utils . defaultAbiCoder . encode (
11285 [ "address" , "uint128" ] ,
113- [ pool . poolKey . currency1 , 0 ] ,
86+ zeroForOne ? [ pool . poolKey . currency1 , 0 ] : [ pool . poolKey . currency0 , 0 ] ,
11487 ) ,
11588 ] ;
11689
0 commit comments