Skip to content

Commit

Permalink
feat(pool-monitor): add pool's initial monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandr-masl committed Sep 12, 2024
1 parent e691a20 commit 5aa078e
Showing 1 changed file with 67 additions and 7 deletions.
74 changes: 67 additions & 7 deletions packages/contracts/src/dollar/monitors/PoolLiquidityMonitor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,31 @@
pragma solidity ^0.8.19;

import "../facets/UbiquityPoolFacet.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import {Modifiers} from "../libraries/LibAppStorage.sol";
import {SafeMath} from "@openzeppelin/contracts/utils/math/SafeMath.sol";

contract PoolLiquidityMonitor is Modifiers {
using SafeMath for uint256;

contract PoolLiquidityMonitor is Ownable {
UbiquityPoolFacet public immutable ubiquityPoolFacet;
address public defenderRelayer;
uint256 public liquidityVertex;
bool public paused;
uint256 public thresholdPercentage;

event LiquidityChecked(uint256 currentLiquidity);
event LiquidityVertexUpdated(uint256 collateralLiquidity);
event MonitorPaused(uint256 collateralLiquidity, uint256 diffPercentage);
event VertexDropped();
event PausedToggled(bool paused);

constructor(address _ubiquityPoolFacetAddress, address _defenderRelayer) {
constructor(
address _ubiquityPoolFacetAddress,
address _defenderRelayer,
uint256 _thresholdPercentage
) {
ubiquityPoolFacet = UbiquityPoolFacet(_ubiquityPoolFacetAddress);
defenderRelayer = _defenderRelayer;
thresholdPercentage = _thresholdPercentage;
}

modifier onlyAuthorized() {
Expand All @@ -23,16 +37,62 @@ contract PoolLiquidityMonitor is Ownable {
_;
}

function setThresholdPercentage(
uint256 _newThresholdPercentage
) external onlyAdmin {
thresholdPercentage = _newThresholdPercentage;
}

function setDefenderRelayer(
address _newDefenderRelayer
) external onlyOwner {
) external onlyAdmin {
defenderRelayer = _newDefenderRelayer;
}

function checkLiquidity() external onlyAuthorized {
function togglePaused() external onlyAdmin {
paused = !paused;
emit PausedToggled(paused);
}

function dropLiquidityVertex() external onlyAdmin {
uint256 currentCollateralLiquidity = ubiquityPoolFacet
.collateralUsdBalance();

emit LiquidityChecked(currentCollateralLiquidity);
require(currentCollateralLiquidity > 0, "Insufficient liquidity");

liquidityVertex = currentCollateralLiquidity;

emit VertexDropped();
}

function checkLiquidityVertex() external onlyAuthorized {
uint256 currentCollateralLiquidity = ubiquityPoolFacet
.collateralUsdBalance();

require(currentCollateralLiquidity > 0, "Insufficient liquidity");
require(!paused, "Monitor paused");

if (currentCollateralLiquidity > liquidityVertex) {
liquidityVertex = currentCollateralLiquidity;

emit LiquidityVertexUpdated(liquidityVertex);
} else {
uint256 liquidityDiffPercentage = liquidityVertex
.sub(currentCollateralLiquidity)
.mul(100)
.div(liquidityVertex);

if (liquidityDiffPercentage >= thresholdPercentage) {
paused = true;

// Pause the UbiquityDollarToken
// Pause LibUbiquityPool by disabling collateral

emit MonitorPaused(
currentCollateralLiquidity,
liquidityDiffPercentage
);
}
}
}
}

0 comments on commit 5aa078e

Please sign in to comment.