Added FundMe & PriceConverter
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.8.8;
|
||||
|
||||
import "@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() internal view returns (uint256) {
|
||||
// Sepolia ETH / USD Address
|
||||
// https://docs.chain.link/data-feeds/price-feeds/addresses#Sepolia%20Testnet
|
||||
AggregatorV3Interface priceFeed = AggregatorV3Interface(
|
||||
0x694AA1769357215DE4FAC081bf1f309aDC325306
|
||||
);
|
||||
(, int256 answer, , , ) = priceFeed.latestRoundData();
|
||||
// ETH/USD rate in 18 digit
|
||||
return uint256(answer * 10000000000);
|
||||
// or (Both will do the same thing)
|
||||
// return uint256(answer * 1e10); // 1* 10 ** 10 == 10000000000
|
||||
}
|
||||
|
||||
// 1000000000
|
||||
function getConversionRate(
|
||||
uint256 ethAmount
|
||||
) internal view returns (uint256) {
|
||||
uint256 ethPrice = getPrice();
|
||||
uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1000000000000000000;
|
||||
// or (Both will do the same thing)
|
||||
// uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1e18; // 1 * 10 ** 18 == 1000000000000000000
|
||||
// the actual ETH/USD conversion rate, after adjusting the extra 0s.
|
||||
return ethAmountInUsd;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user