Skip to content

Commit

Permalink
Merge pull request #24 from Gearbox-protocol/adapter_compressor
Browse files Browse the repository at this point in the history
feat: adapter compressor
  • Loading branch information
0xmikko authored Oct 12, 2024
2 parents 273e1d2 + ca40c83 commit d3f4b03
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 31 deletions.
97 changes: 75 additions & 22 deletions contracts/compressors/AdapterCompressor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,63 @@
// (c) Gearbox Holdings, 2024
pragma solidity ^0.8.17;

import {IStateSerializer} from "../interfaces/IStateSerializer.sol";
import {AdapterType} from "@gearbox-protocol/sdk-gov/contracts/AdapterType.sol";
import {ContractAdapter} from "../types/MarketData.sol";
import {ICreditManagerV3} from "@gearbox-protocol/core-v3/contracts/interfaces/ICreditManagerV3.sol";
import {IAdapterCompressor} from "../interfaces/IAdapterCompressor.sol";
import {ICreditConfiguratorV3} from "@gearbox-protocol/core-v3/contracts/interfaces/ICreditConfiguratorV3.sol";
import {IAdapter} from "@gearbox-protocol/core-v3/contracts/interfaces/base/IAdapter.sol";
import {ICreditManagerV3} from "@gearbox-protocol/core-v3/contracts/interfaces/ICreditManagerV3.sol";
import {IStateSerializer} from "../interfaces/IStateSerializer.sol";
import {IVersion} from "../interfaces/IVersion.sol";

interface ILegacyAdapter {
function _gearboxAdapterVersion() external view returns (uint16);
function _gearboxAdapterType() external view returns (uint8);
}

/// @title Adapter compressor 3.0.
/// @notice Collects data from different adapter types
contract AdaptgerCompressorV3 {
// Contract version
contract AdapterCompressor is IAdapterCompressor {
uint256 public constant version = 3_10;
bytes32 public constant override contractType = "ADAPTER_COMPRESSOR";

mapping(uint8 => bytes32) public contractTypes;

function getContractAdapters(
address creditManager
) external view returns (ContractAdapter[] memory adapters) {
ICreditConfiguratorV3 creditConfigurator = ICreditConfiguratorV3(
ICreditManagerV3(creditManager).creditConfigurator()
);
constructor() {
contractTypes[uint8(AdapterType.ABSTRACT)] = "AD_ABSTRACT";
contractTypes[uint8(AdapterType.UNISWAP_V2_ROUTER)] = "AD_UNISWAP_V2_ROUTER";
contractTypes[uint8(AdapterType.UNISWAP_V3_ROUTER)] = "AD_UNISWAP_V3_ROUTER";
contractTypes[uint8(AdapterType.CURVE_V1_EXCHANGE_ONLY)] = "AD_CURVE_V1_EXCHANGE_ONLY";
contractTypes[uint8(AdapterType.YEARN_V2)] = "AD_YEARN_V2";
contractTypes[uint8(AdapterType.CURVE_V1_2ASSETS)] = "AD_CURVE_V1_2ASSETS";
contractTypes[uint8(AdapterType.CURVE_V1_3ASSETS)] = "AD_CURVE_V1_3ASSETS";
contractTypes[uint8(AdapterType.CURVE_V1_4ASSETS)] = "AD_CURVE_V1_4ASSETS";
contractTypes[uint8(AdapterType.CURVE_V1_STECRV_POOL)] = "AD_CURVE_V1_STECRV_POOL";
contractTypes[uint8(AdapterType.CURVE_V1_WRAPPER)] = "AD_CURVE_V1_WRAPPER";
contractTypes[uint8(AdapterType.CONVEX_V1_BASE_REWARD_POOL)] = "AD_CONVEX_V1_BASE_REWARD_POOL";
contractTypes[uint8(AdapterType.CONVEX_V1_BOOSTER)] = "AD_CONVEX_V1_BOOSTER";
contractTypes[uint8(AdapterType.CONVEX_V1_CLAIM_ZAP)] = "AD_CONVEX_V1_CLAIM_ZAP";
contractTypes[uint8(AdapterType.LIDO_V1)] = "AD_LIDO_V1";
contractTypes[uint8(AdapterType.UNIVERSAL)] = "AD_UNIVERSAL";
contractTypes[uint8(AdapterType.LIDO_WSTETH_V1)] = "AD_LIDO_WSTETH_V1";
contractTypes[uint8(AdapterType.BALANCER_VAULT)] = "AD_BALANCER_VAULT";
contractTypes[uint8(AdapterType.AAVE_V2_LENDING_POOL)] = "AD_AAVE_V2_LENDING_POOL";
contractTypes[uint8(AdapterType.AAVE_V2_WRAPPED_ATOKEN)] = "AD_AAVE_V2_WRAPPED_ATOKEN";
contractTypes[uint8(AdapterType.COMPOUND_V2_CERC20)] = "AD_COMPOUND_V2_CERC20";
contractTypes[uint8(AdapterType.COMPOUND_V2_CETHER)] = "AD_COMPOUND_V2_CETHER";
contractTypes[uint8(AdapterType.ERC4626_VAULT)] = "AD_ERC4626_VAULT";
contractTypes[uint8(AdapterType.VELODROME_V2_ROUTER)] = "AD_VELODROME_V2_ROUTER";
contractTypes[uint8(AdapterType.CURVE_STABLE_NG)] = "AD_CURVE_STABLE_NG";
contractTypes[uint8(AdapterType.CAMELOT_V3_ROUTER)] = "AD_CAMELOT_V3_ROUTER";
contractTypes[uint8(AdapterType.CONVEX_L2_BOOSTER)] = "AD_CONVEX_L2_BOOSTER";
contractTypes[uint8(AdapterType.CONVEX_L2_REWARD_POOL)] = "AD_CONVEX_L2_REWARD_POOL";
contractTypes[uint8(AdapterType.AAVE_V3_LENDING_POOL)] = "AD_AAVE_V3_LENDING_POOL";
contractTypes[uint8(AdapterType.ZIRCUIT_POOL)] = "AD_ZIRCUIT_POOL";
contractTypes[uint8(AdapterType.SYMBIOTIC_DEFAULT_COLLATERAL)] = "AD_SYMBIOTIC_DEFAULT_COLLATERAL";
contractTypes[uint8(AdapterType.MELLOW_LRT_VAULT)] = "AD_MELLOW_LRT_VAULT";
contractTypes[uint8(AdapterType.PENDLE_ROUTER)] = "AD_PENDLE_ROUTER";
}

function getContractAdapters(address creditManager) external view returns (ContractAdapter[] memory adapters) {
ICreditConfiguratorV3 creditConfigurator =
ICreditConfiguratorV3(ICreditManagerV3(creditManager).creditConfigurator());

address[] memory allowedAdapters = creditConfigurator.allowedAdapters();
uint256 len = allowedAdapters.length;
Expand All @@ -30,17 +69,31 @@ contract AdaptgerCompressorV3 {

unchecked {
for (uint256 i = 0; i < len; ++i) {
address allowedAdapter = allowedAdapters[i];
address adapter = allowedAdapters[i];
adapters[i].baseParams.addr = adapter;
try IVersion(adapter).contractType() returns (bytes32 adapterType) {
adapters[i].baseParams.contractType = adapterType;
} catch {
try ILegacyAdapter(adapter)._gearboxAdapterType() returns (uint8 adapterType) {
adapters[i].baseParams.contractType = contractTypes[adapterType];
} catch {
adapters[i].baseParams.contractType = "AD_ABSTRACT";
}
}

try IVersion(adapter).version() returns (uint256 v) {
adapters[i].baseParams.version = v;
} catch {
try ILegacyAdapter(adapter)._gearboxAdapterVersion() returns (uint16 v) {
adapters[i].baseParams.version = uint256(v);
} catch {}
}

/// add try{} catch for serialisation
try IStateSerializer(adapter).serialize() returns (bytes memory serializedParams) {
adapters[i].baseParams.serializedParams = serializedParams;
} catch {}

// adapters[i] = ContractAdapter({
// targetContract: ICreditManagerV3(creditManager).adapterToContract(allowedAdapter),
// adapter: allowedAdapter,
// adapterType: uint8(IAdapter(allowedAdapter)._gearboxAdapterType()),
// version: IAdapter(allowedAdapter)._gearboxAdapterVersion(),
// stateSerialised: stateSerialised
// });
adapters[i].targetContract = ICreditManagerV3(creditManager).adapterToContract(adapter);
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions contracts/compressors/PoolCompressor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import {BaseLib} from "../libraries/BaseLib.sol";
import {GaugeSerializer} from "../serializers/pool/GaugeSerializer.sol";
import {LinearInterestModelSerializer} from "../serializers/pool/LinearInterestModelSerializer.sol";

import {AdapterCompressor} from "./AdapterCompressor.sol";

/// @title Pool compressor
/// @notice Collects data from pool related contracts
/// Do not use for data from data compressor for state-changing functions
Expand All @@ -42,9 +44,12 @@ contract PoolCompressorV3 {
address public immutable gaugeSerializer;
address public immutable linearInterestModelSerializer;

AdapterCompressor adapterCompressor;

constructor() {
gaugeSerializer = address(new GaugeSerializer());
linearInterestModelSerializer = address(new LinearInterestModelSerializer());
adapterCompressor = new AdapterCompressor();
}

function getPoolState(address pool) public view returns (PoolState memory result) {
Expand Down Expand Up @@ -172,8 +177,6 @@ contract PoolCompressorV3 {
function getRateKeeperState(address rateKeeper) external view returns (RateKeeperState memory result) {
IRateKeeper _rateKeeper = IRateKeeper(rateKeeper);

bytes32 contractType;

result.baseParams = BaseLib.getBaseParams(rateKeeper, "GAUGE", gaugeSerializer);

IPoolQuotaKeeperV3 _pqk = IPoolQuotaKeeperV3(PoolV3(_rateKeeper.pool()).poolQuotaKeeper());
Expand Down Expand Up @@ -292,5 +295,7 @@ contract PoolCompressorV3 {
result.totalDebt = _pool.creditManagerBorrowed(creditManager);
result.totalDebtLimit = _pool.creditManagerDebtLimit(creditManager);
result.availableToBorrow = _pool.creditManagerBorrowable(creditManager);

result.adapters = adapterCompressor.getContractAdapters(creditManager);
}
}
12 changes: 12 additions & 0 deletions contracts/interfaces/IAdapterCompressor.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: BUSL-1.1
// Gearbox Protocol. Generalized leverage for DeFi protocols
// (c) Gearbox Holdings, 2024
pragma solidity ^0.8.10;

import {ContractAdapter} from "../types/MarketData.sol";
import {IVersion} from "@gearbox-protocol/core-v3/contracts/interfaces/base/IVersion.sol";

/// @title Adapter compressor 3.1
interface IAdapterCompressor is IVersion {
function getContractAdapters(address creditManager) external view returns (ContractAdapter[] memory result);
}
5 changes: 5 additions & 0 deletions contracts/interfaces/IMarketCompressor.sol
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// SPDX-License-Identifier: BUSL-1.1
// Gearbox Protocol. Generalized leverage for DeFi protocols
// (c) Gearbox Holdings, 2024
pragma solidity ^0.8.10;

import {MarketData} from "../types/MarketData.sol";
import {PoolState} from "../types/PoolState.sol";
import {IVersion} from "@gearbox-protocol/core-v3/contracts/interfaces/base/IVersion.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/types/MarketData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct MarketData {
// Owner who manages market
address owner;
// Syntax sugar ?
address underlying;
// address underlying;
// Risk curator name
string name;
PoolState pool;
Expand Down
10 changes: 4 additions & 6 deletions script/Migrate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,13 @@ contract Migrate2 is Migrate {
AddressProviderV3_1(_deployGovernance3_1(oldAddressProvider, vetoAdmin, deployer));

address priceFeedCompressor = address(new PriceFeedCompressor());
console.log("PriceFeedCompressor: ", priceFeedCompressor);

_addressProvider.setAddress(priceFeedCompressor, true);

address newContract = address(new MarketCompressor(address(_addressProvider), priceFeedCompressor));
_addressProvider.setAddress(newContract, true);
address marketCompressor = address(new MarketCompressor(address(_addressProvider), priceFeedCompressor));
_addressProvider.setAddress(marketCompressor, true);

newContract = address(new CreditAccountCompressor(address(_addressProvider)));
_addressProvider.setAddress(newContract, true);
address creditAccountCompressor = address(new CreditAccountCompressor(address(_addressProvider)));
_addressProvider.setAddress(creditAccountCompressor, true);

vm.stopBroadcast();

Expand Down

0 comments on commit d3f4b03

Please sign in to comment.