add Fund Me Foundry Smart Contract

This commit is contained in:
han
2024-12-18 18:15:59 +07:00
parent e87bce6d80
commit 517ff0c9c9
18 changed files with 617 additions and 57 deletions

View File

@@ -1,19 +0,0 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import {Script, console} from "forge-std/Script.sol";
import {Counter} from "../src/Counter.sol";
contract CounterScript is Script {
Counter public counter;
function setUp() public {}
function run() public {
vm.startBroadcast();
counter = new Counter();
vm.stopBroadcast();
}
}

View File

@@ -0,0 +1,23 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;
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) {
// simulation-tx
HelperConfig helperConfig = new HelperConfig();
address ethUsdPriceFeed = helperConfig.activeNetworkConfig();
// real-tx
vm.startBroadcast();
FundMe fundMe = new FundMe(ethUsdPriceFeed);
vm.stopBroadcast();
return fundMe;
}
}

View File

@@ -0,0 +1,48 @@
// 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 = getOrCreateAnvilEthConfig();
}
}
function getSepoliaEthConfig() public pure returns (NetworkConfig memory) {
NetworkConfig memory sepoliaConfig = NetworkConfig({
priceFeed: 0x694AA1769357215DE4FAC081bf1f309aDC325306
});
return sepoliaConfig;
}
function getOrCreateAnvilEthConfig() 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;
}
}

View File

@@ -0,0 +1,47 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
// Fund
// Withdraw
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 mostRecentDeployed) public {
vm.startBroadcast();
FundMe(payable(mostRecentDeployed)).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 mostRecentDeployed) public {
vm.startBroadcast();
FundMe(payable(mostRecentDeployed)).withdraw();
vm.stopBroadcast();
console.log("Withdraw FundMe");
}
function run() external {
address mostRecentlyDeployed = DevOpsTools.get_most_recent_deployment(
"FundMe",
block.chainid
);
withdrawFundMe(mostRecentlyDeployed);
}
}