From ca40c835f0b147e7a9f3d5ffdeb1a2b18d142f5a Mon Sep 17 00:00:00 2001 From: doomsower <12031673+doomsower@users.noreply.github.com> Date: Thu, 10 Oct 2024 16:22:39 -0300 Subject: [PATCH] feat: adapter compressor --- contracts/compressors/AdapterCompressor.sol | 97 ++++++++++++++++----- contracts/compressors/PoolCompressor.sol | 9 +- contracts/interfaces/IAdapterCompressor.sol | 12 +++ contracts/interfaces/IMarketCompressor.sol | 5 ++ contracts/types/MarketData.sol | 2 +- script/Migrate.sol | 10 +-- 6 files changed, 104 insertions(+), 31 deletions(-) create mode 100644 contracts/interfaces/IAdapterCompressor.sol diff --git a/contracts/compressors/AdapterCompressor.sol b/contracts/compressors/AdapterCompressor.sol index 3e9f906..efd30ba 100644 --- a/contracts/compressors/AdapterCompressor.sol +++ b/contracts/compressors/AdapterCompressor.sol @@ -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; @@ -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); } } } diff --git a/contracts/compressors/PoolCompressor.sol b/contracts/compressors/PoolCompressor.sol index c5842f2..60b6967 100644 --- a/contracts/compressors/PoolCompressor.sol +++ b/contracts/compressors/PoolCompressor.sol @@ -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 @@ -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) { @@ -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()); @@ -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); } } diff --git a/contracts/interfaces/IAdapterCompressor.sol b/contracts/interfaces/IAdapterCompressor.sol new file mode 100644 index 0000000..b1f42fc --- /dev/null +++ b/contracts/interfaces/IAdapterCompressor.sol @@ -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); +} diff --git a/contracts/interfaces/IMarketCompressor.sol b/contracts/interfaces/IMarketCompressor.sol index 25303e7..405d843 100644 --- a/contracts/interfaces/IMarketCompressor.sol +++ b/contracts/interfaces/IMarketCompressor.sol @@ -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"; diff --git a/contracts/types/MarketData.sol b/contracts/types/MarketData.sol index a915d82..af312e4 100644 --- a/contracts/types/MarketData.sol +++ b/contracts/types/MarketData.sol @@ -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; diff --git a/script/Migrate.sol b/script/Migrate.sol index 624ac16..6ec9928 100644 --- a/script/Migrate.sol +++ b/script/Migrate.sol @@ -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();