@gearbox-protocol/periphery-v3 1.0.6 → 1.0.8

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.
@@ -26,7 +26,9 @@ import {IVersion} from "@gearbox-protocol/core-v2/contracts/interfaces/IVersion.
26
26
  import {IAddressProvider} from "@gearbox-protocol/core-v2/contracts/interfaces/IAddressProvider.sol";
27
27
  import {IDataCompressorV2_10} from "../interfaces/IDataCompressorV2_10.sol";
28
28
 
29
- import {CreditAccountData, CreditManagerData, PoolData, TokenBalance, ContractAdapter} from "./Types.sol";
29
+ import {
30
+ COUNT, QUERY, CreditAccountData, CreditManagerData, PoolData, TokenBalance, ContractAdapter
31
+ } from "./Types.sol";
30
32
 
31
33
  // EXCEPTIONS
32
34
  import {ZeroAddressException} from "@gearbox-protocol/core-v2/contracts/interfaces/IErrors.sol";
@@ -43,33 +45,27 @@ contract DataCompressorV2_10 is IDataCompressorV2_10, ContractsRegisterTrait, Li
43
45
 
44
46
  /// @dev Returns CreditAccountData for all opened accounts for particular borrower
45
47
  /// @param borrower Borrower address
46
- function getCreditAccountList(address borrower) external view returns (CreditAccountData[] memory result) {
48
+ function getCreditAccountsByBorrower(address borrower) external view returns (CreditAccountData[] memory result) {
47
49
  // Counts how many opened accounts a borrower has
48
- uint256 count;
49
- uint256 creditManagersLength = IContractsRegister(contractsRegister).getCreditManagersCount();
50
+ address[] memory cms = _listCreditManagersV2();
51
+ uint256 creditManagersLength = cms.length;
52
+
53
+ uint256 index;
50
54
  unchecked {
51
- for (uint256 i = 0; i < creditManagersLength; ++i) {
52
- address creditManager = IContractsRegister(contractsRegister).creditManagers(i);
53
- if (hasOpenedCreditAccount(creditManager, borrower)) {
54
- ++count;
55
+ for (uint256 op = COUNT; op <= QUERY; ++op) {
56
+ if (op == QUERY && index == 0) {
57
+ break;
58
+ } else {
59
+ result = new CreditAccountData[](index);
60
+ index = 0;
55
61
  }
56
- }
57
- }
58
-
59
- result = new CreditAccountData[](count);
60
-
61
- // Get data & fill the array
62
- count = 0;
63
- for (uint256 i = 0; i < creditManagersLength;) {
64
- address creditManager = IContractsRegister(contractsRegister).creditManagers(i);
65
- unchecked {
66
- if (hasOpenedCreditAccount(creditManager, borrower)) {
67
- result[count] = getCreditAccountData(creditManager, borrower);
68
-
69
- count++;
62
+ for (uint256 i = 0; i < creditManagersLength; ++i) {
63
+ address creditManager = cms[i];
64
+ if (hasOpenedCreditAccount(creditManager, borrower)) {
65
+ if (op == QUERY) result[index] = getCreditAccountData(creditManager, borrower);
66
+ ++index;
67
+ }
70
68
  }
71
-
72
- ++i;
73
69
  }
74
70
  }
75
71
  }
@@ -148,15 +144,15 @@ contract DataCompressorV2_10 is IDataCompressorV2_10, ContractsRegisterTrait, Li
148
144
  }
149
145
 
150
146
  /// @dev Returns CreditManagerData for all Credit Managers
151
- function getCreditManagersList() external view returns (CreditManagerData[] memory result) {
152
- uint256 creditManagersCount = IContractsRegister(contractsRegister).getCreditManagersCount();
147
+ function getCreditManagersV2List() external view returns (CreditManagerData[] memory result) {
148
+ address[] memory cms = _listCreditManagersV2();
149
+ uint256 creditManagersLength = cms.length;
153
150
 
154
- result = new CreditManagerData[](creditManagersCount);
151
+ result = new CreditManagerData[](creditManagersLength);
155
152
 
156
153
  unchecked {
157
- for (uint256 i = 0; i < creditManagersCount; ++i) {
158
- address creditManager = IContractsRegister(contractsRegister).creditManagers(i);
159
- result[i] = getCreditManagerData(creditManager);
154
+ for (uint256 i = 0; i < creditManagersLength; ++i) {
155
+ result[i] = getCreditManagerData(cms[i]);
160
156
  }
161
157
  }
162
158
  }
@@ -281,14 +277,14 @@ contract DataCompressorV2_10 is IDataCompressorV2_10, ContractsRegisterTrait, Li
281
277
  }
282
278
 
283
279
  /// @dev Returns PoolData for all registered pools
284
- function getPoolsList() external view returns (PoolData[] memory result) {
285
- uint256 poolsLength = IContractsRegister(contractsRegister).getPoolsCount();
280
+ function getPoolsV1List() external view returns (PoolData[] memory result) {
281
+ address[] memory pools = _listPoolsV1();
282
+ uint256 poolsLength = pools.length;
286
283
 
287
284
  result = new PoolData[](poolsLength);
288
285
  unchecked {
289
286
  for (uint256 i = 0; i < poolsLength; ++i) {
290
- address pool = IContractsRegister(contractsRegister).pools(i);
291
- result[i] = getPoolData(pool);
287
+ result[i] = getPoolData(pools[i]);
292
288
  }
293
289
  }
294
290
  }
@@ -327,4 +323,64 @@ contract DataCompressorV2_10 is IDataCompressorV2_10, ContractsRegisterTrait, Li
327
323
  creditConfigurator = ICreditConfiguratorV2(creditManagerV2.creditConfigurator());
328
324
  ver = ICreditFacadeV2(creditFacade).version();
329
325
  }
326
+
327
+ function _isContractV2(address _cm) internal view returns (bool) {
328
+ uint256 cmVersion = IVersion(_cm).version();
329
+ return cmVersion >= 2 && cmVersion < 2_99;
330
+ }
331
+
332
+ function _isContractV1(address _pool) internal view returns (bool) {
333
+ uint256 cmVersion = IVersion(_pool).version();
334
+ return cmVersion == 1;
335
+ }
336
+
337
+ function _listPoolsV1() internal view returns (address[] memory result) {
338
+ uint256 len = IContractsRegister(contractsRegister).getPoolsCount();
339
+
340
+ uint256 index;
341
+ unchecked {
342
+ for (uint256 op = COUNT; op <= QUERY; ++op) {
343
+ if (op == QUERY && index == 0) {
344
+ break;
345
+ } else {
346
+ result = new address[](index);
347
+ index = 0;
348
+ }
349
+
350
+ for (uint256 i = 0; i < len; ++i) {
351
+ address _pool = IContractsRegister(contractsRegister).pools(i);
352
+
353
+ if (_isContractV1(_pool)) {
354
+ if (op == QUERY) result[index] = _pool;
355
+ ++index;
356
+ }
357
+ }
358
+ }
359
+ }
360
+ }
361
+
362
+ function _listCreditManagersV2() internal view returns (address[] memory result) {
363
+ uint256 len = IContractsRegister(contractsRegister).getCreditManagersCount();
364
+
365
+ uint256 index;
366
+ unchecked {
367
+ for (uint256 op = COUNT; op <= QUERY; ++op) {
368
+ if (op == QUERY && index == 0) {
369
+ break;
370
+ } else {
371
+ result = new address[](index);
372
+ index = 0;
373
+ }
374
+
375
+ for (uint256 i = 0; i < len; ++i) {
376
+ address _cm = IContractsRegister(contractsRegister).creditManagers(i);
377
+
378
+ if (_isContractV2(_cm)) {
379
+ if (op == QUERY) result[index] = _cm;
380
+ ++index;
381
+ }
382
+ }
383
+ }
384
+ }
385
+ }
330
386
  }
@@ -40,6 +40,8 @@ import {AddressProvider} from "@gearbox-protocol/core-v2/contracts/core/AddressP
40
40
  import {IDataCompressorV3_00, PriceOnDemand} from "../interfaces/IDataCompressorV3_00.sol";
41
41
 
42
42
  import {
43
+ COUNT,
44
+ QUERY,
43
45
  CreditAccountData,
44
46
  CreditManagerData,
45
47
  PoolData,
@@ -56,9 +58,6 @@ import {
56
58
  import "@gearbox-protocol/core-v3/contracts/interfaces/IExceptions.sol";
57
59
  import {LinearInterestModelHelper} from "./LinearInterestModelHelper.sol";
58
60
 
59
- uint256 constant COUNT = 0;
60
- uint256 constant QUERY = 1;
61
-
62
61
  /// @title Data compressor 3.0.
63
62
  /// @notice Collects data from various contracts for use in the dApp
64
63
  /// Do not use for data from data compressor for state-changing functions
@@ -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
  }
@@ -5,6 +5,9 @@ pragma solidity ^0.8.17;
5
5
 
6
6
  import {ScheduledWithdrawal} from "@gearbox-protocol/core-v3/contracts/interfaces/IWithdrawalManagerV3.sol";
7
7
 
8
+ uint256 constant COUNT = 0;
9
+ uint256 constant QUERY = 1;
10
+
8
11
  struct TokenBalance {
9
12
  address token;
10
13
  uint256 balance;
@@ -64,6 +67,7 @@ struct CreditAccountData {
64
67
 
65
68
  struct LinearModel {
66
69
  address interestModel;
70
+ uint256 version;
67
71
  uint16 U_1;
68
72
  uint16 U_2;
69
73
  uint16 R_base;
@@ -9,7 +9,7 @@ import {IVersion} from "@gearbox-protocol/core-v2/contracts/interfaces/IVersion.
9
9
  interface IDataCompressorV2_10 is IVersion {
10
10
  /// @dev Returns CreditAccountData for all opened accounts for particular borrower
11
11
  /// @param borrower Borrower address
12
- function getCreditAccountList(address borrower) external view returns (CreditAccountData[] memory);
12
+ function getCreditAccountsByBorrower(address borrower) external view returns (CreditAccountData[] memory);
13
13
 
14
14
  /// @dev Returns whether the borrower has an open credit account with the credit manager
15
15
  /// @param creditManager Credit manager to check
@@ -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,121 @@
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_01_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_02_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
+
117
+ function test_dc_03_credit_accounts() public view {
118
+ CreditAccountData[] memory cas = dc2.getCreditAccountsByBorrower(address(this));
119
+ console.log("V2 credit accounts", cas.length);
120
+ }
121
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/periphery-v3",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
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>",