You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Uniswap PoolAddress.sol类型转换错误:bytes32无法转uint160

Uniswap PoolAddress库computeAddress函数类型转换错误解决

在Hardhat项目和Remix中使用Uniswap的PoolAddress库时,调用computeAddress函数会触发类型转换错误:TypeError: Explicit type conversion not allowed from "bytes32" to "uint160"。参考Solidity文档后,尝试将bytes32转成bytes20再转uint160后错误消失,但担心这种转换会截断bytes32的部分十六进制值,想确认这是不是正确的解决方式。

原错误代码

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
                    )
                )
            )
        );
    }

修改后的代码

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
                    )
                )
            )
            )
        );
    }

正确性说明

这种转换方式完全没问题,是符合以太坊合约地址生成逻辑的:

  • 以太坊地址本质是**20字节(160位)**的数值,而keccak256生成的是32字节(256位)的哈希值
  • 将bytes32转成bytes20时,Solidity会自动截取哈希值的前20字节(高位部分),这正是Uniswap官方计算Pool地址的标准逻辑
  • 后续转uint160再转address,就是把这20字节的数值转换成合法的以太坊地址格式

所以你修改后的代码是正确的,不存在逻辑问题,完全可以放心使用。

内容的提问来源于stack exchange,提问作者Yilmaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 17:25:30