generated from bgd-labs/bgd-forge-template
-
Notifications
You must be signed in to change notification settings - Fork 10
/
PriceCapAdapterStable.sol
88 lines (68 loc) · 2.49 KB
/
PriceCapAdapterStable.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.19;
import {IPriceCapAdapterStable, ICLSynchronicityPriceAdapter, IACLManager, IChainlinkAggregator} from '../interfaces/IPriceCapAdapterStable.sol';
/**
* @title PriceCapAdapterStable
* @author BGD Labs
* @notice Price adapter to cap the price of the USD pegged assets
*/
contract PriceCapAdapterStable is IPriceCapAdapterStable {
/// @inheritdoc IPriceCapAdapterStable
IChainlinkAggregator public immutable ASSET_TO_USD_AGGREGATOR;
/// @inheritdoc IPriceCapAdapterStable
IACLManager public immutable ACL_MANAGER;
/// @inheritdoc ICLSynchronicityPriceAdapter
uint8 public decimals;
/// @inheritdoc ICLSynchronicityPriceAdapter
string public description;
int256 internal _priceCap;
/**
* @param capAdapterStableParams parameters to create stable cap adapter
*/
constructor(CapAdapterStableParams memory capAdapterStableParams) {
if (address(capAdapterStableParams.aclManager) == address(0)) {
revert ACLManagerIsZeroAddress();
}
ASSET_TO_USD_AGGREGATOR = capAdapterStableParams.assetToUsdAggregator;
ACL_MANAGER = capAdapterStableParams.aclManager;
description = capAdapterStableParams.adapterDescription;
decimals = ASSET_TO_USD_AGGREGATOR.decimals();
_setPriceCap(capAdapterStableParams.priceCap);
}
/// @inheritdoc ICLSynchronicityPriceAdapter
function latestAnswer() external view override returns (int256) {
int256 basePrice = ASSET_TO_USD_AGGREGATOR.latestAnswer();
int256 priceCap = _priceCap;
if (basePrice > priceCap) {
return priceCap;
}
return basePrice;
}
/// @inheritdoc IPriceCapAdapterStable
function setPriceCap(int256 priceCap) external {
if (!ACL_MANAGER.isRiskAdmin(msg.sender) && !ACL_MANAGER.isPoolAdmin(msg.sender)) {
revert CallerIsNotRiskOrPoolAdmin();
}
_setPriceCap(priceCap);
}
/// @inheritdoc IPriceCapAdapterStable
function getPriceCap() external view returns (int256) {
return _priceCap;
}
/// @inheritdoc IPriceCapAdapterStable
function isCapped() public view virtual returns (bool) {
return (ASSET_TO_USD_AGGREGATOR.latestAnswer() > this.latestAnswer());
}
/**
* @notice Updates price cap
* @param priceCap the new price cap
*/
function _setPriceCap(int256 priceCap) internal {
int256 basePrice = ASSET_TO_USD_AGGREGATOR.latestAnswer();
if (priceCap < basePrice) {
revert CapLowerThanActualPrice();
}
_priceCap = priceCap;
emit PriceCapUpdated(priceCap);
}
}