-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5520564
commit 172097f
Showing
17 changed files
with
558 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,6 @@ docs/ | |
|
||
# Dotenv file | ||
.env | ||
|
||
broadcast/ | ||
lib/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
[submodule "lib/forge-std"] | ||
path = lib/forge-std | ||
url = https://github.com/foundry-rs/forge-std | ||
[submodule "lib/chainlink-brownie-contracts"] | ||
path = lib/chainlink-brownie-contracts | ||
url = https://github.com/smartcontractkit/chainlink-brownie-contracts | ||
[submodule "lib/foundry-devops"] | ||
path = lib/foundry-devops | ||
url = https://github.com/Cyfrin/foundry-devops |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-include .env | ||
|
||
build:; forge build | ||
|
||
deploy-sepolia: | ||
forge script script/DeployFundMe.s.sol:DeployFundMe --rpc-url $(SEPOLIA_RPC_URL) --private-key $(PRIVATE_KEY) --broadcast --verify --etherscan-api-key $(ETHERSCAN_API_KEY) -vvvv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule chainlink-brownie-contracts
added at
6e324d
Submodule foundry-devops
added at
df9f90
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
import {Script} from "forge-std/Script.sol"; | ||
import {FundMe} from "../src/FundMe.sol"; | ||
import {HelperConfig} from "./HelperConfig.s.sol"; | ||
|
||
contract DeployFundMe is Script { | ||
function run() external returns (FundMe) { | ||
HelperConfig helperConfig = new HelperConfig(); | ||
address ethUsdPriceFeed = helperConfig.activeNetworkConfig(); | ||
|
||
vm.startBroadcast(); | ||
FundMe fundMe = new FundMe(ethUsdPriceFeed); | ||
vm.stopBroadcast(); | ||
return fundMe; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.18; | ||
|
||
import {Script} from "forge-std/Script.sol"; | ||
import {MockV3Aggregator} from "../test/mocks/MockV3Aggregator.sol"; | ||
|
||
contract HelperConfig is Script { | ||
NetworkConfig public activeNetworkConfig; | ||
|
||
uint8 public constant DECIMALS = 8; | ||
int256 public constant INITIAL_PRICE = 2000e8; | ||
|
||
struct NetworkConfig { | ||
address priceFeed; | ||
} | ||
|
||
constructor() { | ||
if (block.chainid == 11155111) { | ||
activeNetworkConfig = getSepoliaEthConfig(); | ||
} else { | ||
activeNetworkConfig = getOrCreateAnvilConfig(); | ||
} | ||
} | ||
|
||
function getSepoliaEthConfig() public pure returns (NetworkConfig memory) { | ||
NetworkConfig memory sepoliaConfig = NetworkConfig({ | ||
priceFeed: 0x694AA1769357215DE4FAC081bf1f309aDC325306 | ||
}); | ||
return sepoliaConfig; | ||
} | ||
|
||
function getOrCreateAnvilConfig() public returns (NetworkConfig memory) { | ||
if (activeNetworkConfig.priceFeed != address(0)) { | ||
return activeNetworkConfig; | ||
} | ||
|
||
vm.startBroadcast(); | ||
MockV3Aggregator mockPriceFeed = new MockV3Aggregator( | ||
DECIMALS, | ||
INITIAL_PRICE | ||
); | ||
vm.stopBroadcast(); | ||
|
||
NetworkConfig memory anvilConfig = NetworkConfig({ | ||
priceFeed: address(mockPriceFeed) | ||
}); | ||
return anvilConfig; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.18; | ||
|
||
import {Script, console} from "forge-std/Script.sol"; | ||
import {DevOpsTools} from "foundry-devops/src/DevOpsTools.sol"; | ||
import {FundMe} from "../src/FundMe.sol"; | ||
|
||
contract FundFundMe is Script { | ||
uint256 constant SEND_VALUE = 0.01 ether; | ||
|
||
function fundFundMe(address mostRecentlyDeployed) public { | ||
vm.startBroadcast(); | ||
FundMe(payable(mostRecentlyDeployed)).fund{value: SEND_VALUE}(); | ||
vm.stopBroadcast(); | ||
console.log("Funded FundMe with %s", SEND_VALUE); | ||
} | ||
|
||
function run() external { | ||
address mostRecentlyDeployed = DevOpsTools.get_most_recent_deployment( | ||
"FundMe", | ||
block.chainid | ||
); | ||
fundFundMe(mostRecentlyDeployed); | ||
} | ||
} | ||
|
||
contract WithdrawFundMe is Script { | ||
function withdrawFundMe(address mostRecentlyDeployed) public { | ||
vm.startBroadcast(); | ||
FundMe(payable(mostRecentlyDeployed)).withdraw(); | ||
vm.stopBroadcast(); | ||
console.log("Withdrawed FundMe"); | ||
} | ||
|
||
function run() external { | ||
address mostRecentlyDeployed = DevOpsTools.get_most_recent_deployment( | ||
"FundMe", | ||
block.chainid | ||
); | ||
withdrawFundMe(mostRecentlyDeployed); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.18; | ||
|
||
// Note: The AggregatorV3Interface might be at a different location than what was in the video! | ||
import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol"; | ||
import {PriceConverter} from "./PriceConverter.sol"; | ||
|
||
error FundMe__NotOwner(); | ||
|
||
contract FundMe { | ||
using PriceConverter for uint256; | ||
|
||
mapping(address => uint256) private s_addressToAmountFunded; | ||
address[] private s_funders; | ||
|
||
// Could we make this constant? /* hint: no! We should make it immutable! */ | ||
address private immutable i_owner; | ||
uint256 public constant MINIMUM_USD = 5 * 10 ** 18; | ||
AggregatorV3Interface private s_priceFeed; | ||
|
||
constructor(address priceFeed) { | ||
i_owner = msg.sender; | ||
s_priceFeed = AggregatorV3Interface(priceFeed); | ||
} | ||
|
||
function fund() public payable { | ||
require( | ||
msg.value.getConversionRate(s_priceFeed) >= MINIMUM_USD, | ||
"You need to spend more ETH!" | ||
); | ||
// require(PriceConverter.getConversionRate(msg.value) >= MINIMUM_USD, "You need to spend more ETH!"); | ||
s_addressToAmountFunded[msg.sender] += msg.value; | ||
s_funders.push(msg.sender); | ||
} | ||
|
||
function getVersion() public view returns (uint256) { | ||
return s_priceFeed.version(); | ||
} | ||
|
||
modifier onlyOwner() { | ||
// require(msg.sender == owner); | ||
if (msg.sender != i_owner) revert FundMe__NotOwner(); | ||
_; | ||
} | ||
|
||
function cheaperWithdraw() public onlyOwner { | ||
uint256 fundersLength = s_funders.length; | ||
for ( | ||
uint256 funderIndex = 0; | ||
funderIndex < fundersLength; | ||
funderIndex++ | ||
) { | ||
address funder = s_funders[funderIndex]; | ||
s_addressToAmountFunded[funder] = 0; | ||
} | ||
s_funders = new address[](0); | ||
(bool callSuccess, ) = payable(msg.sender).call{ | ||
value: address(this).balance | ||
}(""); | ||
require(callSuccess, "Call failed"); | ||
} | ||
|
||
function withdraw() public onlyOwner { | ||
for ( | ||
uint256 funderIndex = 0; | ||
funderIndex < s_funders.length; | ||
funderIndex++ | ||
) { | ||
address funder = s_funders[funderIndex]; | ||
s_addressToAmountFunded[funder] = 0; | ||
} | ||
s_funders = new address[](0); | ||
// // transfer | ||
// payable(msg.sender).transfer(address(this).balance); | ||
|
||
// // send | ||
// bool sendSuccess = payable(msg.sender).send(address(this).balance); | ||
// require(sendSuccess, "Send failed"); | ||
|
||
// call | ||
(bool callSuccess, ) = payable(msg.sender).call{ | ||
value: address(this).balance | ||
}(""); | ||
require(callSuccess, "Call failed"); | ||
} | ||
|
||
// Explainer from: https://solidity-by-example.org/fallback/ | ||
// Ether is sent to contract | ||
// is msg.data empty? | ||
// / \ | ||
// yes no | ||
// / \ | ||
// receive()? fallback() | ||
// / \ | ||
// yes no | ||
// / \ | ||
//receive() fallback() | ||
|
||
fallback() external payable { | ||
fund(); | ||
} | ||
|
||
receive() external payable { | ||
fund(); | ||
} | ||
|
||
function getAddressToAmountFunded( | ||
address fundingAddress | ||
) external view returns (uint256) { | ||
return s_addressToAmountFunded[fundingAddress]; | ||
} | ||
|
||
function getFunder(uint256 index) external view returns (address) { | ||
return s_funders[index]; | ||
} | ||
|
||
function getOwner() external view returns (address) { | ||
return i_owner; | ||
} | ||
} | ||
|
||
// Concepts we didn't cover yet (will cover in later sections) | ||
// 1. Enum | ||
// 2. Events | ||
// 3. Try / Catch | ||
// 4. Function Selector | ||
// 5. abi.encode / decode | ||
// 6. Hash with keccak256 | ||
// 7. Yul / Assembly |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.18; | ||
|
||
import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol"; | ||
|
||
// Why is this a library and not abstract? | ||
// Why not an interface? | ||
library PriceConverter { | ||
// We could make this public, but then we'd have to deploy it | ||
function getPrice( | ||
AggregatorV3Interface priceFeed | ||
) internal view returns (uint256) { | ||
// Sepolia ETH / USD Address | ||
// https://docs.chain.link/data-feeds/price-feeds/addresses | ||
(, int256 answer, , , ) = priceFeed.latestRoundData(); | ||
// ETH/USD rate in 18 digit | ||
return uint256(answer * 10000000000); | ||
} | ||
|
||
// 1000000000 | ||
function getConversionRate( | ||
uint256 ethAmount, | ||
AggregatorV3Interface priceFeed | ||
) internal view returns (uint256) { | ||
uint256 ethPrice = getPrice(priceFeed); | ||
uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1000000000000000000; | ||
// the actual ETH/USD conversion rate, after adjusting the extra 0s. | ||
return ethAmountInUsd; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.