Smart Contract and Solidity Basics
Smart contracts are the digital representation of physical contracts in the real world, but are written with code on a computer rather than ink on paper. They are a small computer program that lays out the terms of an agreement or deal, which is then stored and executed inside of a blockchain. If the conditions of the terms are met, the smart contract will automatically execute, and a transaction will be made. Smart contracts are the building blocks for dApps, which are like web apps, but are entirely open source and stored on a public blockchain. There are a variety of languages used to build smart contracts, but the leader in the DeFi space is Solidity.
Pragma: The first line of any Solidity document is reserved for a pragma directive, which states that the source code is written for Solidity version 0.4.0 or newer. A pragma directive is always local to a source file and if you import another file, the pragmas from that file will not automatically apply to the importing file.
Pragma solidity ^0.6.0
Contracts: Contracts in Solidity are similar to a Class in C++. It is a collection of code and data that resides at a specific address on the Ethereum blockchain.
A Contract has the following properties:
Constructors− A special function declared with constructor keyword which will be executed once per contract and is invoked when a contract is created.
State Variables− store the state of the contract.
Functions− modify the state variables to alter the state of a contract.
Functions and state variables of a Contract have various visibility quantifiers, allowing interactions between Contracts.
external− External functions are meant to be called by other Contracts. They cannot be used for internal call. To call external function within contract this.function_name() call is required. State variables cannot be marked as external.
public− Public functions/ Variables can be used both externally and internally. For public state variable, Solidity automatically creates a getter function.
internal− Internal functions/ Variables can only be used internally or by derived contracts.
private− Private functions/ Variables can only be used internally and not even by derived Contracts.
Solidity supports general value types:
Booleans: Return value as either true or false.
Integers: Solidity supports
int/uintfor both unsigned and signed integers of various sizes. Keywords such as
uint256are used to assign the number of bits assigned to the integer. By default, uint and int are aliases for
Address: There are two types of address used in solidity. Each type can carry up to a 20-byte value.
address: Holds a 20-byte value (size of an Ethereum address).
address payable: Same as
address, but with the additional members
The difference between
address payableis an address you can send Ether to, while a plain
addresscannot. Address variables also contain methods like
transfer, which returns the balance of an Ethereum address and sends Ether to that address in units denominated in Wei.
String Literals: String literals are written with either double or single quotes (
Mapping: Mapping is used as a data storage in the form of key-value pairs which can be any of the built-in data types, i.e arrays, enums, operators and hash values. These mappings are used to return values associated with a particular storage location.
Gas: Gas is a unit that measures the amount of computational effort a particular operation requires in order to execute. Every function, operation, or change of state within a smart contract costs Gas to perform on the Ethereum network.
· Executing a computation x + y might cost (10 gas) and assigning a variable uint x = 5 could cost (5 gas). Adding up every computation of your smart contract will calculate the gas cost for your smart contract to be executed. The exact gas needed for each computation is defined in the Ethereum white papers.
ABI: ABI (Application Binary Interface) is very similar to a web API. ABI defines the methods and structures used to interact with the binary contract, just like an API does but on a lower level.
After building your Solidity smart contract, you may want to build a nice user interface so your end user can interact with your dApp. The top UI libraries used for front end development are Web3.js and Ether.js. They provide specific methods that makes interacting with smart contracts easy. They perform actions like sending Ether from one account to another, reading and writing data from smart contracts, creating smart contracts, and so much more.
If building smart contracts has piqued your interest, there are a number of resources online to get any level of programmer started with Solidity. Some of the best resources I have found were either free or inexpensive.
Crypto Zombies is an excellent free interactive code school that teaches you the fundamentals of smart contract development by building fun games on Ethereum. The course was developed for beginners and walks you through step by step on how to develop interactive smart contracts.
I found YouTube to be another great free resource for learning more about Solidity. There are several helpful engineers who create “how to” videos pertaining to Smart Contract Development. I found Patrick Collins’ channel to be very informative. Also a member of the Chainlink team.
If you are interested in a more structured way of learning Solidity, there are various bootcamps available online. I recommend looking at some Udemy courses, as they provide multiple thorough and in-depth courses for a reasonable price.