0

I'm using a uniswap end to end test but couldn't find the equivalent value of swap with my tests. I'm mimicking the DAI EOA address to achieve result through ganache-cli but it's giving me a wrong swap value For an instance I'm swapping 100 DAI with ALCX and in uniswap web it gives me value of 2.07

Uniswap Web Page

But on my testing smart contract it gives me weird value at all which is 30

UNISWAP SMART CONTRACT

// SPDX-License-Identifier: MIT
pragma solidity ^0.8;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "./interfaces/Uniswap.sol";

contract TestUniswap {
  address private constant UNISWAP_V2_ROUTER =
    0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
  address private constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;

  function swap(
    address _tokenIn,
    address _tokenOut,
    uint _amountIn,
    uint _amountOutMin,
    address _to
  ) external {
    IERC20(_tokenIn).transferFrom(msg.sender, address(this), _amountIn);
    IERC20(_tokenIn).approve(UNISWAP_V2_ROUTER, _amountIn);

    address[] memory path;
    if (_tokenIn == WETH || _tokenOut == WETH) {
      path = new address[](2);
      path[0] = _tokenIn;
      path[1] = _tokenOut;
    } else {
      path = new address[](3);
      path[0] = _tokenIn;
      path[1] = WETH;
      path[2] = _tokenOut;
    }

    IUniswapV2Router(UNISWAP_V2_ROUTER).swapExactTokensForTokens(
      _amountIn,
      _amountOutMin,
      path,
      _to,
      block.timestamp
    );
  }

  function getAmountOutMin(
    address _tokenIn,
    address _tokenOut,
    uint _amountIn
  ) external view returns (uint) {
    address[] memory path;
    if (_tokenIn == WETH || _tokenOut == WETH) {
      path = new address[](2);
      path[0] = _tokenIn;
      path[1] = _tokenOut;
    } else {
      path = new address[](3);
      path[0] = _tokenIn;
      path[1] = WETH;
      path[2] = _tokenOut;
    }

    // same length as path
    uint[] memory amountOutMins =
      IUniswapV2Router(UNISWAP_V2_ROUTER).getAmountsOut(_amountIn, path);

    return amountOutMins[path.length - 1];
  }
}

TEST UNISWAP JS FILE

const BN = require("bn.js");
const { sendEther } = require("./util");
const {  WBTC, WBTC_WHALE, DAI, DAI_WHALE,WETH } = require("./config");

const IERC20 = artifacts.require("IERC20");
const TestUniswap = artifacts.require("TestUniswap");

contract("TestUniSwap",(accounts)=>{
    const WHALE = DAI_WHALE; //MIMICKING DAI EOA
    const AMOUNT_IN =new BN(10).pow(new BN(18)).mul(new BN(100));
    
    const AMOUNT_OUT_MIN = 1;
    const TOKEN_IN = DAI;
    const TOKEN_OUT = "0xdBdb4d16EdA451D0503b854CF79D55697F90c8DF"; //ALCX Contract Address
    const TO = accounts[0];
    console.log(TO);



    it("should swap", async ()=>{
        const tokenIn = await IERC20.at(TOKEN_IN);
        const tokenOut = await IERC20.at(TOKEN_OUT);
        const randToken = await IERC20.at(WETH);
        const testUniswap = await TestUniswap.new();
        console.log(`Before ${await tokenOut.balanceOf(TO)}`);
        // console.log(`Before ${web3.eth.getBalance(this)}`);
        
        await tokenIn.approve(testUniswap.address, AMOUNT_IN, {from: WHALE});

        await testUniswap.swap(
            tokenIn.address,
            tokenOut.address,
            AMOUNT_IN,
            AMOUNT_OUT_MIN,
            TO,
            {
                from :WHALE,
            }
        );

        console.log(`out ${await tokenOut.balanceOf(TO)}`);
        // console.log(`out ${await tokenOut.balanceOf(TO)}`);
        
        // console.log(`out ${await new BN(10).pow(new BN(18)).div(new BN(tokenOut.balanceOf(TO)))}`);

    });

})

GANACHE MAINNET FORK TO MIMICK

ganache-cli --fork https://mainnet.infura.io/v3/MY_API_KEY --unlock 0xF977814e90dA44bFA03b6295A0616a897441aceC --networkId 999

But when i run the test, the test gets passed through but it returns incorrect swap value from DAI TO ALCX which is 30 that is absolutely wrong. Am i missing something, i'm beating my head but couldn't get past of it.

test result

1 Answer 1

0

The reasons, Why you're not find same rate at every swap. because in testing scenarios we haven't add sufficient liquidity to remove these issue. at every swap resevers got change, 1 reserve increased and 1 got decreases, this increment and decrement cause price change at every swap

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.