Matteo Gigli
Matteo Gigli

Reputation: 49

Uniswap pair = address(uint(keccak256(abi.encodePacked() Error

I don't understand why i get this error when i try to compile my contract to swap tokens on different dex.

Here the Code:

//SPDX-License-Identifier: MIT

import "./Interfaces/IUniswapV2Router02.sol";
import "./Interfaces/IUniswapV2Factory.sol";
//import "./Interfaces/IUniswapV2Pair.sol";
import "./Interfaces/IERC20.sol";
import "./Interfaces/UniswapV2Library.sol";
//import "./Interfaces/IUniswapV2Callee.sol";


pragma solidity >0.5.0 < 0.9.0;

contract SwappingDex{

    address public constant uniswapRouter = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
    address public constant uniswapFactory = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;
    address public constant sushiSwapRouter = 0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F;
    address public constant sushiSwapFactory = 0xC0AEe478e3658e2610c5F7A4A2E1777cE9e4f2Ac;
    address public constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;


    function swappingIn2DEX(address tokenIn, address tokenOut, uint amount, address to)external payable{

        address pairOnUniswap;
        address pairOnSushi;
        pairOnUniswap = IUniswapV2Factory(uniswapFactory).getPair(tokenIn, tokenOut);
        pairOnSushi = IUniswapV2Factory(sushiSwapFactory).getPair(tokenIn, tokenOut);

        require(
            pairOnSushi != 0x0000000000000000000000000000000000000000 &&
            pairOnUniswap != 0x0000000000000000000000000000000000000000,
            "No Pair"
        );

        address[] memory path;
        path = new address[](2);
        path[0] = tokenIn;
        path[1] = tokenOut;

        uint[]memory amountOutUniswap = IUniswapV2Router02(uniswapRouter).getAmountsOut(amount, path);


        IERC20(tokenIn).transferFrom(msg.sender, address(this), amount);
        IERC20(tokenIn).approve(uniswapRouter, amount);

        IUniswapV2Router02(uniswapRouter).swapETHForExactTokens{value: msg.value}(
            amountOutUniswap[1],
            path,
            to,
            block.timestamp
        );
}

hardhat.config.js


require("@nomiclabs/hardhat-waffle");
require("@nomiclabs/hardhat-ethers");
require("dotenv").config();


module.exports = {
  solidity: "0.8.4",

  networks: {
    hardhat: {
      forking:{
        url: "*******",
      },
    },
  }
};

Once i go for compile, via npx hardhat compile, i get this error :

TypeError: Explicit type conversion not allowed from "uint256" to "address". --> contracts/Interfaces/UniswapV2Library.sol:20:16: | 20 | pair = address(uint(keccak256(abi.encodePacked( | ^ (Relevant source part starts here and spans across multiple lines).

I was searching for some reason about it and i saw something talking about a cast for the address after the ^0.8.0 version. I already made projects on uniswap with same steps but is the first time i got this error. Can someone explain me why? Thanks a lot!

Upvotes: 1

Views: 956

Answers (1)

Yilmaz
Yilmaz

Reputation: 49551

this is the UniswapV2Library.sol github repo. code complains about this

function pairFor(address factory, address tokenA, address tokenB) internal pure returns (address pair) {
        (address token0, address token1) = sortTokens(tokenA, tokenB);
        pair = address(uint(keccak256(abi.encodePacked(
                hex'ff',
                factory,
                keccak256(abi.encodePacked(token0, token1)),
                hex'96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f' // init code hash
            ))));
    }

You need to apply the settings of UNiswap repo. That repo is using ethereum-waffle.readthedocs and if you check the .waffle.json config in the root of project you see this settings:

{
  "compilerVersion": "./node_modules/solc",
  "outputType": "all",
  "compilerOptions": {
    "outputSelection": {
      "*": {
        "*": [
          "evm.bytecode.object",
          "evm.deployedBytecode.object",
          "abi",
          "evm.bytecode.sourceMap",
          "evm.deployedBytecode.sourceMap",
          "metadata"
        ],
        "": ["ast"]
      }
    },
    "evmVersion": "istanbul",
    "optimizer": {
      "enabled": true,
      "runs": 999999
    }
  }
}

I had a similar issue with UniswapV3 and I asked a question here and then answered. I use hardhat and I applied these settings from UniswapV3 repo.

const DEFAULT_COMPILER_SETTINGS = {
  version: '0.7.6',
  settings: {
    evmVersion: 'istanbul',
    optimizer: {
      enabled: true,
      runs: 1_000_000,
    },
    metadata: {
      bytecodeHash: 'none',
    },
  },
}

I think the main reason is using the evmVersion: 'istanbul',

If not try mutating pair. From here

pair = address(uint160(uint(keccak256(abi.encodePacked(
                hex'ff',
                factory,
                keccak256(abi.encodePacked(token0, token1)),
                hex'96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f' // init code hash
            )))));

Upvotes: 1

Related Questions