Skip to content

Commit

Permalink
feat: add Market Status Provider into protocol, add test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
lacoop6tu committed Apr 29, 2024
1 parent 00be20a commit 606d93c
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/contracts/core/common/State.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ struct CommonState {
/* -------------------------------------------------------------------------- */
mapping(bytes32 role => RoleData data) _roles;
mapping(bytes32 role => EnumerableSet.AddressSet member) _roleMembers;
/* -------------------------------------------------------------------------- */
/* Market Status Provider */
/* -------------------------------------------------------------------------- */
address marketStatusProvider;
}

/* -------------------------------------------------------------------------- */
Expand Down
5 changes: 5 additions & 0 deletions src/contracts/core/common/facets/CommonConfigFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ contract CommonConfigFacet is ICommonConfigFacet, Modifiers, DSModifiers {
cs().feeRecipient = _newFeeRecipient;
}

/// @inheritdoc ICommonConfigFacet
function setMarketStatusProvider(address _provider) public override onlyRole(Role.ADMIN) {
cs().marketStatusProvider = _provider;
}

function setPythEndpoint(address _pythEp) public override onlyRole(Role.ADMIN) {
if (_pythEp == address(0)) revert Errors.PYTH_EP_ZERO();
cs().pythEp = _pythEp;
Expand Down
5 changes: 5 additions & 0 deletions src/contracts/core/common/facets/CommonStateFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ contract CommonStateFacet is ICommonStateFacet {
return address(gm().manager);
}

/// @inheritdoc ICommonStateFacet
function getMarketStatusProvider() external view returns (address) {
return cs().marketStatusProvider;
}

/// @inheritdoc ICommonStateFacet
function getOracleDeviationPct() external view returns (uint16) {
return cs().maxPriceDeviationPct;
Expand Down
5 changes: 3 additions & 2 deletions src/contracts/core/common/funcs/Assets.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {ms} from "minter/MState.sol";
import {scdp} from "scdp/SState.sol";
import {IERC20} from "kresko-lib/token/IERC20.sol";
import {IKISS} from "kiss/interfaces/IKISS.sol";
import {IMarketStatus} from "common/interfaces/IMarketStatus.sol";

library Assets {
using WadRay for uint256;
Expand Down Expand Up @@ -44,8 +45,8 @@ library Assets {
return self.collateralAmountToValue(_amount, true);
}

function isMarketOpen(Asset storage) internal pure returns (bool) {
return true;
function isMarketOpen(Asset storage self) internal view returns (bool) {
return IMarketStatus(cs().marketStatusProvider).getTickerStatus(self.ticker);
}

/* -------------------------------------------------------------------------- */
Expand Down
6 changes: 6 additions & 0 deletions src/contracts/core/common/interfaces/ICommonConfigFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,10 @@ interface ICommonConfigFacet {
* @param _newManager _newManager address
*/
function setGatingManager(address _newManager) external;

/**
* @notice Sets market status provider
* @param _provider market status provider address
*/
function setMarketStatusProvider(address _provider) external;
}
3 changes: 3 additions & 0 deletions src/contracts/core/common/interfaces/ICommonStateFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ interface ICommonStateFacet {
/// @notice gating manager contract address
function getGatingManager() external view returns (address);

/// @notice Get the market status provider address.
function getMarketStatusProvider() external view returns (address);

/// @notice Get the L2 sequencer uptime feed address.
function getSequencerUptimeFeed() external view returns (address);

Expand Down
32 changes: 32 additions & 0 deletions src/contracts/core/common/interfaces/IMarketStatus.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface IMarketStatus {
function allowed(address) external view returns (bool);

function exchanges(bytes32) external view returns (bytes32);

function status(bytes32) external view returns (uint256);

function setStatus(bytes32[] calldata, bool[] calldata) external;

function setTickers(bytes32[] calldata, bytes32[] calldata) external;

function setAllowed(address, bool) external;

function getExchangeStatus(bytes32) external view returns (bool);

function getExchangeStatuses(
bytes32[] calldata
) external view returns (bool[] memory);

function getExchange(bytes32) external view returns (bytes32);

function getTickerStatus(bytes32) external view returns (bool);

function getTickerStatuses(
bytes32[] calldata
) external view returns (bool[] memory);

function owner() external view returns (address);
}
13 changes: 13 additions & 0 deletions src/contracts/mocks/MockMarketStatus.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pragma solidity ^0.8.0;

contract MockMarketStatus {
mapping(bytes32 => bool) public tickers;

function setTickerStatus(bytes32 _ticker, bool _status) external {
tickers[_ticker] = _status;
}

function getTickerStatus(bytes32 _ticker) external view returns (bool) {
return tickers[_ticker];
}
}
76 changes: 76 additions & 0 deletions src/contracts/test/forked/MarketStatusTest.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// solhint-disable state-visibility, max-states-count, no-empty-blocks, no-console
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {Deployed} from "scripts/deploy/libs/Deployed.s.sol";
import {Log, Help} from "kresko-lib/utils/Libs.s.sol";
import {ShortAssert} from "kresko-lib/utils/ShortAssert.t.sol";
import {Tested} from "kresko-lib/utils/Tested.t.sol";
import {IKresko} from "periphery/IKresko.sol";
import {IWETH9} from "kresko-lib/token/IWETH9.sol";
import {FacetCut, FacetCutAction} from "diamond/DSTypes.sol";
import {CommonConfigFacet} from "common/facets/CommonConfigFacet.sol";
import {CommonStateFacet} from "common/facets/CommonStateFacet.sol";
import {MockMarketStatus} from "src/contracts/mocks/MockMarketStatus.sol";
import {ProtocolUpgrader} from "scripts/utils/ProtocolUpgrader.s.sol";

contract MarketStatusTest is Tested, ProtocolUpgrader {
using Log for *;
using Help for *;
using Deployed for *;
using ShortAssert for *;

IKresko kresko = IKresko(0x0000000000177abD99485DCaea3eFaa91db3fe72);

address safe = 0x266489Bde85ff0dfe1ebF9f0a7e6Fed3a973cEc3;

CommonConfigFacet config;
CommonStateFacet state;
MockMarketStatus provider;

function setUp() public pranked(safe) {
vm.createSelectFork("arbitrum", 205696320);

// Deploy new facets and market status
config = new CommonConfigFacet();
state = new CommonStateFacet();
provider = new MockMarketStatus();

// Update CommonConfigFacet
bytes4[] memory selectors = getSelectors("CommonConfigFacet");
address oldFacet = kresko.facetAddress(selectors[0]);
bytes4[] memory oldSelectors = kresko.facetFunctionSelectors(oldFacet);

FacetCut[] memory cuts = new FacetCut[](1);
// Remove Config facet
cuts[0] = (FacetCut({facetAddress: address(0), action: FacetCutAction.Remove, functionSelectors: oldSelectors}));
kresko.diamondCut(cuts, address(0), "");
// Add Config facet
cuts[0] = FacetCut(address(config), FacetCutAction.Add, selectors);
kresko.diamondCut(cuts, address(0), "");

// Update CommonStateFacet
selectors = getSelectors("CommonStateFacet");
oldFacet = kresko.facetAddress(selectors[0]);
oldSelectors = kresko.facetFunctionSelectors(oldFacet);

// Remove State facet
cuts[0] = FacetCut({facetAddress: address(0), action: FacetCutAction.Remove, functionSelectors: oldSelectors});
kresko.diamondCut(cuts, address(0), "");
// Add State facet
cuts[0] = FacetCut(address(state), FacetCutAction.Add, selectors);
kresko.diamondCut(cuts, address(0), "");

// Set Market Status Provider
kresko.getMarketStatusProvider().eq(address(0));

kresko.setMarketStatusProvider(address(provider));
}

function test_Facets_Update() external {
kresko.getMarketStatusProvider().eq(address(provider));
kresko.getPythEndpoint().notEq(address(0));
kresko.getFeeRecipient().eq(safe);
kresko.getGatingManager().notEq(address(0));
}
}

0 comments on commit 606d93c

Please sign in to comment.