Welcome to the repository for the Ophir Solidity-Smartcontract-Class - Beginner to Expert Full Course | by Ophir!
Recommended Testnet: Sepolia
We have to create the repos to work with Sepolia due to Rinkeby and Kovan being sunset, and Goerli being a disaster. Let us know if yo have any issues!
Main Faucet: https://faucets.chain.link Backup Faucet: https://sepoliafaucet.com/
⚠️ All code associated with this course is for demo purposes only. They have not been audited and should not be considered production ready. Please use at your own risk.
- Chat GPT
- Ask questions and chat about the course here!
- Stack Exchange Ethereum
- Great place for asking technical questions about Ethereum
- StackOverflow
- Great place for asking technical questions overall
Introduction to Solidity
- 8yrs Ago, precisely, August 2014, @gavofyork submitted a proposal to develop a language that would transform the #Ethereum ecosystem, implementing amazing “programs” that will be so effective in “blockchain interactions”.
These “programs” are “smart contracts” They allow humans to interact with the blockchain (more on this later)
-
Other developers such as @ethchris, @alexberegszaszi and several other developers with @ethchris as team lead helped develop this language and called it solidity.
-
Solidity is an object Oriented programming language influenced by c++ , JavaScript and Python.
It implements Smart-contracts on various blockchain platforms especially #Ethereum.
-
Object-Oriented languages are languages that have foundation in the use of objects.
-
Objects are the basic units of the language
-
They are used to form classes which are in turn used to produce applications.
-
A brief dive into solidity Syntax
In computer programming, syntax refers to the set of rules that govern the structure and arrangement of code in a particular programming language. It specifies how statements, expressions, and other programming constructs should be written in order to be considered valid and meaningful by the computer.
In solidity the syntax simply refers to the way solidity codes are written.
Solidity is similar to JavaScript and C++
It is not possible to list all of the Solidity syntax here, as it is a comprehensive programming language with many different features and constructs. However, here is an overview of some of the main syntax elements used in Solidity:
Solidity supports both:
-
single-line comments
(denoted by "//")
-
multi-line comments
(denoted by "/* ... */").
Solidity supports several data types for variables, including:
(represents true or false values.)
pragma solidity ^0.8.0;
contract ExampleContract {
bool myBool = true;
function getBool() public view returns (bool) {
return myBool;
}
}
In this example, we define a boolean variable "myBool" and initialize it to true. The "getBool" function returns the value of "myBool".
(represents whole numbers, either signed or unsigned.)
pragma solidity ^0.8.0;
contract ExampleContract {
int256 myInt = -123;
uint256 myUint = 456;
function getInt() public view returns (int256) {
return myInt;
}
function getUint() public view returns (uint256) {
return myUint;
}
}
In this example, we define an integer variable "myInt" and initialize it to -123, and an unsigned integer variable "myUint" and initialize it to 456. The "getInt" function returns the value of "myInt", and the "getUint" function returns the value of "myUint".
(represents decimal numbers with a fixed number of digits.)
pragma solidity ^0.8.0;
1contract ExampleContract {
fixed256x18 myFixed = 123.456;
function getFixed() public view returns (fixed256x18) {
return myFixed;
}
}
In this example, we define a fixed point number variable "myFixed" with 18 decimal places and initialize it to 123.456. The "getFixed" function returns the value of "myFixed".
(represents an Ethereum address.)
pragma solidity ^0.8.0;
contract ExampleContract {
address payable myAddress = payable(0x1234567890123456789012345678901234567890);
function getAddress() public view returns (address payable) {
return myAddress;
}
}
In this example, we define an address variable "myAddress" and initialize it to the Ethereum address "0x1234567890123456789012345678901234567890". The "getAddress" function returns the value of "myAddress".
(represents a single byte of data.)
pragma solidity ^0.8.0;
contract ExampleContract {
bytes32 myBytes = hex"00112233445566778899aabbccddeeff";
function getBytes() public view returns (bytes32) {
return myBytes;
}
}
In this example, we define a bytes variable "myBytes" and initialize it to the hex value "00112233445566778899aabbccddeeff". The "getBytes" function returns the value of "myBytes".
(represents a set of named constants.)
pragma solidity ^0.8.0;
contract ExampleContract {
enum State { New, InProgress, Completed }
State myState = State.New;
function getState() public view returns (State) {
return myState;
}
}
In this example, we define an enum "State" with three possible values: "New", "InProgress", and "Completed". We also define an enum variable "myState" and initialize it to "New". The "getState" function returns the value of "myState".
Variables can be declared using the syntax "type name = value;".
(Solidity supports both fixed-size and dynamic arrays, which can be of any data type.)
pragma solidity ^0.8.0;
contract ExampleContract {
uint256[] myArray;
function addValue(uint256 newValue) public {
myArray.push(newValue);
}
function getValue(uint256 index) public view returns (uint256) {
return myArray[index];
}
}
In this example, we define an empty dynamic array of uint256 values called "myArray". The "addValue" function allows values to be added to the array, and the "getValue" function allows values to be retrieved from the array using an index value
(Structs are used to group related data together. They can contain multiple data types, including other structs or arrays.)
pragma solidity ^0.8.0;
contract ExampleContract {
struct Person {
string name;
uint256 age;
}
Person myPerson;
function setPerson(string memory newName, uint256 newAge) public {
myPerson = Person(newName, newAge);
}
function getPerson() public view returns (string memory, uint256) {
return (myPerson.name, myPerson.age);
}
}
In this example, we define a struct "Person" with two fields: "name" and "age". We then define a variable "myPerson" of type "Person". The "setPerson" function allows a new Person object to be created and assigned to "myPerson", and the "getPerson" function returns the values of "myPerson"'s "name" and "age" fields.
(Mappings are used to create key-value pairs, similar to dictionaries or hash tables in other languages. They are particularly useful for creating data lookups.)
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleMapping { // Declare a mapping that maps an address to a uint mapping(address => uint) public balances;
// Function to update the balance of a specific address
function updateBalance(address _user, uint _amount) public {
balances[_user] = _amount;
}
// Function to get the balance of a specific address
function getBalance(address _user) public view returns (uint) {
return balances[_user];
}
}
In the example above:
We declare a mapping called balances
that maps an address
to a uint
(unsigned integer). This mapping will store the balances of users identified by their Ethereum addresses.
We create a function updateBalance
that takes an address _user
and a uint _amount
as input. This function updates the balance of the specified address in the balances
mapping.
We create a function getBalance that
takes an address _user
as input and returns the balance associated with that address from the balances mapping.
Keep in mind that mappings in Solidity don't have a length or size property, and you can't iterate over their keys directly. If you need to track the keys used in a mapping or the number of entries, you can use an array or another data structure in combination with the mapping.
(represents a variable-length string of characters.)
pragma solidity ^0.8.0;
contract ExampleContract {
string myString;
function setString(string memory newValue) public {
myString = newValue;
}
function getString() public view returns (string memory) {
return myString;
}
}
In this example, we define a string variable called "myString". The "setString" function allows a new string value to be assigned to "myString", and the "getString" function returns the value of "myString".
Note: There are two types of Data Types:
Value Data types and Reference data types.
To group related data together in a structured and organized way, we use Data Structures:
Examples of data structures in Solidity include arrays, structs, mappings, and enums. Data structures can contain both value data types and reference data types, and are typically used to create more complex data models for smart contracts.
Examples of an Array containing value and reference data type:
pragma solidity ^0.8.0;
contract ExampleContract {
uint256[] myUintArray;
string[] myStringArray;
function addValue(uint256 newValue, string memory newString) public {
myUintArray.push(newValue);
myStringArray.push(newString);
}
function getValue(uint256 index) public view returns (uint256, string memory) {
return (myUintArray[index], myStringArray[index]);
}
}
In this example, we define two dynamic arrays: "myUintArray" of type uint256, and "myStringArray" of type string. The "addValue" function allows new values to be added to the arrays, with one uint256 value and one string value added at a time. The "getValue" function allows the values of both arrays at a given index to be retrieved together, with a tuple containing one uint256 value and one string value returned. Here, "myUintArray" is an example of an array containing a value data type (uint256), while "myStringArray" is an example of an array containing a reference data type (string).
Other Types of syntax includes:
Inheritance, Events, Functions, modifiers, contracts and control structures.
More sources to sharpen your skills 👇
Ethereum is a decentralized, open-source blockchain with smart contract functionality. Ether is the native cryptocurrency of the platform.
Among cryptocurrencies, ether is second only to bitcoin in market capitalization. Ethereum was conceived in 2013 by programmer Vitalik Buterin.
Smart contracts are self-executing digital contracts that run on a blockchain platform, and are programmed to automatically execute the terms of the contract when certain conditions are met.
Smart contracts are written in programming languages, and Solidity is one of the most popular languages used for writing smart contracts on the Ethereum blockchain.
Smart contracts are used in Solidity to automate the exchange of assets between parties without the need for a middleman or trusted third party.
This makes them ideal for applications such as crowdfunding, escrow services, and other types of financial transactions where trust is important.
An escrow contract is a type of smart contract that holds funds in escrow until certain conditions are met.
For example, in a real estate transaction, an escrow contract could be used to hold the purchase price of the property until the buyer has completed all necessary inspections and due diligence, at which point the funds would be released to the seller.
In Solidity, an escrow contract could be written using a combination of state variables, functions, and events to define the terms of the contract, as well as a mapping to track the funds held in escrow
A crowdfunding contract is a type of smart contract that allows multiple parties to pool their resources to support a common goal or project.
For example, a crowdfunding contract could be used to raise funds for a new business venture, with investors receiving a share of the profits if the venture is successful.
In Solidity, a crowdfunding contract could be written using a combination of state variables, functions, and events to define the terms of the contract, as well as a mapping to track the contributions made by each investor for example the fund me smart contract below 👇
⌨️ (04:21:94) : Welcome to Remix! Ophir Bank
A blockchain is a distributed ledger technology.
The Word “Ledger”means A book or other collections of financial transactions.
The Word “Distributed” denotes Location.
So it basically means that these ledgers are stored in different locations & these locations are Reffered to as Nodes.
While ledger is continuously updated and validated through consensus algorithms.
Centralized blockchains Decentralized blockchains Hybrid blockchains Consortium blockchains
These are blockchains that have a central authority controls the whole process(read, write and validate transactions (
Transactions are malleable ( it can be deleted), they are known as permissioned blockchains.
Used in banks for private and restricted purposes.
Used in the government for keeping private data like tax records
Decentralized blockchains are a type of distributed ledger technology (DLT) that allows multiple parties to maintain a shared, tamper-proof database without the need or a central authority. Instead, the blockchain uses a network of computers, known as nodes, to verify and record transactions.
It is immutable, Transparent and secure.
Applications vary widely in crypto currency transactions, supply chain management, voting systems and more
Bitcoin facilities peer to peer transactions
Examples of decentralized blockchains in crypto: Ethereum and litecoin
This is the combination of both public and centralized blockchains.
In this case an aspect of the blockchain is for the public to interact while the other is restricted or permissioned.
Examples are Ripple and hyper ledger.
They Offer a balance between the transparency and security of public blockchains and control and privacy of private blockchains.
Termed as a form of “Decentralized Control”
By groups of organizations they work together to validate transactions.
It allows for privacy and still operates in a decentralized fashion it is mostly used in the supply chain industry.
Examples include:
R3 Corda Platform Enterprise Ethereum Alliance
In this section you will learn how to define a token contract Add functions for transferring and tracking token balance Implementing the ERC-20 standard for interoperability Deploy your token contractHere's a step-by-step process for creating, testing, and deploying an ERC-20 token contract using Remix and OpenZeppelin:
-
Open Remix IDE: • Visit the Remix IDE at
-
Create the token contract: • In the "File Explorer" tab, click the "+" icon to create a new file • Name the file "the token of `your choice"
4 Click on the GO TO DOCS
tab on the CONTRACTS section
5 Click on Wizard
6 On ERC20 TAB Set Name
& Symbol
7 Tick the Mintable
section to set token to mintable
These are other versions of creating an ERC20 to click the link and learn more
đź’» Code Set supply with constructor manually before deploying the token
đź’» Code Set supply in the contract
đź’» Code Set supply after deploying the contract
In this section you will learn how to Define an NFT contract Add unique identifier, metadata, and ownership information Adding functions for transferring and tracking NFT ownership Implementing the ERC-721 standard for interoperability and deploying your NFT contractHere's a step-by-step process for creating, testing, and deploying an ERC-20 token contract using Remix and OpenZeppelin:
-
Open Remix IDE: • Visit the Remix IDE at
-
Create the token contract: • In the "File Explorer" tab, click the "+" icon to create a new file • Name the file "the token of `your choice"
-
Click on the
GO TO DOCS
tab on the CONTRACTS section -
Click on
Wizard
-
On ERC721 TAB Set
Name
&Symbol
-
Tick the
Mintable
section to set token to mintable -
Tick the
Auto Increment Ids
section to set token to mintable -
Tick the
Enumerable
section to set token to Enumerable -
Tick the
URI Storage
section to set token to URI Storage
Check out this repository for more information
- Goto Pinata Cloud
- Sign Up
- Click on add File
- Drag and Drop File to the section or sellect a file and choose file from your device
- You will see a string like this:
QmPehA9qAXn5yKZ8Cg2gWVcEWbmLmjaoXXEfTbGLPE4MPw
it is called "Content Identifier" - Copy and Paste it in front of the IPFS gateway: https://ipfs.io/ipfs/ Like this: https://ipfs.io/ipfs/QmPehA9qAXn5yKZ8Cg2gWVcEWbmLmjaoXXEfTbGLPE4MPw
- Copy and paste the IPFS gateway and CID to the URI section inside the safemint function and input the address to mint