Skip to content

Commit

Permalink
Fixed underlying asset decimals validation
Browse files Browse the repository at this point in the history
  • Loading branch information
amit-momin committed May 15, 2024
1 parent be080ca commit 0212347
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
5 changes: 3 additions & 2 deletions contracts/PriceOracle/PriceOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,10 @@ contract PriceOracle is Ownable2Step {
* @param config TokenConfig struct that needs to be validated
*/
function _validateTokenConfig(LoadConfig memory config) internal view {
// Check if cToken is zero address
if (config.cToken == address(0)) revert MissingCTokenAddress();
// Check if underlyingAssetDecimals exists and non-zero
if (config.underlyingAssetDecimals == 0) revert InvalidUnderlyingAssetDecimals();
// Check if both price feed and fixed price are empty
if (config.priceFeed == address(0) && config.fixedPrice == 0) revert InvalidPriceConfigs(config.priceFeed, config.fixedPrice);
// Check if both price feed and fixed price are set
Expand All @@ -281,8 +284,6 @@ contract PriceOracle is Ownable2Step {
* @param underlyingAssetDecimals The underlying asset decimals set in the config
*/
function _validateDecimals(address priceFeed, uint8 underlyingAssetDecimals) internal view {
// Check underlyingAssetDecimals exists and non-zero
if (underlyingAssetDecimals == 0) revert InvalidUnderlyingAssetDecimals();
AggregatorV3Interface aggregator = AggregatorV3Interface(priceFeed);
// Retrieve decimals from feed for formatting
uint8 feedDecimals = aggregator.decimals();
Expand Down
17 changes: 13 additions & 4 deletions test/PriceOracle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,16 +346,25 @@ describe("PriceOracle", () => {
);
});
it("should revert for 0 underlyingAssetDecimals in config", async () => {
const invalidConfig: TokenConfig = {
const invalidConfigWithPriceFeed: TokenConfig = {
cToken: "0x041171993284df560249B57358F931D9eB7b925D",
underlyingAssetDecimals: "0",
priceFeed: "0x09023c0da49aaf8fc3fa3adf34c6a7016d38d5e3",
fixedPrice: "0",
};
const invalidConfigWithFixedPrice: TokenConfig = {
cToken: "0x041171993284df560249B57358F931D9eB7b925D",
underlyingAssetDecimals: "0",
priceFeed: zeroAddress,
fixedPrice: "1000",
};

await expect(priceOracle.addConfig(invalidConfig)).to.be.revertedWith(
"InvalidUnderlyingAssetDecimals"
);
await expect(
priceOracle.addConfig(invalidConfigWithPriceFeed)
).to.be.revertedWith("InvalidUnderlyingAssetDecimals");
await expect(
priceOracle.addConfig(invalidConfigWithFixedPrice)
).to.be.revertedWith("InvalidUnderlyingAssetDecimals");
});
it("should revert for underlyingAssetDecimals too high in config", async () => {
const mockedEthAggregator = await deployMockContract(
Expand Down

0 comments on commit 0212347

Please sign in to comment.