Last active
December 29, 2023 15:55
-
-
Save Farhan0016/64ace9439ce04742db9ed74fec3fbc23 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.8+commit.dddeac2f.js&optimize=false&runs=200&gist=
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; | |
interface AggregatorV3Interface { | |
function decimals() external view returns (uint8); | |
function description() external view returns (string memory); | |
function version() external view returns (uint256); | |
function getRoundData( | |
uint80 _roundId | |
) | |
external | |
view | |
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound); | |
function latestRoundData() | |
external | |
view | |
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound); | |
} |
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; | |
import "./SimpleStorage.sol"; | |
contract ExtraStorage is SimpleStorage { | |
// override | |
// virtual override | |
function store(uint256 _favoriteNumber) override public { | |
favoriteNumber = _favoriteNumber + 5; | |
} | |
} |
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
// Get funds from User | |
// withdraw funds | |
// Set a minimum funding value in USD | |
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
import "./PriceConverter.sol"; | |
error NotOwner(); | |
contract FundMe { | |
using PriceConverter for uint256; | |
uint256 public constant MINIMUM_USD = 50 * 1e18; // 1 * 10 ** 18 | |
// 585,847 no-constant (execution cost) | |
// 566,341 constant // it is for gas optimization | |
address[] public funders; | |
mapping (address => uint256) public addressToAmountFunded; | |
address public immutable i_owner; // immutable is also used for gas optimization | |
// 566,341 before immutable | |
// 543,450 after immutable keyword | |
constructor() { | |
i_owner = msg.sender; | |
} | |
function fund() public payable { | |
// 18 decimals | |
// require(msg.value >= 1e18, "Didn't send enough!"); // 1e18 = 1000000000000000000 = 1Ethr | |
// require(getPriceConversion(msg.value) >= MINIMUM_USD, "Didn't send enough!"); | |
require(msg.value.getPriceConversion() >= MINIMUM_USD, "Didn't send enough!"); | |
funders.push(msg.sender); | |
addressToAmountFunded[msg.sender] = msg.value; // msg.value means number of Wei | |
} | |
function withdraw() public onlyOwner { | |
/* starting index; ending index; step amount */ | |
for(uint256 funderIndex = 0; funderIndex < funders.length; funderIndex++) { | |
address funder = funders[funderIndex]; | |
addressToAmountFunded[funder] = 0; | |
} | |
// Reset the funders array having no objects or elements | |
funders = new address[](0); // actually withdraw the funds | |
// // transfer | |
// // msg.sender = address | |
// // payable(msg.sender) = payable address | |
// payable(msg.sender).transfer(address(this).balance); | |
// // send | |
// bool sendSuccess = payable(msg.sender).send(address(this).balance); | |
// require(sendSuccess, "Send failed."); | |
// call | |
(bool callSuccess, /*bytes memory dataReturned*/) = payable(msg.sender).call{value: address(this).balance}(""); | |
require(callSuccess, "Call failed."); | |
revert(); // for gas optimization | |
} | |
modifier onlyOwner { | |
// require(msg.sender == i_owner, "Sender is not owner!"); | |
if(msg.sender != i_owner) { /// this is for gas optimization | |
revert NotOwner(); | |
} | |
_; // execute the code of withdraw() after checking the condition | |
} | |
// what happens if someone send this contract ETH without calling the fund() function | |
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.0; | |
import "./AggregatorV3Interface.sol"; | |
library PriceConverter { | |
function getPrice() public view returns(uint256) { | |
// ABI | |
// Address 0x694AA1769357215DE4FAC081bf1f309aDC325306 | |
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x694AA1769357215DE4FAC081bf1f309aDC325306); | |
// (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound) = priceFeed.latestRoundData(); | |
(, int256 price, , ,) = priceFeed.latestRoundData(); | |
// ETH in terms of USD | |
return uint256(price * 1e10); // 1**10 = 10000000000 | |
} | |
// function getVersion() public view returns(uint256) { | |
// AggregatorV3Interface priceFeed = AggregatorV3Interface(0x694AA1769357215DE4FAC081bf1f309aDC325306); | |
// return priceFeed.version(); | |
// } | |
function getPriceConversion(uint256 ethAmount) public view returns(uint256) { | |
uint256 ethPrice = getPrice(); | |
uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1e18; // if we don't divide then decimal places is returns more | |
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.6.0; | |
// contract SafeMathTester { | |
// uint8 public bigNumber = 255; | |
// function add() public { | |
// bigNumber = bigNumber + 1; | |
// } | |
// } | |
pragma solidity ^0.8.0; | |
contract SafeMathTester { | |
uint8 public bigNumber = 255; | |
function add() public { | |
bigNumber = bigNumber + 1; | |
// unchecked {bigNumber = bigNumber + 1;} // like ^0.6.0 version after upper limit it will be back to lower limit | |
} | |
} |
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; | |
// pragma solidity ^0.8.0; | |
// pragma solidity >=0.8.0 <0.9.0; | |
contract SimpleStorage { | |
uint256 favoriteNumber; | |
struct People { | |
uint256 favoriteNumber; | |
string name; | |
} | |
People[] public people; | |
mapping (string => uint256) public nameToFavoriteNumber; | |
function store(uint256 _favoriteNumber) virtual public { | |
favoriteNumber = _favoriteNumber; | |
} | |
function retrieve() public view returns(uint256) { | |
return favoriteNumber; | |
} | |
function addPerson(string memory _name, uint256 _favoriteNumber) public { | |
people.push(People(_favoriteNumber, _name)); | |
nameToFavoriteNumber[_name] = _favoriteNumber; | |
} | |
} |
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; | |
import "./SimpleStorage.sol"; | |
contract StorageFactory { | |
SimpleStorage[] public simpleStorageArray; | |
function createSimpleStorageContract() public { | |
SimpleStorage simpleStorgae = new SimpleStorage(); | |
simpleStorageArray.push(simpleStorgae); | |
} | |
function sfStore(uint256 _simpleStorageIndex, uint256 _simpleStorageNumber) public { | |
// for interacting the contract we need two things: | |
// 1. Address of contract | |
// 2. ABI - Application Binary Interface | |
// SimpleStorage simpleStorage = simpleStorageArray[_simpleStorageIndex]; | |
// simpleStorage.store(_simpleStorageNumber); | |
simpleStorageArray[_simpleStorageIndex].store(_simpleStorageNumber); | |
} | |
function sfGet(uint256 _simpleStorageIndex) public view returns(uint256) { | |
// SimpleStorage simpleStorage = simpleStorageArray[_simpleStorageIndex]; | |
// return simpleStorage.retrieve(); | |
return simpleStorageArray[_simpleStorageIndex].retrieve(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment