@gearbox-protocol/periphery-v3 1.7.0-next.11 → 1.7.0-next.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,44 @@
1
+ // SPDX-License-Identifier: BUSL-1.1
2
+ // Gearbox Protocol. Generalized leverage for DeFi protocols
3
+ // (c) Gearbox Holdings, 2024
4
+ pragma solidity ^0.8.17;
5
+
6
+ import {IStateSerializer} from "../interfaces/IStateSerializer.sol";
7
+ import {ContractAdapter} from "../types/CreditManagerState.sol";
8
+ import {ICreditManagerV3} from "@gearbox-protocol/core-v3/contracts/interfaces/ICreditManagerV3.sol";
9
+ import {ICreditConfiguratorV3} from "@gearbox-protocol/core-v3/contracts/interfaces/ICreditConfiguratorV3.sol";
10
+ import {IAdapter} from "@gearbox-protocol/core-v3/contracts/interfaces/base/IAdapter.sol";
11
+
12
+ /// @title Adapter compressor 3.0.
13
+ /// @notice Collects data from different adapter types
14
+ contract AdaptgerCompressorV3 {
15
+ // Contract version
16
+ uint256 public constant version = 3_10;
17
+
18
+ function getContractAdapters(address creditManager) external view returns (ContractAdapter[] memory adapters) {
19
+ ICreditConfiguratorV3 creditConfigurator =
20
+ ICreditConfiguratorV3(ICreditManagerV3(creditManager).creditConfigurator());
21
+
22
+ address[] memory allowedAdapters = creditConfigurator.allowedAdapters();
23
+ uint256 len = allowedAdapters.length;
24
+
25
+ adapters = new ContractAdapter[](len);
26
+ bytes memory stateSerialised;
27
+
28
+ unchecked {
29
+ for (uint256 i = 0; i < len; ++i) {
30
+ address allowedAdapter = allowedAdapters[i];
31
+
32
+ /// add try{} catch for serialisation
33
+
34
+ adapters[i] = ContractAdapter({
35
+ targetContract: ICreditManagerV3(creditManager).adapterToContract(allowedAdapter),
36
+ adapter: allowedAdapter,
37
+ adapterType: uint8(IAdapter(allowedAdapter)._gearboxAdapterType()),
38
+ version: IAdapter(allowedAdapter)._gearboxAdapterVersion(),
39
+ stateSerialised: stateSerialised
40
+ });
41
+ }
42
+ }
43
+ }
44
+ }
@@ -0,0 +1,175 @@
1
+ // SPDX-License-Identifier: BUSL-1.1
2
+ // Gearbox Protocol. Generalized leverage for DeFi protocols
3
+ // (c) Gearbox Holdings, 2023
4
+ pragma solidity ^0.8.10;
5
+ pragma experimental ABIEncoderV2;
6
+
7
+ import {PoolV3} from "@gearbox-protocol/core-v3/contracts/pool/PoolV3.sol";
8
+ import {IPoolQuotaKeeperV3} from "@gearbox-protocol/core-v3/contracts/interfaces/IPoolQuotaKeeperV3.sol";
9
+ import {IGaugeV3} from "@gearbox-protocol/core-v3/contracts/interfaces/IGaugeV3.sol";
10
+
11
+ import {PoolState, CreditManagerDebtParams} from "../types/PoolState.sol";
12
+ import {PoolQuotaKeeperState, QuotaTokenParams} from "../types/PoolQuotaKeeperState.sol";
13
+ import {RateKeeperState, Rate} from "../types/RateKeeperState.sol";
14
+ import {PERCENTAGE_FACTOR, RAY} from "@gearbox-protocol/core-v3/contracts/libraries/Constants.sol";
15
+ import {IRateKeeper} from "@gearbox-protocol/core-v3/contracts/interfaces/base/IRateKeeper.sol";
16
+
17
+ import {RateKeeperType} from "../types/Enums.sol";
18
+ // Serializers
19
+ import {GaugeSerializer} from "../serializers/pool/GaugeSerializer.sol";
20
+
21
+ /// @title Pool compressor
22
+ /// @notice Collects data from pool related contracts
23
+ /// Do not use for data from data compressor for state-changing functions
24
+ contract PoolCompressorV3 {
25
+ // Contract version
26
+ uint256 public constant version = 3_10;
27
+
28
+ address public immutable gaugeSerializer;
29
+
30
+ constructor() {
31
+ gaugeSerializer = address(new GaugeSerializer());
32
+ }
33
+
34
+ function getPoolState(address pool) public view returns (PoolState memory result) {
35
+ PoolV3 _pool = PoolV3(pool);
36
+ //
37
+ // CONTRACT PARAMETERS
38
+ //
39
+ // address addr;
40
+ result.addr = pool;
41
+ // uint16 poolType;
42
+ // uint256 version;
43
+ result.version = _pool.version();
44
+
45
+ //
46
+ // ERC20 Properties
47
+ //
48
+ // string symbol;
49
+ result.symbol = _pool.symbol();
50
+ // string name;
51
+ result.name = _pool.name();
52
+ // uint8 decimals;
53
+ result.decimals = _pool.decimals();
54
+ // uint256 totalSupply;
55
+ result.totalSupply = _pool.totalSupply();
56
+
57
+ // // connected contracts
58
+
59
+ // address poolQuotaKeeper;
60
+ result.poolQuotaKeeper = _pool.poolQuotaKeeper();
61
+ // address interestRateModel;
62
+ result.interestRateModel = _pool.interestRateModel();
63
+ //
64
+ // address underlying;
65
+ result.underlying = _pool.underlyingToken();
66
+
67
+ // uint256 availableLiquidity;
68
+ result.availableLiquidity = _pool.availableLiquidity();
69
+ // uint256 expectedLiquidity;
70
+ result.expectedLiquidity = _pool.expectedLiquidity();
71
+ // //
72
+ // uint256 baseInterestIndex;
73
+ result.baseInterestIndex = _pool.baseInterestIndex();
74
+ // uint256 baseInterestRate;
75
+ result.baseInterestRate = _pool.baseInterestRate();
76
+ // uint256 dieselRate_RAY;
77
+ result.dieselRate_RAY = _pool.convertToAssets(RAY);
78
+ // uint256 totalBorrowed;
79
+ result.totalBorrowed = _pool.totalBorrowed();
80
+ // uint256 totalDebtLimit;
81
+ result.totalDebtLimit = _pool.totalDebtLimit();
82
+ // uint256 totalAssets;
83
+ result.totalAssets = _pool.totalAssets();
84
+
85
+ // uint256 supplyRate;
86
+ result.supplyRate = _pool.supplyRate();
87
+ // uint256 withdrawFee;
88
+ result.withdrawFee = _pool.withdrawFee();
89
+ // uint256 lastBaseInterestUpdate;
90
+ result.lastBaseInterestUpdate = _pool.lastBaseInterestUpdate();
91
+ // uint256 baseInterestIndexLU;
92
+ result.baseInterestIndexLU = _pool.baseInterestIndexLU();
93
+
94
+ // CreditManagerDebtParams[] creditManagerDebtParams;
95
+ address[] memory creditManagers = _pool.creditManagers();
96
+ uint256 len = creditManagers.length;
97
+ result.creditManagerDebtParams = new CreditManagerDebtParams[](len);
98
+
99
+ unchecked {
100
+ for (uint256 i; i < len; ++i) {
101
+ address creditManager = creditManagers[i];
102
+ result.creditManagerDebtParams[i] = CreditManagerDebtParams({
103
+ creditManager: creditManager,
104
+ borrowed: _pool.creditManagerBorrowed(creditManager),
105
+ limit: _pool.creditManagerDebtLimit(creditManager),
106
+ availableToBorrow: _pool.creditManagerBorrowable(creditManager)
107
+ });
108
+ }
109
+ }
110
+ // bool isPaused;
111
+ result.isPaused = _pool.paused();
112
+ }
113
+
114
+ function getPoolQuotaKeeperState(address pqk) external view returns (PoolQuotaKeeperState memory result) {
115
+ IPoolQuotaKeeperV3 _pqk = IPoolQuotaKeeperV3(pqk);
116
+
117
+ result.addr = pqk;
118
+
119
+ // address version;
120
+ result.version = _pqk.version();
121
+ // address rateKeeper;
122
+ result.rateKeeper = _pqk.gauge();
123
+ // QuotaTokenParams[] quotas;
124
+ address[] memory quotaTokens = _pqk.quotedTokens();
125
+ uint256 len = quotaTokens.length;
126
+ result.quotas = new QuotaTokenParams[](len);
127
+ unchecked {
128
+ for (uint256 i; i < len; ++i) {
129
+ result.quotas[i].token = quotaTokens[i];
130
+ (
131
+ result.quotas[i].rate,
132
+ ,
133
+ result.quotas[i].quotaIncreaseFee,
134
+ result.quotas[i].totalQuoted,
135
+ result.quotas[i].limit,
136
+ result.quotas[i].isActive
137
+ ) = _pqk.getTokenQuotaParams(quotaTokens[i]);
138
+ }
139
+ }
140
+
141
+ // address[] creditManagers;
142
+
143
+ // uint40 lastQuotaRateUpdate;
144
+ result.lastQuotaRateUpdate = _pqk.lastQuotaRateUpdate();
145
+ }
146
+
147
+ function getRateKeeperState(address rateKeeper) external view returns (RateKeeperState memory result) {
148
+ IRateKeeper _rateKeeper = IRateKeeper(rateKeeper);
149
+
150
+ IPoolQuotaKeeperV3 _pqk;
151
+
152
+ result.addr = rateKeeper;
153
+
154
+ result.version = _rateKeeper.version();
155
+ if (result.version == 3_00) {
156
+ result.rateKeeperType = uint16(RateKeeperType.Gauge);
157
+ _pqk = IPoolQuotaKeeperV3(PoolV3(IGaugeV3(rateKeeper).pool()).poolQuotaKeeper());
158
+ result.serialisedData = GaugeSerializer(gaugeSerializer).serialize(rateKeeper);
159
+ } else {
160
+ // resuilt.rateKeeperType = uint16(_rateKeeper.rateKeeperType());
161
+ // _pqk = IPoolQuotaKeeperV3(IGaugeV3(rateKeeper).quotaKeeper());
162
+ }
163
+
164
+ address[] memory quotaTokens = _pqk.quotedTokens();
165
+ uint256 quotaTokensLen = quotaTokens.length;
166
+
167
+ uint16[] memory rawRates = _rateKeeper.getRates(quotaTokens);
168
+ result.rates = new Rate[](quotaTokensLen);
169
+
170
+ for (uint256 i; i < quotaTokensLen; ++i) {
171
+ result.rates[i].token = quotaTokens[i];
172
+ result.rates[i].rate = rawRates[i];
173
+ }
174
+ }
175
+ }
@@ -6,7 +6,7 @@ pragma solidity ^0.8.17;
6
6
  import {AddressIsNotContractException} from "@gearbox-protocol/core-v3/contracts/interfaces/IExceptions.sol";
7
7
  import {IPriceOracleV3, PriceFeedParams} from "@gearbox-protocol/core-v3/contracts/interfaces/IPriceOracleV3.sol";
8
8
  import {IPriceFeed, IUpdatablePriceFeed} from "@gearbox-protocol/core-v3/contracts/interfaces/base/IPriceFeed.sol";
9
- import {IVersion} from "@gearbox-protocol/core-v3/contracts/interfaces/base/IVersion.sol";
9
+ import {IPriceFeedCompressor} from "../interfaces/IPriceFeedCompressor.sol";
10
10
  import {PriceFeedType} from "@gearbox-protocol/sdk-gov/contracts/PriceFeedType.sol";
11
11
 
12
12
  import {IStateSerializerLegacy} from "../interfaces/IStateSerializerLegacy.sol";
@@ -17,7 +17,7 @@ import {BPTWeightedPriceFeedSerializer} from "../serializers/oracles/BPTWeighted
17
17
  import {LPPriceFeedSerializer} from "../serializers/oracles/LPPriceFeedSerializer.sol";
18
18
  import {PythPriceFeedSerializer} from "../serializers/oracles/PythPriceFeedSerializer.sol";
19
19
  import {RedstonePriceFeedSerializer} from "../serializers/oracles/RedstonePriceFeedSerializer.sol";
20
- import {PriceFeedAnswer, PriceFeedMapEntry, PriceFeedTreeNode} from "./Types.sol";
20
+ import {PriceFeedAnswer, PriceFeedMapEntry, PriceFeedTreeNode} from "../types/PriceOracleState.sol";
21
21
 
22
22
  interface ImplementsPriceFeedType {
23
23
  /// @dev Annotates `priceFeedType` as `uint8` instead of `PriceFeedType` enum to support future types
@@ -35,7 +35,7 @@ interface IPriceOracleV3Legacy {
35
35
  /// @title Price feed compressor
36
36
  /// @notice Allows to fetch all useful data from price oracle in a single call
37
37
  /// @dev The contract is not gas optimized and is thus not recommended for on-chain use
38
- contract PriceFeedCompressor is IVersion {
38
+ contract PriceFeedCompressor is IPriceFeedCompressor {
39
39
  using NestedPriceFeeds for IPriceFeed;
40
40
 
41
41
  /// @notice Contract version
@@ -45,9 +45,6 @@ contract PriceFeedCompressor is IVersion {
45
45
  /// @dev Serializers only apply to feeds that don't implement `IStateSerializer` themselves
46
46
  mapping(uint8 => address) public serializers;
47
47
 
48
- /// @notice Emitted when new state serializer is set for a given price feed type
49
- event SetSerializer(uint8 indexed priceFeedType, address indexed serializer);
50
-
51
48
  /// @notice Constructor
52
49
  /// @dev Sets serializers for existing price feed types.
53
50
  /// It is recommended to implement `IStateSerializer` in new price feeds.
@@ -82,6 +79,7 @@ contract PriceFeedCompressor is IVersion {
82
79
  function getPriceFeeds(address priceOracle)
83
80
  external
84
81
  view
82
+ override
85
83
  returns (PriceFeedMapEntry[] memory priceFeedMap, PriceFeedTreeNode[] memory priceFeedTree)
86
84
  {
87
85
  address[] memory tokens = IPriceOracleV3(priceOracle).getTokens();
@@ -92,6 +90,7 @@ contract PriceFeedCompressor is IVersion {
92
90
  function getPriceFeeds(address priceOracle, address[] memory tokens)
93
91
  public
94
92
  view
93
+ override
95
94
  returns (PriceFeedMapEntry[] memory priceFeedMap, PriceFeedTreeNode[] memory priceFeedTree)
96
95
  {
97
96
  uint256 numTokens = tokens.length;
@@ -130,6 +129,30 @@ contract PriceFeedCompressor is IVersion {
130
129
  }
131
130
  }
132
131
 
132
+ function loadPriceFeedTree(address[] memory priceFeeds)
133
+ external
134
+ view
135
+ override
136
+ returns (PriceFeedTreeNode[] memory priceFeedTree)
137
+ {
138
+ uint256 len = priceFeeds.length;
139
+ uint256 priceFeedTreeSize;
140
+
141
+ for (uint256 i; i < len; ++i) {
142
+ priceFeedTreeSize += _getPriceFeedTreeSize(priceFeeds[i]);
143
+ }
144
+
145
+ priceFeedTree = new PriceFeedTreeNode[](priceFeedTreeSize);
146
+ uint256 offset;
147
+ for (uint256 i; i < len; ++i) {
148
+ offset = _loadPriceFeedTree(priceFeeds[i], priceFeedTree, offset);
149
+ }
150
+ // trim array to its actual size in case there were duplicates
151
+ assembly {
152
+ mstore(priceFeedTree, offset)
153
+ }
154
+ }
155
+
133
156
  // --------- //
134
157
  // INTERNALS //
135
158
  // --------- //
@@ -191,6 +214,7 @@ contract PriceFeedCompressor is IVersion {
191
214
  function _getPriceFeedTreeNode(address priceFeed) internal view returns (PriceFeedTreeNode memory data) {
192
215
  data.priceFeed = priceFeed;
193
216
  data.decimals = IPriceFeed(priceFeed).decimals();
217
+ data.version = IPriceFeed(priceFeed).version();
194
218
 
195
219
  try ImplementsPriceFeedType(priceFeed).priceFeedType() returns (uint8 priceFeedType) {
196
220
  data.priceFeedType = priceFeedType;
@@ -55,7 +55,7 @@ struct CreditAccountFilter {
55
55
  bool reverting;
56
56
  }
57
57
 
58
- /// @notice Credit manager filters
58
+ // @notice Credit manager filters
59
59
  /// @param curator If set, match credit managers managed by given curator
60
60
  /// @param pool If set, match credit managers connected to a given pool
61
61
  /// @param underlying If set, match credit managers with given underlying
@@ -65,46 +65,6 @@ struct CreditManagerFilter {
65
65
  address underlying;
66
66
  }
67
67
 
68
- /// @notice Price feed answer packed in a struct
69
- struct PriceFeedAnswer {
70
- int256 price;
71
- uint256 updatedAt;
72
- bool success;
73
- }
74
-
75
- /// @notice Represents an entry in the price feed map of a price oracle
76
- /// @dev `stalenessPeriod` is always 0 if price oracle's version is below `3_10`
77
- struct PriceFeedMapEntry {
78
- address token;
79
- bool reserve;
80
- address priceFeed;
81
- uint32 stalenessPeriod;
82
- }
83
-
84
- /// @notice Represents a node in the price feed "tree"
85
- /// @param priceFeed Price feed address
86
- /// @param decimals Price feed's decimals (might not be equal to 8 for lower-level)
87
- /// @param priceFeedType Price feed type (same as `PriceFeedType` but annotated as `uint8` to support future types),
88
- /// defaults to `PriceFeedType.CHAINLINK_ORACLE`
89
- /// @param skipCheck Whether price feed implements its own staleness and sanity check, defaults to `false`
90
- /// @param updatable Whether it is an on-demand updatable (aka pull) price feed, defaults to `false`
91
- /// @param specificParams ABI-encoded params specific to this price feed type, filled if price feed implements
92
- /// `IStateSerializer` or there is a state serializer set for this type
93
- /// @param underlyingFeeds Array of underlying feeds, filled when `priceFeed` is nested
94
- /// @param underlyingStalenessPeriods Staleness periods of underlying feeds, filled when `priceFeed` is nested
95
- /// @param answer Price feed answer packed in a struct
96
- struct PriceFeedTreeNode {
97
- address priceFeed;
98
- uint8 decimals;
99
- uint8 priceFeedType;
100
- bool skipCheck;
101
- bool updatable;
102
- bytes specificParams;
103
- address[] underlyingFeeds;
104
- uint32[] underlyingStalenessPeriods;
105
- PriceFeedAnswer answer;
106
- }
107
-
108
68
  /// @notice Info on credit account's holdings of a token
109
69
  /// @param token Token address
110
70
  /// @param mask Token mask in the credit manager
@@ -55,13 +55,13 @@ import {
55
55
 
56
56
  // EXCEPTIONS
57
57
  import "@gearbox-protocol/core-v3/contracts/interfaces/IExceptions.sol";
58
- import {LinearInterestModelHelper} from "./LinearInterestModelHelper.sol";
58
+
59
59
  import {IZapperRegister} from "../interfaces/IZapperRegister.sol";
60
60
 
61
61
  /// @title Data compressor 3.0.
62
62
  /// @notice Collects data from various contracts for use in the dApp
63
63
  /// Do not use for data from data compressor for state-changing functions
64
- contract DataCompressorV3 is IDataCompressorV3, ContractsRegisterTrait, LinearInterestModelHelper {
64
+ contract DataCompressorV3 is IDataCompressorV3, ContractsRegisterTrait {
65
65
  // Contract version
66
66
  uint256 public constant version = 3_00;
67
67
 
@@ -328,7 +328,7 @@ contract DataCompressorV3 is IDataCompressorV3, ContractsRegisterTrait, LinearIn
328
328
 
329
329
  result.baseBorrowRate = _getBaseInterestRate(address(pool));
330
330
  result.availableToBorrow = pool.creditManagerBorrowable(_cm);
331
- result.lirm = _getInterestRateModel(address(pool));
331
+ // result.lirm = _getInterestRateModel(address(pool));
332
332
  }
333
333
 
334
334
  (result.minDebt, result.maxDebt) = creditFacade.debtLimits();
@@ -431,7 +431,7 @@ contract DataCompressorV3 is IDataCompressorV3, ContractsRegisterTrait, LinearIn
431
431
  result.version = _getVersion(address(pool));
432
432
 
433
433
  result.quotas = _getQuotas(_pool);
434
- result.lirm = _getInterestRateModel(_pool);
434
+ // result.lirm = _getInterestRateModel(_pool);
435
435
  result.isPaused = _getPaused(_pool);
436
436
 
437
437
  // Adding zappers
@@ -559,9 +559,9 @@ contract DataCompressorV3 is IDataCompressorV3, ContractsRegisterTrait, LinearIn
559
559
  name = _getName(token);
560
560
  }
561
561
 
562
- function _getInterestRateModel(address pool) internal view returns (LinearModel memory) {
563
- return getLIRMData(IPoolV3(pool).interestRateModel());
564
- }
562
+ // function _getInterestRateModel(address pool) internal view returns (LinearModel memory) {
563
+ // return address(0); //getLIRMData(IPoolV3(pool).interestRateModel());
564
+ // }
565
565
 
566
566
  function _getQuotedTokens(IPoolQuotaKeeperV3 pqk) internal view returns (address[] memory result) {
567
567
  result = pqk.quotedTokens();
@@ -0,0 +1,28 @@
1
+ // SPDX-License-Identifier: BUSL-1.1
2
+ // Gearbox Protocol. Generalized leverage for DeFi protocols
3
+ // (c) Gearbox Holdings, 2023
4
+ pragma solidity ^0.8.10;
5
+
6
+ import {PriceFeedAnswer, PriceFeedMapEntry, PriceFeedTreeNode} from "../types/PriceOracleState.sol";
7
+
8
+ import {IVersion} from "@gearbox-protocol/core-v3/contracts/interfaces/base/IVersion.sol";
9
+
10
+ interface IPriceFeedCompressor is IVersion {
11
+ /// @notice Emitted when new state serializer is set for a given price feed type
12
+ event SetSerializer(uint8 indexed priceFeedType, address indexed serializer);
13
+
14
+ function getPriceFeeds(address priceOracle)
15
+ external
16
+ view
17
+ returns (PriceFeedMapEntry[] memory priceFeedMap, PriceFeedTreeNode[] memory priceFeedTree);
18
+
19
+ function getPriceFeeds(address priceOracle, address[] memory tokens)
20
+ external
21
+ view
22
+ returns (PriceFeedMapEntry[] memory priceFeedMap, PriceFeedTreeNode[] memory priceFeedTree);
23
+
24
+ function loadPriceFeedTree(address[] memory priceFeeds)
25
+ external
26
+ view
27
+ returns (PriceFeedTreeNode[] memory priceFeedTree);
28
+ }