Solidity

在建構子中使用 selfdestruct

  • December 24, 2020

呼叫這個存根的目的是什麼?

contract Sacrifice {
   constructor(address payable _recipient) payable {
       selfdestruct(_recipient);
   }
}

用法:

if (_token == address(0)) {
   if (!_to.send(_amount)) {
           (new Sacrifice).value(_amount)(_to);
}

編譯器錯誤:

CompileError: /token/contracts/EasyStaking.sol:313:17: TypeError: Member "call" not found or not visible after argument-dependent lookup in function (address payable) payable returns (contract Sacrifice). Did you intend to call the function?
               (new Sacrifice).call(_amount)(_to);
               ^------------------^

用這個修復它:

       if (_token == address(0)) {
           if (!_to.send(_amount)) { // solium-disable-line security/no-send
               (new Sacrifice){value: _amount}(_to);
           }
       } 

強制將selfdestruct合約餘額轉移到目標地址。

該程式碼首先嘗試_to.send(_amount)轉移乙太幣。如果回退函式reverssend將返回false。如果失敗,它會使用 new/selfdestruct 無條件地將乙太幣轉移到目標地址。

引用自:https://ethereum.stackexchange.com/questions/91453