// SPDX-License-Identifier: MIT pragma solidity ^0.8.24; import {IAggregatorV3Like} from "./interfaces/IPluginFeed.sol"; /// @title ChainlinkScaledAdapter /// @notice Optional wrapper: rescales a (checked) AggregatorV3-like source to 8 decimals /// for consumers that hard-code the Chainlink 8-dp convention. contract ChainlinkScaledAdapter is IAggregatorV3Like { uint8 public constant DECIMALS = 8; IAggregatorV3Like public immutable source; uint8 public immutable sourceDecimals; error PrecisionLoss(int256 answer); error BadSource(); constructor(IAggregatorV3Like _source) { source = _source; sourceDecimals = _source.decimals(); if (sourceDecimals > 36) revert BadSource(); } function decimals() external pure returns (uint8) { return DECIMALS; } function description() external view returns (string memory) { return source.description(); } function version() external pure returns (uint256) { return 1; } function latestRoundData() external view returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound) { (roundId, answer, startedAt, updatedAt, answeredInRound) = source.latestRoundData(); if (sourceDecimals > DECIMALS) { answer = answer / int256(10 ** (sourceDecimals - DECIMALS)); if (answer == 0) revert PrecisionLoss(answer); } else if (sourceDecimals < DECIMALS) { answer = answer * int256(10 ** (DECIMALS - sourceDecimals)); } } }