|
1 | 1 | .. index:: contract, state variable, function, event, struct, enum, function;modifier
|
2 | 2 |
|
| 3 | +.. _contract_structure: |
| 4 | + |
3 | 5 | ***********************
|
4 | 6 | Structure of a Contract
|
5 | 7 | ***********************
|
6 | 8 |
|
7 | 9 | Contracts in Solidity are similar to classes in object-oriented languages.
|
8 |
| -Each contract can contain declarations of **state variables**, **functions**, |
9 |
| -**function modifiers**, **events**, **structs types** and **enum types**. |
| 10 | +Each contract can contain declarations of :ref:`structure-state-variables`, :ref:`structure-functions`, |
| 11 | +:ref:`structure-function-modifiers`, :ref:`structure-events`, :ref:`structure-structs-types` and :ref:`structure-enum-types`. |
10 | 12 | Furthermore, contracts can inherit from other contracts.
|
11 | 13 |
|
12 |
| -* State variables are values which are permanently stored in contract storage. |
13 |
| -* Functions are the executable units of code within a contract. |
14 |
| -* Function modifiers can be used to amend the semantics of functions in a declarative way. |
15 |
| -* Events are convenience interfaces with the EVM logging facilities. |
16 |
| -* Structs are custom defined types that can group several variables. |
17 |
| -* Enums can be used to create custom types with a finite set of values. |
| 14 | +.. _structure-state-variables: |
| 15 | + |
| 16 | +State Variables |
| 17 | +=============== |
| 18 | + |
| 19 | +State variables are values which are permanently stored in contract storage. |
| 20 | + |
| 21 | +:: |
| 22 | + |
| 23 | + contract SimpleStorage { |
| 24 | + uint storedData; // State variable |
| 25 | + // ... |
| 26 | + } |
| 27 | + |
| 28 | +See the :ref:`types` section for valid state variable types and |
| 29 | +:ref:`visibility-and-accessors` for possible choices for |
| 30 | +visability. |
| 31 | + |
| 32 | +.. _structure-functions: |
| 33 | + |
| 34 | +Functions |
| 35 | +========= |
| 36 | + |
| 37 | +Functions are the executable units of code within a contract. |
| 38 | + |
| 39 | +:: |
| 40 | + |
| 41 | + contract SimpleAuction { |
| 42 | + function bid() { // Function |
| 43 | + // ... |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | +:ref:`function-calls` can happen internally or externally |
| 48 | +and have different levels of visibility (:ref:`visibility-and-accessors`) |
| 49 | +towards other contracts. |
| 50 | + |
| 51 | +.. _structure-function-modifiers: |
| 52 | + |
| 53 | +Function Modifiers |
| 54 | +================== |
| 55 | + |
| 56 | +Function modifiers can be used to amend the semantics of functions in a declarative way |
| 57 | +(see :ref:`modifiers` in contracts section). |
| 58 | + |
| 59 | +:: |
| 60 | + |
| 61 | + contract Purchase { |
| 62 | + address public seller; |
| 63 | + |
| 64 | + modifier onlySeller() { // Modifier |
| 65 | + if (msg.sender != seller) throw; |
| 66 | + _ |
| 67 | + } |
| 68 | + |
| 69 | + function abort() onlySeller { // Modifier usage |
| 70 | + // ... |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + in the section on contracts for a more in-depth explanation. |
| 75 | + |
| 76 | +.. _structure-events: |
| 77 | + |
| 78 | +Events |
| 79 | +====== |
| 80 | + |
| 81 | +Events are convenience interfaces with the EVM logging facilities. |
| 82 | + |
| 83 | +:: |
| 84 | + |
| 85 | + contract SimpleAuction { |
| 86 | + event HighestBidIncreased(address bidder, uint amount); // Event |
| 87 | + |
| 88 | + function bid() { |
| 89 | + // ... |
| 90 | + HighestBidIncreased(msg.sender, msg.value); // Triggering event |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | +See :ref:`events` in contracts section for information on how events are declared |
| 95 | +and can be used from within a dapp. |
| 96 | + |
| 97 | +.. _structure-structs-types: |
| 98 | + |
| 99 | +Structs Types |
| 100 | +============= |
| 101 | + |
| 102 | +Structs are custom defined types that can group several variables (see |
| 103 | +:ref:`structs` in types section). |
| 104 | + |
| 105 | +:: |
| 106 | + |
| 107 | + contract Ballot { |
| 108 | + struct Voter { // Struct |
| 109 | + uint weight; |
| 110 | + bool voted; |
| 111 | + address delegate; |
| 112 | + uint vote; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | +.. _structure-enum-types: |
| 117 | + |
| 118 | +Enum Types |
| 119 | +========== |
| 120 | + |
| 121 | +Enums can be used to create custom types with a finite set of values (see |
| 122 | +:ref:`enums` in types section). |
| 123 | + |
| 124 | +:: |
| 125 | + |
| 126 | + contract Purchase { |
| 127 | + enum State { Created, Locked, Inactive } // Enum |
| 128 | + } |
0 commit comments