【发布时间】:2021-11-24 21:57:57
【问题描述】:
是否可以同时销毁(使用 selftDestruct 函数)和生成相同的合约?
我们假设第一个合约有1Eth,然后将所有的eth提取到另一个erc20地址,完全提取后,第一个合约调用selfdestruct函数,然后重新部署同一个合约,循环往复。
也许最简单的方法是使用条件函数?
【问题讨论】:
标签: function blockchain ethereum smartcontracts
是否可以同时销毁(使用 selftDestruct 函数)和生成相同的合约?
我们假设第一个合约有1Eth,然后将所有的eth提取到另一个erc20地址,完全提取后,第一个合约调用selfdestruct函数,然后重新部署同一个合约,循环往复。
也许最简单的方法是使用条件函数?
【问题讨论】:
标签: function blockchain ethereum smartcontracts
不可能“同时”,需要在单独的事务中进行。
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")}();
}
}
【讨论】: