modifier in solidity

modifier is used to declare some code that will be re-used throughout the contract. Write once, use everywhere. For example, instead of doing the require check on owner, declare this modifier: modifier onlyOwner { require(msg.sender == owner, "Only contract owner can do this"); _; // run the function } and can…

assert in solidity

assert is used to check the contract's code for errors and make sure code runs like it's supposed to. Usually by testing certain invariants (an invariant is a condition that should always be true at a certain stage in the code). These are the cases when…

require in solidity

require will check if a condition is true and allow code to flow only if condition is true. If the condition is false, require will throw an error, the rest of the code will not be executed and the transaction will revert. The first param of require call is the…

View vs Pure in Solidity

pure function cannot read from state or write to state. In functional programming terminology, it is a function that does not have any side-effects. // pure function cannot read from state or write to state function alwaysOne() public pure returns(int) { return 1; } view function can read from state, but can…

Structs in Solidity

Struct is user-defined/custom data type where you can define some arbitrary object with some properties. You can't return the struct directly, but can return a tuple of its properties. // SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.4; contract HelloWorld { // struct is user-defined/custom data type where you…