@@ -10,14 +10,23 @@ import {ICollateralManagement} from "./interfaces/ICollateralManagement.sol";
1010import {Flyover} from "./libraries/Flyover.sol " ;
1111import {Quotes} from "./libraries/Quotes.sol " ;
1212
13+ /// @title Collateral Management
14+ /// @notice This contract is used to manage the collateral related aspects of the Flyover system.
15+ /// This involves adding, slashing, resigning and withdrawing collateral.
16+ /// @author Rootstock Labs
1317contract CollateralManagementContract is
1418 AccessControlDefaultAdminRulesUpgradeable ,
1519 ReentrancyGuardUpgradeable ,
1620 ICollateralManagement
1721{
1822 /// @notice The version of the contract
1923 string constant public VERSION = "1.0.0 " ;
24+
25+ /// @notice The role that can add collateral to the contract by using
26+ /// the addPegInCollateralTo or addPegOutCollateralTo functions
2027 bytes32 public constant COLLATERAL_ADDER = keccak256 ("COLLATERAL_ADDER " );
28+ /// @notice The role that can slash collateral from the contract by using
29+ /// the slashPegInCollateral or slashPegOutCollateral functions
2130 bytes32 public constant COLLATERAL_SLASHER = keccak256 ("COLLATERAL_SLASHER " );
2231
2332 uint256 private _minCollateral;
@@ -29,8 +38,17 @@ contract CollateralManagementContract is
2938 mapping (address => uint256 ) private _resignationBlockNum;
3039 mapping (address => uint256 ) private _rewards;
3140
41+ /// @notice Emitted when the minimum collateral is set
42+ /// @param oldMinCollateral The old minimum collateral
43+ /// @param newMinCollateral The new minimum collateral
3244 event MinCollateralSet (uint256 indexed oldMinCollateral , uint256 indexed newMinCollateral );
45+ /// @notice Emitted when the resignation delay in blocks is set
46+ /// @param oldResignDelayInBlocks The old resignation delay in blocks
47+ /// @param newResignDelayInBlocks The new resignation delay in blocks
3348 event ResignDelayInBlocksSet (uint256 indexed oldResignDelayInBlocks , uint256 indexed newResignDelayInBlocks );
49+ /// @notice Emitted when the reward percentage is set
50+ /// @param oldReward The old reward percentage
51+ /// @param newReward The new reward percentage
3452 event RewardPercentageSet (uint256 indexed oldReward , uint256 indexed newReward );
3553
3654 modifier onlyRegisteredForPegIn (address addr ) {
@@ -50,22 +68,32 @@ contract CollateralManagementContract is
5068 revert Flyover.PaymentNotAllowed ();
5169 }
5270
71+ /// @inheritdoc ICollateralManagement
5372 function addPegInCollateralTo (address addr ) external onlyRole (COLLATERAL_ADDER) payable override {
5473 _addPegInCollateralTo (addr, msg .value );
5574 }
5675
76+ /// @inheritdoc ICollateralManagement
5777 function addPegInCollateral () external onlyRegisteredForPegIn (msg .sender ) payable override {
5878 _addPegInCollateralTo (msg .sender , msg .value );
5979 }
6080
81+ /// @inheritdoc ICollateralManagement
6182 function addPegOutCollateralTo (address addr ) external onlyRole (COLLATERAL_ADDER) payable override {
6283 _addPegOutCollateralTo (addr, msg .value );
6384 }
6485
86+ /// @inheritdoc ICollateralManagement
6587 function addPegOutCollateral () external onlyRegisteredForPegOut (msg .sender ) payable override {
6688 _addPegOutCollateralTo (msg .sender , msg .value );
6789 }
6890
91+ /// @notice Initializes the contract
92+ /// @param owner The owner of the contract
93+ /// @param initialDelay The initial delay for changes in the default admin role
94+ /// @param minCollateral The minimum collateral required for a liquidity provider **per operation**
95+ /// @param resignDelayInBlocks The delay in blocks before a liquidity provider can withdraw their collateral
96+ /// @param rewardPercentage The reward percentage from the penalty fee of the quotes that the punisher will receive
6997 // solhint-disable-next-line comprehensive-interface
7098 function initialize (
7199 address owner ,
@@ -82,24 +110,31 @@ contract CollateralManagementContract is
82110 _penalties = 0 ;
83111 }
84112
113+ /// @notice Sets the minimum collateral required for a liquidity provider **per operation**
114+ /// @param minCollateral The new minimum collateral
85115 // solhint-disable-next-line comprehensive-interface
86116 function setMinCollateral (uint minCollateral ) external onlyRole (DEFAULT_ADMIN_ROLE) {
87117 emit MinCollateralSet (_minCollateral, minCollateral);
88118 _minCollateral = minCollateral;
89119 }
90120
121+ /// @notice Sets the resignation delay in blocks
122+ /// @param resignDelayInBlocks The new resignation delay in blocks
91123 // solhint-disable-next-line comprehensive-interface
92124 function setResignDelayInBlocks (uint resignDelayInBlocks ) external onlyRole (DEFAULT_ADMIN_ROLE) {
93125 emit ResignDelayInBlocksSet (_resignDelayInBlocks, resignDelayInBlocks);
94126 _resignDelayInBlocks = resignDelayInBlocks;
95127 }
96128
129+ /// @notice Sets the reward percentage from the penalty fee of the quotes that the punisher will receive
130+ /// @param rewardPercentage The new reward percentage
97131 // solhint-disable-next-line comprehensive-interface
98132 function setRewardPercentage (uint256 rewardPercentage ) external onlyRole (DEFAULT_ADMIN_ROLE) {
99133 emit RewardPercentageSet (_rewardPercentage, rewardPercentage);
100134 _rewardPercentage = rewardPercentage;
101135 }
102136
137+ /// @inheritdoc ICollateralManagement
103138 function slashPegInCollateral (
104139 address punisher ,
105140 Quotes.PegInQuote calldata quote ,
@@ -123,6 +158,7 @@ contract CollateralManagementContract is
123158 );
124159 }
125160
161+ /// @inheritdoc ICollateralManagement
126162 function slashPegOutCollateral (
127163 address punisher ,
128164 Quotes.PegOutQuote calldata quote ,
@@ -146,6 +182,7 @@ contract CollateralManagementContract is
146182 );
147183 }
148184
185+ /// @inheritdoc ICollateralManagement
149186 function withdrawCollateral () external nonReentrant override {
150187 address providerAddress = msg .sender ;
151188 uint resignationBlock = _resignationBlockNum[providerAddress];
@@ -165,6 +202,7 @@ contract CollateralManagementContract is
165202 if (! success) revert WithdrawalFailed (providerAddress, amount);
166203 }
167204
205+ /// @inheritdoc ICollateralManagement
168206 function withdrawRewards () external override {
169207 address addr = msg .sender ;
170208 uint256 rewards = _rewards[addr];
@@ -175,6 +213,7 @@ contract CollateralManagementContract is
175213 if (! success) revert WithdrawalFailed (addr, rewards);
176214 }
177215
216+ /// @inheritdoc ICollateralManagement
178217 function resign () external override {
179218 address providerAddress = msg .sender ;
180219 if (providerAddress == address (0 )) revert Flyover.InvalidAddress (providerAddress);
@@ -186,34 +225,42 @@ contract CollateralManagementContract is
186225 emit Resigned (providerAddress);
187226 }
188227
228+ /// @inheritdoc ICollateralManagement
189229 function getPegInCollateral (address addr ) external view override returns (uint256 ) {
190230 return _pegInCollateral[addr];
191231 }
192232
233+ /// @inheritdoc ICollateralManagement
193234 function getPegOutCollateral (address addr ) external view override returns (uint256 ) {
194235 return _pegOutCollateral[addr];
195236 }
196237
238+ /// @inheritdoc ICollateralManagement
197239 function getResignationBlock (address addr ) external view override returns (uint256 ) {
198240 return _resignationBlockNum[addr];
199241 }
200242
243+ /// @inheritdoc ICollateralManagement
201244 function getRewardPercentage () external view override returns (uint256 ) {
202245 return _rewardPercentage;
203246 }
204247
248+ /// @inheritdoc ICollateralManagement
205249 function getResignDelayInBlocks () external view override returns (uint256 ) {
206250 return _resignDelayInBlocks;
207251 }
208252
253+ /// @inheritdoc ICollateralManagement
209254 function getMinCollateral () external view override returns (uint256 ) {
210255 return _minCollateral;
211256 }
212257
258+ /// @inheritdoc ICollateralManagement
213259 function isRegistered (Flyover.ProviderType providerType , address addr ) external view override returns (bool ) {
214260 return _isRegistered (providerType, addr);
215261 }
216262
263+ /// @inheritdoc ICollateralManagement
217264 function isCollateralSufficient (
218265 Flyover.ProviderType providerType ,
219266 address addr
@@ -229,24 +276,41 @@ contract CollateralManagementContract is
229276 }
230277 }
231278
279+ /// @inheritdoc ICollateralManagement
232280 function getRewards (address addr ) external view override returns (uint256 ) {
233281 return _rewards[addr];
234282 }
235283
284+ /// @inheritdoc ICollateralManagement
236285 function getPenalties () external view override returns (uint256 ) {
237286 return _penalties;
238287 }
239288
289+ /// @notice Adds peg in collateral to an account
290+ /// @dev Is very important for this function to remain private as the public function
291+ /// is the one protected by the role checks
292+ /// @param addr The address of the account
293+ /// @param amount The amount of peg in collateral to add
240294 function _addPegInCollateralTo (address addr , uint256 amount ) private {
241295 _pegInCollateral[addr] += amount;
242296 emit ICollateralManagement.PegInCollateralAdded (addr, amount);
243297 }
244298
299+ /// @notice Adds peg out collateral to an account
300+ /// @dev Is very important for this function to remain private as the public function
301+ /// is the one protected by the role checks
302+ /// @param addr The address of the account
303+ /// @param amount The amount of peg out collateral to add
245304 function _addPegOutCollateralTo (address addr , uint256 amount ) private {
246305 _pegOutCollateral[addr] += amount;
247306 emit ICollateralManagement.PegOutCollateralAdded (addr, amount);
248307 }
249308
309+ /// @notice Checks if an account is registered
310+ /// @dev Registered means having added collateral to the contract and not having resigned
311+ /// @param providerType The type of provider
312+ /// @param addr The address of the account
313+ /// @return True if the account is registered, false otherwise
250314 function _isRegistered (Flyover.ProviderType providerType , address addr ) private view returns (bool ) {
251315 if (providerType == Flyover.ProviderType.PegIn) {
252316 return _pegInCollateral[addr] > 0 && _resignationBlockNum[addr] == 0 ;
0 commit comments