-
Here is the FundMe.sol Contract. // Get Funds from User
// Withdraw Funds
// Set a minimum funding value in USD
// SPDX-License-Identifier: MIT
// Pragma
pragma solidity ^0.8.13;
// Imports
import "./PriceConverter.sol";
// 756935 gas
// 737202 gas (After adding constant keyword)
// Error Codes
error FundMe__NotOwner();
// Interfaces, Libraries, Contracts
/**
* @title A contract for crowd Funding
* @author Nikhil
* @notice This contract is to demo a simple Crowd Funding Contract
* @dev This implements price feeds as our Library
*/
contract FundMe {
// Type Declarations
using PriceConverter for uint256;
// State Variables
uint256 public constant MINIMUM_USD = 50 * 1e18;
// 2571 gas
// 347 gas (After adding constant keyword)
address public i_owner;
// 2549 gas
// 439 gas (After adding immutable keyword)
address[] public funders;
mapping(address => uint256) public addressToAmount;
AggregatorV3Interface public priceFeed;
// Modifier
modifier onlyOwner() {
// require(msg.sender == i_owner, "Sender is not the Owner");
if (msg.sender != i_owner) revert FundMe__NotOwner();
_; // Tells the function to execute the remaining Code.
}
constructor(AggregatorV3Interface priceFeedAddress) {
i_owner = msg.sender;
priceFeed = AggregatorV3Interface(priceFeedAddress);
}
receive() external payable {
fund();
}
fallback() external payable {
fund();
}
/**
* @notice This Function funds this contract
* @dev This implements pricefeed as our Library
*/
function fund() public payable {
// Want to be able to set a minimum fund amount in USD
// 1. How do we send ETH to this contract ?
require(
msg.value.getConversionRate(priceFeed) >= MINIMUM_USD,
"Don't have enough Balance,"
);
//require(getConversionRate(msg.value) >= MINIMUM_USD,"Don't have enough Balance,");
// 18 decimal places
funders.push(msg.sender);
addressToAmount[msg.sender] = msg.value;
}
function withdraw() public onlyOwner {
for (
uint256 funderIndex = 0;
funderIndex < funders.length;
funderIndex++
) {
address funder = funders[funderIndex];
addressToAmount[funder] = 0;
}
// reset the array
funders = new address[](0);
// actually withdraw the fund
/* Methods to transfer money */
// transfer
// payable(msg.sender).transfer(address(this).balance);
// send
// bool sendSuccess = payable(msg.sender).send(address(this).balance);
// require(sendSuccess,"Call Failed");
// The Above 2 is capped at 2300 gas and above that will throw error
// call
(bool callSuccess /* bool memory dataReturned */, ) = payable(msg.sender)
.call{value: address(this).balance}("");
require(callSuccess, "Call Failed");
}
/* modifier onlyOwner {
_; // Tells the function to execute the remaining Code first.
require(msg.sender == owner, "Sender is not the Owner");
}*/
}
// $1885.995833960000000000
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
test script using the popular testing framework in Ethereum development, Truffle. // Import necessary modules from Truffle and Chai contract('FundMe', (accounts) => { before(async () => { it('should receive funds via receive function', async () => {
}); it('should receive funds via fallback function', async () => {
}); Remember to replace /* pass your PriceFeedAddress here */ with the actual address of your PriceFeed contract. |
Beta Was this translation helpful? Give feedback.
test script using the popular testing framework in Ethereum development, Truffle.
// Import necessary modules from Truffle and Chai
const { expect } = require('chai');
const FundMe = artifacts.require('FundMe');
const PriceConverter = artifacts.require('PriceConverter'); // Assuming you have a PriceConverter contract
contract('FundMe', (accounts) => {
let fundMeInstance;
before(async () => {
// Deploy the FundMe contract
fundMeInstance = await FundMe.new( /* pass your PriceFeedAddress here */ );
});
it('should receive funds via receive function', async () => {
const initialBalance = await web3.eth.getBalance(fundMeInstance.address);