Solidity Smart Contract Anatomy

Jeroen Ouwehand
Sep 3, 2018 · 4 min read
“gray laptop computer turned on” by Rich Tervet on Unsplash

Today I will explain you the anatomy of a sample Solidity smart contract. This article will be a brief explaining so you can get an idea what is used inside a Solidity contract, no in-depth stuff. My code is not perfect! So if you find something or have questions, please reach out to me.

What is Solidity:

“Solidity is a contract-oriented, high-level language for implementing smart contracts. It was influenced by C++, Python and JavaScript and is designed to target the Ethereum Virtual Machine (EVM).”

The example contract is called “Freelancer.sol”, the idea is that a freelancer can set:

  • Own data
  • Own assets

A freelancer can get:

  • A look at his own account data.

A freelancer has:

  • A first name
  • A last name
  • An amount of ‘coins’
  • An amount of ‘cash’
  • And offers a ‘service’

Version

The latest version at this moment is 0.4.24 which we are using in this contract. Avoid setting a caret (^) before the version number, because this automatically will update your contract to the latest version which can break the contract. In this example, we are using two experimental versions. Which are optional. By setting pragma experimental “v0.5.0”; we are using already a yet to be released version so we can use already the newest features. The last line pragma experimental ABIEncoderV2; is needed because we will return a struct in a function (will be shown in the function section).

Contract

Inside the contact we are declaring four variables:

  • address public owner;
  • uint256 private coins;
  • uint256 private cash;
  • bytes32 private service;

As you can see we use public and private, besides these two you can also use internal or external.

  • external — can’ t be accessed internally, only externally (saves around 50% gas compared to public).
  • public — everyone can access.
  • internal — only this contract and contracts deriving from it can access.
  • private — can be accessed only from this contract.

I’ve used the bytes32 type instead of the string type to save some more gas.

Events

Struct

  • bytes32 firstname;
  • bytes32 lastname;
  • uint256 coins;
  • uint256 cash;
  • bytes32 service;

Mapping

“Mappings can be seen as hash tables which are virtually initialized such that every possible key exists and is mapped to a value whose byte-representation is all zeros: a type’s default value. The similarity ends here, though: The key data is not actually stored in a mapping, only its keccak256hash used to look up the value.”

We made a mapping which is called FreelancersData (note the plural). We can use the address to get the FreelancerData of the freelancer we want to use.

Modifier

Constructor

Functions

A function looks like this:

  • function
  • [function_name([parameters])] —name of the function
  • [external/public/internal/private] — give the amount of access.
  • [modifier: optional] — give a modifier you created earlier
  • [view/pure/payable: optional] — make it more strict or give a fallback.
  • returns ([type] [name: optional]) — what to return inside the function.
  • { [function_code] } . — your code which going to be executed upon calling.

Inside the myAccount() function we also made use of memory:

“memory, of which a contract obtains a freshly cleared instance for each message call. Memory is linear and can be addressed at byte level, but reads are limited to a width of 256 bits, while writes can be either 8 bits or 256 bits wide. Memory is expanded by a word (256-bit), when accessing (either reading or writing) a previously untouched memory word (ie. any offset within a word). At the time of expansion, the cost in gas must be paid. Memory is more costly the larger it grows (it scales quadratically).”

Examples:

You can easily test out this smart contract online with Remix. The complete code of Freelancer.sol:


Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Jeroen Ouwehand

Written by

🔥Writing new articles every month — Software Developer from The Netherlands — Angular-TypeScript-Flutter-Golang-Node.js — https://github.com/jeroenouw

Coinmonks

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade