|
1 |
| -# Contract Structure |
| 1 | +# Structure of a Contract |
2 | 2 |
|
3 |
| -TODO: Add Lesson for Contract Structure |
| 3 | +Vyper contracts are contained within files. Each file contains exactly |
| 4 | +one contract. |
| 5 | + |
| 6 | +This section provides a quick overview of the types of data present |
| 7 | +within a contract, with links to other sections where you can obtain |
| 8 | +more details. |
| 9 | + |
| 10 | +## Version Pragma |
| 11 | + |
| 12 | +Vyper supports a version pragma to ensure that a contract is only |
| 13 | +compiled by the intended compiler version, or range of versions. Version |
| 14 | +strings use [NPM](https://docs.npmjs.com/misc/semver) style syntax. |
| 15 | + |
| 16 | +```python |
| 17 | +# @version ^0.2.0 |
| 18 | +``` |
| 19 | + |
| 20 | +In the above example, the contract only compiles with Vyper versions |
| 21 | +`0.2.x`. |
| 22 | + |
| 23 | +## State Variables |
| 24 | + |
| 25 | +State variables are values which are permanently stored in contract |
| 26 | +storage. They are declared outside of the body of any functions, and |
| 27 | +initially contain the default value\<types-initial\> for their type. |
| 28 | + |
| 29 | +```python |
| 30 | +storedData: int128 |
| 31 | +``` |
| 32 | + |
| 33 | +State variables are accessed via the self\<constants-self\> object. |
| 34 | + |
| 35 | +```python |
| 36 | +self.storedData = 123 |
| 37 | +``` |
| 38 | + |
| 39 | +See the documentation on Types \<types\> or [Scoping and |
| 40 | +Declarations](scoping) for more information. |
| 41 | + |
| 42 | +## Functions |
| 43 | + |
| 44 | +Functions are executable units of code within a contract. |
| 45 | + |
| 46 | +```python |
| 47 | +@external |
| 48 | +def bid(): |
| 49 | + ... |
| 50 | +``` |
| 51 | + |
| 52 | +Functions may be called internally or externally depending on their |
| 53 | +visibility \<function-visibility\>. Functions may accept input arguments |
| 54 | +and return variables in order to pass values between them. |
| 55 | + |
| 56 | +See the Functions \<control-structures-functions\> documentation for |
| 57 | +more information. |
| 58 | + |
| 59 | +## Events |
| 60 | + |
| 61 | +Events provide an interface for the EVM's logging facilities. Events may |
| 62 | +be logged with specially indexed data structures that allow clients, |
| 63 | +including light clients, to efficiently search for them. |
| 64 | + |
| 65 | +```python |
| 66 | +event Payment: |
| 67 | + amount: int128 |
| 68 | + sender: indexed(address) |
| 69 | + |
| 70 | +total_paid: int128 |
| 71 | + |
| 72 | +@external |
| 73 | +@payable |
| 74 | +def pay(): |
| 75 | + self.total_paid += msg.value |
| 76 | + log Payment(msg.value, msg.sender) |
| 77 | +``` |
| 78 | + |
| 79 | +See the Event \<event-logging\> documentation for more information. |
| 80 | + |
| 81 | +## Interfaces |
| 82 | + |
| 83 | +An interface is a set of function definitions used to enable calls |
| 84 | +between smart contracts. A contract interface defines all of that |
| 85 | +contract's externally available functions. By importing the interface, |
| 86 | +your contract now knows how to call these functions in other contracts. |
| 87 | + |
| 88 | +Interfaces can be added to contracts either through inline definition, |
| 89 | +or by importing them from a seperate file. |
| 90 | + |
| 91 | +```python |
| 92 | +interface FooBar: |
| 93 | + def calculate() -> uint256: view |
| 94 | + def test1(): nonpayable |
| 95 | +``` |
| 96 | + |
| 97 | +```python |
| 98 | +from foo import FooBar |
| 99 | +``` |
| 100 | + |
| 101 | +Once defined, an interface can then be used to make external calls to a |
| 102 | +given address: |
| 103 | + |
| 104 | +```python |
| 105 | +@external |
| 106 | +def test(some_address: address): |
| 107 | + FooBar(some_address).calculate() |
| 108 | +``` |
| 109 | + |
| 110 | +See the Interfaces \<interfaces\> documentation for more information. |
| 111 | + |
| 112 | +## Structs |
| 113 | + |
| 114 | +A struct is custom defined type that can allows you to group several |
| 115 | +variables together: |
| 116 | + |
| 117 | +```python |
| 118 | +struct MyStruct: |
| 119 | + value1: int128 |
| 120 | + value2: decimal |
| 121 | +``` |
| 122 | + |
| 123 | +See the Structs \<types-struct\> documentation for more information. |
4 | 124 |
|
5 | 125 | <!-- tabs:start -->
|
6 | 126 |
|
7 | 127 | #### ** Template **
|
8 | 128 |
|
9 |
| -[embedded-code](../assets/1/1.1-template-code.vy ":include :type=code embed-template") |
| 129 | +[embedded-code](../assets/1/1.1-template-code.vy ':include :type=code embed-template') |
10 | 130 |
|
11 | 131 | #### ** Solution **
|
12 | 132 |
|
13 |
| -[embedded-code-final](../assets/1/1.1-finished-code.vy ":include :type=code embed-final") |
| 133 | +[embedded-code-final](../assets/1/1.1-finished-code.vy ':include :type=code embed-final') |
14 | 134 |
|
15 | 135 | #### ** Previous Chapter Solution **
|
16 | 136 |
|
17 |
| -[embedded-code-previous](../assets/1/1.0-finished-code.vy ":include :type=code embed-previous") |
| 137 | +[embedded-code-previous](../assets/1/1.0-finished-code.vy ':include :type=code embed-previous') |
18 | 138 |
|
19 | 139 | <!-- tabs:end -->
|
0 commit comments