原创

solidity合约创建合约


参考uniswap的Factory合约创建交易对

pragma solidity ^0.8.1;

contract Pair{
    address public factory;
    address public token0;
    address public token1;
    constructor(address _token0, address _token1) payable{
        token0 = _token0;
        token1 = _token1;
        factory = msg.sender;
    }
}

contract Factory{
// 存储创建的子合约地址
    Pair[] public allPairs;
    // 创建pair
    function createPair(address _token0, address _token1) public{
        Pair pair = new Pair(_token0, _token1);
        allPairs.push(pair);
    }
    // 支付 创建pair并
    function createPairV(address _token0, address _token1) public payable{
        Pair pair = (new Pair){value: msg.value}(_token0, _token1);
        allPairs.push(pair);
    }
}
以太坊
  • 作者:零三(联系作者)
  • 最后更新时间:2022-03-17 15:43
  • 版权声明:自由转载-非商用-非衍生-保持署名
  • 转载声明:来源地址 https://web03.cn