3 - Solidity basis
On this page
What is Solidity ?
- Solidity is an object-oriented, high-level language for implementing smart contracts. It is designed to target Ethereum Virtual Machine(EVM)
- It is a compiled language. The source code with a .sol extension can be compiled to hex-serialized binary using the
solc
compiler. - It uses an ABI to define how data structures and functions are accessed in machine code (it is specified as a JSON array of function descriptions and events)
- It is statically typed: bool, int256/uint256, fixed/ufixed, address, arrays, map
- It supports inheritance, libraries and complex user-defined types and scopes using keywords: struct, inheritance, modifier, private, public.
- It implements primitive functions like crypto or time and it uses a logging mechanism based on events
Example of Faucet.sol
// SPDX-License-Identifier: CC-BY-SA-4.0
// Version of Solidity compiler this program was written for
pragma solidity 0.6.4;
// Our first contract is a faucet!
contract Faucet {
// Accept any incoming amount
receive() external payable {}
// Give out ether to anyone who asks
function withdraw(uint withdraw_amount) public {
// Limit withdrawal amount
require(withdraw_amount <= 100000000000000000);
// Send the amount to the address that requested it
msg.sender.transfer(withdraw_amount);
}
}
Note that Solidity offers a compiler directive known as a version pragma that instructs the compiler that the program expects a specific compiler (and language) version. Letβs look at an example: pragma solidity ^0.6.0;
$ solc --optimize --bin Faucet.sol
======= Faucet.sol:Faucet =======
Binary:
608060405234801561001057600080fd5b5060cc8061001f6000396000f3fe6080604052600436106
01f5760003560e01c80632e1a7d4d14602a576025565b36602557005b600080fd5b34801560355760
0080fd5b50605060048036036020811015604a57600080fd5b50356052565b005b67016345785d8a0
000811115606657600080fd5b604051339082156108fc029083906000818181858888f19350505050
1580156092573d6000803e3d6000fd5b505056fea26469706673582212205cf23994b22f7ba19eee5
6c77b5fb127bceec1276b6f76ca71b5f95330ce598564736f6c63430006040033
$ solc --abi Faucet.sol
======= Faucet.sol:Faucet =======
Contract JSON ABI
[{"inputs":[{"internalType":"uint256","name":"withdraw_amount","type":"uint256"}], \
"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"}, \
{"stateMutability":"payable","type":"receive"}]
To learn Solidity, we recommend to start with this video guide π¬ [01:39:10].
You can also try some examples π and this interactive tutorial named Cryptozombies π.
And read the full documentation π and have a close look to the style guide π.
Conclusion
In this lesson, we learn the solidity basis to develop Smart Contract. Next lesson is an hands-on to apply those concepts.