Skip to content

Commit bdf87ce

Browse files
authored
Merge pull request #22 from Gearbox-protocol/token_compressor
fix: add token compressor to market compressor
2 parents f8d2fa6 + 0d4875f commit bdf87ce

File tree

5 files changed

+55
-67
lines changed

5 files changed

+55
-67
lines changed

contracts/compressors/MarketCompressor.sol

Lines changed: 29 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ import {MarketFilter} from "../types/CreditAccountState.sol";
1717

1818
import {PoolCompressorV3} from "./PoolCompressor.sol";
1919
import {PriceFeedCompressor} from "./PriceFeedCompressor.sol";
20+
import {TokenCompressor} from "./TokenCompressor.sol";
2021

2122
// // EXCEPTIONS
2223
// import "@gearbox-protocol/core-v3/contracts/interfaces/IExceptions.sol";
2324

24-
import {MarketData, TokenInfo} from "../types/MarketData.sol";
25+
import {MarketData} from "../types/MarketData.sol";
26+
import {TokenData} from "../types/TokenData.sol";
2527
import {PoolState} from "../types/PoolState.sol";
2628

2729
import {IMarketCompressor} from "../interfaces/IMarketCompressor.sol";
@@ -41,6 +43,7 @@ contract MarketCompressor is IMarketCompressor {
4143
/// @notice Address provider contract address
4244
address public immutable ADDRESS_PROVIDER;
4345

46+
TokenCompressor tokenCompressor;
4447
PriceFeedCompressor priceOracleCompressor;
4548
PoolCompressorV3 poolCompressor;
4649

@@ -49,15 +52,12 @@ contract MarketCompressor is IMarketCompressor {
4952
constructor(address addressProvider, address priceOracleCompressorAddress) {
5053
ADDRESS_PROVIDER = addressProvider;
5154
addressProvider = addressProvider;
55+
tokenCompressor = new TokenCompressor();
5256
poolCompressor = new PoolCompressorV3();
53-
priceOracleCompressor = PriceFeedCompressor(
54-
priceOracleCompressorAddress
55-
);
57+
priceOracleCompressor = PriceFeedCompressor(priceOracleCompressorAddress);
5658
}
5759

58-
function getMarkets(
59-
MarketFilter memory filter
60-
) external view returns (MarketData[] memory result) {
60+
function getMarkets(MarketFilter memory filter) external view returns (MarketData[] memory result) {
6161
address[] memory pools = _getPools(filter);
6262
result = new MarketData[](pools.length);
6363

@@ -66,59 +66,39 @@ contract MarketCompressor is IMarketCompressor {
6666
}
6767
}
6868

69-
function getMarketData(
70-
address pool
71-
) public view returns (MarketData memory result) {
69+
function getMarketData(address pool) public view returns (MarketData memory result) {
7270
result.pool = poolCompressor.getPoolState(pool);
73-
result.poolQuotaKeeper = poolCompressor.getPoolQuotaKeeperState(
74-
result.pool.poolQuotaKeeper
75-
);
76-
result.rateKeeper = poolCompressor.getRateKeeperState(
77-
result.poolQuotaKeeper.rateKeeper
78-
);
79-
result.interestRateModel = poolCompressor.getInterestModelState(
80-
result.pool.interestRateModel
81-
);
71+
result.poolQuotaKeeper = poolCompressor.getPoolQuotaKeeperState(result.pool.poolQuotaKeeper);
72+
result.rateKeeper = poolCompressor.getRateKeeperState(result.poolQuotaKeeper.rateKeeper);
73+
result.interestRateModel = poolCompressor.getInterestModelState(result.pool.interestRateModel);
8274

8375
address priceOracle = _getPriceOracle(result.pool);
84-
address[] memory tokens = IPoolQuotaKeeperV3(
85-
result.pool.poolQuotaKeeper
86-
).quotedTokens();
76+
address[] memory tokens = IPoolQuotaKeeperV3(result.pool.poolQuotaKeeper).quotedTokens();
8777

88-
// TODO: add token data
89-
result.tokens = new TokenInfo[](tokens.length + 1);
90-
// result.tokens[0] = tokenCompressor.getTokenInfo(result.pool.underlying);
78+
result.tokens = new TokenData[](tokens.length + 1);
79+
address[] memory underlyingAndTokens = new address[](tokens.length + 1);
80+
result.tokens[0] = tokenCompressor.getTokenInfo(result.pool.underlying);
81+
underlyingAndTokens[0] = result.pool.underlying;
9182

92-
// for (uint256 i = 0; i < tokens.length; i++) {
93-
// result.tokens[i + 1] = tokenCompressor.getTokenInfo(tokens[i]);
94-
// }
83+
for (uint256 i = 0; i < tokens.length; i++) {
84+
result.tokens[i + 1] = tokenCompressor.getTokenInfo(tokens[i]);
85+
underlyingAndTokens[i + 1] = tokens[i];
86+
}
9587
// How to query if no credit mangers are deployed?
96-
// //
97-
// TODO: add local address[]
98-
// result.priceOracleData = priceOracleCompressor.getPriceOracleState(
99-
// priceOracle,
100-
// result.tokens
101-
// );
88+
result.priceOracleData = priceOracleCompressor.getPriceOracleState(priceOracle, underlyingAndTokens);
10289
}
10390

104-
function _getPriceOracle(
105-
PoolState memory ps
106-
) internal view returns (address) {
91+
function _getPriceOracle(PoolState memory ps) internal view returns (address) {
10792
if (ps.creditManagerDebtParams.length == 0) {
10893
return address(0);
10994
}
11095

111-
return
112-
ICreditManagerV3(ps.creditManagerDebtParams[0].creditManager)
113-
.priceOracle();
96+
return ICreditManagerV3(ps.creditManagerDebtParams[0].creditManager).priceOracle();
11497
}
11598

11699
/// @dev Pools discovery
117-
function _getPools(
118-
MarketFilter memory filter
119-
) internal view returns (address[] memory pools) {
120-
address[] memory configurators = IAddressProviderV3_1(ADDRESS_PROVIDER)
121-
.marketConfigurators();
100+
function _getPools(MarketFilter memory filter) internal view returns (address[] memory pools) {
101+
address[] memory configurators = IAddressProviderV3_1(ADDRESS_PROVIDER).marketConfigurators();
122102

123103
// rough estimate of maximum number of credit pools
124104
uint256 max;
@@ -133,24 +113,20 @@ contract MarketCompressor is IMarketCompressor {
133113

134114
for (uint256 i; i < configurators.length; ++i) {
135115
if (filter.curators.length != 0) {
136-
if (
137-
!filter.curators.contains(
138-
IMarketConfiguratorV3(configurators[i]).owner()
139-
)
140-
) continue;
116+
if (!filter.curators.contains(IMarketConfiguratorV3(configurators[i]).owner())) continue;
141117
}
142118

143-
address[] memory poolsMC = IMarketConfiguratorV3(configurators[i])
144-
.pools();
119+
address[] memory poolsMC = IMarketConfiguratorV3(configurators[i]).pools();
145120
for (uint256 j; j < poolsMC.length; ++j) {
146121
address currentPool = poolsMC[j];
147122
if (filter.pools.length != 0) {
148123
if (!filter.pools.contains(currentPool)) continue;
149124
}
150125

151126
if (filter.underlying != address(0)) {
152-
if (IPoolV3(currentPool).asset() != filter.underlying)
127+
if (IPoolV3(currentPool).asset() != filter.underlying) {
153128
continue;
129+
}
154130
}
155131

156132
pools[num++] = currentPool;

contracts/compressors/TokenCompressor.sol

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@
33
// (c) Gearbox Foundation, 2024.
44
pragma solidity ^0.8.17;
55

6-
import {TokenInfo} from "../types/MarketData.sol";
6+
import {TokenData} from "../types/TokenData.sol";
77
import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
88
import {LibString} from "@solady/utils/LibString.sol";
9+
import {ITokenCompressor} from "../interfaces/ITokenCompressor.sol";
910

10-
contract TokenCompressor {
11-
function getTokenInfo(address token) public view returns (TokenInfo memory result) {
12-
result.token = token;
11+
/// @title Token compressor 3.0.
12+
/// @notice Helper contract to fetch ERC20 token metadata
13+
contract TokenCompressor is ITokenCompressor {
14+
/// @notice Contract version
15+
uint256 public constant override version = 3_10;
16+
bytes32 public constant override contractType = "TOKEN_COMPRESSOR";
17+
18+
function getTokenInfo(address token) public view returns (TokenData memory result) {
19+
result.addr = token;
1320
result.decimals = IERC20Metadata(token).decimals();
1421

1522
// Fallback to low-level call to handle bytes32 symbol

contracts/interfaces/IMarketCompressor.sol

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import {PoolState} from "../types/PoolState.sol";
33
import {IVersion} from "@gearbox-protocol/core-v3/contracts/interfaces/base/IVersion.sol";
44
import {MarketFilter} from "../types/CreditAccountState.sol";
55

6-
import "forge-std/console.sol";
7-
86
/// @title Data compressor 3.0.
97
/// @notice Collects data from various contracts for use in the dApp
108
/// Do not use for data from data compressor for state-changing functions
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.10;
5+
6+
import {TokenData} from "../types/TokenData.sol";
7+
import {IVersion} from "@gearbox-protocol/core-v3/contracts/interfaces/base/IVersion.sol";
8+
9+
/// @title Token compressor 3.0.
10+
/// @notice Helper contract to fetch ERC20 token metadata
11+
interface ITokenCompressor is IVersion {
12+
function getTokenInfo(address token) external view returns (TokenData memory result);
13+
}

contracts/types/MarketData.sol

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {PriceOracleState} from "./PriceOracleState.sol";
1111
import {CreditManagerState} from "./CreditManagerState.sol";
1212
import {CreditFacadeState} from "./CreditFacadeState.sol";
1313
import {BaseState, BaseParams} from "./BaseState.sol";
14+
import {TokenData} from "./TokenData.sol";
1415

1516
struct MarketData {
1617
// MarketConfigurator baseParams
@@ -26,7 +27,7 @@ struct MarketData {
2627
BaseState interestRateModel;
2728
RateKeeperState rateKeeper;
2829
PriceOracleState priceOracleData;
29-
TokenInfo[] tokens; // <- V3: collateral tokens, V3.1. from local price oracle
30+
TokenData[] tokens; // <- V3: collateral tokens, V3.1. from local price oracle
3031
CreditManagerData[] creditManagers;
3132
// ZappersInfo
3233
ZapperInfo[] zappers;
@@ -44,13 +45,6 @@ struct ZapperInfo {
4445
address tokenOut;
4546
}
4647

47-
struct TokenInfo {
48-
address token;
49-
uint8 decimals;
50-
string symbol;
51-
string name;
52-
}
53-
5448
struct ContractAdapter {
5549
BaseParams baseParams;
5650
address targetContract;

0 commit comments

Comments
 (0)