forked from bgd-labs/aave-capo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PriceCapAdapterBase.sol
211 lines (169 loc) · 6.37 KB
/
PriceCapAdapterBase.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.19;
import {IPriceCapAdapter, ICLSynchronicityPriceAdapter, IACLManager, IChainlinkAggregator} from '../interfaces/IPriceCapAdapter.sol';
/**
* @title PriceCapAdapterBase
* @author BGD Labs
* @notice Price adapter to cap the price of the underlying asset.
*/
abstract contract PriceCapAdapterBase is IPriceCapAdapter {
/// @inheritdoc IPriceCapAdapter
uint256 public constant PERCENTAGE_FACTOR = 1e4;
/// @inheritdoc IPriceCapAdapter
uint256 public constant MINIMAL_RATIO_INCREASE_LIFETIME = 3;
/// @inheritdoc IPriceCapAdapter
uint256 public constant SECONDS_PER_YEAR = 365 days;
/// @inheritdoc IPriceCapAdapter
IChainlinkAggregator public immutable BASE_TO_USD_AGGREGATOR;
/// @inheritdoc IPriceCapAdapter
IACLManager public immutable ACL_MANAGER;
/// @inheritdoc IPriceCapAdapter
address public immutable RATIO_PROVIDER;
/// @inheritdoc IPriceCapAdapter
uint8 public immutable DECIMALS;
/// @inheritdoc IPriceCapAdapter
uint8 public immutable RATIO_DECIMALS;
/// @inheritdoc IPriceCapAdapter
uint48 public immutable MINIMUM_SNAPSHOT_DELAY;
/**
* @notice Description of the pair
*/
string private _description;
/**
* @notice Ratio at the time of snapshot
*/
uint104 private _snapshotRatio;
/**
* @notice Timestamp at the time of snapshot
*/
uint48 private _snapshotTimestamp;
/**
* @notice Ratio growth per second
*/
uint104 private _maxRatioGrowthPerSecond;
/**
* @notice Max yearly growth percent
*/
uint16 private _maxYearlyRatioGrowthPercent;
/**
* @param capAdapterBaseParams parameters to create adapter
*/
constructor(CapAdapterBaseParams memory capAdapterBaseParams) {
if (address(capAdapterBaseParams.aclManager) == address(0)) {
revert ACLManagerIsZeroAddress();
}
ACL_MANAGER = capAdapterBaseParams.aclManager;
BASE_TO_USD_AGGREGATOR = IChainlinkAggregator(capAdapterBaseParams.baseAggregatorAddress);
RATIO_PROVIDER = capAdapterBaseParams.ratioProviderAddress;
DECIMALS = BASE_TO_USD_AGGREGATOR.decimals();
RATIO_DECIMALS = capAdapterBaseParams.ratioDecimals;
MINIMUM_SNAPSHOT_DELAY = capAdapterBaseParams.minimumSnapshotDelay;
_description = capAdapterBaseParams.pairDescription;
_setCapParameters(capAdapterBaseParams.priceCapParams);
}
/// @inheritdoc ICLSynchronicityPriceAdapter
function description() external view returns (string memory) {
return _description;
}
/// @inheritdoc ICLSynchronicityPriceAdapter
function decimals() external view returns (uint8) {
return DECIMALS;
}
/// @inheritdoc IPriceCapAdapter
function getSnapshotRatio() external view returns (uint256) {
return _snapshotRatio;
}
/// @inheritdoc IPriceCapAdapter
function getSnapshotTimestamp() external view returns (uint256) {
return _snapshotTimestamp;
}
/// @inheritdoc IPriceCapAdapter
function getMaxYearlyGrowthRatePercent() external view returns (uint256) {
return _maxYearlyRatioGrowthPercent;
}
/// @inheritdoc IPriceCapAdapter
function getMaxRatioGrowthPerSecond() external view returns (uint256) {
return _maxRatioGrowthPerSecond;
}
/// @inheritdoc IPriceCapAdapter
function setCapParameters(PriceCapUpdateParams memory priceCapParams) external {
if (!ACL_MANAGER.isRiskAdmin(msg.sender) && !ACL_MANAGER.isPoolAdmin(msg.sender)) {
revert CallerIsNotRiskOrPoolAdmin();
}
_setCapParameters(priceCapParams);
}
/// @inheritdoc ICLSynchronicityPriceAdapter
function latestAnswer() external view override returns (int256) {
// get the current lst to underlying ratio
int256 currentRatio = getRatio();
// get the base price
int256 basePrice = BASE_TO_USD_AGGREGATOR.latestAnswer();
if (basePrice <= 0 || currentRatio <= 0) {
return 0;
}
// calculate the ratio based on snapshot ratio and max growth rate
int256 maxRatio = _getMaxRatio();
if (maxRatio < currentRatio) {
currentRatio = maxRatio;
}
// calculate the price of the underlying asset
int256 price = (basePrice * currentRatio) / int256(10 ** RATIO_DECIMALS);
return price;
}
/**
* @notice Updates price cap parameters
* @param priceCapParams parameters to set price cap
*/
function _setCapParameters(PriceCapUpdateParams memory priceCapParams) internal {
// if snapshot ratio is 0 then growth will not work as expected
if (priceCapParams.snapshotRatio == 0) {
revert SnapshotRatioIsZero();
}
// new snapshot timestamp should be gt then stored one, but not gt then timestamp of the current block
if (
_snapshotTimestamp >= priceCapParams.snapshotTimestamp ||
priceCapParams.snapshotTimestamp > block.timestamp - MINIMUM_SNAPSHOT_DELAY
) {
revert InvalidRatioTimestamp(priceCapParams.snapshotTimestamp);
}
_snapshotRatio = priceCapParams.snapshotRatio;
_snapshotTimestamp = priceCapParams.snapshotTimestamp;
_maxYearlyRatioGrowthPercent = priceCapParams.maxYearlyRatioGrowthPercent;
_maxRatioGrowthPerSecond = uint104(
(uint256(priceCapParams.snapshotRatio) * priceCapParams.maxYearlyRatioGrowthPercent) /
PERCENTAGE_FACTOR /
SECONDS_PER_YEAR
);
// if the ratio on the current growth speed can overflow less then in a MINIMAL_RATIO_INCREASE_LIFETIME years, revert
if (
uint256(_snapshotRatio) +
(_maxRatioGrowthPerSecond * SECONDS_PER_YEAR * MINIMAL_RATIO_INCREASE_LIFETIME) >
type(uint104).max
) {
revert SnapshotMayOverflowSoon(
priceCapParams.snapshotRatio,
priceCapParams.maxYearlyRatioGrowthPercent
);
}
emit CapParametersUpdated(
priceCapParams.snapshotRatio,
priceCapParams.snapshotTimestamp,
_maxRatioGrowthPerSecond,
priceCapParams.maxYearlyRatioGrowthPercent
);
}
/// @inheritdoc IPriceCapAdapter
function getRatio() public view virtual returns (int256);
/// @inheritdoc IPriceCapAdapter
function isCapped() public view returns (bool) {
// get the current lst to underlying ratio
int256 currentRatio = getRatio();
// calculate the ratio based on snapshot ratio and max growth rate
int256 maxRatio = _getMaxRatio();
return currentRatio > maxRatio;
}
function _getMaxRatio() internal view returns (int256) {
return
int256(_snapshotRatio + _maxRatioGrowthPerSecond * (block.timestamp - _snapshotTimestamp));
}
}