Require
You can use Blockchain.require() to add requirements or restrictions for an action to be executed. It checks the provided condition is true or not. If it is false, it triggers a revert. This is sometimes called a modifier.
You can view further documentation into Blockchain.require() on the AVM API website.
Example Contract
This contract contains a common requirement function, onlyOwner
. Calling this function at the top of a function to make sure that function to only be executed by the owner of the contract. The contract owner is set when the contract is deployed.
In the above example, the setString
function will fail if the address that calls the function is not the owner.
Blockchain.caller()
will only get the caller address when it’s called by a contract transaction, since for method call, from
account is not required.