Last active
January 18, 2023 02:58
-
-
Save nooruddin/b77ba39af2e448a52ae4445fc2da9211 to your computer and use it in GitHub Desktop.
FundMe Contracts
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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.8; | |
// functions receive() and fallback() are used to receive low-level call data | |
// used when there is no valid function to be called in contract | |
contract FallbackExample { | |
uint256 public result; | |
receive() external payable { | |
result = 1; | |
} | |
fallback() external payable { | |
result = 2; | |
} | |
} |
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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.8; | |
import "./PriceConverter.sol"; | |
error NotEnoughFunds(); | |
error CallFailed(); | |
error NotOwner(); | |
contract FundMe { | |
using PriceConverter for uint256; | |
uint256 public constant MINIMUM_USD = 50 * 1e18; | |
address[] public funders; | |
mapping(address => uint256) public addressToAmountFunded; | |
address public immutable i_owner; | |
function fund() public payable { | |
// 1e18 Wei = 1 ETH = minimum amount to send | |
// require(msg.value.getConversionRate() >= MINIMUM_USD, "Didn't send enough!"); | |
if(msg.value.getConversionRate() < MINIMUM_USD) { | |
revert NotEnoughFunds(); | |
} | |
funders.push(msg.sender); | |
addressToAmountFunded[msg.sender] += msg.value; | |
} | |
constructor() { | |
i_owner = msg.sender; | |
} | |
function withdraw() public onlyOwner { | |
for(uint256 funderIndex = 0; funderIndex < funders.length; funderIndex++) { | |
address funder = funders[funderIndex]; | |
addressToAmountFunded[funder] = 0; | |
} | |
// reset array | |
funders = new address[](0); | |
/** | |
* Different ways to withdraw | |
*/ | |
// 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"); | |
if(!callSuccess) { | |
revert CallFailed(); | |
} | |
} | |
modifier onlyOwner { | |
// require(msg.sender == i_owner, "Sender is not Owner!"); | |
if(msg.sender != i_owner) { | |
revert NotOwner(); | |
} | |
_; // placeholder for code to be executed for this modifier | |
} | |
// low-level functions if fund() is not called directly | |
// e.g. going directly to metamask and sending funds to the contract address | |
receive() external payable { | |
fund(); | |
} | |
fallback() external payable { | |
fund(); | |
} | |
} |
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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.8; | |
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; | |
library PriceConverter { | |
function getPrice() internal view returns(uint256) { | |
// ABI | |
// Address - 0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e | |
AggregatorV3Interface priceFeed = AggregatorV3Interface(0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e); | |
( | |
, | |
/*uint80 roundID*/ int256 price /*uint startedAt*/ /*uint timeStamp*/ /*uint80 answeredInRound*/, | |
, | |
, | |
) = priceFeed.latestRoundData(); | |
return uint256(price * 1e10); | |
} | |
function getVersion() internal view returns (uint256) { | |
return AggregatorV3Interface(0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e).version(); | |
} | |
function getConversionRate(uint256 ethAmount) internal view returns(uint256) { | |
uint256 ethPrice = getPrice(); | |
uint256 ethAmountinUsd = (ethPrice * ethAmount) / 1e18; | |
return ethAmountinUsd; | |
} | |
} |
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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
contract SathMathTester { | |
uint8 public bigNumber = 255; | |
function add() public { | |
unchecked { bigNumber = bigNumber + 1; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment