My first smart contract
Exciting right? Let’s use Remix — a simple storage tool :)
After opening a storage we got to decide on the version of solidity — some might be more stable:
First steps:
// SPDX-License-Identifier: MIT — everyone can use it
pragma solidity 0.8.18; // if we use ^ before the number of the version it means it will work with this or a higher version. You can also choose versions in the range by using >= smaller or equal than, bigger than etc
Compiling code — changing the code from human to “machine” code
BITS VS BYTES MUDAFACKA
bit 1 or 0 — — — BYTE 8 bits
What are the types of variables in Solidity:
uint256
,int256
,bool
,string
,address
,bytes32
Bool is true of false bool = true;
uint256 = uint — the same
bytes32 =\ bytes — not the same
Next lesson
A function is a piece of code when called will execute a specific small piece of code
Function Store will store the new favouritenumber
If we add “public” before the uint it means that this data can be called externally
Remember your brackets
When you have variable that is only in some of the brackets {} you will not be able to get it outside of it.
Some important stuff —
there are functions that do not have to run or send the transaction for you to call them:
view — you are going to just view the state of the blockchain. It means you won’t be able to modify the state. The code below won’t complie
function retrieve() public view returns(uint256){
favoriteNumber = favoriteNumber+1
return favoriteNumber;
}
Pure — do not modify the state AND do not read the state of the block chain — just scope of the function.
You won’t spend any gas on view and pure so they are blue buttons.
Solidity Arrays and Structs
In arrays is 0 index so the first number is 0 (we start counting from 0)
Own structures we create with “struct”
Array -data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key
If we leave it without anything example Person [] its dynamic array . if you put something inside it means it's static array and can have only the provided number.
struct Person {
uint256 favoriteNumber;
string name;
}
Person [] public listOfPeople;
Adding person to the contract (its a second one) means it have number one on the listOfPeople. Why? It’s the 0 index as mentioned before.