Reputation: 49551
This is Uniswap PoolAddress library repo
this code is throwing error while I was working on a hardhat project:
function computeAddress(address factory, PoolKey memory key) internal pure returns (address pool) {
require(key.token0 < key.token1);
//*******************this is causing the error***************
pool = address(
uint160(
keccak256(
abi.encodePacked(
hex'ff',
factory,
keccak256(abi.encode(key.token0, key.token1, key.fee)),
POOL_INIT_CODE_HASH
)
)
)
);
}
I copied the full code on Remix and I get an error for the pool=address
line: 'TypeError: Explicit type conversion not allowed from "bytes32" to "uint160".'
I read the docs:
Explicit conversions to and from address are allowed for uint160, integer literals, bytes20 and contract types.
it also has a warning and I do not understand what it means:
If you convert a type that uses a larger byte size to an address, for example bytes32, then the address is truncated. To reduce conversion ambiguity, starting with version 0.4.24, the compiler will force you to make the truncation explicit in the conversion. Take for example the 32-byte value 0x111122223333444455556666777788889999AAAABBBBCCCCDDDDEEEEFFFFCCCC.
You can use address(uint160(bytes20(b))), which results in 0x111122223333444455556666777788889999aAaa, or you can use address(uint160(uint256(b))), which results in 0x777788889999AaAAbBbbCcccddDdeeeEfFFfCcCc.
Since docs mentions that I can convert from bytes20 to int160, I tried this and the error message has gone:
function computeAddress(address factory, PoolKey memory key) internal pure returns (address pool) {
require(key.token0 < key.token1);
pool = address(
uint160(
bytes20(
keccak256(
abi.encodePacked(
hex'ff',
factory,
keccak256(abi.encode(key.token0, key.token1, key.fee)),
POOL_INIT_CODE_HASH
)
)
)
)
);
}
But I do not think that converting from bytes32
to bytes20
is a valid approach because it will remove some of the hex values in bytes32
.
Upvotes: 0
Views: 493
Reputation: 49551
I checked the hardhat.config.ts of the repo and this is the settings that have implemented:
const DEFAULT_COMPILER_SETTINGS = {
version: '0.7.6',
settings: {
evmVersion: 'istanbul',
optimizer: {
enabled: true,
runs: 1_000_000,
},
metadata: {
bytecodeHash: 'none',
},
},
}
Then applied those in
solidity: {
compilers: [DEFAULT_COMPILER_SETTINGS],
...
}
Upvotes: 0