Smart Contracts Anatomy
An in-depth look into the anatomy of a smart contact – the functions, data, and variables.
A smart contract is a program that runs at an address on MAPO-Relay-Chain. They're made up of data and functions that can execute upon receiving a transaction. Here's an overview of what makes up a smart contract.
The following MAPO-Relay-Chain is collectively referred to as MAPO.
prerequisites
Make sure you've read about smart contracts first. This document assumes you're already familiar with programming languages such as JavaScript or Python.
data
Any contract data must be assigned to a location: either to storage or memory. It's costly to modify storage in a smart contract so you need to consider where your data should live.
storage
Persistent data is referred to as storage and is represented by state variables. These values get stored permanently on the blockchain. You need to declare the type so that the contract can keep track of how much storage on the blockchain it needs when it compiles.
// Solidity example
contract SimpleStorage {
uint storedData; // State variable
// ...
}If you've already programmed object-oriented languages, you'll likely be familiar with most types. However address should be new to you if you're new to Ethereum development.
An address type can hold an MAPO address which equates to 20 bytes or 160 bits. It returns in hexadecimal notation with a leading 0x.
Other types include:
boolean
integer
fixed point numbers
fixed-size byte arrays
dynamically-sized byte arrays
Rational and integer literals
String literals
Hexadecimal literals
Enums
For more explanation, take a look at the docs:
memory
Values that are only stored for the lifetime of a contract function's execution are called memory variables. Since these are not stored permanently on the blockchain, they are much cheaper to use.
Learn more about how the EVM stores data (Storage, Memory, and the Stack) in the Solidity docs.
environment-variables
In addition to the variables you define on your contract, there are some special global variables. They are primarily used to provide information about the blockchain or current transaction.
Examples:
Prop
State variable
Description
block.timestamp
uint256
Current block epoch timestamp
msg.sender
address
Sender of the message (current call)
functions
In the most simplistic terms, functions can get information or set information in response to incoming transactions.
There are two types of function calls:
internal– these don't create an EVM callInternal functions and state variables can only be accessed internally (i.e. from within the current contract or contracts deriving from it)
external– these do create an EVM callExternal functions are part of the contract interface, which means they can be called from other contracts and via transactions. An external function
fcannot be called internally (i.e.f()does not work, butthis.f()works).
They can also be public or private
publicfunctions can be called internally from within the contract or externally via messagesprivatefunctions are only visible for the contract they are defined in and not in derived contracts
Both functions and state variables can be made public or private
Here's a function for updating a state variable on a contract:
The parameter
valueof typestringis passed into the function:update_nameIt's declared
public, meaning anyone can access itIt's not declared
view, so it can modify the contract state
view-functions
These functions promise not to modify the state of the contract's data. Common examples are "getter" functions – you might use this to receive a user's balance for example.
What is considered modifying state:
Writing to state variables.
Using
selfdestruct.Sending ether via calls.
Calling any function not marked
vieworpure.Using low-level calls.
Using inline assembly that contains certain opcodes.
constructor-functions
constructor functions are only executed once when the contract is first deployed. Like constructor in many class-based programming languages, these functions often initialize state variables to their specified values.
built-in-functions
In addition to the variables and functions you define on your contract, there are some special built-in functions. The most obvious example is:
address.send()– Solidity
These allow contracts to send MAPO to other accounts.
writing-functions
Your function needs:
parameter variable and type (if it accepts parameters)
declaration of internal/external
declaration of pure/view/payable
returns type (if it returns a value)
A complete contract might look something like this. Here the constructor function provides an initial value for the dapp_name variable.
events-and-logs
Events let you communicate with your smart contract from your frontend or other subscribing applications. When a transaction is mined, smart contracts can emit events and write logs to the blockchain that the frontend can then process.
annotated-examples
These are some examples written in Solidity. If you'd like to play with the code, you can interact with them in Remix.
Hello world
Token
Unique digital asset
further-reading
Check out Solidity's documentation for a more complete overview of smart contracts:
related-topics
Last updated