In Progress Add Mock

This commit is contained in:
2024-08-04 04:51:48 +08:00
parent 89363c573a
commit 6e9c6dd633
9 changed files with 254 additions and 78 deletions
+10 -8
View File
@@ -6,13 +6,14 @@ import "@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.so
// Why is this a library and not abstract?
// Why not an interface?
library PriceConverter {
// Sepolia ETH / USD Address
// https://docs.chain.link/data-feeds/price-feeds/addresses#Sepolia%20Testnet
//0x694AA1769357215DE4FAC081bf1f309aDC325306
// 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
);
function getPrice(
AggregatorV3Interface priceFeed
) internal view returns (uint256) {
(, int256 answer, , , ) = priceFeed.latestRoundData();
// ETH/USD rate in 18 digit
return uint256(answer * 10000000000);
@@ -22,9 +23,10 @@ library PriceConverter {
// 1000000000
function getConversionRate(
uint256 ethAmount
uint256 ethAmount,
AggregatorV3Interface priceFeed
) internal view returns (uint256) {
uint256 ethPrice = getPrice();
uint256 ethPrice = getPrice(priceFeed);
uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1000000000000000000;
// or (Both will do the same thing)
// uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1e18; // 1 * 10 ** 18 == 1000000000000000000