0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

solidityにおけるエラーハンドリング、assert()とrequire()

Posted at

solidityでは状態を元に戻す例外が使われている。assert()もrequire()も
変更を元に戻して例外を返す。assert()は内部エラーの検証や不変量の確認に使われ、
require()はインプットやコントラクタの状態変数が有効かの確認に使われる。
下の例ではrequire()がインプット(msg.value)が偶数かチェックし、assert()はコントラクトの
の結果の正しさをチェックしている。

exception.sol
contract Sharer {
    function sendHalf(address addr) public payable returns (uint balance) {
        require(msg.value % 2 == 0); 
        uint balanceBeforeTransfer = this.balance;
        addr.transfer(msg.value / 2);
        assert(this.balance == balanceBeforeTransfer - msg.value / 2);
        return this.balance;
    }
}

またassert()はすべてのGasを使い切るが、require()は残ったGasを返却する。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?