Skip to content

Commit debea29

Browse files
Merge pull request #26 from olujimiAdebakin/feature/BAOBAB_ORACLE
refactor(core): Re-architect oracle & security, refine position manag…
2 parents 4f085be + 2a3e484 commit debea29

34 files changed

Lines changed: 2697 additions & 2363 deletions

src/access/RoleRegistry.sol

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ library RoleRegistry {
8888
*/
8989
bytes32 public constant FEE_DISTRIBTOR_ROLE = keccak256("FEE_DISTRIBUTOR_ROLE");
9090

91-
9291
// bytes32 public constant FEE_MANAGER_ROLE = keccak256("FEE_MANAGER");
9392

9493
/**

src/core/data/OrderStorage.sol

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ pragma solidity ^0.8.24;
44
import {CommonStructs} from "../../libraries/structs/CommonStructs.sol";
55
import {AddressUtils} from "../../libraries/utils/AddressUtils.sol";
66

7-
87
/**
98
* @title OrderStorage
109
* @notice Stores and manages all limit/stop orders for BAOBAB

src/core/markets/MarketRegistry.sol

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,7 @@ contract MarketRegistry is SecurityBase {
278278
* - This mapping provides the bridge between marketId and actual token address
279279
* - Must be set before positions can be opened in the market
280280
*/
281-
function setQuoteAsset(bytes32 marketId, address quoteAssetAddress)
282-
external
283-
onlyAdmin
284-
{
281+
function setQuoteAsset(bytes32 marketId, address quoteAssetAddress) external onlyAdmin {
285282
if (!marketExists[marketId]) revert MarketRegistry__MarketNotFound();
286283
quoteAssetAddress.validateContract();
287284

@@ -294,10 +291,7 @@ contract MarketRegistry is SecurityBase {
294291
* @param marketId Market identifier
295292
* @param baseAssetAddress Token address of the base asset
296293
*/
297-
function setBaseAsset(bytes32 marketId, address baseAssetAddress)
298-
external
299-
onlyAdmin
300-
{
294+
function setBaseAsset(bytes32 marketId, address baseAssetAddress) external onlyAdmin {
301295
if (!marketExists[marketId]) revert MarketRegistry__MarketNotFound();
302296
baseAssetAddress.validateContract();
303297

@@ -347,11 +341,7 @@ contract MarketRegistry is SecurityBase {
347341
riskParameters[marketId].tradingFeeBps = newTradingFeeBps;
348342

349343
emit RiskParametersUpdated(
350-
marketId,
351-
newMaxLeverage,
352-
newMaintenanceMarginBps,
353-
newLiquidationFeeBps,
354-
newTradingFeeBps
344+
marketId, newMaxLeverage, newMaintenanceMarginBps, newLiquidationFeeBps, newTradingFeeBps
355345
);
356346
}
357347

@@ -366,10 +356,7 @@ contract MarketRegistry is SecurityBase {
366356
* - Market must exist
367357
* - New adapter must be valid contract
368358
*/
369-
function updateOracleAdapter(bytes32 marketId, address newOracleAdapter)
370-
external
371-
onlyAdmin
372-
{
359+
function updateOracleAdapter(bytes32 marketId, address newOracleAdapter) external onlyAdmin {
373360
if (!marketExists[marketId]) revert MarketRegistry__MarketNotFound();
374361
newOracleAdapter.validateContract();
375362

@@ -418,11 +405,7 @@ contract MarketRegistry is SecurityBase {
418405
* @param marketId Market identifier
419406
* @return Market struct with all metadata
420407
*/
421-
function getMarket(bytes32 marketId)
422-
external
423-
view
424-
returns (CommonStructs.Market memory)
425-
{
408+
function getMarket(bytes32 marketId) external view returns (CommonStructs.Market memory) {
426409
if (!marketExists[marketId]) revert MarketRegistry__MarketNotFound();
427410
return markets[marketId];
428411
}
@@ -432,11 +415,7 @@ contract MarketRegistry is SecurityBase {
432415
* @param marketId Market identifier
433416
* @return Risk parameters struct
434417
*/
435-
function getRiskParameters(bytes32 marketId)
436-
external
437-
view
438-
returns (CommonStructs.RiskParameters memory)
439-
{
418+
function getRiskParameters(bytes32 marketId) external view returns (CommonStructs.RiskParameters memory) {
440419
if (!marketExists[marketId]) revert MarketRegistry__MarketNotFound();
441420
return riskParameters[marketId];
442421
}
@@ -488,8 +467,7 @@ contract MarketRegistry is SecurityBase {
488467
* @return True if market exists and is in ACTIVE status
489468
*/
490469
function isMarketActive(bytes32 marketId) external view returns (bool) {
491-
return marketExists[marketId]
492-
&& markets[marketId].status == CommonStructs.MarketStatus.ACTIVE;
470+
return marketExists[marketId] && markets[marketId].status == CommonStructs.MarketStatus.ACTIVE;
493471
}
494472

495473
/**
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)