0

I am testing performing swaps using solidity ^0.8.24 smart contracts with hardhat on Polygon and Quickswap exchange. But I am getting relatively large price discrepancies between my swap and one showing on the quickswap exchange.

For this purpose I have in my code defined the interface for UniswapV2Router:

interface IUniswapV2Router {
    function swapExactTokensForTokens(
        uint256 amountIn,
        uint256 amountOutMin,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external returns (uint256[] memory amounts);
}

And,

address[] memory path = new address[](2);
path[0] = USDC; //USDC.e contract from polygonscan
path[1] = USDT; //USDT contract from polygonscan
IERC20(USDC).approve(address(quickswapRouter), amount);
quickswapRouter.swapExactTokensForTokens(amount, 0, path, address(this), block.timestamp);

In testing, I am performing a simple swap of 1000 USDC.e to USDT on forked mainnet using hardhat node. In my logs I get that the exchange was from 1000.000000 to 993.774345 tokens, while on the quickswap it is showing it should go for expected 999 tokens or minimum of 998 tokens, which is logical as both are stablecoins.

USDC.e/USDT should have a direct path, so I am not sure what I am doing wrong that in my swap I am getting much less than the minimum amount shown on the quickswap exchange.

1
  • From 1000 to 999 is a 0.1% fee, which is less than the 0.3% fee of uniswap V2. I feel like quickswap UI was showing you a route that involved concentrated liquidity pools.
    – 0xSanson
    Commented Apr 10 at 9:47

1 Answer 1

0

The price you get depends on the available liquidity and path.

It is likely that your code and Hardhat fork took a different path, or does not have direct liquidity available, different what Quickswap user interface routes.

Without seeing the routes and block height, it is impossible to tell, though.

2
  • What should I focus on in debugging this problem? Should I focus on determining how a path looks like in a forked network. Am I maybe doing forking wrong? I use npx hardhat node with alchemy mainnet rpc and then all other scripts are called with --network localhost. Should I maybe use direct swap and not interface for uniswap router? Any help would be welcome
    – SolTester
    Commented Apr 9 at 14:36
  • Do a swap in the user interface and compare the raw transaction to your own Commented Apr 10 at 10:24

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.