Contract Structure
Contract structure
State and local variables
State variables are variables declared at the beginning of the contract, outside the scope of local variables declared in function.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.0 <0.9.0;
contract SimpleStorage {
uint public storedData; // State variable
// ...
function updateStoredData(uint newData) public {
uint formattedData = newData * 2; // formattedData is local variable
storedData = formattedData;
}
function getFormattedData() public view returns (uint) {
return formattedData; // failed can't compile because formattedData is local scope to the other function
}
function getStoredData() public view returns (uint) {
return storedData; // can compile because global variable
}
}Functions
Function are functions declared to perform calculations, change the value of variables, etc. A sample function is given below.
Function modifiers
Function modifier are declarations for function to create conditions for running actions of that function.
Events
event is a feature for recording smart contract activities. event is often used in building interactive UI with smart contracts.
Errors
error is used to inform the user why the action failed, and error has a lower gas cost than returning string.
Struct types
struct is used to declare a type of object.
Enum types
enum is used to declare a type whose values ββare constant.
Last updated