-
Notifications
You must be signed in to change notification settings - Fork 3
/
CLSynchronicityPriceAdapterPegToBase.sol
82 lines (69 loc) · 2.57 KB
/
CLSynchronicityPriceAdapterPegToBase.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import {IChainlinkAggregator} from '../interfaces/IChainlinkAggregator.sol';
import {ICLSynchronicityPriceAdapter} from '../interfaces/ICLSynchronicityPriceAdapter.sol';
/**
* @title CLSynchronicityPriceAdapter
* @author BGD Labs
* @notice Price adapter to calculate price of (Asset / Base) pair by using
* @notice Chainlink Data Feeds for (Asset / Peg) and (Peg / Base) pairs.
* @notice For example it can be used to calculate stETH / USD
* @notice based on stETH / ETH and ETH / USD feeds.
*/
contract CLSynchronicityPriceAdapterPegToBase is ICLSynchronicityPriceAdapter {
/**
* @notice Price feed for (Base / Peg) pair
*/
IChainlinkAggregator public immutable PEG_TO_BASE;
/**
* @notice Price feed for (Asset / Peg) pair
*/
IChainlinkAggregator public immutable ASSET_TO_PEG;
/**
* @notice Number of decimals in the output of this price adapter
*/
uint8 public immutable DECIMALS;
/**
* @notice This is a parameter to bring the resulting answer with the proper precision.
* @notice will be equal to 10 to the power of the sum decimals of feeds
*/
int256 public immutable DENOMINATOR;
/**
* @notice Maximum number of resulting and feed decimals
*/
uint8 public constant MAX_DECIMALS = 18;
/**
* @param pegToBaseAggregatorAddress the address of PEG / BASE feed
* @param assetToPegAggregatorAddress the address of the ASSET / PEG feed
* @param decimals precision of the answer
*/
constructor(
address pegToBaseAggregatorAddress,
address assetToPegAggregatorAddress,
uint8 decimals
) {
PEG_TO_BASE = IChainlinkAggregator(pegToBaseAggregatorAddress);
ASSET_TO_PEG = IChainlinkAggregator(assetToPegAggregatorAddress);
if (decimals > MAX_DECIMALS) revert DecimalsAboveLimit();
if (PEG_TO_BASE.decimals() > MAX_DECIMALS) revert DecimalsAboveLimit();
if (ASSET_TO_PEG.decimals() > MAX_DECIMALS) revert DecimalsAboveLimit();
DECIMALS = decimals;
// equal to 10 to the power of the sum decimals of feeds
unchecked {
DENOMINATOR = int256(
10 ** (PEG_TO_BASE.decimals() + ASSET_TO_PEG.decimals())
);
}
}
/// @inheritdoc ICLSynchronicityPriceAdapter
function latestAnswer() public view virtual override returns (int256) {
int256 assetToPegPrice = ASSET_TO_PEG.latestAnswer();
int256 pegToBasePrice = PEG_TO_BASE.latestAnswer();
if (assetToPegPrice <= 0 || pegToBasePrice <= 0) {
return 0;
}
return
(assetToPegPrice * pegToBasePrice * int256(10 ** DECIMALS)) /
(DENOMINATOR);
}
}