Skip to content

Commit

Permalink
feat: add getUpdatablePriceFeeds to market compressor
Browse files Browse the repository at this point in the history
  • Loading branch information
lekhovitsky committed Oct 21, 2024
1 parent d3f4b03 commit 8b7b39b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
66 changes: 52 additions & 14 deletions contracts/compressors/MarketCompressor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import {TokenCompressor} from "./TokenCompressor.sol";
// // EXCEPTIONS
// import "@gearbox-protocol/core-v3/contracts/interfaces/IExceptions.sol";

import {BaseParams} from "../types/BaseState.sol";
import {MarketData, CreditManagerData} from "../types/MarketData.sol";
import {TokenData} from "../types/TokenData.sol";
import {PoolState} from "../types/PoolState.sol";
import {PriceOracleState} from "../types/PriceOracleState.sol";

import {IMarketCompressor} from "../interfaces/IMarketCompressor.sol";

Expand Down Expand Up @@ -66,38 +68,74 @@ contract MarketCompressor is IMarketCompressor {
}
}

function getUpdatablePriceFeeds(MarketFilter memory filter)
external
view
returns (BaseParams[] memory updatablePriceFeeds)
{
address[] memory pools = _getPools(filter);
uint256 numPools = pools.length;
PriceOracleState[] memory poStates = new PriceOracleState[](numPools);
uint256 numUpdatable;
for (uint256 i; i < numPools; ++i) {
address[] memory tokens = _getTokens(pools[i]);
address priceOracle = _getPriceOracle(poolCompressor.getPoolState(pools[i]));
poStates[i] = priceOracleCompressor.getPriceOracleState(priceOracle, tokens);
for (uint256 j; j < poStates[i].priceFeedStructure.length; ++j) {
if (poStates[i].priceFeedStructure[j].updatable) ++numUpdatable;
}
}

updatablePriceFeeds = new BaseParams[](numUpdatable);
uint256 k;
for (uint256 i; i < numPools; ++i) {
for (uint256 j; j < poStates[i].priceFeedStructure.length; ++j) {
if (poStates[i].priceFeedStructure[j].updatable) {
updatablePriceFeeds[k++] = poStates[i].priceFeedStructure[j].baseParams;
}
}
}
}

function getMarketData(address pool) public view returns (MarketData memory result) {
result.acl = IPoolV3(pool).acl();
result.pool = poolCompressor.getPoolState(pool);
result.poolQuotaKeeper = poolCompressor.getPoolQuotaKeeperState(result.pool.poolQuotaKeeper);
result.rateKeeper = poolCompressor.getRateKeeperState(result.poolQuotaKeeper.rateKeeper);
result.interestRateModel = poolCompressor.getInterestModelState(result.pool.interestRateModel);

address priceOracle = _getPriceOracle(result.pool);
address[] memory tokens = IPoolQuotaKeeperV3(result.pool.poolQuotaKeeper).quotedTokens();

result.tokens = new TokenData[](tokens.length + 1);
address[] memory underlyingAndTokens = new address[](tokens.length + 1);
result.tokens[0] = tokenCompressor.getTokenInfo(result.pool.underlying);
underlyingAndTokens[0] = result.pool.underlying;

for (uint256 i = 0; i < tokens.length; i++) {
result.tokens[i + 1] = tokenCompressor.getTokenInfo(tokens[i]);
underlyingAndTokens[i + 1] = tokens[i];
address[] memory tokens = _getTokens(pool);
result.tokens = new TokenData[](tokens.length);
for (uint256 i; i < tokens.length; i++) {
result.tokens[i] = tokenCompressor.getTokenInfo(tokens[i]);
}

// creditManager

uint256 len = result.pool.creditManagerDebtParams.length;
result.creditManagers = new CreditManagerData[](len);
for (uint256 i = 0; i < len; i++) {
result.creditManagers[i] =
poolCompressor.getCreditManagerData(result.pool.creditManagerDebtParams[i].creditManager);
}
// How to query if no credit mangers are deployed?
result.priceOracleData = priceOracleCompressor.getPriceOracleState(priceOracle, underlyingAndTokens);

address priceOracle = _getPriceOracle(result.pool);
result.priceOracleData = priceOracleCompressor.getPriceOracleState(priceOracle, tokens);
}

function _getTokens(address pool) internal view returns (address[] memory tokens) {
// under proper configuration, equivalent to price oracle's `getTokens`
address quotaKeeper = IPoolV3(pool).poolQuotaKeeper();
address[] memory quotedTokens = IPoolQuotaKeeperV3(quotaKeeper).quotedTokens();
uint256 numTokens = quotedTokens.length;
tokens = new address[](numTokens + 1);
tokens[0] = IPoolV3(pool).asset();
for (uint256 i; i < numTokens; ++i) {
tokens[i + 1] = quotedTokens[i];
}
}

function _getPriceOracle(PoolState memory ps) internal view returns (address) {
// TODO: read from market configurator instead once structure is known
if (ps.creditManagerDebtParams.length == 0) {
return address(0);
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/compressors/PriceFeedCompressor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ contract PriceFeedCompressor is IPriceFeedCompressor {
contractTypes[uint8(PriceFeedType.CURVE_CRYPTO_ORACLE)] = "PF_CURVE_CRYPTO_LP_ORACLE";
contractTypes[uint8(PriceFeedType.CURVE_USD_ORACLE)] = "PF_CURVE_USD_ORACLE";
contractTypes[uint8(PriceFeedType.ERC4626_VAULT_ORACLE)] = "PF_ERC4626_ORACLE";
contractTypes[uint8(PriceFeedType.WRAPPED_AAVE_V2_ORACLE)] = "NOT_USER";
contractTypes[uint8(PriceFeedType.WRAPPED_AAVE_V2_ORACLE)] = "NOT_USED";
contractTypes[uint8(PriceFeedType.WSTETH_ORACLE)] = "PF_WSTETH_ORACLE";
contractTypes[uint8(PriceFeedType.YEARN_ORACLE)] = "PF_YEARN_ORACLE";
contractTypes[uint8(PriceFeedType.MELLOW_LRT_ORACLE)] = "PF_MELLOW_LRT_ORACLE";
Expand Down
1 change: 1 addition & 0 deletions contracts/types/MarketData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct MarketData {
BaseParams baseParams;
// Owner who manages market
address owner;
address acl;
// Syntax sugar ?
// address underlying;
// Risk curator name
Expand Down

0 comments on commit 8b7b39b

Please sign in to comment.