|
| 1 | +// SPDX-License-Identifier: BUSL-1.1 |
| 2 | +pragma solidity ^0.8.24; |
| 3 | + |
| 4 | +import {IPriceFeed} from "../../interfaces/IPriceFeed.sol"; |
| 5 | +import {ComputedOracle} from "./adapters/ComputedOracle.sol"; |
| 6 | +// import { BaobabOracleRegistry} from "./OracleRegistry.sol"; |
| 7 | +import {IOracleRegistry} from "../../interfaces/IOracleRegistry.sol"; |
| 8 | + |
| 9 | +import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; |
| 10 | +import {AddressUtils} from "../../libraries/utils/AddressUtils.sol"; |
| 11 | + |
| 12 | +/** |
| 13 | + * @title ComputedOracleFactory |
| 14 | + * @notice Factory for deploying and registering new instances of ComputedOracle.sol. |
| 15 | + * @dev Centralizes deployment logic and ensures new oracles are immediately registered. |
| 16 | + */ |
| 17 | +contract ComputedOracleFactory is Ownable { |
| 18 | + using AddressUtils for address; |
| 19 | + // The OracleRegistry is immutable because the Factory must always register to the same central hub. |
| 20 | + // BaobabOracleRegistry public immutable ORACLE_REGISTRY; |
| 21 | + |
| 22 | + IOracleRegistry public immutable iORACLE_REGISTRY; |
| 23 | + |
| 24 | + event ComputedOracleCreated( |
| 25 | + address indexed targetAssetAddress, |
| 26 | + address indexed computedOracleAddress, |
| 27 | + address inputAssetA, |
| 28 | + address inputAssetB |
| 29 | + ); |
| 30 | + |
| 31 | + error ComputedOracleFactory__ZeroAddressRegistry(); |
| 32 | + error ComputedOracleFactory__InvalidInputAsset(address asset); |
| 33 | + |
| 34 | + constructor(address oracleRegistry_, address initialOwner) Ownable(initialOwner) { |
| 35 | + if (oracleRegistry_ == address(0)) { |
| 36 | + revert ComputedOracleFactory__ZeroAddressRegistry(); |
| 37 | + } |
| 38 | + // ORACLE_REGISTRY = BaobabOracleRegistry(oracleRegistry_); |
| 39 | + iORACLE_REGISTRY = IOracleRegistry(oracleRegistry_); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @notice Deploys a new ComputedOracle instance and immediately registers it |
| 44 | + * with the OracleRegistry under the specified targetAssetAddress. |
| 45 | + * @dev Only the owner (governance) can call this. |
| 46 | + * @param targetAssetAddress The address representing the derived asset (e.g., the synthetic token address). |
| 47 | + * @param feedA The address of the primary input feed (Asset A, e.g., ETH/USD). |
| 48 | + * @param feedB The address of the secondary input feed (Asset B, e.g., BTC/USD). |
| 49 | + * @return computedOracleAddress The address of the newly deployed ComputedOracle contract. |
| 50 | + */ |
| 51 | + function deployAndRegisterOracle( |
| 52 | + address targetAssetAddress, |
| 53 | + address feedA, |
| 54 | + address feedB, |
| 55 | + ComputedOracle.Operation operation, |
| 56 | + uint256 heartbeat |
| 57 | + ) external onlyOwner returns (address computedOracleAddress) { |
| 58 | + // INPUT VALIDATION |
| 59 | + feedA.validateNotZero(); |
| 60 | + feedB.validateNotZero(); |
| 61 | + targetAssetAddress.validateNotZero(); |
| 62 | + |
| 63 | + // DEPLOY THE NEW ORACLE INSTANCE |
| 64 | + // The `new` keyword executes the constructor of ComputedOracle.sol. |
| 65 | + // The newly deployed contract automatically inherits the security configuration (A, B, Registry) |
| 66 | + |
| 67 | + ComputedOracle newOracle = new ComputedOracle(feedA, feedB, operation); |
| 68 | + |
| 69 | + computedOracleAddress = address(newOracle); |
| 70 | + |
| 71 | + // REGISTER WITH ORACLE REGISTRY |
| 72 | + |
| 73 | + // Assuming OracleRegistry has a function to set the primary feed for an asset. |
| 74 | + iORACLE_REGISTRY.setOracle( |
| 75 | + targetAssetAddress, |
| 76 | + newOracle, |
| 77 | + IPriceFeed(address(0)), |
| 78 | + heartbeat |
| 79 | + ); |
| 80 | + |
| 81 | + emit ComputedOracleCreated(targetAssetAddress, computedOracleAddress, feedA, feedB); |
| 82 | + return computedOracleAddress; |
| 83 | + } |
| 84 | +} |
0 commit comments