Skip to content

Commit

Permalink
feat: minor code dump
Browse files Browse the repository at this point in the history
  • Loading branch information
lekhovitsky committed Jan 9, 2025
1 parent c0cd2ea commit 1df5e22
Show file tree
Hide file tree
Showing 20 changed files with 301 additions and 185 deletions.
41 changes: 3 additions & 38 deletions contracts/factories/AbstractFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
// (c) Gearbox Foundation, 2024.
pragma solidity ^0.8.23;

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

import {AbstractDeployer} from "../helpers/AbstractDeployer.sol";

import {IFactory} from "../interfaces/factories/IFactory.sol";
Expand All @@ -16,28 +13,12 @@ import {Call} from "../interfaces/Types.sol";
import {AP_MARKET_CONFIGURATOR_FACTORY, NO_VERSION_CONTROL} from "../libraries/ContractLiterals.sol";

abstract contract AbstractFactory is AbstractDeployer, IFactory {
// --------------- //
// STATE VARIABLES //
// --------------- //

address public immutable override marketConfiguratorFactory;

// --------- //
// MODIFIERS //
// --------- //

modifier onlyMarketConfigurators() {
_ensureCallerIsMarketConfigurator();
_;
}

// ----------- //
// CONSTRUCTOR //
// ----------- //

constructor(address addressProvider_) AbstractDeployer(addressProvider_) {
marketConfiguratorFactory = _getAddressOrRevert(AP_MARKET_CONFIGURATOR_FACTORY, NO_VERSION_CONTROL);
}
constructor(address addressProvider_) AbstractDeployer(addressProvider_) {}

// ------------- //
// CONFIGURATION //
Expand All @@ -56,29 +37,13 @@ abstract contract AbstractFactory is AbstractDeployer, IFactory {
// --------- //

function _ensureCallerIsMarketConfigurator() internal view {
// QUESTION: can MCF be upgraded?
address marketConfiguratorFactory = _getAddressOrRevert(AP_MARKET_CONFIGURATOR_FACTORY, NO_VERSION_CONTROL);
if (IMarketConfiguratorFactory(marketConfiguratorFactory).isMarketConfigurator(msg.sender)) {
revert CallerIsNotMarketConfiguratorException(msg.sender);
}
}

function _isVotingContract(address votingContract) internal view returns (bool) {
try IVotingContract(votingContract).voter() returns (address) {
return true;
} catch {
return false;
}
}

function _setVotingContractStatus(address votingContract, bool allowed) internal view returns (Call memory) {
return Call({
target: marketConfiguratorFactory,
callData: abi.encodeCall(
IMarketConfiguratorFactory.setVotingContractStatus,
(votingContract, allowed ? VotingContractStatus.ALLOWED : VotingContractStatus.UNVOTE_ONLY)
)
});
}

function _authorizeFactory(address marketConfigurator, address suite, address target)
internal
view
Expand Down
17 changes: 4 additions & 13 deletions contracts/factories/InterestRateModelFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import {AbstractFactory} from "./AbstractFactory.sol";
import {AbstractMarketFactory} from "./AbstractMarketFactory.sol";

contract InterestRateModelFactory is AbstractMarketFactory, IInterestRateModelFactory {
using CallBuilder for Call[];

/// @notice Contract version
uint256 public constant override version = 3_10;

Expand Down Expand Up @@ -58,20 +56,13 @@ contract InterestRateModelFactory is AbstractMarketFactory, IInterestRateModelFa
// MARKET HOOKS //
// ------------ //

function onUpdateInterestRateModel(address pool, address newInterestRateModel, address oldInterestRateModel)
function onUpdateInterestRateModel(address pool, address, address oldInterestRateModel)
external
view
override(AbstractMarketFactory, IMarketFactory)
returns (Call[] memory calls)
returns (Call[] memory)
{
if (_isVotingContract(oldInterestRateModel)) {
calls = calls.append(_setVotingContractStatus(oldInterestRateModel, false));
}
if (_isVotingContract(newInterestRateModel)) {
calls = calls.append(_setVotingContractStatus(newInterestRateModel, true));
}

calls = calls.append(_unauthorizeFactory(msg.sender, pool, oldInterestRateModel));
return CallBuilder.build(_unauthorizeFactory(msg.sender, pool, oldInterestRateModel));
}

// ------------- //
Expand All @@ -82,7 +73,7 @@ contract InterestRateModelFactory is AbstractMarketFactory, IInterestRateModelFa
external
view
override(AbstractFactory, IFactory)
returns (Call[] memory calls)
returns (Call[] memory)
{
return CallBuilder.build(Call(_interestRateModel(pool), callData));
}
Expand Down
16 changes: 5 additions & 11 deletions contracts/factories/PoolFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {AbstractFactory} from "./AbstractFactory.sol";
import {AbstractMarketFactory} from "./AbstractMarketFactory.sol";

interface IConfigureActions {
function setWithdrawFee(uint256 fee) external;
function setTotalDebtLimit(uint256 limit) external;
function setCreditManagerDebtLimit(address creditManager, uint256 limit) external;
function setTokenLimit(address token, uint96 limit) external;
Expand Down Expand Up @@ -110,9 +109,9 @@ contract PoolFactory is AbstractMarketFactory, IPoolFactory {
external
view
override(AbstractMarketFactory, IMarketFactory)
returns (Call[] memory calls)
returns (Call[] memory)
{
calls = CallBuilder.build(
return CallBuilder.build(
_setInterestRateModel(pool, interestRateModel), _setRateKeeper(_quotaKeeper(pool), rateKeeper)
);
}
Expand All @@ -121,13 +120,13 @@ contract PoolFactory is AbstractMarketFactory, IPoolFactory {
external
view
override(AbstractMarketFactory, IMarketFactory)
returns (Call[] memory calls)
returns (Call[] memory)
{
if (IPoolV3(pool).totalBorrowed() != 0) {
revert CantShutdownMarketWithNonZeroDebtException(pool);
}

calls = CallBuilder.build(_setTotalDebtLimit(pool, 0), _setWithdrawFee(pool, 0));
return CallBuilder.build(_setTotalDebtLimit(pool, 0));
}

function onCreateCreditSuite(address creditManager)
Expand Down Expand Up @@ -188,8 +187,7 @@ contract PoolFactory is AbstractMarketFactory, IPoolFactory {
{
bytes4 selector = bytes4(callData);
if (
selector == IConfigureActions.setWithdrawFee.selector
|| selector == IConfigureActions.setTotalDebtLimit.selector
selector == IConfigureActions.setTotalDebtLimit.selector
|| selector == IConfigureActions.setCreditManagerDebtLimit.selector
|| selector == IConfigureActions.pause.selector || selector == IConfigureActions.unpause.selector
) {
Expand Down Expand Up @@ -281,10 +279,6 @@ contract PoolFactory is AbstractMarketFactory, IPoolFactory {
return Call(pool, abi.encodeCall(IPoolV3.setCreditManagerDebtLimit, (creditManager, limit)));
}

function _setWithdrawFee(address pool, uint256 fee) internal pure returns (Call memory) {
return Call(pool, abi.encodeCall(IPoolV3.setWithdrawFee, (fee)));
}

function _setRateKeeper(address quotaKeeper, address rateKeeper) internal pure returns (Call memory) {
return Call(quotaKeeper, abi.encodeCall(IPoolQuotaKeeperV3.setGauge, (rateKeeper)));
}
Expand Down
52 changes: 22 additions & 30 deletions contracts/factories/RateKeeperFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ contract RateKeeperFactory is AbstractMarketFactory, IRateKeeperFactory {
override(AbstractMarketFactory, IMarketFactory)
returns (Call[] memory)
{
return _installRateKeeper(rateKeeper, _getRateKeeperType(rateKeeper));
if (_getRateKeeperType(rateKeeper) == "RATE_KEEPER::GAUGE") {
return CallBuilder.build(_setFrozenEpoch(rateKeeper, false));
}
return CallBuilder.build();
}

function onShutdownMarket(address pool)
Expand All @@ -92,7 +95,10 @@ contract RateKeeperFactory is AbstractMarketFactory, IRateKeeperFactory {
returns (Call[] memory)
{
address rateKeeper = _rateKeeper(_quotaKeeper(pool));
return _uninstallRateKeeper(rateKeeper, _getRateKeeperType(rateKeeper));
if (_getRateKeeperType(rateKeeper) == "RATE_KEEPER::GAUGE") {
return CallBuilder.build(_setFrozenEpoch(rateKeeper, true));
}
return CallBuilder.build();
}

function onUpdateRateKeeper(address pool, address newRateKeeper, address oldRateKeeper)
Expand All @@ -108,11 +114,13 @@ contract RateKeeperFactory is AbstractMarketFactory, IRateKeeperFactory {
for (uint256 i; i < numTokens; ++i) {
calls[i] = _addToken(newRateKeeper, tokens[i], type_);
}
calls = calls.extend(
_uninstallRateKeeper(oldRateKeeper, _getRateKeeperType(oldRateKeeper)).extend(
_installRateKeeper(newRateKeeper, type_)
).append(_unauthorizeFactory(msg.sender, pool, oldRateKeeper))
);
if (_getRateKeeperType(oldRateKeeper) == "RATE_KEEPER::GAUGE") {
calls = calls.append(_setFrozenEpoch(oldRateKeeper, true));
}
if (type_ == "RATE_KEEPER::GAUGE") {
calls = calls.append(_setFrozenEpoch(oldRateKeeper, false));
}
calls = calls.append(_unauthorizeFactory(msg.sender, pool, oldRateKeeper));
}

function onAddToken(address pool, address token, address)
Expand Down Expand Up @@ -149,44 +157,28 @@ contract RateKeeperFactory is AbstractMarketFactory, IRateKeeperFactory {
try IRateKeeper(rateKeeper).contractType() returns (bytes32 type_) {
return type_;
} catch {
return "RK_GAUGE";
return "RATE_KEEPER::GAUGE";
}
}

function _isForbiddenConfigurationCall(address rateKeeper, bytes4 selector) internal view returns (bool) {
if (_getRateKeeperType(rateKeeper) == "RK_GAUGE") {
if (_getRateKeeperType(rateKeeper) == "RATE_KEEPER::GAUGE") {
return selector == IRateKeeper.addToken.selector || selector == IGaugeV3.addQuotaToken.selector
|| selector == IGaugeV3.setFrozenEpoch.selector || selector == bytes4(keccak256("setController(address)"));
}
return selector == IRateKeeper.addToken.selector;
}

function _installRateKeeper(address rateKeeper, bytes32 type_) internal view returns (Call[] memory calls) {
if (type_ == "RK_GAUGE") {
calls = CallBuilder.build(Call(rateKeeper, abi.encodeCall(IGaugeV3.setFrozenEpoch, false)));
}

if (_isVotingContract(rateKeeper)) {
calls = calls.append(_setVotingContractStatus(rateKeeper, true));
}
}

function _uninstallRateKeeper(address rateKeeper, bytes32 type_) internal view returns (Call[] memory calls) {
if (type_ == "RK_GAUGE") {
calls = CallBuilder.build(Call(rateKeeper, abi.encodeCall(IGaugeV3.setFrozenEpoch, true)));
}

if (_isVotingContract(rateKeeper)) {
calls = calls.append(_setVotingContractStatus(rateKeeper, false));
}
}

function _addToken(address rateKeeper, address token, bytes32 type_) internal pure returns (Call memory) {
return Call(
rateKeeper,
type_ == "RK_GAUGE"
type_ == "RATE_KEEPER::GAUGE"
? abi.encodeCall(IGaugeV3.addQuotaToken, (token, 1, 1))
: abi.encodeCall(IRateKeeper.addToken, token)
);
}

function _setFrozenEpoch(address gauge, bool status) internal pure returns (Call memory) {
return Call(gauge, abi.encodeCall(IGaugeV3.setFrozenEpoch, status));
}
}
2 changes: 1 addition & 1 deletion contracts/helpers/AbstractDeployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ abstract contract AbstractDeployer {

function _getContractType(bytes32 domain, bytes32 postfix) internal pure returns (bytes32) {
if (postfix == 0) return domain;
return string.concat(domain.fromSmallString(), "_", postfix.fromSmallString()).toSmallString();
return string.concat(domain.fromSmallString(), "::", postfix.fromSmallString()).toSmallString();
}

function _deploy(bytes32 contractType, uint256 version, bytes memory constructorParams, bytes32 salt)
Expand Down
2 changes: 1 addition & 1 deletion contracts/helpers/DefaultIRM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {IInterestRateModel} from "@gearbox-protocol/core-v3/contracts/interfaces

contract DefaultIRM is IInterestRateModel {
uint256 public constant override version = 3_10;
bytes32 public constant override contractType = "IRM_DEFAULT";
bytes32 public constant override contractType = "IRM::DEFAULT";

function calcBorrowRate(uint256, uint256, bool) external pure override returns (uint256) {
return 0;
Expand Down
2 changes: 1 addition & 1 deletion contracts/helpers/DefaultLossPolicy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {ACLTrait} from "@gearbox-protocol/core-v3/contracts/traits/ACLTrait.sol"

contract DefaultLossPolicy is ACLTrait {
uint256 public constant version = 3_10;
bytes32 public constant contractType = "LOSS_POLICY_DEFAULT";
bytes32 public constant contractType = "LOSS_POLICY::DEFAULT";

bool public enabled;

Expand Down
1 change: 0 additions & 1 deletion contracts/interfaces/IMarketConfigurator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ interface IMarketConfigurator is IVersion {
function emergencyAdmin() external view returns (address);

function addressProvider() external view returns (address);
function marketConfiguratorFactory() external view returns (address);
function acl() external view returns (address);
function contractsRegister() external view returns (address);
function treasury() external view returns (address);
Expand Down
17 changes: 11 additions & 6 deletions contracts/interfaces/IMarketConfiguratorFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@
pragma solidity ^0.8.23;

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

interface IMarketConfiguratorFactory is IVersion {
event CreateMarketConfigurator(address indexed marketConfigurator, string name);
event ShutdownMarketConfigurator(address indexed marketConfigurator);

error AddressIsNotMarketConfiguratorException();
error AddressIsNotMarketConfiguratorException(address addr);
error CallerIsNotCrossChainGovernanceException(address caller);
error CallerIsNotMarketConfiguratorException(address caller);
error CallerIsNotMarketConfiguratorAdminException(address caller);
error CantShutdownMarketConfiguratorException();
error MarketConfiguratorIsAlreadyAddedException(address marketConfigurator);
error MarketConfiguratorIsAlreadyShutdownException(address marketConfigruator);

function isMarketConfigurator(address account) external view returns (bool);
function getMarketConfigurators() external view returns (address[] memory);
function getShutdownMarketConfigurators() external view returns (address[] memory);
function createMarketConfigurator(string calldata name) external returns (address marketConfigurator);
function createMarketConfigurator(
address admin,
address emergencyAdmin,
string calldata curatorName,
bool deployGovernor
) external returns (address marketConfigurator);
function shutdownMarketConfigurator(address marketConfigurator) external;

function configureGearStaking(bytes calldata data) external;
function setVotingContractStatus(address votingContract, VotingContractStatus status) external;
function addMarketConfigurator(address marketConfigurator) external;
}
Loading

0 comments on commit 1df5e22

Please sign in to comment.