ETHEREUM SMART CONTRACTS DEVELOPMENT
Solidity Fundamentals
Layout of a Contract
Let’s briefly talk about what this series is about. Here are some topics that we will discuss, I hope you will enjoy it.
- Solidity Fundamentals
- Infrastructure (Metamask, Infura, DappNode)
- Testing Ethereum Smart Contracts (Waffle, Truffle Suite, Dapp Tools)
- Smart Contracts Security Best Practices
Solidity Fundamentals
In this part, we will briefly examine the layout of a solidity source file. I encourage you not to run the given code recipes for now but observe and understand each given piece of information. Later, we will be coding enough to get familiar with all concepts.
Every source file should start with a comment indicating its license such as;
pragma
keyword specifies what version of the compiler are valid to compile this contract file. More details will be discussed later.
Solidity supports import statements to help modularise your code that are similar to those available in JavaScript (from ES6 on). However, Solidity does not support the concept of a default export. See examples below;
For documenting code, using comments will be very helpful.
A triple slash (///) or a double asterisk block (/** */) is called natspec comment which is used directly above function declarations or statements to give information about that function or statement.
According to solidity official docs, the best practice to organize your layout of contract is below.
Layout contract elements in the following order:
- Pragma statements
- Import statements
- Interfaces
- Libraries
- Contracts
Inside each contract, library or interface, use following order:
- State variables
- Events
- Function Modifiers
- Struct, Arrays or Enums
- Constructor
- Fallback — Receive function
- External visible functions
- Public visible functions
- Internal visible functions
- Private visible functions
Example recipe for Layout of a contract. Please read comments and examine the below examples carefully, concentrate on the layout not the logic of given codes.
Next part we will work on value types such as booleans, integers, addresses and dynamically sized and fixed sized types — bytes and strings. Thanks for reading.