aboutsummaryrefslogtreecommitdiffstats
path: root/docs/structure-of-a-contract.rst
blob: 1b80b1c2235ed54e18e3880926ccbe7aee13e849 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
.. index:: contract, state variable, function, event, struct, enum, function;modifier

.. _contract_structure:

***********************
Structure of a Contract
***********************

Contracts in Solidity are similar to classes in object-oriented languages.
Each contract can contain declarations of :ref:`state_variables`, :ref:`functions`,
:ref:`function_modifiers`, :ref:`events`, :ref:`structs_types` and :ref:`enum_types`.
Furthermore, contracts can inherit from other contracts.

.. _state_variables:

State Variables
===============

State variables are values which are permanently stored in contract storage.

::

  contract SimpleStorage {
    uint storedData; // State variable
    // ...
  }

.. _functions:

Functions
=========

Functions are the executable units of code within a contract.

::

  contract SimpleAuction {
    function bid() { // Function
      // ...
    }
  }

.. _function_modifiers:

Function Modifiers
==================

Function modifiers can be used to amend the semantics of functions in a declarative way.

::
  
  contract Purchase {
    address public seller;
    
    modifier onlySeller() { // Modifier
        if (msg.sender != seller) throw;
        _
    }
    
    function abort() onlySeller { // Modifier usage
        // ...
    }
  }

.. _events:

Events
======

Events are convenience interfaces with the EVM logging facilities.

::

  contract SimpleAuction {
    event HighestBidIncreased(address bidder, uint amount); // Event
    
    function bid() {
      // ...
      HighestBidIncreased(msg.sender, msg.value); // Triggering event
    }
  }

.. _structs_types:

Structs Types
=============

Structs are custom defined types that can group several variables.

::

  contract Ballot {
    struct Voter { // Struct
      uint weight;
      bool voted;
      address delegate;
      uint vote;
    }
  }

.. _enum_types:

Enum Types
==========

Enums can be used to create custom types with a finite set of values.

::
  
  contract Purchase {
    enum State { Created, Locked, Inactive } // Enum
  }