Skip to content

Commit cfa38c9

Browse files
authored
Merge pull request #7 from Gearbox-protocol/optimising-curve
refactor: Optimising curve
2 parents d7d918a + 1914bc4 commit cfa38c9

19 files changed

+602
-493
lines changed

contracts/oracles/CompositePriceFeed.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ contract CompositePriceFeed is PriceFeedChecker, AggregatorV3Interface, IPriceFe
7272
override
7373
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound)
7474
{
75-
(uint80 roundId0, int256 answer0, uint256 startedAt0, uint256 updatedAt0, uint80 answeredInRound0) =
75+
(uint80 roundId0, int256 answer0,, uint256 updatedAt0, uint80 answeredInRound0) =
7676
targetToBasePriceFeed.latestRoundData();
7777

7878
_checkAnswer(roundId0, answer0, updatedAt0, answeredInRound0);

contracts/oracles/aave/AavePriceFeed.sol renamed to contracts/oracles/aave/WrappedAaveV2PriceFeed.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {ZeroAddressException} from "@gearbox-protocol/core-v2/contracts/interfac
1818
uint256 constant RANGE_WIDTH = 200; // 2%
1919

2020
/// @title Aave V2 wrapped aToken price feed
21-
contract AavePriceFeed is LPPriceFeed {
21+
contract WrappedAaveV2PriceFeed is LPPriceFeed {
2222
/// @dev Chainlink price feed for the aToken's underlying token
2323
AggregatorV3Interface public immutable priceFeed;
2424

@@ -28,7 +28,7 @@ contract AavePriceFeed is LPPriceFeed {
2828
/// @dev Scale of the waToken's exchange rate
2929
uint256 public constant decimalsDivider = WAD;
3030

31-
PriceFeedType public constant override priceFeedType = PriceFeedType.AAVE_ORACLE;
31+
PriceFeedType public constant override priceFeedType = PriceFeedType.WRAPPED_AAVE_V2_ORACLE;
3232
uint256 public constant override version = 1;
3333

3434
/// @dev Whether to skip price sanity checks.

contracts/oracles/compound/CompoundPriceFeed.sol renamed to contracts/oracles/compound/CompoundV2PriceFeed.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {ZeroAddressException} from "@gearbox-protocol/core-v2/contracts/interfac
1717
uint256 constant RANGE_WIDTH = 200; // 2%
1818

1919
/// @title Compound V2 cToken price feed
20-
contract CompoundPriceFeed is LPPriceFeed {
20+
contract CompoundV2PriceFeed is LPPriceFeed {
2121
/// @dev Chainlink price feed for the underlying token
2222
AggregatorV3Interface public immutable priceFeed;
2323

@@ -27,7 +27,7 @@ contract CompoundPriceFeed is LPPriceFeed {
2727
/// @dev Scale of the cToken's exchangeRate
2828
uint256 public constant decimalsDivider = WAD;
2929

30-
PriceFeedType public constant override priceFeedType = PriceFeedType.COMPOUND_ORACLE;
30+
PriceFeedType public constant override priceFeedType = PriceFeedType.COMPOUND_V2_ORACLE;
3131
uint256 public constant override version = 1;
3232

3333
/// @dev Whether to skip price sanity checks.

contracts/oracles/curve/CurveLP2PriceFeed.sol

Lines changed: 0 additions & 77 deletions
This file was deleted.

contracts/oracles/curve/CurveLP3PriceFeed.sol

Lines changed: 0 additions & 97 deletions
This file was deleted.

contracts/oracles/curve/CurveLP4PriceFeed.sol

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,23 @@ import {PriceFeedType} from "@gearbox-protocol/sdk/contracts/PriceFeedType.sol";
1010

1111
// EXCEPTIONS
1212
import {
13-
ZeroAddressException, NotImplementedException
14-
} from "@gearbox-protocol/core-v2/contracts/interfaces/IErrors.sol";
13+
ZeroAddressException,
14+
NotImplementedException
15+
} from "@gearbox-protocol/core-v3/contracts/interfaces/IExceptions.sol";
1516

1617
/// @title CurveLP pricefeed for 4 assets
1718
contract CurveLP4PriceFeed is AbstractCurveLPPriceFeed {
1819
/// @dev Price feed of coin 0 in the pool
19-
AggregatorV3Interface public immutable priceFeed1;
20+
address public immutable priceFeed1;
2021

2122
/// @dev Price feed of coin 1 in the pool
22-
AggregatorV3Interface public immutable priceFeed2;
23+
address public immutable priceFeed2;
2324

2425
/// @dev Price feed of coin 2 in the pool
25-
AggregatorV3Interface public immutable priceFeed3;
26+
address public immutable priceFeed3;
2627

2728
/// @dev Price feed of coin 3 in the pool
28-
AggregatorV3Interface public immutable priceFeed4;
29+
address public immutable priceFeed4;
2930

3031
PriceFeedType public constant override priceFeedType = PriceFeedType.CURVE_4LP_ORACLE;
3132

@@ -38,15 +39,12 @@ contract CurveLP4PriceFeed is AbstractCurveLPPriceFeed {
3839
address _priceFeed4,
3940
string memory _description
4041
) AbstractCurveLPPriceFeed(addressProvider, _curvePool, _description) {
41-
if (
42-
_priceFeed1 == address(0) || _priceFeed2 == address(0) || _priceFeed3 == address(0)
43-
|| _priceFeed4 == address(0)
44-
) revert ZeroAddressException();
45-
46-
priceFeed1 = AggregatorV3Interface(_priceFeed1); // F:[OCLP-1]
47-
priceFeed2 = AggregatorV3Interface(_priceFeed2); // F:[OCLP-1]
48-
priceFeed3 = AggregatorV3Interface(_priceFeed3); // F:[OCLP-1]
49-
priceFeed4 = AggregatorV3Interface(_priceFeed4); // F:[OCLP-1]
42+
if (_priceFeed1 == address(0) || _priceFeed2 == address(0)) revert ZeroAddressException();
43+
44+
priceFeed1 = _priceFeed1; // F:[OCLP-1]
45+
priceFeed2 = _priceFeed2; // F:[OCLP-1]
46+
priceFeed3 = _priceFeed3; // F:[OCLP-1]
47+
priceFeed4 = _priceFeed4; // F:[OCLP-1]
5048
}
5149

5250
/// @dev Returns the USD price of the pool's LP token
@@ -57,19 +55,35 @@ contract CurveLP4PriceFeed is AbstractCurveLPPriceFeed {
5755
view
5856
override
5957
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound)
58+
{
59+
(roundId, answer, startedAt, updatedAt, answeredInRound) = getAnswer(); // F:[OCLP-2]
60+
61+
uint256 virtualPrice = curvePool.get_virtual_price();
62+
63+
// Checks that virtual_priceis in limits
64+
virtualPrice = _checkAndUpperBoundValue(virtualPrice); // F: [OCLP-7]
65+
66+
answer = (answer * int256(virtualPrice)) / decimalsDivider; // F:[OCLP-4]
67+
}
68+
69+
function getAnswer()
70+
internal
71+
view
72+
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound)
6073
{
6174
uint80 roundIdA;
6275
int256 answerA;
6376
uint256 startedAtA;
6477
uint256 updatedAtA;
6578
uint80 answeredInRoundA;
6679

67-
(roundId, answer, startedAt, updatedAt, answeredInRound) = priceFeed1.latestRoundData(); // F:[OCLP-6]
80+
(roundId, answer, startedAt, updatedAt, answeredInRound) = AggregatorV3Interface(priceFeed1).latestRoundData(); // F:[OCLP-6]
6881

6982
// Sanity check for chainlink pricefeed
7083
_checkAnswer(roundId, answer, updatedAt, answeredInRound);
7184

72-
(roundIdA, answerA, startedAtA, updatedAtA, answeredInRoundA) = priceFeed2.latestRoundData(); // F:[OCLP-6]
85+
(roundIdA, answerA, startedAtA, updatedAtA, answeredInRoundA) =
86+
AggregatorV3Interface(priceFeed2).latestRoundData(); // F:[OCLP-6]
7387

7488
// Sanity check for chainlink pricefeed
7589
_checkAnswer(roundIdA, answerA, updatedAtA, answeredInRoundA);
@@ -82,7 +96,12 @@ contract CurveLP4PriceFeed is AbstractCurveLPPriceFeed {
8296
answeredInRound = answeredInRoundA;
8397
} // F:[OCLP-6]
8498

85-
(roundIdA, answerA, startedAtA, updatedAtA, answeredInRoundA) = priceFeed3.latestRoundData(); // F:[OCLP-6]
99+
if (priceFeed3 == address(0)) {
100+
return (roundId, answer, startedAt, updatedAt, answeredInRound);
101+
}
102+
103+
(roundIdA, answerA, startedAtA, updatedAtA, answeredInRoundA) =
104+
AggregatorV3Interface(priceFeed3).latestRoundData(); // F:[OCLP-6]
86105

87106
// Sanity check for chainlink pricefeed
88107
_checkAnswer(roundIdA, answerA, updatedAtA, answeredInRoundA);
@@ -95,7 +114,12 @@ contract CurveLP4PriceFeed is AbstractCurveLPPriceFeed {
95114
answeredInRound = answeredInRoundA;
96115
} // F:[OCLP-6]
97116

98-
(roundIdA, answerA, startedAtA, updatedAtA, answeredInRoundA) = priceFeed4.latestRoundData(); // F:[OCLP-6]
117+
if (priceFeed4 == address(0)) {
118+
return (roundId, answer, startedAt, updatedAt, answeredInRound);
119+
}
120+
121+
(roundIdA, answerA, startedAtA, updatedAtA, answeredInRoundA) =
122+
AggregatorV3Interface(priceFeed4).latestRoundData(); // F:[OCLP-6]
99123

100124
// Sanity check for chainlink pricefeed
101125
_checkAnswer(roundIdA, answerA, updatedAtA, answeredInRoundA);
@@ -107,12 +131,5 @@ contract CurveLP4PriceFeed is AbstractCurveLPPriceFeed {
107131
updatedAt = updatedAtA;
108132
answeredInRound = answeredInRoundA;
109133
} // F:[OCLP-6]
110-
111-
uint256 virtualPrice = curvePool.get_virtual_price();
112-
113-
// Checks that virtual_priceis in limits
114-
virtualPrice = _checkAndUpperBoundValue(virtualPrice); // F: [OCLP-7]
115-
116-
answer = (answer * int256(virtualPrice)) / decimalsDivider; // F:[OCLP-4]
117134
}
118135
}

contracts/oracles/redstone/RedstonePriceFeed.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ contract RedstonePriceFeed is
195195
/// - A timestamp expected to be in all Redstone data packages
196196
/// - Redstone payload with price update
197197
function updatePrice(bytes calldata data) external {
198-
(uint256 expectedPayloadTimestamp, bytes memory payload) = abi.decode(data, (uint256, bytes));
198+
(uint256 expectedPayloadTimestamp,) = abi.decode(data, (uint256, bytes));
199199

200200
// We want to minimize price update execution, in case, e.g., when several users submit
201201
// the same price update in a short span of time. So only updates with a larger payload timestamp

0 commit comments

Comments
 (0)