Skip to content

Commit

Permalink
feat: ap 3_1 and iMarketConfigurator
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmikko committed Sep 2, 2024
1 parent b0a6885 commit 6c4d38f
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 59 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,4 @@ dist
# Forge output
out
cache
broadcast
21 changes: 11 additions & 10 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"solidity.packageDefaultDependenciesContractsDirectory": "contracts",
"solidity.packageDefaultDependenciesDirectory": "node_modules",
"solidity.compileUsingRemoteVersion": "v0.8.22",
"solidity.formatter": "forge",
"[solidity]": {
"editor.defaultFormatter": "JuanBlanco.solidity",
"editor.formatOnSave": true,
"editor.tabSize": 4,
},
{
"solidity.packageDefaultDependenciesContractsDirectory": "contracts",
"solidity.packageDefaultDependenciesDirectory": "node_modules",
"solidity.compileUsingRemoteVersion": "v0.8.22",
"solidity.formatter": "forge",
"[solidity]": {
"editor.defaultFormatter": "JuanBlanco.solidity",
"editor.formatOnSave": true,
"editor.tabSize": 4
},
"editor.formatOnSave": true
}
6 changes: 3 additions & 3 deletions contracts/factories/AbstractFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pragma solidity ^0.8.17;

import {APOwnerTrait} from "../traits/APOwnerTrait.sol";
import {IAddressProviderV3} from "../interfaces/IAddressProviderV3.sol";
import {IAddressProviderV3_1} from "../interfaces/IAddressProviderV3_1.sol";
import {AP_BYTECODE_REPOSITORY, NO_VERSION_CONTROL} from "../libraries/ContractLiterals.sol";

abstract contract AbstractFactory is APOwnerTrait {
Expand All @@ -13,14 +13,14 @@ abstract contract AbstractFactory is APOwnerTrait {
error CallerIsNotMarketConfiguratorException();

modifier marketConfiguratorOnly() {
if (IAddressProviderV3(addressProvider).isMarketConfigurator(msg.sender)) {
if (IAddressProviderV3_1(addressProvider).isMarketConfigurator(msg.sender)) {
revert CallerIsNotMarketConfiguratorException();
}
_;
}

constructor(address _addressProvider) APOwnerTrait(_addressProvider) {
bytecodeRepository =
IAddressProviderV3(_addressProvider).getAddressOrRevert(AP_BYTECODE_REPOSITORY, NO_VERSION_CONTROL);
IAddressProviderV3_1(_addressProvider).getAddressOrRevert(AP_BYTECODE_REPOSITORY, NO_VERSION_CONTROL);
}
}
6 changes: 3 additions & 3 deletions contracts/factories/MarketConfiguratorFactoryV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {AbstractFactory} from "./AbstractFactory.sol";
import {MarketConfigurator} from "../market/MarketConfigurator.sol";
import {ACL} from "../market/ACL.sol";
import {ContractsRegister} from "../market/ContractsRegister.sol";
import {IAddressProviderV3} from "../interfaces/IAddressProviderV3.sol";
import {IAddressProviderV3_1} from "../interfaces/IAddressProviderV3_1.sol";
import {IVersion} from "@gearbox-protocol/core-v3/contracts/interfaces/base/IVersion.sol";

import {IBytecodeRepository} from "../interfaces/IBytecodeRepository.sol";
Expand Down Expand Up @@ -54,13 +54,13 @@ contract MarketConfiguratorFactoryV3 is AbstractFactory, IVersion {
/// Makes market configurator contract owner
acl.transferOwnership(_marketConfigurator);

IAddressProviderV3(addressProvider).addMarketConfigurator(_marketConfigurator);
IAddressProviderV3_1(addressProvider).addMarketConfigurator(_marketConfigurator);
}

function removeMarketConfigurator(address _marketConfigurator) external apOwnerOnly {
if (MarketConfigurator(_marketConfigurator).pools().length != 0) {
revert CantRemoveMarketConfiguratorWithExistingPoolsException();
}
IAddressProviderV3(addressProvider).removeMarketConfigurator(_marketConfigurator);
IAddressProviderV3_1(addressProvider).removeMarketConfigurator(_marketConfigurator);
}
}
24 changes: 21 additions & 3 deletions contracts/global/AddressProviderV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {ICreditManagerV3} from "@gearbox-protocol/core-v3/contracts/interfaces/I
import {IMarketConfiguratorV3} from "../interfaces/IMarketConfiguratorV3.sol";
import {IContractsRegister} from "../interfaces/IContractsRegister.sol";

import {IAddressProviderV3} from "../interfaces/IAddressProviderV3.sol";
import {IAddressProviderV3_1, ContractValue} from "../interfaces/IAddressProviderV3_1.sol";
import {
AddressNotFoundException,
CallerNotConfiguratorException
Expand All @@ -29,9 +29,14 @@ import {

import {LibString} from "@solady/utils/LibString.sol";

struct ContractKey {
string key;
uint256 version;
}

/// @title Address provider V3
/// @notice Stores addresses of important contracts
contract AddressProviderV3_1 is Ownable2Step, IAddressProviderV3 {
contract AddressProviderV3_1 is Ownable2Step, IAddressProviderV3_1 {
using EnumerableSet for EnumerableSet.AddressSet;
// using LibString for string;
using LibString for bytes32;
Expand All @@ -55,6 +60,8 @@ contract AddressProviderV3_1 is Ownable2Step, IAddressProviderV3 {

mapping(string => uint256) public latestVersions;

ContractKey[] internal contractKeys;

modifier marketConfiguratorFactoryOnly() {
if (msg.sender != getAddressOrRevert(AP_MARKET_CONFIGURATOR_FACTORY, NO_VERSION_CONTROL)) {
revert MarketConfiguratorsOnlyException();
Expand Down Expand Up @@ -123,6 +130,7 @@ contract AddressProviderV3_1 is Ownable2Step, IAddressProviderV3 {
if (_version > latestVersion) {
latestVersions[key] = _version;
}
contractKeys.push(ContractKey(key, _version));

emit SetAddress(key, value, _version);
}
Expand Down Expand Up @@ -181,7 +189,17 @@ contract AddressProviderV3_1 is Ownable2Step, IAddressProviderV3 {
return marketConfigurator;
}

function owner() public view override(Ownable, IAddressProviderV3) returns (address) {
function owner() public view override(Ownable, IAddressProviderV3_1) returns (address) {
return Ownable.owner();
}

function getAllSavedContracts() external view returns (ContractValue[] memory) {
ContractValue[] memory result = new ContractValue[](contractKeys.length);
for (uint256 i = 0; i < contractKeys.length; i++) {
result[i] = ContractValue(
contractKeys[i].key, addresses[contractKeys[i].key][contractKeys[i].version], contractKeys[i].version
);
}
return result;
}
}
1 change: 1 addition & 0 deletions contracts/interfaces/IAddressProviderV3Leagcy.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,11 @@ pragma solidity ^0.8.17;

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

// uint256 constant NO_VERSION_CONTROL = 0;

// // string constant AP_CONTRACTS_REGISTER = "CONTRACTS_REGISTER";
// // string constant AP_ACL = "ACL";
// string constant AP_PRICE_ORACLE = "PRICE_ORACLE";
// string constant AP_ACCOUNT_FACTORY = "ACCOUNT_FACTORY";

// // string constant AP_DATA_COMPRESSOR = "DATA_COMPRESSOR";
// string constant AP_TREASURY = "TREASURY";
// string constant AP_GEAR_TOKEN = "GEAR_TOKEN";
// string constant AP_WETH_TOKEN = "WETH_TOKEN";
// // string constant AP_WETH_GATEWAY = "WETH_GATEWAY";
// string constant AP_ROUTER = "ROUTER";
// string constant AP_BOT_LIST = "BOT_LIST";
// string constant AP_GEAR_STAKING = "GEAR_STAKING";
// string constant AP_ZAPPER_REGISTER = "ZAPPER_REGISTER";

// string constant AP_BYTECODE_REPOSITORY = "BYTECODE_REPOSITORY";
struct ContractValue {
string key;
address value;
uint256 version;
}

interface IAddressProviderV3Events {
/// @notice Emitted when an address is set for a contract key
Expand All @@ -36,7 +23,7 @@ interface IAddressProviderV3Events {
}

/// @title Address provider V3 interface
interface IAddressProviderV3 is IAddressProviderV3Events, IVersion {
interface IAddressProviderV3_1 is IAddressProviderV3Events, IVersion {
function owner() external view returns (address);

function addresses(string memory key, uint256 _version) external view returns (address);
Expand All @@ -45,6 +32,8 @@ interface IAddressProviderV3 is IAddressProviderV3Events, IVersion {

function getAddressOrRevert(bytes32 key, uint256 _version) external view returns (address);

function getAllSavedContracts() external view returns (ContractValue[] memory);

function getLatestAddressOrRevert(string memory key) external view returns (address);

function getLatestAddressOrRevert(bytes32 _key) external view virtual returns (address result);
Expand Down
4 changes: 4 additions & 0 deletions contracts/interfaces/IMarketConfiguratorV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ interface IMarketConfiguratorV3 is IVersion {
function adapterFactory() external view returns (address);

function controller() external view returns (address);

function pools() external view returns (address[] memory);

function owner() external view returns (address);
}
27 changes: 14 additions & 13 deletions contracts/market/MarketConfigurator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {IContractsRegister} from "../interfaces/IContractsRegister.sol";
import {IPoolV3} from "@gearbox-protocol/core-v3/contracts/interfaces/IPoolV3.sol";
import {IPoolQuotaKeeperV3} from "@gearbox-protocol/core-v3/contracts/interfaces/IPoolQuotaKeeperV3.sol";

import {IAddressProviderV3} from "../interfaces/IAddressProviderV3.sol";
import {IAddressProviderV3_1} from "../interfaces/IAddressProviderV3_1.sol";
import {IContractsRegister} from "../interfaces/IContractsRegister.sol";
import {IACL} from "../interfaces/IACL.sol";

Expand Down Expand Up @@ -121,11 +121,12 @@ contract MarketConfigurator is ACLTrait, IMarketConfiguratorV3 {
name = _name;
treasury = _treasury;

interestModelFactory = IAddressProviderV3(_addressProvider).getLatestAddressOrRevert(AP_INTEREST_MODEL_FACTORY);
poolFactory = IAddressProviderV3(_addressProvider).getLatestAddressOrRevert(AP_POOL_FACTORY);
creditFactory = IAddressProviderV3(_addressProvider).getLatestAddressOrRevert(AP_CREDIT_FACTORY);
priceOracleFactory = IAddressProviderV3(_addressProvider).getLatestAddressOrRevert(AP_PRICE_ORACLE_FACTORY);
adapterFactory = IAddressProviderV3(_addressProvider).getLatestAddressOrRevert(AP_ADAPTER_FACTORY);
interestModelFactory =
IAddressProviderV3_1(_addressProvider).getLatestAddressOrRevert(AP_INTEREST_MODEL_FACTORY);
poolFactory = IAddressProviderV3_1(_addressProvider).getLatestAddressOrRevert(AP_POOL_FACTORY);
creditFactory = IAddressProviderV3_1(_addressProvider).getLatestAddressOrRevert(AP_CREDIT_FACTORY);
priceOracleFactory = IAddressProviderV3_1(_addressProvider).getLatestAddressOrRevert(AP_PRICE_ORACLE_FACTORY);
adapterFactory = IAddressProviderV3_1(_addressProvider).getLatestAddressOrRevert(AP_ADAPTER_FACTORY);

controller = address(new ControllerTimelockV3(_acl, _vetoAdmin));
}
Expand Down Expand Up @@ -207,14 +208,14 @@ contract MarketConfigurator is ACLTrait, IMarketConfiguratorV3 {

address creditManager = CreditFactoryV3(creditFactory).deployCreditManager(
pool,
IAddressProviderV3(addressProvider).getLatestAddressOrRevert(AP_ACCOUNT_FACTORY),
IAddressProviderV3_1(addressProvider).getLatestAddressOrRevert(AP_ACCOUNT_FACTORY),
priceOracles[pool],
_name,
latestVersions[AP_CREDIT_MANAGER], // TODO: Fee token case(?)
salt
);

IAddressProviderV3(addressProvider).registerCreditManager(creditManager);
IAddressProviderV3_1(addressProvider).registerCreditManager(creditManager);

bool expirable = expirationDate != 0;

Expand All @@ -238,7 +239,7 @@ contract MarketConfigurator is ACLTrait, IMarketConfiguratorV3 {

address pqk = IPoolV3(pool).poolQuotaKeeper();
IPoolQuotaKeeperV3(pqk).addCreditManager(creditManager);
IAddressProviderV3(addressProvider).registerCreditManager(creditManager);
IAddressProviderV3_1(addressProvider).registerCreditManager(creditManager);
}

function updateCreditFacade(address creditManager, address _degenNFT, bool _expirable, uint256 _version)
Expand Down Expand Up @@ -397,13 +398,13 @@ contract MarketConfigurator is ACLTrait, IMarketConfiguratorV3 {
emit SetName(_newName);
}

//
function pools() external view virtual returns (address[] memory) {
// returns all pools
function pools() external view virtual override returns (address[] memory) {
return IContractsRegister(acl).getPools();
}

//
function owner() external view returns (address) {
// returns owner
function owner() external view override returns (address) {
return IACL(acl).owner();
}
}
4 changes: 2 additions & 2 deletions contracts/market/MarketConfiguratorLegacy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {IPoolV3} from "@gearbox-protocol/core-v3/contracts/interfaces/IPoolV3.so
import {IPoolQuotaKeeperV3} from "@gearbox-protocol/core-v3/contracts/interfaces/IPoolQuotaKeeperV3.sol";

import {IVersion} from "@gearbox-protocol/core-v3/contracts/interfaces/base/IVersion.sol";
import {IAddressProviderV3} from "../interfaces/IAddressProviderV3.sol";
import {IAddressProviderV3_1} from "../interfaces/IAddressProviderV3_1.sol";
import {IContractsRegister} from "../interfaces/IContractsRegister.sol";
import {IACL} from "../interfaces/IACL.sol";

Expand Down Expand Up @@ -82,7 +82,7 @@ contract MarketConfiguratorLegacy is MarketConfigurator {
address pool = _pools[i];

priceOracles[pool] = priceOracle;
IAddressProviderV3(addressProvider).registerPool(pool);
IAddressProviderV3_1(addressProvider).registerPool(pool);
emit CreateMarket(pool, IPoolV3(pool).asset(), IPoolV3(pool).name(), IPoolV3(pool).symbol());
}

Expand Down
4 changes: 2 additions & 2 deletions contracts/traits/APOwnerTrait.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// (c) Gearbox Foundation, 2024.
pragma solidity ^0.8.17;

import {IAddressProviderV3} from "../interfaces/IAddressProviderV3.sol";
import {IAddressProviderV3_1} from "../interfaces/IAddressProviderV3_1.sol";
import {CallerNotConfiguratorException} from "@gearbox-protocol/core-v3/contracts/interfaces/IExceptions.sol";
import {SanityCheckTrait} from "@gearbox-protocol/core-v3/contracts/traits/SanityCheckTrait.sol";

Expand All @@ -28,7 +28,7 @@ abstract contract APOwnerTrait is SanityCheckTrait {
/// @dev Reverts if the caller is not the configurator
/// @dev Used to cut contract size on modifiers
function _ensureCallerIsConfigurator() internal view {
if (IAddressProviderV3(addressProvider).owner() != msg.sender) {
if (IAddressProviderV3_1(addressProvider).owner() != msg.sender) {
revert CallerNotConfiguratorException();
}
}
Expand Down
3 changes: 2 additions & 1 deletion remappings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ ds-test/=lib/forge-std/lib/ds-test/src/
forge-std/=lib/forge-std/src/
@1inch/=lib/@1inch/
@openzeppelin/=lib/@openzeppelin/
@gearbox-protocol=lib/@gearbox-protocol/
@gearbox-protocol=lib/@gearbox-protocol/
@solady=lib/@solady/src/
9 changes: 6 additions & 3 deletions script/Migrate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity ^0.8.17;

import "forge-std/Script.sol";
import {IAddressProviderV3} from "../contracts/interfaces/IAddressProviderV3.sol";
import {IAddressProviderV3_1} from "../contracts/interfaces/IAddressProviderV3_1.sol";
import {AddressProviderV3_1} from "../contracts/global/AddressProviderV3.sol";

import {
Expand Down Expand Up @@ -89,7 +89,8 @@ contract Migrate is Script {
_addressProvider.transferOwnership(IACL(acl).owner());

/// AddressProvider migration
APMigration[16] memory migrations = [
APMigration[17] memory migrations = [
APMigration({name: AP_GEAR_TOKEN, version: 0}),
APMigration({name: AP_WETH_TOKEN, version: 0}),
APMigration({name: AP_GEAR_TOKEN, version: 0}),
APMigration({name: AP_BOT_LIST, version: 300}),
Expand All @@ -111,11 +112,13 @@ contract Migrate is Script {
uint256 len = migrations.length;

for (uint256 i; i < len; i++) {
string memory key = string(abi.encodePacked(migrations[i].name));
string memory key = migrations[i].name.fromSmallString();

try IAddressProviderV3Legacy(oldAddressProvider).getAddressOrRevert(
migrations[i].name, migrations[i].version
) returns (address oldAddress) {
console.log("migrating", key, oldAddress);

_addressProvider.setAddress({
key: key,
value: oldAddress,
Expand Down

0 comments on commit 6c4d38f

Please sign in to comment.