4. Fund Me
Sending ETH through a function & reverts
contract FundMe {
// Smart Contracts can hold funds like wallets can
function fund() public payable {
// Want to be able to set a mininum fund amount in USD
// 1. How do we send ETH to this contract?
// require means this function needs this specific condition to fulfill to run
// else revert : undo any action before, and send remaining gas back
// 1e18 === 1 * 10 ** 18 === 1000000000000000000 === value in wei of 1 ethereum
require(msg.value > 1e18, "Didn't send enough!");
}
}Chainlinks & Oracles
Oracles are centralized network which introduce single point of failure
Chainlinks are Decentralized Oracle Network
Chainlink has many features out of the box
Chainlink Price Feeds
Chainlink Data Feeds are the quickest way to connect your smart contracts to the real-world market prices of assets. https://docs.chain.link/docs/data-feeds/price-feeds/
Chainlink VRF (Verifiable Random Function)
It is a provably fair and verifiable random number generator (RNG) that enables smart contracts to access random values without compromising security or usability. They are also used in building blockchain games and NFTs.
Chainlink Automation (Chainlink Keepers)
Enables conditional execution of your smart contracts based on trigger of an event. https://docs.chain.link/docs/chainlink-automation/
Chainlink Any API
Chainlink nodes can request any API using any api. It requires LINK tokens for the transaction. https://docs.chain.link/docs/any-api/
Interfaces and Price Feeds
https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol
Importing from GitHub & NPM
Floating Point Math in Solidity
Libraries
Similar to contracts but you can't declare any state variable and you can't send ether
It is embedded into contract if all library functions are internal
Otherwise, library must be deployed and then linked before the contract is deployed
PriceConverter.sol
FundMe.sol
SafeMath, Overflow Checking, and the "unchecked" keyword
Was used widely before version 0.8 of solidity
Unsigned integers were going unchecked which led to overflows
uint256 bigNumber = 255
bigNumber = bigNumber + 1
// value becomes 0
Safemath library was used for checking the upper limits to avoid overflows
Onward version 0.8, this check was automatically added
We can still use
unchecked {}keyword to revert back to unchecked mode
For Loops
Resetting an Array
Withdrawing Ethereum
Constructor
Modifiers
Advanced Solidity Concepts
Immutable & Constant
Used to make contract gas efficient
Instead of storing these variables in the storage class, we save them in the byte code of the contract hence gas efficient.
If a variable is assigned value at compile time and is never changed, then we can add
constantkeyword with it.Naming convention is usually all caps
Variables that we set one time but not on the same line where they are declared, we add
immutablekeyword with it.Name convention is usually
i_<name-of-var>
Custom Errors
Introduced in version 0.8.4
We can declare custom errors on reverts
Used for gas efficiency
Since a string is called in require statement
Receive & Fallback
What happens if someone send this contract ETH without calling any function
Final Contracts Samples
FundMe.sol
PriceConverter.sol
Last updated