2015-12-07 20:16:25 +00:00
|
|
|
.. index:: contract, state variable, function, event, struct, enum, function;modifier
|
|
|
|
|
2016-02-18 10:45:15 +00:00
|
|
|
.. _contract_structure:
|
|
|
|
|
2015-12-07 20:16:25 +00:00
|
|
|
***********************
|
|
|
|
Structure of a Contract
|
|
|
|
***********************
|
|
|
|
|
|
|
|
Contracts in Solidity are similar to classes in object-oriented languages.
|
2016-02-19 10:47:16 +00:00
|
|
|
Each contract can contain declarations of :ref:`structure-state-variables`, :ref:`structure-functions`,
|
|
|
|
:ref:`structure-function-modifiers`, :ref:`structure-events`, :ref:`structure-structs-types` and :ref:`structure-enum-types`.
|
2015-12-07 20:16:25 +00:00
|
|
|
Furthermore, contracts can inherit from other contracts.
|
|
|
|
|
2016-02-19 10:47:16 +00:00
|
|
|
.. _structure-state-variables:
|
2016-02-18 10:45:15 +00:00
|
|
|
|
|
|
|
State Variables
|
|
|
|
===============
|
|
|
|
|
|
|
|
State variables are values which are permanently stored in contract storage.
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
contract SimpleStorage {
|
|
|
|
uint storedData; // State variable
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
|
2016-02-19 10:30:00 +00:00
|
|
|
See the :ref:`types` section for valid state variable types and
|
|
|
|
:ref:`visibility-and-accessors` for possible choices for
|
2016-03-01 11:15:20 +00:00
|
|
|
visibility.
|
2016-02-19 10:30:00 +00:00
|
|
|
|
2016-02-19 10:47:16 +00:00
|
|
|
.. _structure-functions:
|
2016-02-18 10:45:15 +00:00
|
|
|
|
|
|
|
Functions
|
|
|
|
=========
|
|
|
|
|
|
|
|
Functions are the executable units of code within a contract.
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
contract SimpleAuction {
|
|
|
|
function bid() { // Function
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-19 10:47:16 +00:00
|
|
|
:ref:`function-calls` can happen internally or externally
|
2016-02-19 10:30:00 +00:00
|
|
|
and have different levels of visibility (:ref:`visibility-and-accessors`)
|
|
|
|
towards other contracts.
|
|
|
|
|
2016-02-19 10:47:16 +00:00
|
|
|
.. _structure-function-modifiers:
|
2016-02-18 10:45:15 +00:00
|
|
|
|
|
|
|
Function Modifiers
|
|
|
|
==================
|
|
|
|
|
2016-02-19 11:05:56 +00:00
|
|
|
Function modifiers can be used to amend the semantics of functions in a declarative way
|
|
|
|
(see :ref:`modifiers` in contracts section).
|
2016-02-18 10:45:15 +00:00
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
contract Purchase {
|
|
|
|
address public seller;
|
|
|
|
|
|
|
|
modifier onlySeller() { // Modifier
|
|
|
|
if (msg.sender != seller) throw;
|
|
|
|
_
|
|
|
|
}
|
|
|
|
|
|
|
|
function abort() onlySeller { // Modifier usage
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-19 10:47:16 +00:00
|
|
|
.. _structure-events:
|
2016-02-18 10:45:15 +00:00
|
|
|
|
|
|
|
Events
|
|
|
|
======
|
|
|
|
|
|
|
|
Events are convenience interfaces with the EVM logging facilities.
|
|
|
|
|
|
|
|
::
|
|
|
|
|
2016-05-05 17:28:45 +00:00
|
|
|
contract SimpleAuction {
|
2016-02-18 10:45:15 +00:00
|
|
|
event HighestBidIncreased(address bidder, uint amount); // Event
|
|
|
|
|
|
|
|
function bid() {
|
|
|
|
// ...
|
|
|
|
HighestBidIncreased(msg.sender, msg.value); // Triggering event
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-19 11:05:56 +00:00
|
|
|
See :ref:`events` in contracts section for information on how events are declared
|
|
|
|
and can be used from within a dapp.
|
|
|
|
|
2016-02-19 10:47:16 +00:00
|
|
|
.. _structure-structs-types:
|
2016-02-18 10:45:15 +00:00
|
|
|
|
|
|
|
Structs Types
|
|
|
|
=============
|
|
|
|
|
2016-02-19 11:05:56 +00:00
|
|
|
Structs are custom defined types that can group several variables (see
|
|
|
|
:ref:`structs` in types section).
|
2016-02-18 10:45:15 +00:00
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
contract Ballot {
|
|
|
|
struct Voter { // Struct
|
|
|
|
uint weight;
|
|
|
|
bool voted;
|
|
|
|
address delegate;
|
|
|
|
uint vote;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-19 10:47:16 +00:00
|
|
|
.. _structure-enum-types:
|
2016-02-18 10:45:15 +00:00
|
|
|
|
|
|
|
Enum Types
|
|
|
|
==========
|
|
|
|
|
2016-02-19 11:05:56 +00:00
|
|
|
Enums can be used to create custom types with a finite set of values (see
|
|
|
|
:ref:`enums` in types section).
|
2016-02-18 10:45:15 +00:00
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
contract Purchase {
|
|
|
|
enum State { Created, Locked, Inactive } // Enum
|
|
|
|
}
|