Solidity Fast Track: Learn Solidity Fast
'Learn X in Y minutes' this time with X = Solidity 0.7 and Y = 20
You might be familiar with the Learn X in Y minutes. For example you could learn JavaScript in 20 minutes at https://learnxinyminutes.com/docs/javascript/. Unfortunately there is no equivalent for Solidity, but this is about to change.
Do you have 20 minutes to learn all of the basics? We even include the required blockchain basics.

So we have no time to loose. Let's start.
What is Bitcoin? (Blockchain 101)
Bitcoin fundamentally is a digital currency, but there is one major issue with any digital currency. It can be copied an infinite amount of times by anyone. This is what Bitcoin solves.
Technically a 'Bitcoin' doesn't actually exist. It's not an actual coin someone owns on their hard drive. Rather it's a long list of transactions. The first transaction for a created Bitcoin comes from mining, the details here are not important now, just know that a miner is rewarded for doing hard work.
So to obtain Bitcoin, someone has to send it to me. This someone creates a transaction saying 'Send 1 BTC to Markus'. He publishes this transaction. Nodes in the Bitcoin network will eventually see it. They collect transactions and store them together in a block. Those blocks will be put together sequentially into a chain, also known as 'blockchain'. To prevent spamming the network, any transaction must also pay a fee.
In other words, a blockchain is just a list of strictly ordered transactions. To obtain the balance of an address, all we need to do is go through this long list until the end and keep track of all the incoming Bitcoins and all the outgoing Bitcoins. The current balance of an address equals the sum of all incoming minus the sum of all outgoing Bitcoins. Pretty simple right?

What's different in Ethereum? (Blockchain 102)
Now that we know how Bitcoin works, let's explore what's new in Ethereum. And that's the capability to create smart contracts. I like to think of smart contracts as essentially automated, verifiable addresses. In Bitcoin, every address is just a human sending Bitcoin from A to B, or a software acting on behalf of a human. Let's compare the difference between those two automations:
- Let's imagine I run a computer software that automatically trades Bitcoin. You could think of it as an automated account. It listens to external output (who is sending me how much money, what is the current Bitcoin price etc.) and reacts by sending Bitcoins. Who knows and can change the behavior of this software? Only me, it's running only on my personal computer.
- Now compare that to an Ethereum smart contract. Just like before it listens to external output and reacts by sending Ether (or other things). Who knows what the software is doing? Everyone. Who can change the behavior? Nobody (unless its designed to be changed). The software is running on every Ethereum node, and in many cases, for all eternity.
So that's a smart contract in a nutshell. An automated account that is immutable and verifiable, because it runs on every machine of the network.

Let's get to Solidity!
Now it's time to talk about Solidity. Those automated accounts (as mentioned about, let's just call them smart contracts from now on) can be programmed. If you're a super nerd, you can just write direct machine code operations for it, also known as OPCODES. But if you are a normal person, you will want to use a higher level abstraction.
Solidity is here to help you. Just like many other languages, given the Solidity source code a compiler is generating the machine code for the Ethereum Virtual Machine (EVM).
Smart Contracts 101
You can send a transaction to this address, just like you can to a normal address. One difference here is that you can send data along in your transaction which will define
- what function code on the contract to execute
- what are the input parameters to the function
Another difference is the concept of gas costs. They are essentially just the transaction fees, but in a normal transaction that just sends money from A to B, we know exactly how many resources are required by Ethereum nodes to process and verify the transaction (not a lot). But in a smart contract, you could do very heavy computations and store large amount of data. Every Ethereum node would have to compute and store this data. So to prevent spamming the network, we need a way to determine the resource impact of a transaction to a contract. Every OPCODE has a pre-defined gas cost.
In general you can know that there are four tiers of gas costs:
- Writing to Storage: Very expensive.
- Reading from Storage: Average.
- In-memory data and computations: Cheap.
- Deleting (freeing up) storage data: You get money back.
One very important thing for you to know is that there is a maximum amount of gas you can use in a single transaction. You can see here that it's changing over time and tends to go slowly up. But always know that there is a max. So you should never loop over arbitrary large arrays. This is bad design for contracts. Instead find a way to split the computation into smaller steps.
For example instead of sending money to all defined addresses, have a single function that sends the money to only who just called the contract. Then everyone who wants their money, needs to call the function. This is called the withdrawal pattern. (If the last part was a bit over your head still, don't worry. Read the rest and come back here afterwards.)
1. Let's talk about Functions
Let's look at our first example. This would be a pretty stupid smart contract to actually have, but it makes a great example. Anyone could send Ether to this contract and it would automatically send only half the Ether back. A money eating machine. Don't actually use this.
We could deploy this contract, meaning we put it on the blockchain and it will get an address for everyone to send transactions to. Once it's deployed, nobody can stop it, it will run forever, always with the same pre-defined behavior.
Let's break down each line.
// SPDX-License-Identifier: MIT
pragma solidity 0.7.5;
contract FastTrack {
function payMeBackHalf() external payable {
uint256 halfAmount = msg.value / 2;
(bool success, ) = msg.sender.call{value: halfAmount}("");
require(success, "return transaction failed");
}
}
// SPDX-License-Identifier: MIT
: Just a software license identifier that's required since 0.6.8. Check out available identifiers here.pragma solidity 0.7.5
: Specify which version of the Solidity compiler the contract is written for.contract FastTrack
: Specify the name of your smart contract.function payMeBackHalf() external payable
: A function we define that other people can use when interacting with out contract. Here we specify the name, visibility and declare it as payable. The visibility isexternal
, meaning the function can only be called from outside of this contract. The opposite would beprivate
, meaning the function is just a helper function we use in our contract to make it more readable. Declaring the function aspayable
means someone calling this function is allowed to send Ether along with the call. If it's not declared as payable, people couldn't send any Ether at all when calling this particular function.uint256 halfAmount = msg.value / 2
: This line is simple. Themsg.value
is a pre-defined variable and just holds the amount of Ether that is being sent to this contract. Now we set an unsigned integer with 256 bits to half of this value. Why unsigned? Because the value must be positive. Why 256 bits? The EVM operates on 32 bytes == 256 bits registers. You could use smaller types (like uint32), but this makes only sense in very specific situations. Don't worry about it now and just use 32 bytes types.(bool success, ) = msg.sender.call{value: halfAmount}("")
: Okay this one might look a little bit complicated, but it's not. We also could have used the previous way to write it which wasmsg.transfer(halfAmount)
. Same effect, but its usage is discouraged and the call way is also great for explaining more since the.transfer
just does a lot under the hood. Themsg.sender.call
is creating another transaction. Remember a contract is just an automated address, so of course it can create transactions itself. In this case we send it tomsg.sender
, another pre-defined variable which is the current person (or contract) sending the transaction to this contract. The.call
returns two valuesbool success
andbytes result
. Bool is just a value that is either true or false and bytes is just any undefined data (0's and 1's). However we do not care about the result data and just ignore it. We also send only an empty string via.call("")
, meaning we don't send any data with our call, but we setvalue
tohalfAmount
.. This is effectively just a transfer of Ether transaction.require(success, "return transaction failed")
: Now thisrequire
you will see a lot of times in contracts. It's a short form forif (!success) revert("return transaction failed")
. And it reverts a transaction with the given error message. If a transaction is reverted, it's considered invalid. So anytime this would happen in your smart contract, the transaction is just ignored by Ethereum nodes and it will never be integrated into the blockchain.

2. Let's talk about Storage
In the above example we have only used data that was sent to in the transaction itself, but that alone would be pretty useless. We want to be able to store and read data inside the contract to enable more advanced use cases.
Let's build an escrow contract that enables a buyer and a seller that don't trust each other to do a trade online. They choose a trusted third-party arbitrator in case of a conflict. The contract will introduce the concept of
- Storage
- Modifiers
- Two more data types: address and mappings
- Constructors
Storage variables are defined at the top of the contract. You define the data type, the visibility and the name.
- Type:
address
- Visibility:
public
- Name:
seller
Types: We've already introduced the most important types, namely bool, integers and bytes. Each of those can be either a single value or a list of values (arrays). The two new types we added are:
- address: This type represents an Ethereum address. This could be a human-controlled account address, also known as externally-owned account (EOA), or a smart contract address. There are a few other types, but don't worry about those for now.
- mapping: This is just a key-value storage. You define the types for the key and the value, in our case address and bool. Now we can read and write to this variable:
- read:
myMapping[myAddressKey]
- write:
myMapping[myAddressKey] = myBool
Visibility: In storage you can choose between private, internal or public. Internal has to do with inheritance which is out of the scope of this introduction, so just think about private vs. public. Most of the times you probably want a public variable. This is just a convenience for people interacting with the contract to easily read the value. A private value does not mean the data is secret. A clever person could still find out the value.
Constructor: In our first contract we just deployed it without any additional data. But sometimes you might want to define some parameters when creating the contract. In our escrow we want to set the addresses for our three entities, seller, buyer and arbitrator.
Modifiers: We introduce the concept of modifiers here. You don't have to use them, but they can make the code more readable. You can define a bunch of requirements for a function call in them via the require statement. The '_;'
indicates that here the function code will be executed here.
How does our Escrow Contract work?
Look at the example on the right. You should have all the knowledge to understand the code now. But take your time. Can you see what is happening?
When doing an online trade, the seller can discuss with the buyer which arbitrator to choose. Once decided, someone can deploy this contract. Now the buyer can call depositIntoEscrow
and transfer the purchase price amount into the contract. Then the money is locked in our contract.
In the normal case the seller would now ship the product, the buyer receives it and calls confirmPurchase
. Then the seller can also call confirmPurchase
and the money will be sent to him.
In the case of a refund, the buyer would send the product back to the seller. Now the seller would call confirmRefund
. Next the buyer can also call confirmRefund
and the money will be sent back to him.
In the case of a dispute, a buyer might claim to never have received a product. So the buyer calls confirmRefund
, while the seller would call confirmPurchase
. Now nobody gets any money, but the arbitrator can be called. The arbitrator can investigate the case and then decide if to call confirmPurchase
or confirmRefund
.
Job for you: Try to improve on this for the dispute case by allowing the arbitrator to split the remaining funds in a ratio that he defines. So for example the arbitrator could decide, send 30% to the buyer and 70% to the seller.
contract Escrow {
address public seller;
address public buyer;
address public arbitrator;
uint256 public purchasePrice;
uint256 public purchaseConfirmationCount;
uint256 public refundConfirmationCount;
mapping(address => bool) public hasConfirmedPurchase;
mapping(address => bool) public hasConfirmedRefund;
constructor(
address seller_,
address buyer_,
address arbitrator_
) {
seller = seller_;
buyer = buyer_;
arbitrator = arbitrator_;
}
modifier isRegisteredParticipant {
require(
msg.sender == seller ||
msg.sender == buyer ||
msg.sender == arbitrator,
"Only registered participants can call this"
);
_;
}
function depositIntoEscrow() external payable {
require(purchasePrice == 0, "Already deposited");
require(
msg.sender == buyer,
"Only buyer should deposit into escrow"
);
purchasePrice = msg.value;
}
function confirmPurchase() external isRegisteredParticipant {
require(
!hasConfirmedPurchase[msg.sender],
"Already confirmed purchase"
);
hasConfirmedPurchase[msg.sender] = true;
purchaseConfirmationCount = purchaseConfirmationCount + 1;
if (purchaseConfirmationCount >= 2) {
_sendFundsTo(seller);
}
}
function confirmRefund() external isRegisteredParticipant {
require(
!hasConfirmedRefund[msg.sender],
"Already confirmed refund"
);
hasConfirmedRefund[msg.sender] = true;
refundConfirmationCount = refundConfirmationCount + 1;
if (refundConfirmationCount >= 2) {
_sendFundsTo(buyer);
}
}
function _sendFundsTo(address recipient) private {
(bool success, ) = recipient.call{value: purchasePrice}("");
require(success, "Sending funds transaction failed");
}
}

Congratulations. That's it.
- Events: A special type of storage that cannot be accessed by contracts themselves.
- Structs: Custom types you can define that are a collection of sub-types.
- ERC-20: A very popular contract standard. See here for how you can implement one easily.
- Libraries: Special contracts that have no storage, but can be used only for implementation code.
- Inheritance: Solidity supports inheritance including polymorphism. A great way to structure your contracts.
And last but not least, check out my overview about Solidity development and all the relevant tools and infrastructure here.
Solidity Developer
More great blog posts from Markus Waas
How to use ChatGPT with Solidity
Using the Solidity Scholar and other GPT tips
How to integrate Uniswap 4 and create custom hooks
Let's dive into Uniswap v4's new features and integration
How to integrate Wormhole in your smart contracts
Entering a New Era of Blockchain Interoperability
Solidity Deep Dive: New Opcode 'Prevrandao'
All you need to know about the latest opcode addition
How Ethereum scales with Arbitrum Nitro and how to use it
A blockchain on a blockchain deep dive
The Ultimate Merkle Tree Guide in Solidity
Everything you need to know about Merkle trees and their future
The New Decentralized The Graph Network
What are the new features and how to use it
zkSync Guide - The future of Ethereum scaling
How the zero-knowledge tech works and how to use it
Exploring the Openzeppelin CrossChain Functionality
What is the new CrossChain support and how can you use it.
Deploying Solidity Contracts in Hedera
What is Hedera and how can you use it.
Writing ERC-20 Tests in Solidity with Foundry
Blazing fast tests, no more BigNumber.js, only Solidity
ERC-4626: Extending ERC-20 for Interest Management
How the newly finalized standard works and can help you with Defi
Advancing the NFT standard: ERC721-Permit
And how to avoid the two step approve + transferFrom with ERC721-Permit (EIP-4494)
Moonbeam: The EVM of Polkadot
Deploying and onboarding users to Moonbeam or Moonriver
Advanced MultiSwap: How to better arbitrage with Solidity
Making multiple swaps across different decentralized exchanges in a single transaction
Deploying Solidity Smart Contracts to Solana
What is Solana and how can you deploy Solidity smart contracts to it?
Smock 2: The powerful mocking tool for Hardhat
Features of smock v2 and how to use them with examples
How to deploy on Evmos: The first EVM chain on Cosmos
Deploying and onboarding users to Evmos
EIP-2535: A standard for organizing and upgrading a modular smart contract system.
Multi-Facet Proxies for full control over your upgrades
MultiSwap: How to arbitrage with Solidity
Making multiple swaps across different decentralized exchanges in a single transaction
The latest tech for scaling your contracts: Optimism
How the blockchain on a blockchain works and how to use it
Ultimate Performance: The Aurora Layer2 Network
Deploying and onboarding users to the Aurora Network powered by NEAR Protocol
What is ecrecover in Solidity?
A dive into the waters of signatures for smart contracts
How to use Binance Smart Chain in your Dapp
Deploying and onboarding users to the Binance Smart Chain (BSC)
Using the new Uniswap v3 in your contracts
What's new in Uniswap v3 and how to integrate Uniswap v3
What's coming in the London Hardfork?
Looking at all the details of the upcoming fork
Welcome to the Matrix of blockchain
How to get alerted *before* getting hacked and prevent it
The Ultimate Ethereum Mainnet Deployment Guide
All you need to know to deploy to the Ethereum mainnet
SushiSwap Explained!
Looking at the implementation details of SushiSwap
Solidity Fast Track 2: Continue Learning Solidity Fast
Continuing to learn Solidity fast with the advanced basics
What's coming in the Berlin Hardfork?
Looking at all the details of the upcoming fork
Using 1inch ChiGas tokens to reduce transaction costs
What are gas tokens and example usage for Uniswap v2
Openzeppelin Contracts v4 in Review
Taking a look at the new Openzeppelin v4 Release
EIP-3156: Creating a standard for Flash Loans
A new standard for flash loans unifying the interface + wrappers for existing ecosystems
Tornado.cash: A story of anonymity and zk-SNARKs
What is Tornado.cash, how to use it and the future
High Stakes Roulette on Ethereum
Learn by Example: Building a secure High Stakes Roulette
How to implement generalized meta transactions
We'll explore a powerful design for meta transactions based on 0x
Utilizing Bitmaps to dramatically save on Gas
A simple pattern which can save you a lot of money
Using the new Uniswap v2 as oracle in your contracts
How does the Uniswap v2 oracle function and how to integrate with it
Smock: The powerful mocking tool for Hardhat
Features of smock and how to use them with examples
How to build and use ERC-721 tokens in 2021
An intro for devs to the uniquely identifying token standard and its future
Trustless token management with Set Protocol
How to integrate token sets in your contracts
Exploring the new Solidity 0.8 Release
And how to upgrade your contracts to Solidity 0.8
How to build and use ERC-1155 tokens
An intro to the new standard for having many tokens in one
Leveraging the power of Bitcoins with RSK
Learn how RSK works and how to deploy your smart contracts to it
Sourcify: The future of a Decentralized Etherscan
Learn how to use the new Sourcify infrastructure today
Integrating the 0x API into your contracts
How to automatically get the best prices via 0x
How to build and use ERC-777 tokens
An intro to the new upgraded standard for ERC-20 tokens
COMP Governance Explained
How Compound's Decentralized Governance is working under the hood
How to prevent stuck tokens in contracts
And other use cases for the popular EIP-165
Understanding the World of Automated Smart Contract Analyzers
What are the best tools today and how can you use them?
A Long Way To Go: On Gasless Tokens and ERC20-Permit
And how to avoid the two step approve + transferFrom with ERC20-Permit (EIP-2612)!
Smart Contract Testing with Waffle 3
What are the features of Waffle and how to use them.
How to use xDai in your Dapp
Deploying and onboarding users to xDai to avoid the high gas costs
Stack Too Deep
Three words of horror
Integrating the new Chainlink contracts
How to use the new price feeder oracles
TheGraph: Fixing the Web3 data querying
Why we need TheGraph and how to use it
Adding Typescript to Truffle and Buidler
How to use TypeChain to utilize the powers of Typescript in your project
Integrating Balancer in your contracts
What is Balancer and how to use it
Navigating the pitfalls of securely interacting with ERC20 tokens
Figuring out how to securely interact might be harder than you think
Why you should automatically generate interests from user funds
How to integrate Aave and similar systems in your contracts
How to use Polygon (Matic) in your Dapp
Deploying and onboarding users to Polygon to avoid the high gas costs
Migrating from Truffle to Buidler
And why you should probably keep both.
Contract factories and clones
How to deploy contracts within contracts as easily and gas-efficient as possible
How to use IPFS in your Dapp?
Using the interplanetary file system in your frontend and contracts
Downsizing contracts to fight the contract size limit
What can you do to prevent your contracts from getting too large?
Using EXTCODEHASH to secure your systems
How to safely integrate anyone's smart contract
Using the new Uniswap v2 in your contracts
What's new in Uniswap v2 and how to integrate Uniswap v2
Solidity and Truffle Continuous Integration Setup
How to setup Travis or Circle CI for Truffle testing along with useful plugins.
Upcoming Devcon 2021 and other events
The Ethereum Foundation just announced the next Devcon in 2021 in Colombia
The Year of the 20: Creating an ERC20 in 2020
How to use the latest and best tools to create an ERC-20 token contract
How to get a Solidity developer job?
There are many ways to get a Solidity job and it might be easier than you think!
Design Pattern Solidity: Mock contracts for testing
Why you should make fun of your contracts
Kickstart your Dapp frontend development with create-eth-app
An overview on how to use the app and its features
The big picture of Solidity and Blockchain development in 2020
Overview of the most important technologies, services and tools that you need to know
Design Pattern Solidity: Free up unused storage
Why you should clean up after yourself
How to setup Solidity Developer Environment on Windows
What you need to know about developing on Windows
Avoiding out of gas for Truffle tests
How you do not have to worry about gas in tests anymore
Design Pattern Solidity: Stages
How you can design stages in your contract
Web3 1.2.5: Revert reason strings
How to use the new feature
Gaining back control of the internet
How Ocelot is decentralizing cloud computing
Devcon 5 - Review
Impressions from the conference
Devcon 5 - Information, Events, Links, Telegram
What you need to know
Design Pattern Solidity: Off-chain beats on-chain
Why you should do as much as possible off-chain
Design Pattern Solidity: Initialize Contract after Deployment
How to use the Initializable pattern
Consensys Blockchain Jobs Report
What the current blockchain job market looks like
Provable — Randomness Oracle
How the Oraclize random number generator works
Solidity Design Patterns: Multiply before Dividing
Why the correct order matters!
Devcon 5 Applications closing in one week
Devcon 5 Applications closing
Randomness and the Blockchain
How to achieve secure randomness for Solidity smart contracts?