Smart Contract and Solidity Basics

Smart Contracts

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.

Solidity

Solidity is the industry leading programming language for smart contract development and is heavily influenced by C++, Python, and JavaScript languages. At its core, Solidity is a statically typed object-oriented language designed to run on the Ethereum Virtual Machine (EVM). The language was designed to be easy to learn for programmers that are familiar with one of the more modern programming languages.

Solidity Basics

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 supportsint/uintfor both unsigned and signed integers of various sizes. Keywords such as uint8 and uint256 are used to assign the number of bits assigned to the integer. By default, uint and int are aliases for uint256 and int256, respectively.
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 transfer and send.
The difference betweenaddress payable andaddress is address payableis an address you can send Ether to, while a plainaddress cannot. Address variables also contain methods likebalance and 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 ("foo" or 'bar').
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.

Compilation and Testing of Smart Contracts

Usually, contracts cannot be changed or altered once deployed onto the blockchain. Because of that, we want to make sure to thoroughly test our contracts for bugs and vulnerabilities. The Hardhat development framework is one of the best choices for Solidity debugging. Their simplicity for compiling, testing, and deploying contracts to either your local network, Test net, or the main Ethereum network makes it popular to use. The framework is extremely flexible, with customizable plugins to make your environment easy to use, and utilizes two of the main JavaScript APIs, Ether.js and Web3.js.
If you want to learn more about Hardhat, they have great documentation on their website.
Quick link to a Hardhat tutorial

Front End and Web3 Development

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.

Where to learn Smart Contract Development

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.
The official Solidity Documentation on smart contacts is very well written and will provide all the information needed to get started with smart contract development. solidity-by-example.org has simple examples of Solidity development and is a great resource.
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.