13 tweets 26 reads Jun 10, 2022
10 Solidity Tips. A thread ↓
Solidity Contract
A Solidity contract is similar to a class in OOP.
It contains:
- data in state variables that are at a specific address on the Ethereum blockchain.
- functions to modify the variables.
On creation, its constructor (if it exists) is executed once.
Events in Solidity
One of my favorite Solidity features so far is how events are simply and effectively implemented.
3 Steps:
1. you declare the event (using the event keyword).
2. you fire the event (emit).
3. you handle the event with JS on the frontend.
ERC-20 Token Smart Contract
It's one of the most important smart contract standards.
It's used for smart contracts on the Ethereum blockchain for fungible token implementations.
Reserved Keywords
These are the currently reserved keywords in Solidity
A reserved keyword is a word that cannot be used as an identifier (e.g. variable name).
Function Types
The type of function, which is declared after the params, defines who can access it:
· the contract itself
· the contracts deriving from it
· the external contracts
Types:
· private
· internal
· external
· public
The default type is internal.
View & Pure Functions
Functions can be declared as view or pure
VIEW:
- ensure to not modify the state.
- Getter methods are view functions by default.
PURE:
- ensure to not read/modify the state.
- can use the revert/require functions if an error occurs.
MSG Global Variable
MSG VARIABLE contains properties to access the blockchain.
· msg.sender: message sender.
· msg.value: Wei sent with msg.
· msg.gas: remaining gas.
· msg.sig: calldata's first 4 bytes.
· msg. data: complete calldata.
State and Local Variables
- State Variables:
Values are permanently stored in contract storage.
- Local Variables:
Values are present till the function is in execution.
Require
It makes the function stop executing and throw an error if some condition is not true.
You can also pass a message that will be used when the error will be thrown.
It turns complex boilerplate logic into something simple and understandable.
Block global variable.
in Solidity, there are global variables, used to access important properties for objects in the blockchain.
One of these is "block".
Its values are:
-basefee
-chainId
-coinbase
-difficulty
-number
-timestamp
-gaslimit
🟩Free Web3 resources
Here is a GitHub Repository with all the tips day by day, and a list of resources to get started and learn web3 for free.
Currently 1998 Stars.
github.com
If this thread has been useful, retweet the tweet below and follow @FrancescoCiull4

Report this thread