0

I'm using single swap implementation from this tutorial: Single Swaps (in my hardhat testing environment which is Mainnet fork).

tokenIn and tokenOut pair of my example is DAI and USDT.

The amountIn parameter that I pass from test.js file is BigNumber of DAI tokens.

        // Naively set amountOutMinimum to 0. In production, use an oracle or other data source to choose a safer value for amountOutMinimum.
        // We also set the sqrtPriceLimitx96 to be 0 to ensure we swap our exact input amount.
        ISwapRouter.ExactInputSingleParams memory params = ISwapRouter
            .ExactInputSingleParams({
                tokenIn: tokenIn, // DAI address
                tokenOut: tokenOut, // USDT address
                fee: 3000, // default
                recipient: msg.sender,
                deadline: block.timestamp,
                amountIn: amountIn, // DAI
                amountOutMinimum: 0,
                sqrtPriceLimitX96: 0
            });

        // The call to `exactInputSingle` executes the swap.
        uint256 amountOut = swapRouter.exactInputSingle(params);

So when this swap is done the amountOut value that I get is strange. For example, if I use BigNumber 7706622438316931417300 (~ 7706 DAI), the amountOut is 183540559 (~ 183 USDT). But it's incorrect value, because DAI price is approximately equal to USDT.

So what is the problem?

Maybe some of BigNumber-Number conversions are wrong, because of different decimals? Or I just need to use this swap example in some different way?

Please help.

1 Answer 1

0

Do you have enough liquidity? Because there's one point in which no matter how mut tokenIn you pass the tokenOut will remain the same.

1
  • Yea, seems that is the problem. I've tried to reduce the amountIn and results of amountOut are correct. Thanks, man!
    – Ruarin
    Nov 12, 2022 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.