0

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 ?

4
  • Hi there. Just to check, you've set factory, token0 and token1 as addresses? May 17 at 9:52
  • Yeah all 3 are addresses, the problem seems to be when Solidity want to convert from uint to address here : address(uint(keccak256(abi.encodePacked(...
    – Kioup
    May 17 at 14:16
  • Did you found out the solution to this issue, im having exact same error on exact same contract (uniswapv2library) using Remix online and i'm not able to find any solution for the moment ...
    – Olivain
    Jun 8 at 16:15
  • Do you use the same compiler version as the factory?
    – Undead8
    Jun 8 at 17:14
0

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 address pair = address(uint160(uint(keccak256(abi.encodePacked(
  hex'ff',
  factory,
  keccak256(abi.encodePacked(token0, token1)),
  hex'96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f'
)))));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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