Skip to content

Commit 996b73f

Browse files
committed
fix: dc2 and dc3 getters fixed
1 parent 85838d2 commit 996b73f

File tree

5 files changed

+203
-12
lines changed

5 files changed

+203
-12
lines changed

contracts/data/DataCompressor_2_1.sol

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ import {CreditAccountData, CreditManagerData, PoolData, TokenBalance, ContractAd
3232
import {ZeroAddressException} from "@gearbox-protocol/core-v2/contracts/interfaces/IErrors.sol";
3333
import {LinearInterestModelHelper} from "./LinearInterestModelHelper.sol";
3434

35+
uint256 constant COUNT = 0;
36+
uint256 constant QUERY = 1;
37+
3538
/// @title Data compressor 2.1.
3639
/// @notice Collects data from various contracts for use in the dApp
3740
/// Do not use for data from data compressor for state-changing functions
@@ -148,15 +151,15 @@ contract DataCompressorV2_10 is IDataCompressorV2_10, ContractsRegisterTrait, Li
148151
}
149152

150153
/// @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;
153157

154158
result = new CreditManagerData[](creditManagersCount);
155159

156160
unchecked {
157161
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]);
160163
}
161164
}
162165
}
@@ -281,14 +284,14 @@ contract DataCompressorV2_10 is IDataCompressorV2_10, ContractsRegisterTrait, Li
281284
}
282285

283286
/// @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;
286290

287291
result = new PoolData[](poolsLength);
288292
unchecked {
289293
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]);
292295
}
293296
}
294297
}
@@ -327,4 +330,64 @@ contract DataCompressorV2_10 is IDataCompressorV2_10, ContractsRegisterTrait, Li
327330
creditConfigurator = ICreditConfiguratorV2(creditManagerV2.creditConfigurator());
328331
ver = ICreditFacadeV2(creditFacade).version();
329332
}
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+
}
330393
}

contracts/data/LinearInterestModelHelper.sol

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,24 @@
44
pragma solidity ^0.8.17;
55

66
import {LinearInterestRateModelV3} from "@gearbox-protocol/core-v3/contracts/pool/LinearInterestRateModelV3.sol";
7+
import {LinearInterestRateModel} from "@gearbox-protocol/core-v2/contracts/pool/LinearInterestRateModel.sol";
78
import {LinearModel} from "./Types.sol";
89

910
contract LinearInterestModelHelper {
1011
function getLIRMData(address _model) internal view returns (LinearModel memory irm) {
1112
irm.interestModel = _model;
13+
irm.version = LinearInterestRateModel(_model).version();
1214

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+
}
1526
}
1627
}

contracts/data/Types.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ struct CreditAccountData {
6464

6565
struct LinearModel {
6666
address interestModel;
67+
uint256 version;
6768
uint16 U_1;
6869
uint16 U_2;
6970
uint16 R_base;

contracts/interfaces/IDataCompressorV2_10.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface IDataCompressorV2_10 is IVersion {
2525
returns (CreditAccountData memory);
2626

2727
/// @dev Returns CreditManagerData for all Credit Managers
28-
function getCreditManagersList() external view returns (CreditManagerData[] memory);
28+
function getCreditManagersV2List() external view returns (CreditManagerData[] memory);
2929

3030
/// @dev Returns CreditManagerData for a particular _creditManager
3131
/// @param _creditManager CreditManager address
@@ -36,7 +36,7 @@ interface IDataCompressorV2_10 is IVersion {
3636
function getPoolData(address _pool) external view returns (PoolData memory);
3737

3838
/// @dev Returns PoolData for all registered pools
39-
function getPoolsList() external view returns (PoolData[] memory);
39+
function getPoolsV1List() external view returns (PoolData[] memory);
4040

4141
/// @dev Returns the adapter address for a particular creditManager and targetContract
4242
function getAdapter(address _creditManager, address _allowedContract) external view returns (address adapter);

contracts/test/DC_Printer.sol

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
 (0)