From 5aa078ee687c8e803ddeb441f6a9ba928e0c5236 Mon Sep 17 00:00:00 2001 From: green Date: Thu, 12 Sep 2024 22:27:51 +0200 Subject: [PATCH] feat(pool-monitor): add pool's initial monitor --- .../dollar/monitors/PoolLiquidityMonitor.sol | 74 +++++++++++++++++-- 1 file changed, 67 insertions(+), 7 deletions(-) diff --git a/packages/contracts/src/dollar/monitors/PoolLiquidityMonitor.sol b/packages/contracts/src/dollar/monitors/PoolLiquidityMonitor.sol index 52bfed422..6ed8fee24 100644 --- a/packages/contracts/src/dollar/monitors/PoolLiquidityMonitor.sol +++ b/packages/contracts/src/dollar/monitors/PoolLiquidityMonitor.sol @@ -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() { @@ -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 + ); + } + } } }