這個章節開始講Error

為什麼要特別一個章節拿出來說呢?智能合約的Error 一但發生就會停止執行在錯誤發生之後的gas

所以Error下得好也是會省gas的

來看一下寫法

大致可分為四種

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract ErrorExample{
    
	function useRequire(uint _i){
		require(_i > 10,"Input must be greater the 10");
	}
	function useRevert(uint _i){
		if(_i >10){
			revert("Input must be greater than 10 ");
		}
	}
	uint public num;
	function useAssert(){
		assert(num == 0);
	}

	error Unauthorized(string error);
	address owner = msg.sender;

	function useError(){
		if(msg.sender != owner)
			revert Unauthorized("not owner");

			// do something ...
	}
}

11.Address & msg

有任何問題可反饋: [email protected]