ETHEREUM SMART CONTRACTS DEVELOPMENT

Solidity Fundamentals

Layout of a Contract

Ferdi Kurt
Coinmonks
Published in
2 min readNov 26, 2020

--

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.

GIPHY

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:

  1. Pragma statements
  2. Import statements
  3. Interfaces
  4. Libraries
  5. Contracts

Inside each contract, library or interface, use following order:

  1. State variables
  2. Events
  3. Function Modifiers
  4. Struct, Arrays or Enums
  5. Constructor
  6. Fallback — Receive function
  7. External visible functions
  8. Public visible functions
  9. Internal visible functions
  10. 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.

Feel free to ask any question.

Be well, do good work, and keep in touch!

Ferdi Kurt

--

--