Personal Notes-Basics of Solidity-1.0.0
//SPDX-License-Identifier: MIT
pragma solidity 0.8.7; //pragma mentions the version of solidity this contract is using.
contract SimpleStorage {
//contract is a keyword
//SimpleStorage is the name of the contract
//common datatype
/*
*boolean
*uint -> uint8, uint16, uint32,.... multiples of 8
*int
*address
*bytes
*string
*/
uint public favoriteNumber; //public allows us to view favoriteNumber;
//functions are methods that will executes a series of instructions when called.
function store(uint256 _favoriteNumber) public {
favoriteNumber=_favoriteNumber;
}
//Function Visibility Specifiers
/*
* public : visible externally and internally => creates a getter funtions automatically
* private: only visible in the current contract.
* external: is visible to only functions
* internally: only visible internally
*/
//everytime we change the state, it is considered as a transaction.
//understanding view, and pure
//view is used to just display a state
// you cannot update the function if its declared as view
function retrieve() public view returns(uint256){
return favoriteNumber;
}
/*
A pure function is a funtion that executes a instruction that
does not read state variables
*/
function add() pure public returns(uint16){
return (1+1);
}
// keep in mind, calling a function costs gas, even if its pure/view
//Arrays and Structs
//A structure is a collection of states under a common name
struct People {
uint256 favoriteNumber;
string name;
}
//assigning a value to a structure
//this displays a struct called person.
People public person =People({
favoriteNumber:21,
name:"Aaron Rebelo"
});
//saving struct values
/*
People({
favoriteNumber:21,
name:"Aaron Rebelo"
});
*/
//Array is a way to store object of information
//syntax: dataType[]
//an array of type People called people
People[] public people;
uint256[] public values;
// A mapping is a data structure where a key is "mapped" to a single value
mapping(string => uint256) public nameToFavoriteNunber;
function addPerson(uint256 _favoriteNumber, string memory _name) public {
// we need to add memory to string, as string is actually an array of bytes.
people.push(People({favoriteNumber:_favoriteNumber,name:_name}));
// A mapping is a data structure where a key is "mapped" to a single value
nameToFavoriteNunber[_name] = _favoriteNumber;
}
//Memory,Storage, And Calldata
//There are 6 places you can store data in solidity
/*
* Stack
* Memory -> variable will exist only temporarly => can modify value
* Storage
* Calldata -> variable will exist only temporarly => cannot modify value
* Code
* Logs
*/
}