Structure of a Contract
Last updated
Was this helpful?
Last updated
Was this helpful?
Contracts in Solidity are similar to classes in object-oriented languages. Each contract can contain declarations of , , , , , and . Furthermore, contracts can inherit from other contracts.
There are also special kinds of contracts called and .
The section about contains more details than this section, which serves to provide a quick overview.
State variables are variables whose values are permanently stored in contract storage.
See the section for valid state variable types and for possible choices for visibility.
Functions are the executable units of code. Functions are usually defined inside a contract, but they can also be defined outside of contracts.
Overloading, that is, having the same modifier name with different parameters, is not possible.
Events are convenience interfaces with the EVM logging facilities.
can happen internally or externally and have different levels of towards other contracts. accept to pass parameters and values between them.
Function modifiers can be used to amend the semantics of functions in a declarative way (see in the contracts section).
Like functions, modifiers can be .
See in contracts section for information on how events are declared and can be used from within a dapp.
Errors allow you to define descriptive names and data for failure situations. Errors can be used in . In comparison to string descriptions, errors are much cheaper and allow you to encode additional data. You can use NatSpec to describe the error to the user.
See in the contracts section for more information.
Structs are custom defined types that can group several variables (see in types section).
Enums can be used to create custom types with a finite set of ‘constant values’ (see in types section).