[Ethereum] Calculation of Uniswap Pair address

factorysolidityuniswap

I want to know the address of an Uniswap pair contract with Solidity, even if the pair doesn't exist yet.
I can't use the function getPair of the factory contract because it return 0 when the pair doesn't exist

So I need to calculate it on my code, I checked this page : https://uniswap.org/docs/v2/smart-contract-integration/getting-pair-addresses/ and it seems to be this :

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

However when I put this in solidity I have the error : Explicit type conversion not allowed from "uint256" to "address"

Any idea why ?

Best Answer

Since Solidity 0.8.0 explicit conversions from literals larger than type(uint160).max to address are disallowed.

See this for more information: https://docs.soliditylang.org/en/develop/080-breaking-changes.html#new-restrictions

One workaround here would be casting it to uint160 first:

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