Skip to content

Commit b84e3af

Browse files
committed
docs: add documentation for collateral management
1 parent 67d6ec8 commit b84e3af

File tree

2 files changed

+188
-0
lines changed

2 files changed

+188
-0
lines changed

contracts/CollateralManagement.sol

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,23 @@ import {ICollateralManagement} from "./interfaces/ICollateralManagement.sol";
1010
import {Flyover} from "./libraries/Flyover.sol";
1111
import {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
1317
contract 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;

contracts/interfaces/ICollateralManagement.sol

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,43 @@ import {Quotes} from "../libraries/Quotes.sol";
66

77
event CollateralManagementSet(address indexed oldAddress, address indexed newAddress);
88

9+
/// @title Collateral Management interface
10+
/// @notice This interface is used to expose the required functions to
11+
/// provide the Flyover collateral management service.
12+
/// This involves, slashing, resigning and registering processes.
913
interface ICollateralManagement {
14+
15+
/// @notice Emitted when the collateral is withdrawn
16+
/// @param addr The address of the liquidity provider
17+
/// @param amount The amount of collateral withdrawn
1018
event WithdrawCollateral(address indexed addr, uint indexed amount);
19+
20+
/// @notice Emitted when the liquidity provider resigns
21+
/// @param addr The address of the liquidity provider
1122
event Resigned(address indexed addr);
23+
24+
/// @notice Emitted when the rewards are withdrawn by a punisher
25+
/// @param addr The address of the punisher
26+
/// @param amount The amount of rewards withdrawn
1227
event RewardsWithdrawn(address indexed addr, uint256 indexed amount);
28+
29+
/// @notice Emitted when the peg in collateral is added
30+
/// @param addr The address of the liquidity provider
31+
/// @param amount The amount of peg in collateral added
1332
event PegInCollateralAdded(address indexed addr, uint256 indexed amount);
33+
34+
/// @notice Emitted when the peg out collateral is added
35+
/// @param addr The address of the liquidity provider
36+
/// @param amount The amount of peg out collateral added
1437
event PegOutCollateralAdded(address indexed addr, uint256 indexed amount);
38+
39+
/// @notice Emitted when a liquidity provider is penalized by not behaving as expected
40+
/// @param liquidityProvider The address of the liquidity provider
41+
/// @param punisher The address of the punisher
42+
/// @param quoteHash The hash of the quote
43+
/// @param collateralType The type of collateral
44+
/// @param penalty The penalty amount for the liquidity provider
45+
/// @param reward The reward amount for the punisher
1546
event Penalized(
1647
address indexed liquidityProvider,
1748
address indexed punisher,
@@ -21,38 +52,131 @@ interface ICollateralManagement {
2152
uint256 reward
2253
);
2354

55+
/// @notice Emitted when a liquidity provider has already resigned
56+
/// @param from The address of the liquidity provider
2457
error AlreadyResigned(address from);
58+
59+
/// @notice Emitted when a liquidity provider has not resigned yet
60+
/// @param from The address of the liquidity provider
2561
error NotResigned(address from);
62+
63+
/// @notice Emitted when a liquidity provider has not resigned yet
64+
/// @param from The address of the liquidity provider
65+
/// @param resignationBlockNum The block number at which the liquidity provider resigned
66+
/// @param resignDelayInBlocks The delay in blocks before a liquidity provider can withdraw their collateral
2667
error ResignationDelayNotMet(address from, uint resignationBlockNum, uint resignDelayInBlocks);
68+
69+
/// @notice Emitted when a liquidity provider tries to withdraw collateral but the withdrawal fails
70+
/// @param from The address of the liquidity provider
71+
/// @param amount The amount of collateral that was attempted to be withdrawn
2772
error WithdrawalFailed(address from, uint amount);
73+
74+
/// @notice Emitted when a liquidity provider tries to withdraw collateral but has nothing to withdraw
75+
/// @param from The address of the liquidity provider
2876
error NothingToWithdraw(address from);
2977

78+
/// @notice Adds peg in collateral to an account
79+
/// @param addr The address of the account
80+
/// @dev This function requires the COLLATERAL_ADDER role
3081
function addPegInCollateralTo(address addr) external payable;
82+
83+
/// @notice Adds peg in collateral to the caller
84+
/// @dev This function can only be called by a liquidity provider. This means an
85+
/// account that has been registered by adding collateral with addPegInCollateralTo.
86+
/// This means the COLLATERAL_ADDER can't use this function unless they register themselves.
3187
function addPegInCollateral() external payable;
88+
89+
/// @notice Slashes peg in collateral from a liquidity provider. The slashed amount
90+
/// is the penalty fee of the quote. Depending on the reward percentage, the punisher
91+
/// will receive a reward. The rest of the penalty fee remains in the contract.
92+
/// @param punisher The address of the punisher
93+
/// @param quote The quote of the peg in collateral
94+
/// @param quoteHash The hash of the quote
95+
/// @dev This function requires the COLLATERAL_SLASHER role
3296
function slashPegInCollateral(
3397
address punisher,
3498
Quotes.PegInQuote calldata quote,
3599
bytes32 quoteHash
36100
) external;
101+
102+
/// @notice Adds peg out collateral to an account
103+
/// @param addr The address of the account
104+
/// @dev This function requires the COLLATERAL_ADDER role
37105
function addPegOutCollateralTo(address addr) external payable;
106+
107+
/// @notice Adds peg out collateral to the caller
108+
/// @dev This function can only be called by a liquidity provider. This means an
109+
/// account that has been registered by adding collateral with addPegOutCollateralTo.
110+
/// This means the COLLATERAL_ADDER can't use this function unless they register themselves.
38111
function addPegOutCollateral() external payable;
112+
113+
/// @notice Slashes peg out collateral from a liquidity provider. The slashed amount
114+
/// is the penalty fee of the quote. Depending on the reward percentage, the punisher
115+
/// will receive a reward. The rest of the penalty fee remains in the contract.
116+
/// @param punisher The address of the punisher
117+
/// @param quote The quote of the peg out collateral
118+
/// @param quoteHash The hash of the quote
119+
/// @dev This function requires the COLLATERAL_SLASHER role
39120
function slashPegOutCollateral(
40121
address punisher,
41122
Quotes.PegOutQuote calldata quote,
42123
bytes32 quoteHash
43124
) external;
125+
126+
/// @notice Withdraws rewards from the contract. This implies that the caller has
127+
/// been a punisher at some point in time.
44128
function withdrawRewards() external;
129+
130+
/// @notice Withdraws collateral from the contract. This requires the liquidity provider
131+
/// to have resigned and the resignation delay to have passed.
45132
function withdrawCollateral() external;
133+
134+
/// @notice Resigns a liquidity provider
46135
function resign() external;
47136

137+
/// @notice Gets the peg in collateral of an account
138+
/// @param addr The address of the account
139+
/// @return The amount of peg in collateral
48140
function getPegInCollateral(address addr) external view returns (uint256);
141+
142+
/// @notice Gets the peg out collateral of an account
143+
/// @param addr The address of the account
144+
/// @return The amount of peg out collateral
49145
function getPegOutCollateral(address addr) external view returns (uint256);
146+
147+
/// @notice Gets the block number at which a liquidity provider resigned
50148
function getResignationBlock(address addr) external view returns (uint256);
149+
150+
/// @notice Gets the reward percentage from the penalty fee that the punisher will receive
151+
/// @return The reward percentage
51152
function getRewardPercentage() external view returns (uint256);
153+
154+
/// @notice Gets the resignation delay in blocks
155+
/// @return The resignation delay in blocks
52156
function getResignDelayInBlocks() external view returns (uint256);
157+
158+
/// @notice Gets the minimum collateral **per operation** required for a liquidity provider
159+
/// @return The minimum collateral required for a liquidity provider
53160
function getMinCollateral() external view returns (uint256);
161+
162+
/// @notice Checks if an account is registered this means having added collateral to the
163+
/// contract and not having resigned
164+
/// @param providerType The type of provider
165+
/// @param addr The address of the account
54166
function isRegistered(Flyover.ProviderType providerType, address addr) external view returns (bool);
167+
168+
/// @notice Checks if an account has sufficient collateral. This means having at least the minimum collateral
169+
/// for that operation and not having resigned.
170+
/// @param providerType The type of provider
171+
/// @param addr The address of the account
55172
function isCollateralSufficient(Flyover.ProviderType providerType, address addr) external view returns (bool);
173+
174+
/// @notice Gets the rewards of an account accumulated from the punishments
175+
/// @param addr The address of the account
176+
/// @return The rewards of the account
56177
function getRewards(address addr) external view returns (uint256);
178+
179+
/// @notice Gets the total penalties stored in the contract from the penalized liquidity providers
180+
/// @return The penalties amount
57181
function getPenalties() external view returns (uint256);
58182
}

0 commit comments

Comments
 (0)