The nitty-gritty of Ethereum and Solidity : Smart Contracts (Part 1).

Alberto Molina
Coinmonks
4 min readJun 28, 2022

--

Ethereum Smart contracts, at a very basic level, are like regular external accounts (EOA) that users own and use to interact with the blockchain. Smart contracts have an address, can own ether (or any other token) and can call other smart contracts. However there are some differences between smart contracts (SCs) and EOA that make SCs special:

  • Nobody owns them (there is no private key attached to a smart contract)
  • They contain pieces of code (byte code) and a state.
  • They cannot trigger a transaction, in other words, in order for a SC to execute its code (transfer ether, call another SC, …) it has to be invoked either by an EOA or another SC.

Anatomy of a smart contract

In a nutshell, smart contracts share some properties with regular externally owned accounts (EOAs) while also having their unique characteristics:

  • Address : Both smart contracts and EOAs are identified by 20-byte addresses, which are unique identifiers.
  • Balance : The balance represents the amount of ether that the contract currently holds, this feature is common to both smart contracts and EOAs.
  • Nonce : Integer that starts at 0 and increases by one every time the smart contract deploys another smart contract. Common to EOAs too.
  • Bytecode : Smart contract’s logic. Specific to Smart contracts.
  • State : Smart contracts set of variables. Specific to Smart contracts.

The most important part of a smart contract is its bytecode and state.

The bytecode is simply a very large string of hexadecimal characters, representing opcodes or values. Developers implement smart contracts using high level languages like Solidity, then compile it into byte code.

  • Opcodes : opcodes are the commands the EVM executes, they are very simple operations like adding or subtracting two values.
  • Values : some opcodes require values to run and sometimes those values are hardcoded into the byte code.

608060405234801561001057600080fd5b5060e38061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c806360fe47b114602d575b600080fd5b60436004803603810190603f91906085565b6045565b005b8060008190555050565b600080fd5b6000819050919050565b6065816054565b8114606f57600080fd5b50565b600081359050607f81605e565b92915050565b6000602082840312156098576097604f565b5b600060a4848285016072565b9150509291505056fea26469706673582212208be6548ef9ea0e99e9a3a3cf8f89ab64aa8d2c06c1974cefb0ab2eb160aa777964736f6c63430008120033

example of a smart contract’s byte code

The state is simply a set of variables associated to the smart contract and saved onto the blockchain. These variables can only be modified by the contract’s bytecode but can be read by anyone having access to the blockchain.

Smart contracts are what turn the Ethereum blockchain into a world-wide distributed computer.

Once a smart contract is deployed, its bytecode cannot be changed. However, you can effectively “delete” it using the “selfdestruct” opcode. Selfdestruct will first transfer the contract’s current balance to another address, then the contract’s bytecode and state will be permanently removed and the contract’s address will be “released” (another contract could be deployed to the same address).

Smart Contracts in Solidity

Smart contracts can be implemented in Solidity, arguably the most widely adopted programming language for smart contracts. The Solidity compiler generates the bytecode that must be included in the “data” field of a transaction for deployment.

A solidity smart contract can be roughly divided in the following way:

  • State Variables: These are a list of variables that will be persistently stored in the smart contract’s storage.
  • Events: These define the events that can be emitted (logged into the blockchain) during contract execution.
  • Errors: This section defines the error messages that can be returned in a transaction response when a transaction reverts or encounters an issue.
  • Modifiers: Modifiers are pieces of code that can be applied before and/or after the execution of functions.
  • Constructor: The constructor contains code that is executed only once, specifically when the contract is deployed. Typically, the constructor is used to initialize the contract’s state and set immutable variables.
  • External & Public Functions: These are functions that can be invoked from outside the contract, whether from an externally owned account (EOA) or another smart contract. Public functions can also be called from within the contract itself.
  • Internal & Private Functions: These functions can only be invoked from within the contract itself. Internal functions, in contrast to private functions, can also be called by contracts that inherit from the current contract, with this restriction enforced by the Solidity compiler.

Please check the second part of this blog series to see some other Smart Contract particularities :

The nitty-gritty of Ethereum and Solidity : Smart Contracts (Part 2).

--

--