【问题标题】:Smart contract Ethereum - How to recreate same contract after used selfDestruct()智能合约以太坊 - 如何在使用 selfDestruct() 后重新创建相同的合约
【发布时间】:2021-11-24 21:57:57
【问题描述】:

是否可以同时销毁(使用 selftDestruct 函数)和生成相同的合约?

我们假设第一个合约有1Eth,然后将所有的eth提取到另一个erc20地址,完全提取后,第一个合约调用selfdestruct函数,然后重新部署同一个合约,循环往复。

也许最简单的方法是使用条件函数?

【问题讨论】:

    标签: function blockchain ethereum smartcontracts


    【解决方案1】:

    不可能“同时”,需要在单独的事务中进行。

    selfdestruct 阻止对合约执行后续操作,并将合约标记为删除。

    但是,EVM 直到交易处理结束才删除其字节码。因此,在下一次 tx 之前,您将无法将新的字节码部署到同一地址。

    pragma solidity ^0.8;
    
    contract Destructable {
        function destruct() external {
            selfdestruct(payable(msg.sender));
        }
    }
    
    contract Deployer {
        function deployAndDestruct() external {
            Destructable destructable = new Destructable{salt: keccak256("hello")}();
            destructable.destruct();
    
            // The second deployment (within the same transation) causes a revert
            // because the bytecode on the desired address is still non-zero
            Destructable destructable2 = new Destructable{salt: keccak256("hello")}();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-04-07
      • 2016-02-28
      • 2021-02-24
      • 2018-07-22
      • 1970-01-01
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      相关资源
      最近更新 更多