Skip to content

Commit

Permalink
Add USDC value feed contract
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Jan 2, 2025
1 parent 3227e8a commit 2065758
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,8 @@ The ETH value should handle the decimal of the erc 20

```sol
npx hardhat size-contracts
```
```

Network: Ethereum Mainnet
USDC/USD Price Feed Address: 0x8fFfFfd4AfB6115b954Bd326cbe7B4BA576818f6
ETH/USD Price Feed Address: 0x5f4ec3df9cbd43714fe2740f5e3616155c5b8419
60 changes: 60 additions & 0 deletions contracts/example/USDCV1EMPETHValueFeed.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;


import { AggregatorV3Interface } from "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

import { IV1EMPETHValueFeed } from "../interface/IV1EMPETHValueFeed.sol";


contract USDCV1EMPETHValueFeed is
IV1EMPETHValueFeed
{
AggregatorV3Interface internal _usdcUsdPriceFeed;
AggregatorV3Interface internal _ethUsdPriceFeed;


constructor (address __usdcUsdPriceFeed, address __ethUsdPriceFeed)
{
_usdcUsdPriceFeed = AggregatorV3Interface(__usdcUsdPriceFeed);
_ethUsdPriceFeed = AggregatorV3Interface(__ethUsdPriceFeed);
}


/// @inheritdoc IV1EMPETHValueFeed
function utilizedERC20ETHValue()
public
view
override
returns (uint256)
{
// Get the latest USDC/USD price
(, int256 usdcUsdPrice, , , ) = _usdcUsdPriceFeed.latestRoundData();

// Get the latest ETH/USD price
(, int256 ethUsdPrice, , , ) = _ethUsdPriceFeed.latestRoundData();

uint8 usdcDecimals = _usdcUsdPriceFeed.decimals();
uint8 ethDecimals = _ethUsdPriceFeed.decimals();

// Ensure prices are positive
require(usdcUsdPrice > 0 && ethUsdPrice > 0, "Invalid price");

// Normalize to 18 decimals
uint256 normalizedUsdcUsdPrice = uint256(usdcUsdPrice) * 10 ** (18 - usdcDecimals);
uint256 normalizedEthUsdPrice = uint256(ethUsdPrice) * 10 ** (18 - ethDecimals);

// Calculate USDC price in ETH
return (normalizedUsdcUsdPrice * 1e18) / normalizedEthUsdPrice;
}

/// @inheritdoc IV1EMPETHValueFeed
function eRC20Decimals()
public
view
override
returns (uint8)
{
return _usdcUsdPriceFeed.decimals();
}
}
2 changes: 1 addition & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"dictionaryDefinitions": [],
"dictionaries": [],
"words": [],
"ignoreWords": ["EMPERC", "EMPETH", "existant", "IERC", "inheritdoc", "Interactor", "Reentrancy"],
"ignoreWords": ["chainlink", "EMPERC", "EMPETH", "existant", "IERC", "inheritdoc", "Interactor", "Reentrancy", "USDCV"],
"import": []
}

0 comments on commit 2065758

Please sign in to comment.