@gearbox-protocol/periphery-v3 1.0.5 → 1.0.7

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.
@@ -32,6 +32,9 @@ import {CreditAccountData, CreditManagerData, PoolData, TokenBalance, ContractAd
32
32
  import {ZeroAddressException} from "@gearbox-protocol/core-v2/contracts/interfaces/IErrors.sol";
33
33
  import {LinearInterestModelHelper} from "./LinearInterestModelHelper.sol";
34
34
 
35
+ uint256 constant COUNT = 0;
36
+ uint256 constant QUERY = 1;
37
+
35
38
  /// @title Data compressor 2.1.
36
39
  /// @notice Collects data from various contracts for use in the dApp
37
40
  /// Do not use for data from data compressor for state-changing functions
@@ -148,15 +151,15 @@ contract DataCompressorV2_10 is IDataCompressorV2_10, ContractsRegisterTrait, Li
148
151
  }
149
152
 
150
153
  /// @dev Returns CreditManagerData for all Credit Managers
151
- function getCreditManagersList() external view returns (CreditManagerData[] memory result) {
152
- uint256 creditManagersCount = IContractsRegister(contractsRegister).getCreditManagersCount();
154
+ function getCreditManagersV2List() external view returns (CreditManagerData[] memory result) {
155
+ address[] memory cms = _listCreditManagersV2();
156
+ uint256 creditManagersCount = cms.length;
153
157
 
154
158
  result = new CreditManagerData[](creditManagersCount);
155
159
 
156
160
  unchecked {
157
161
  for (uint256 i = 0; i < creditManagersCount; ++i) {
158
- address creditManager = IContractsRegister(contractsRegister).creditManagers(i);
159
- result[i] = getCreditManagerData(creditManager);
162
+ result[i] = getCreditManagerData(cms[i]);
160
163
  }
161
164
  }
162
165
  }
@@ -281,14 +284,14 @@ contract DataCompressorV2_10 is IDataCompressorV2_10, ContractsRegisterTrait, Li
281
284
  }
282
285
 
283
286
  /// @dev Returns PoolData for all registered pools
284
- function getPoolsList() external view returns (PoolData[] memory result) {
285
- uint256 poolsLength = IContractsRegister(contractsRegister).getPoolsCount();
287
+ function getPoolsV1List() external view returns (PoolData[] memory result) {
288
+ address[] memory pools = _listPoolsV1();
289
+ uint256 poolsLength = pools.length;
286
290
 
287
291
  result = new PoolData[](poolsLength);
288
292
  unchecked {
289
293
  for (uint256 i = 0; i < poolsLength; ++i) {
290
- address pool = IContractsRegister(contractsRegister).pools(i);
291
- result[i] = getPoolData(pool);
294
+ result[i] = getPoolData(pools[i]);
292
295
  }
293
296
  }
294
297
  }
@@ -327,4 +330,64 @@ contract DataCompressorV2_10 is IDataCompressorV2_10, ContractsRegisterTrait, Li
327
330
  creditConfigurator = ICreditConfiguratorV2(creditManagerV2.creditConfigurator());
328
331
  ver = ICreditFacadeV2(creditFacade).version();
329
332
  }
333
+
334
+ function _isContractV2(address _cm) internal view returns (bool) {
335
+ uint256 cmVersion = IVersion(_cm).version();
336
+ return cmVersion >= 2 && cmVersion < 2_99;
337
+ }
338
+
339
+ function _isContractV1(address _pool) internal view returns (bool) {
340
+ uint256 cmVersion = IVersion(_pool).version();
341
+ return cmVersion == 1;
342
+ }
343
+
344
+ function _listPoolsV1() internal view returns (address[] memory result) {
345
+ uint256 len = IContractsRegister(contractsRegister).getPoolsCount();
346
+
347
+ uint256 index;
348
+ unchecked {
349
+ for (uint256 op = COUNT; op <= QUERY; ++op) {
350
+ if (op == QUERY && index == 0) {
351
+ break;
352
+ } else {
353
+ result = new address[](index);
354
+ index = 0;
355
+ }
356
+
357
+ for (uint256 i = 0; i < len; ++i) {
358
+ address _pool = IContractsRegister(contractsRegister).pools(i);
359
+
360
+ if (_isContractV1(_pool)) {
361
+ if (op == QUERY) result[index] = _pool;
362
+ ++index;
363
+ }
364
+ }
365
+ }
366
+ }
367
+ }
368
+
369
+ function _listCreditManagersV2() internal view returns (address[] memory result) {
370
+ uint256 len = IContractsRegister(contractsRegister).getCreditManagersCount();
371
+
372
+ uint256 index;
373
+ unchecked {
374
+ for (uint256 op = COUNT; op <= QUERY; ++op) {
375
+ if (op == QUERY && index == 0) {
376
+ break;
377
+ } else {
378
+ result = new address[](index);
379
+ index = 0;
380
+ }
381
+
382
+ for (uint256 i = 0; i < len; ++i) {
383
+ address _cm = IContractsRegister(contractsRegister).creditManagers(i);
384
+
385
+ if (_isContractV2(_cm)) {
386
+ if (op == QUERY) result[index] = _cm;
387
+ ++index;
388
+ }
389
+ }
390
+ }
391
+ }
392
+ }
330
393
  }
@@ -4,13 +4,24 @@
4
4
  pragma solidity ^0.8.17;
5
5
 
6
6
  import {LinearInterestRateModelV3} from "@gearbox-protocol/core-v3/contracts/pool/LinearInterestRateModelV3.sol";
7
+ import {LinearInterestRateModel} from "@gearbox-protocol/core-v2/contracts/pool/LinearInterestRateModel.sol";
7
8
  import {LinearModel} from "./Types.sol";
8
9
 
9
10
  contract LinearInterestModelHelper {
10
11
  function getLIRMData(address _model) internal view returns (LinearModel memory irm) {
11
12
  irm.interestModel = _model;
13
+ irm.version = LinearInterestRateModel(_model).version();
12
14
 
13
- (irm.U_1, irm.U_2, irm.R_base, irm.R_slope1, irm.R_slope2, irm.R_slope3) =
14
- LinearInterestRateModelV3(_model).getModelParameters();
15
+ if (irm.version == 1) {
16
+ (uint256 U_1, uint256 R_base, uint256 R_slope1, uint256 R_slope2) =
17
+ LinearInterestRateModel(_model).getModelParameters();
18
+ irm.U_1 = uint16(U_1);
19
+ irm.R_base = uint16(R_base);
20
+ irm.R_slope1 = uint16(R_slope1);
21
+ irm.R_slope2 = uint16(R_slope2);
22
+ } else {
23
+ (irm.U_1, irm.U_2, irm.R_base, irm.R_slope1, irm.R_slope2, irm.R_slope3) =
24
+ LinearInterestRateModelV3(_model).getModelParameters();
25
+ }
15
26
  }
16
27
  }
@@ -64,6 +64,7 @@ struct CreditAccountData {
64
64
 
65
65
  struct LinearModel {
66
66
  address interestModel;
67
+ uint256 version;
67
68
  uint16 U_1;
68
69
  uint16 U_2;
69
70
  uint16 R_base;
@@ -160,3 +161,79 @@ struct GaugeVote {
160
161
  uint96 totalVotesLpSide;
161
162
  uint96 totalVotesCaSide;
162
163
  }
164
+
165
+ struct CreditManagerDataV2 {
166
+ address addr;
167
+ address underlying;
168
+ address pool;
169
+ bool isWETH;
170
+ bool canBorrow;
171
+ uint256 borrowRate;
172
+ uint256 minAmount;
173
+ uint256 maxAmount;
174
+ uint256 maxLeverageFactor; // for V1 only
175
+ uint256 availableLiquidity;
176
+ address[] collateralTokens;
177
+ ContractAdapter[] adapters;
178
+ uint256[] liquidationThresholds;
179
+ uint8 version;
180
+ address creditFacade; // V2 only: address of creditFacade
181
+ address creditConfigurator; // V2 only: address of creditConfigurator
182
+ bool isDegenMode; // V2 only: true if contract is in Degen mode
183
+ address degenNFT; // V2 only: degenNFT, address(0) if not in degen mode
184
+ bool isIncreaseDebtForbidden; // V2 only: true if increasing debt is forbidden
185
+ uint256 forbiddenTokenMask; // V2 only: mask which forbids some particular tokens
186
+ uint8 maxEnabledTokensLength; // V2 only: in V1 as many tokens as the CM can support (256)
187
+ uint16 feeInterest; // Interest fee protocol charges: fee = interest accrues * feeInterest
188
+ uint16 feeLiquidation; // Liquidation fee protocol charges: fee = totalValue * feeLiquidation
189
+ uint16 liquidationDiscount; // Miltiplier to get amount which liquidator should pay: amount = totalValue * liquidationDiscount
190
+ uint16 feeLiquidationExpired; // Liquidation fee protocol charges on expired accounts
191
+ uint16 liquidationDiscountExpired; // Multiplier for the amount the liquidator has to pay when closing an expired account
192
+ }
193
+
194
+ struct CreditAccountDataV2 {
195
+ address addr;
196
+ address borrower;
197
+ bool inUse;
198
+ address creditManager;
199
+ address underlying;
200
+ uint256 borrowedAmountPlusInterest;
201
+ uint256 borrowedAmountPlusInterestAndFees;
202
+ uint256 totalValue;
203
+ uint256 healthFactor;
204
+ uint256 borrowRate;
205
+ TokenBalance[] balances;
206
+ uint256 repayAmount; // for v1 accounts only
207
+ uint256 liquidationAmount; // for v1 accounts only
208
+ bool canBeClosed; // for v1 accounts only
209
+ uint256 borrowedAmount;
210
+ uint256 cumulativeIndexAtOpen;
211
+ uint256 since;
212
+ uint8 version;
213
+ uint256 enabledTokenMask;
214
+ }
215
+
216
+ struct PoolDataV2 {
217
+ address addr;
218
+ bool isWETH;
219
+ address underlying;
220
+ address dieselToken;
221
+ uint256 linearCumulativeIndex;
222
+ uint256 availableLiquidity;
223
+ uint256 expectedLiquidity;
224
+ uint256 expectedLiquidityLimit;
225
+ uint256 totalBorrowed;
226
+ uint256 depositAPY_RAY;
227
+ uint256 borrowAPY_RAY;
228
+ uint256 dieselRate_RAY;
229
+ uint256 withdrawFee;
230
+ uint256 cumulativeIndex_RAY;
231
+ uint256 timestampLU;
232
+ uint8 version;
233
+ }
234
+
235
+ struct TokenInfoV2 {
236
+ address addr;
237
+ string symbol;
238
+ uint8 decimals;
239
+ }
@@ -0,0 +1,53 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Gearbox Protocol. Generalized leverage for DeFi protocols
3
+ // (c) Gearbox Holdings, 2022
4
+ pragma solidity ^0.8.10;
5
+
6
+ import {CreditAccountDataV2, CreditManagerDataV2, PoolDataV2, TokenInfoV2} from "../data/Types.sol";
7
+ import {IVersion} from "@gearbox-protocol/core-v2/contracts/interfaces/IVersion.sol";
8
+
9
+ interface IDataCompressorV2Exceptions {
10
+ /// @dev Thrown if attempting to get data on a contract that is not a registered
11
+ /// Credit Manager
12
+ error NotCreditManagerException();
13
+
14
+ /// @dev Thrown if attempting the get data on a contract that is not a registered
15
+ /// pool
16
+ error NotPoolException();
17
+ }
18
+
19
+ interface IDataCompressorV2 is IDataCompressorV2Exceptions, IVersion {
20
+ /// @dev Returns CreditAccountData for all opened accounts for particular borrower
21
+ /// @param borrower Borrower address
22
+ function getCreditAccountList(address borrower) external view returns (CreditAccountDataV2[] memory);
23
+
24
+ /// @dev Returns whether the borrower has an open credit account with the credit manager
25
+ /// @param creditManager Credit manager to check
26
+ /// @param borrower Borrower to check
27
+ function hasOpenedCreditAccount(address creditManager, address borrower) external view returns (bool);
28
+
29
+ /// @dev Returns CreditAccountData for a particular Credit Account account, based on creditManager and borrower
30
+ /// @param _creditManager Credit manager address
31
+ /// @param borrower Borrower address
32
+ function getCreditAccountData(address _creditManager, address borrower)
33
+ external
34
+ view
35
+ returns (CreditAccountDataV2 memory);
36
+
37
+ /// @dev Returns CreditManagerData for all Credit Managers
38
+ function getCreditManagersList() external view returns (CreditManagerDataV2[] memory);
39
+
40
+ /// @dev Returns CreditManagerData for a particular _creditManager
41
+ /// @param _creditManager CreditManager address
42
+ function getCreditManagerData(address _creditManager) external view returns (CreditManagerDataV2 memory);
43
+
44
+ /// @dev Returns PoolData for a particular pool
45
+ /// @param _pool Pool address
46
+ function getPoolData(address _pool) external view returns (PoolDataV2 memory);
47
+
48
+ /// @dev Returns PoolData for all registered pools
49
+ function getPoolsList() external view returns (PoolDataV2[] memory);
50
+
51
+ /// @dev Returns the adapter address for a particular creditManager and targetContract
52
+ function getAdapter(address _creditManager, address _allowedContract) external view returns (address adapter);
53
+ }
@@ -25,7 +25,7 @@ interface IDataCompressorV2_10 is IVersion {
25
25
  returns (CreditAccountData memory);
26
26
 
27
27
  /// @dev Returns CreditManagerData for all Credit Managers
28
- function getCreditManagersList() external view returns (CreditManagerData[] memory);
28
+ function getCreditManagersV2List() external view returns (CreditManagerData[] memory);
29
29
 
30
30
  /// @dev Returns CreditManagerData for a particular _creditManager
31
31
  /// @param _creditManager CreditManager address
@@ -36,7 +36,7 @@ interface IDataCompressorV2_10 is IVersion {
36
36
  function getPoolData(address _pool) external view returns (PoolData memory);
37
37
 
38
38
  /// @dev Returns PoolData for all registered pools
39
- function getPoolsList() external view returns (PoolData[] memory);
39
+ function getPoolsV1List() external view returns (PoolData[] memory);
40
40
 
41
41
  /// @dev Returns the adapter address for a particular creditManager and targetContract
42
42
  function getAdapter(address _creditManager, address _allowedContract) external view returns (address adapter);
@@ -0,0 +1,116 @@
1
+ // SPDX-License-Identifier: UNLICENSED
2
+ // Gearbox Protocol. Generalized leverage for DeFi protocols
3
+ // (c) Gearbox Foundation, 2023.
4
+ pragma solidity ^0.8.17;
5
+
6
+ import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
7
+
8
+ import {DataCompressorV2_10} from "../data/DataCompressor_2_1.sol";
9
+ import {DataCompressorV3_00} from "../data/DataCompressor_3_0.sol";
10
+ import {CreditAccountData, CreditManagerData, PoolData, TokenBalance, ContractAdapter} from "../data/Types.sol";
11
+
12
+ import "forge-std/console.sol";
13
+
14
+ address constant ap = 0x5BcB06c56e8F28da0b038a373199240ca3F5a2f4;
15
+
16
+ contract DCTest {
17
+ DataCompressorV2_10 public dc2;
18
+ DataCompressorV3_00 public dc3;
19
+
20
+ function setUp() public {
21
+ dc2 = new DataCompressorV2_10(ap);
22
+ dc3 = new DataCompressorV3_00(ap);
23
+ }
24
+
25
+ function _printPools(PoolData[] memory pools) internal view {
26
+ uint256 len = pools.length;
27
+ unchecked {
28
+ for (uint256 i; i < len; ++i) {
29
+ PoolData memory pool = pools[i];
30
+ console.log("\n\n");
31
+ console.log(IERC20Metadata(pool.underlying).symbol(), pool.addr);
32
+ console.log("-------------------------------");
33
+
34
+ console.log("dieselToken: ", pool.dieselToken);
35
+ ///
36
+ console.log("linearCumulativeIndex: ", pool.linearCumulativeIndex);
37
+ console.log("availableLiquidity: ", pool.availableLiquidity);
38
+ console.log("expectedLiquidity: ", pool.expectedLiquidity);
39
+ //
40
+ console.log("totalBorrowed: ", pool.totalBorrowed);
41
+ console.log("totalDebtLimit: ", pool.totalDebtLimit);
42
+ // CreditManagerDebtParams[] creditManagerDebtParams;
43
+ console.log("totalAssets: ", pool.totalAssets);
44
+ console.log("totalSupply: ", pool.totalSupply);
45
+ console.log("supplyRate", pool.supplyRate);
46
+ console.log("baseInterestRate: ", pool.baseInterestRate);
47
+ console.log("dieselRate_RAY: ", pool.dieselRate_RAY);
48
+ console.log("withdrawFee", pool.withdrawFee);
49
+ console.log("cumulativeIndex_RAY:", pool.cumulativeIndex_RAY);
50
+ console.log("baseInterestIndexLU:", pool.baseInterestIndexLU);
51
+ console.log("version: ", pool.version);
52
+ // QuotaInfo[] quotas;
53
+ // LinearModel lirm;
54
+ console.log("isPaused", pool.isPaused);
55
+ }
56
+ }
57
+ }
58
+
59
+ function _printCreditManagers(CreditManagerData[] memory cms) internal view {
60
+ uint256 len = cms.length;
61
+ unchecked {
62
+ for (uint256 i; i < len; ++i) {
63
+ CreditManagerData memory cm = cms[i];
64
+ console.log("\n\n");
65
+ console.log(IERC20Metadata(cm.underlying).symbol(), cm.addr);
66
+ console.log("-------------------------------");
67
+ console.log("cfVersion: ", cm.cfVersion);
68
+ console.log("creditFacace: ", cm.creditFacade); // V2 only: address of creditFacade
69
+ console.log("creditConfigurator: ", cm.creditConfigurator); // V2 only: address of creditConfigurator
70
+ console.log("pool: ", cm.pool);
71
+ console.log("totalDebt: ", cm.totalDebt);
72
+ console.log("totalDebtLimit: ", cm.totalDebtLimit);
73
+ console.log("baseBorrowRate: ", cm.baseBorrowRate);
74
+ console.log("minDebt: ", cm.minDebt);
75
+ console.log("maxDebt: ", cm.maxDebt);
76
+ console.log("availableToBorrow: ", cm.availableToBorrow);
77
+ // address[] collateralTokens);
78
+ // ContractAdapter[] adapters);
79
+ // uint256[] liquidationThresholds);
80
+ console.log("isDegenMode: ", cm.isDegenMode); // V2 only: true if contract is in Degen mode
81
+ console.log("degenNFT: ", cm.degenNFT); // V2 only: degenNFT, address(0) if not in degen mode
82
+ console.log("forbiddenTokenMask: ", cm.forbiddenTokenMask); // V2 only: mask which forbids some particular tokens
83
+ console.log("maxEnabledTokensLength: ", cm.maxEnabledTokensLength); // V2 only: in V1 as many tokens as the CM can support (256)
84
+ console.log("feeInterest: ", cm.feeInterest); // Interest fee protocol charges: fee = interest accrues * feeInterest
85
+ console.log("feeLiquidation: ", cm.feeLiquidation); // Liquidation fee protocol charges: fee = totalValue * feeLiquidation
86
+ console.log("liquidationDiscount: ", cm.liquidationDiscount); // Miltiplier to get amount which liquidator should pay: amount = totalValue * liquidationDiscount
87
+ console.log("feeLiquidationExpired: ", cm.feeLiquidationExpired); // Liquidation fee protocol charges on expired accounts
88
+ console.log("liquidationDiscountExpired: ", cm.liquidationDiscountExpired); // Multiplier for the amount the liquidator has to pay when closing an expired account
89
+ // V3 Fileds
90
+ // QuotaInfo[] quotas);
91
+ // LinearModel lirm);
92
+ console.log("sPaused: ", cm.isPaused);
93
+ }
94
+ }
95
+ }
96
+
97
+ function test_dc_pools() public view {
98
+ PoolData[] memory pools = dc2.getPoolsV1List();
99
+ console.log("V1 pools");
100
+ _printPools(pools);
101
+
102
+ pools = dc3.getPoolsV3List();
103
+ console.log("\nV3 pools");
104
+ _printPools(pools);
105
+ }
106
+
107
+ function test_dc_credit_managers() public view {
108
+ CreditManagerData[] memory cms = dc2.getCreditManagersV2List();
109
+ console.log("V2 credit managers");
110
+ _printCreditManagers(cms);
111
+
112
+ cms = dc3.getCreditManagersV3List();
113
+ console.log("\n\nV3 credit managers");
114
+ _printCreditManagers(cms);
115
+ }
116
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/periphery-v3",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "main": "index.js",
5
5
  "repository": "git@github.com:Gearbox-protocol/periphery-v3.git",
6
6
  "author": "Mikael <26343374+0xmikko@users.noreply.github.com>",