Structure of a smart contract

There's a number of key concepts defining U2U smart contracts that differ from a web2 development paradigm.

Types

The contract type in Solidity is a struct that groups a number of connected functions for a single objective.
Address types are 20-byte-long Ethereum addresses that start with the prefix 0x. They are written in hexadecimal format.
The majority of other kinds, such as booleans, integers, fixed-point numbers, byte arrays, and literals, will be recognisable to you as a developer.

Data Storage

In Solidity, reference data values can be stored as storage, memory or calldata depending on the role of the data stored. In particular:
  • storage keeps the data permanently on the blockchain, and is extremely expensive.
  • memory values are stored only for the lifetime of the smart contract's execution, and are inexpensive to use (costing only small amounts of gas).
  • calldata is a special data location that contains the function arguments, and is only available for external function call parameters.

Function Modifiers

Functions exist to get / set information based on calls initiated by external transactions. In particular, a smart contract can never run unless initiated by an external transaction - they don't execute silently in the background.
Access modifiers include:
  • public: Can be accessed by all functions or callers
  • external: Can be accessed only by external callers, not internal functions
  • internal: Can be accessed only by this contract, or contracts deriving from it
  • private: Can be accessed only from this contract itself
Other modifiers include:
  • view: This guarantees that the function will not modify the state of the contract's data (or data in storage).
  • pure: This guarantees that the function with neither read nor modify the state of the contract's data.
  • payable: Functions and addresses declared payable can receive ether into their contracts.

Special Functions and Variables

There are a number of global variables and functions that will be helpful to remember you have access to! Some special variables are:
  • block.number (uint256): The number of the most recent block.
  • block.timestamp (uint256): The UNIX timestamp of the most recent block.
  • block.gaslimit (uint256): The gas limit of the current block.
  • msg.sender (address payable): The sender of the transaction triggering the contract.
  • msg.value (uint256): The number of wei transferred with the message.
Special functions include:
  • receive(): Contracts may only have one of these functions declared. It serves as the default destination when a contract is sent Ether. It cannot have arguments, return anything, and must be external and payable.
  • fallback(): Contracts may only have one of these functions declared. It serves as the fallback if a call to the contract does not match any function, or if no data was supplied and the receive() function was not declared. It cannot have arguments, return anything, and must be external.

Events

Solidity events are roughly equivalent to logging in other programming paradigms. They're emitted when a contract is executed, stored permanently on the blockchain, but aren't accessible to be modified / read by smart contracts.
You can declare and emit an event like this:
event TestEvent (
uint256 date,
string value
);
emit TestEvent(block.timestamp,’My first event!”);
Events can be accessed in a variety of different ways:
  1. 1.
    Events are stored in the receipts of transactions, and can be accessed there.
  2. 2.
    You can subscribe to an event with myContract.events.TestEvent([options][, callback])
  3. 3.
    You can request past events using a request like myContract.getPastEvents(event[, options][, callback]).
This should give you a broad overview of the key components of a smart contract! You can find more detail in the Solidity Documentation linked here.

Further

Now, you are ready to write your own contract. Please visit next section with Your first smart contract.