Skip to content

Commit 53b1fc9

Browse files
feat(oracle): Introduce robust and secure IPriceFeed infrastructure
This commit finalizes the core components of the Baobab Protocol's decentralized oracle infrastructure, migrating to a unified `IPriceFeed` interface across all data sources (Chainlink, Pyth, Computed Feeds, and TWAP). The central goal is to enhance manipulation resistance, data consistency, and security through a multi-layered validation approach enforced by the new `BaobabOracleSecurity` contract. ### Key Features and Changes: 1. **BaobabOracleSecurity Integration:** - Implements a central security gateway (`getValidatedPrice`) enforcing sequential checks: Circuit Breakers, Global Pause, Global Staleness (`maxStalenessPeriod`), and contextual risk-based Confidence checks (`PriceUse`). - **Refinement:** Streamlined price fetching to rely solely on the `OracleRegistry` output, eliminating redundant internal calls and improving gas efficiency. 2. **OracleRegistry Role:** - Relies on the core `OracleRegistry` to manage asset configuration (Primary/Fallback feeds) and enforce internal heartbeat/staleness logic before data is passed to the Security layer. - The Registry acts as the sole source of data for the `BaobabOracleSecurity` layer. 3. **ComputedOracle (Derived Prices):** - Introduced a system for deriving secondary asset prices (e.g., ETH/BTC) via multiplication or division of two base feeds. - **Security Fix:** Updated `latestTimestamp()` logic to return the **oldest** component timestamp, preventing the computed price from falsely appearing "fresh" when an underlying feed is stale. - **Refinement:** Optimized fixed-point division math for gas efficiency while preserving 8-decimal output precision. 4. **Oracle Adapters (Chainlink & Pyth):** - Created `ChainlinkAdapter` and `PythAdapter` to strictly conform to the `IPriceFeed` interface. - **Consistency:** Standardized output to 8 decimals for Pyth by implementing a robust `_scalePrice` helper that handles variable exponents. - **Efficiency:** Refactored all adapters to ensure only a single external call is made per price query, drastically reducing transaction gas costs. - **Fail Safe:** Ensured adapters return the sentinel value (`type(int256).min`) on any failure (call revert, stale data, non-positive price). 5. **TWAPAdapter (Manipulation Resistance):** - Implemented a Time-Weighted Average Price (TWAP) adapter used for sensitive actions, sourcing only validated prices from `BaobabOracleSecurity`. - **Security Principle:** The price pushing mechanism is permissionless to decentralize maintenance cost and increase data freshness. - **Note on Pruning:** Identified gas-inefficiency in the current `_shiftLeft` array pruning method; marked for future refactor to a Ring/Circular Buffer structure for superior scalability.
1 parent b3772f1 commit 53b1fc9

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

src/core/oracles/adapters/PythAdapter.sol

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,19 @@ contract PythAdapter is IPriceFeed {
148148
*/
149149
function _scalePrice(int64 price, int32 expo) internal pure returns (int256) {
150150
int256 scaledPrice = int256(price);
151-
int32 scaleFactor = expo + 8; // How many decimals to adjust
151+
// How many decimals to adjust
152+
int32 scaleFactor = expo + 8;
152153

153154
if (scaleFactor > 0) {
154155
// Need to multiply (add decimals)
155156
uint32 power = uint32(scaleFactor);
156157
scaledPrice = scaledPrice * int256(10**power);
157158
} else if (scaleFactor < 0) {
158159
// Need to divide (remove decimals)
159-
uint32 power = uint32(-scaleFactor); // Convert negative to positive
160+
uint32 power = uint32(-scaleFactor); // Converting negative to positive
160161
scaledPrice = scaledPrice / int256(10**power);
161162
}
162-
// scaleFactor == 0: already at 8 decimals
163+
// NB: if scaleFactor == 0: already at 8 decimals
163164

164165
return scaledPrice;
165166
}

0 commit comments

Comments
 (0)