以太坊合约地址是怎么计算出来的?(附源码实现)

  • darren
  • 更新于 2022-03-17 19:18
  • 阅读 3597

Uniswap 交易对地址是如何计算出来的

背景

在Uniswap源码看到工厂合约 UniswapV2 Factory中createPair方法,使用了create2,预先知道配对合约地址,参考:https://github.com/Uniswap/v2-core/blob/master/contracts/UniswapV2Factory.sol

在之后路由合约拿配对的合约地址的时候,就不用从工厂合约里取得了,通过pairFor方法和Token地址获取.参考:https://github.com/Uniswap/v2-periphery/blob/master/contracts/libraries/UniswapV2Library.sol

它是如何实现的呢? 看下面solidity源码就可以明白.

实现

pragma solidity ^0.8;

contract Factory{
     event Deployed(address addr,uint256 salt);

     // 得到将要部署的合约的bytecode
     function getBytecode(address _owner,uint _foo) public pure returns(bytes memory){
         bytes memory bytecode= type(TestContract).creationCode;
         return abi.encodePacked(bytecode,abi.encode(_owner,_foo));
     }

     // 2.计算合约的地址
     // keccak256(0xff + sender.Address + salt + keccak256(getBytecode))
     // 得到最后20bytes
     function getAddress(bytes memory bytecode,uint _salt) public view returns(address){
         bytes32 hash = keccak256(
             abi.encodePacked(
                 bytes1(0xff),
                 address(this),
                 _salt,
                 keccak256(bytecode)
             )
         );
         return address(uint160(uint256(hash)));
     }

     // 3 部署合约

     function deploy(bytes memory bytecode,uint _salt) public payable{
         address addr;
         /*
          how to call create
          create2(v,p,n,s)
          v amount of eth to send
          p pointer to start of code in memory 
          n size of code
          s salt
         */
         assembly {
          addr := create2(
              // weisent with current call
             callvalue(),
             add(bytecode,0x20),
             mload(bytecode),
             _salt

          )
         }

         emit Deployed(addr,_salt);
     }

}

contract TestContract{
    address public owner;
    uint public foo;

    constructor(address _owner,uint _foo) payable{
        owner =_owner;
        foo= _foo;
    }
    function getBalance() public view returns(uint){
        return address(this).balance;
    }
}

在remix进行部署,先调用getBytecode得到bytecode,然后通过bytecode 与_salt随机得到地址.

image.png

检验

通过点击deploy方法,触发Deployed事件,可以看到两者的地址一致.

image.png

通过address得到TestContract合约

image.png

点赞 1
收藏 5
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

3 条评论

请先 登录 后评论
darren
darren
江湖只有他的大名,没有他的介绍。