solidity/docs/structure-of-a-contract.rst

139 lines
2.9 KiB
ReStructuredText
Raw Normal View History

2015-12-07 20:16:25 +00:00
.. index:: contract, state variable, function, event, struct, enum, function;modifier
.. _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.
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.
.. _structure-state-variables:
State Variables
===============
State variables are values which are permanently stored in contract storage.
::
pragma solidity ^0.4.0;
contract SimpleStorage {
2016-05-18 15:05:28 +00:00
uint storedData; // State variable
// ...
}
See the :ref:`types` section for valid state variable types and
2017-02-02 23:52:34 +00:00
:ref:`visibility-and-getters` for possible choices for
2016-03-01 11:15:20 +00:00
visibility.
.. _structure-functions:
Functions
=========
Functions are the executable units of code within a contract.
::
pragma solidity ^0.4.0;
contract SimpleAuction {
2016-09-05 14:29:08 +00:00
function bid() payable { // Function
2016-05-18 15:05:28 +00:00
// ...
}
}
:ref:`function-calls` can happen internally or externally
2017-02-02 23:52:34 +00:00
and have different levels of visibility (:ref:`visibility-and-getters`)
2016-05-13 14:32:35 +00:00
towards other contracts.
.. _structure-function-modifiers:
Function Modifiers
==================
Function modifiers can be used to amend the semantics of functions in a declarative way
(see :ref:`modifiers` in contracts section).
::
2016-05-13 14:32:35 +00:00
pragma solidity ^0.4.0;
contract Purchase {
2016-05-18 15:05:28 +00:00
address public seller;
2016-05-13 14:32:35 +00:00
2016-05-18 15:05:28 +00:00
modifier onlySeller() { // Modifier
if (msg.sender != seller) throw;
2016-09-05 14:29:08 +00:00
_;
2016-05-18 15:05:28 +00:00
}
2016-05-13 14:32:35 +00:00
2016-05-18 15:05:28 +00:00
function abort() onlySeller { // Modifier usage
// ...
}
}
.. _structure-events:
Events
======
Events are convenience interfaces with the EVM logging facilities.
::
pragma solidity ^0.4.0;
contract SimpleAuction {
2016-05-18 15:05:28 +00:00
event HighestBidIncreased(address bidder, uint amount); // Event
2016-05-13 14:32:35 +00:00
2016-09-05 14:29:08 +00:00
function bid() payable {
2016-05-18 15:05:28 +00:00
// ...
HighestBidIncreased(msg.sender, msg.value); // Triggering event
}
}
2016-05-13 14:32:35 +00:00
See :ref:`events` in contracts section for information on how events are declared
and can be used from within a dapp.
.. _structure-structs-types:
Structs Types
=============
2016-05-13 14:32:35 +00:00
Structs are custom defined types that can group several variables (see
:ref:`structs` in types section).
::
pragma solidity ^0.4.0;
contract Ballot {
2016-05-18 15:05:28 +00:00
struct Voter { // Struct
uint weight;
bool voted;
address delegate;
uint vote;
}
}
.. _structure-enum-types:
Enum Types
==========
2016-05-13 14:32:35 +00:00
Enums can be used to create custom types with a finite set of values (see
:ref:`enums` in types section).
::
2016-05-13 14:32:35 +00:00
pragma solidity ^0.4.0;
contract Purchase {
2016-05-18 15:05:28 +00:00
enum State { Created, Locked, Inactive } // Enum
}