Solidity Background and Context
Solidity is "an object-oriented, high-level language for implementing smart contracts…programs which govern the behaviour of accounts within the Ethereum state” (source). While a few smart contract programming languages have developed over the years, Solidity remains the most dominant one.
As we mentioned earlier, Solidity is a higher-order language meaning that the code deployed with a smart contract is not Solidity, but rather EVM bytecode. Solidity is an abstraction on top of that fundamental language so we don’t have to be writing code that looks like this:
PUSH1 0x80 PUSH1 0x40 MSTORE PUSH1 0xE PUSH1 0x0 SSTORE CALLVALUE DUP1
ISZERO PUSH1 0x14 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH1 0x35
DUP1 PUSH1 0x22 PUSH1 0x0 CODECOPY PUSH1 0x0 RETURN INVALID PUSH1 0x80
PUSH1 0x40 MSTORE PUSH1 0x0 DUP1 REVERT INVALID LOG1 PUSH6 0x627A7A723058 KECCAK256 RETURNDATASIZE 0x25 0xb6 0xcf CALLDATALOAD LOG1 DUP16 PUSH9
0x27AC943141CFD6D0FA MSTORE SHL DUP6 LOG4 PUSH8 0x7FADA153CC03D771
BLOCKHASH 0xb3 STOP 0x29
But, rather code that looks like this:
pragma solidity >=0.4.0 <0.9.0;
contract MyContract {
uint i = 10 + 2 * 2;
}
While both these blocks of code describe the same operation, the second version (Solidity) is much easier to read and write.
General Characteristics of Solidity
Solidity uses the ECMAScript syntax like JavaScript, to make it approachable for web developers. However, do not be lulled into complacency! Solidity is much more demanding in its requirements.
Some of Solidity’s basic characteristics:
- Case Sensitive
- Statement termination via a semicolon
;
- Files use the
.sol
extension - Statically typed Types need to be known at compile time. This makes Solidity more like TypeScript than JavaScript in practice.
- Various compilation options Solidity compiling can be done on the command line by Solc, you can compile it in a web-browser using Remix, or compile it as part of a smart contract development framework like Truffle or Hardhat.
- Compiles down to EVM bytecode Which then runs within the Ethereum Virtual Machine when deployed to a network
Overall, the Solidity workflow can be illustrated as follows:
In this section, we’re going to go over the basic conventions of Solidity. We’re also going to start building smart contracts and discussing their general structure and design patterns.
We can’t emphasize this enough: smart contract coding is both really exciting and really dangerous! We see smart contracts rekt
every day! We want to both give you all the tools to both build with creativity and confidence. Let’s go!
Additional Material
If you’d like some other places to learn Solidity, you can also check out these great resources. To be clear, we’ll definitely be teaching you Solidity along with how to develop dapps. We also want to provide any external help we can as well!
- Course: CryptoZombies One of the most well-known introductions to Solidity
- Course: Ethernaut (OpenZeppelin) An excellent in-browser “game” teaching Solidity from a security perspective.
- Course: Intro to Solidity (Chainshot) Chainshot uses a very cool interactive platform to teach Solidity
- Wiki: Solidity by Example A bunch of great examples of Solidity, including excellent design patterns, hacks and security tips.
- Wiki: Use Web3 Another good collection of learning resources
- Article: Test Driven Introduction to Solidity From an older pragma version of Solidity, but might be interesting to check out! Repo accompanying the article here.
- Thread: What Does Ethereum Development Look Like Today? Santiago Palladino, who wrote Ethereum for Web Developers, updates parts of his book for the current Ethereum ecosystem.
- Article: Learn X in Y Minutes (Solidity) A bit long and rough, but a comprehensive overview of learning Solidity