0

I'm using my owned cloned UniswapV2 smart contracts(core+periphery) and deployed them using ganache network, so when i start testing functions, i started with createPair() function located in UniswapV2Factory and it's working fine. But when i start using uniswapV2router functions like addliquidity() or swapexactTokensForTokens() it dosent't working, and throw error message :

Returned error: VM Exception while processing transaction: revert

This is my deploy file where i used addLiquidity() function located in testliquidity contract:

const { sendEther, pow } = require("./util");
const IERC20 = artifacts.require("IERC20");
const TestLiquidity = artifacts.require("TestLiquidity");
const { ethers } = require("ethers");
const TOKEN_A = "0xFDAc1a07bB190930c45113BA0119b4F9c964aAb9";
const TOKEN_B = "0x21A347769867E169AF7444fe85F4E4bdE4A9c97e";
const ROUTER = "0xeEea9fc1B8c453105258F09BCa4d06086a909c91";
const TOKEN_A_AMOUNT = pow(10, 18);
const TOKEN_B_AMOUNT = pow(10, 18);
 
module.exports = async function (deployer, networks, accounts) {

  const tokenA = await IERC20.at(TOKEN_A);
  const tokenB = await IERC20.at(TOKEN_B);

  await deployer.deploy(TestLiquidity)
  const testliquidity = await TestLiquidity.deployed()

  await tokenA.approve(testliquidity.address, TOKEN_A_AMOUNT);
  await tokenB.approve(testliquidity.address, TOKEN_B_AMOUNT);

  let tx = await testliquidity.addLiquidity(
    tokenA.address,
    tokenB.address,
    TOKEN_A_AMOUNT,
    TOKEN_B_AMOUNT
  );
};

and this is the testliquidity contract where i called the addliquidity() function:

contract TestLiquidity {

  address private constant ROUTER = 0xeEea9fc1B8c453105258F09BCa4d06086a909c91;
  event Log(string message, uint val);

  function addLiquidity(
    address _tokenA,
    address _tokenB,
    uint _amountA,
    uint _amountB
  ) external {
    IERC20(_tokenA).transferFrom(msg.sender, address(this), _amountA);
    IERC20(_tokenB).transferFrom(msg.sender, address(this), _amountB);
    IERC20(_tokenA).approve(ROUTER, _amountA);
    IERC20(_tokenB).approve(ROUTER, _amountB);
    

    (uint amountA, uint amountB, uint liquidity) =
      IUniswapV2Router02(ROUTER).addLiquidity(
        _tokenA,
        _tokenB,
        _amountA,
        _amountB,
        1,
        1,
        address(this),
        block.timestamp
      );

    emit Log("amountA", amountA);
    emit Log("amountB", amountB);
    emit Log("liquidity", liquidity);
  }


}

and here is my ERC20 tokens contracts tokenA and tokenB which i created to add them in my liquidity pair:

contract tokenA is ERC20 {
  constructor() ERC20('USDC Coin', 'USDC') {
    uint256 n = 1000;
    _mint(msg.sender, n * 10**uint(decimals()));

  }

contract tokenB is ERC20 {
  constructor() ERC20('WBTC Coin', 'WBTC') {
      uint256 n = 1000;
      _mint(msg.sender, n * 10**uint(decimals()));
  }
}
}

I can't undestant from where the problem exactly, i'm sure the uniswapV2 contracts are correct and well deployed.

7
  • which network? How are you getting the contract addresses? Jun 14, 2022 at 13:20
  • development newtork port 8545 using truffle ganache Jun 14, 2022 at 14:54
  • Why are the contract addresses fixed? Should they not change on every deployment? Jun 15, 2022 at 1:23
  • Im deploying them seperatly so each time i deploy them i get the contract addresses and call them, starting with core uniswapv2 contracts where i deployed factory and pair contracts then periphery contracts where i deployed weth and uniswapV2router using factory address that i've elrady deployed it, then i used that uniswapv2router contract address in testliquidity contract. Jun 15, 2022 at 10:02
  • i said maybe from the tokenAamout and tokenBamount when i use the pow function, when i console log them i get: <BN: 3782dace9d9> <BN: 3782dace9d9> Jun 15, 2022 at 10:24

1 Answer 1

0

I also wanted to test contracts using test file provided by truffle and got a similar error. here is my test file:

const BN = require("bn.js");
const { sendEther, pow } = require("./util");
const TestLiquidity = artifacts.require("TestLiquidity");
const USDC = artifacts.require("USDC");
const WBTC = artifacts.require("WBTC");

contract("TestUniswapLiquidity", (accounts) => {
  const CALLER = accounts[0];
  const TOKEN_A_AMOUNT = pow(10, 18);
  const TOKEN_B_AMOUNT = pow(10, 18);
  let tokenA;
  let tokenB;

  beforeEach(async () => {

    tokenA =await USDC.new(); 
    tokenB = await WBTC.new(); 
    contract = await TestLiquidity.new(); 
    
    await tokenA.approve(contract.address, TOKEN_A_AMOUNT, { from: CALLER });
    await tokenB.approve(contract.address, TOKEN_B_AMOUNT, { from: CALLER });

    
  });

  it("add liquidity", async () => {
    let tx = await contract.addLiquidity(
      tokenA.address,
      tokenB.address,
      TOKEN_A_AMOUNT,
      TOKEN_B_AMOUNT,
      {
        from: CALLER,
      }
    );
    console.log("=== add liquidity ===");
    for (const log of tx.logs) {
      console.log(`${log.args.message} ${log.args.val}`);
    }   
  });
});

and got this error:


Contract: TestUniswapLiquidity
    1) add liquidity

    Events emitted during test:
    ---------------------------

    Ambiguous event, possible interpretations:
    * ERC20.Transfer(
        from: <indexed> 0x0000000000000000000000000000000000000000 (type: address),
        to: <indexed> 0x138A24E2dCD31c145616386b15f336A70F661E8d (type: address),
        value: 1000000000000000000000 (type: uint256)
      )
    * ERC20.Transfer(
        from: <indexed> 0x0000000000000000000000000000000000000000 (type: address),
        to: <indexed> 0x138A24E2dCD31c145616386b15f336A70F661E8d (type: address),
        value: 1000000000000000000000 (type: uint256)
      )

    Ambiguous event, possible interpretations:
    * ERC20.Transfer(
        from: <indexed> 0x0000000000000000000000000000000000000000 (type: address),
        to: <indexed> 0x138A24E2dCD31c145616386b15f336A70F661E8d (type: address),
        value: 1000000000000000000000 (type: uint256)
      )
    * ERC20.Transfer(
        from: <indexed> 0x0000000000000000000000000000000000000000 (type: address),
        to: <indexed> 0x138A24E2dCD31c145616386b15f336A70F661E8d (type: address),
        value: 1000000000000000000000 (type: uint256)
      )

    Ambiguous event, possible interpretations:
    * ERC20.Approval(
        owner: <indexed> 0x138A24E2dCD31c145616386b15f336A70F661E8d (type: address),
        spender: <indexed> 0xc02F361d24ef98E74A579EaA8c5277aCFc83435e (type: address),
        value: 1000000000000000000 (type: uint256)
      )
    * ERC20.Approval(
        owner: <indexed> 0x138A24E2dCD31c145616386b15f336A70F661E8d (type: address),
        spender: <indexed> 0xc02F361d24ef98E74A579EaA8c5277aCFc83435e (type: address),
        value: 1000000000000000000 (type: uint256)
      )

    Ambiguous event, possible interpretations:
    * ERC20.Approval(
        owner: <indexed> 0x138A24E2dCD31c145616386b15f336A70F661E8d (type: address),
        spender: <indexed> 0xc02F361d24ef98E74A579EaA8c5277aCFc83435e (type: address),
        value: 1000000000000000000 (type: uint256)
      )
    * ERC20.Approval(
        owner: <indexed> 0x138A24E2dCD31c145616386b15f336A70F661E8d (type: address),
        spender: <indexed> 0xc02F361d24ef98E74A579EaA8c5277aCFc83435e (type: address),
        value: 1000000000000000000 (type: uint256)
      )


    ---------------------------


  0 passing (8s)
  1 failing

  1) Contract: TestUniswapLiquidity
       add liquidity:
     Error: Returned error: VM Exception while processing transaction: revert
      at Context.<anonymous> (test\test_liquidity.js:38:29)
      at runMicrotasks (<anonymous>)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)

Your Answer

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

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