Skip to content

Commit

Permalink
Fixed price oracle tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amit-momin committed Jan 22, 2024
1 parent c335858 commit ca6dab8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
8 changes: 6 additions & 2 deletions contracts/PriceOracle/PriceOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ contract PriceOracle is Ownable2Step {
error MissingCTokenAddress();

/// @notice UnderlyingAssetDecimals is missing or set to value 0
error InvalidUnderlyingAssetDecimals(uint8 underlyingAssetDecimals);
error InvalidUnderlyingAssetDecimals();

/// @notice Sum of price feed's decimals and underlyingAssetDecimals is greater than MAX_DECIMALS
error FormattingDecimalsTooHigh(uint16 decimals);

/// @notice Price feed missing or duplicated
/// @param priceFeed Price feed address provided
Expand Down Expand Up @@ -205,7 +208,8 @@ contract PriceOracle is Ownable2Step {
// Retrieve decimals from feed for formatting
uint8 feedDecimals = aggregator.decimals();
// Check underlyingAssetDecimals exists and non-zero
if (underlyingAssetDecimals <= 0) revert InvalidUnderlyingAssetDecimals();
// Cap the sum of feed decimals and underlying asset decimals to avoid overflows when formatting prices.
if (underlyingAssetDecimals <= 0 || feedDecimals + underlyingAssetDecimals > MAX_DECIMALS) revert InvalidUnderlyingAssetDecimals(underlyingAssetDecimals);
if (feedDecimals + underlyingAssetDecimals > MAX_DECIMALS) revert FormattingDecimalsTooHigh(feedDecimals + underlyingAssetDecimals);
}
}
28 changes: 17 additions & 11 deletions test/PriceOracle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ const testConfigMap: Record<
mockPrice: 101000000,
feedDecimals: 8,
},
"0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643": {
mockPrice: 99000000,
feedDecimals: 75,
},
"0xf650C3d88D12dB855b8bf7D11Be6C55A4e07dCC9": {
mockPrice: 99800000,
feedDecimals: 8,
Expand Down Expand Up @@ -228,12 +224,6 @@ describe("PriceOracle", () => {
await priceOracle.getUnderlyingPrice(batCToken)
);
});
it("should return 0 price for invalid feeds decimal", async () => {
const daiCToken = "0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643";
expect(BigNumber.from("0")).to.equal(
await priceOracle.getUnderlyingPrice(daiCToken)
);
});
it("should return 0 price for invalid price from feed", async () => {
const wbtcCToken = "0xccf4429db6322d5c611ee964527d42e5d685dd6a";
expect(BigNumber.from("0")).to.equal(
Expand Down Expand Up @@ -298,7 +288,23 @@ describe("PriceOracle", () => {
};

await expect(priceOracle.addConfig(invalidConfig)).to.be.revertedWith(
"InvalidUnderlyingAssetDecimals"
"FormattingDecimalsTooHigh"
);
});
it("should revert for feed decimals too high", async () => {
const mockedEthAggregator = await deployMockContract(
deployer,
mockAggregatorAbi
);
mockedEthAggregator.mock.decimals.returns(255);
const invalidConfig: TokenConfig = {
cToken: "0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643",
underlyingAssetDecimals: "18",
priceFeed: mockedEthAggregator.address,
};

await expect(priceOracle.addConfig(invalidConfig)).to.be.revertedWith(
"FormattingDecimalsTooHigh"
);
});
});
Expand Down

0 comments on commit ca6dab8

Please sign in to comment.