Skip to content

Commit 9d53869

Browse files
committed
feat: code dump
1 parent 7654517 commit 9d53869

24 files changed

+285
-718
lines changed

contracts/factories/AbstractMarketFactory.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ abstract contract AbstractMarketFactory is AbstractFactory, IMarketFactory {
3636

3737
function onUpdateRateKeeper(address, address, address) external virtual override returns (Call[] memory) {}
3838

39-
function onUpdateLossLiquidator(address, address, address) external virtual override returns (Call[] memory) {}
39+
function onUpdateLossPolicy(address, address, address) external virtual override returns (Call[] memory) {}
4040

4141
function onAddToken(address, address, address) external virtual override returns (Call[] memory) {}
4242

@@ -79,8 +79,8 @@ abstract contract AbstractMarketFactory is AbstractFactory, IMarketFactory {
7979
return IContractsRegister(_contractsRegister(pool)).getPriceOracle(pool);
8080
}
8181

82-
function _lossLiquidator(address pool) internal view returns (address) {
83-
return IContractsRegister(_contractsRegister(pool)).getLossLiquidator(pool);
82+
function _lossPolicy(address pool) internal view returns (address) {
83+
return IContractsRegister(_contractsRegister(pool)).getLossPolicy(pool);
8484
}
8585

8686
function _rateKeeper(address quotaKeeper) internal view returns (address) {

contracts/factories/CreditFactory.sol

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,10 @@ contract CreditFactory is AbstractFactory, ICreditFactory {
105105
onlyMarketConfigurators
106106
returns (DeployResult memory)
107107
{
108-
address contractsRegister = IMarketConfigurator(msg.sender).contractsRegister();
109-
address priceOracle = IContractsRegister(contractsRegister).getPriceOracle(pool);
110-
address lossLiquidator = IContractsRegister(contractsRegister).getLossLiquidator(pool);
111-
112108
(CreditManagerParams memory params, CreditFacadeParams memory facadeParams) =
113109
abi.decode(encodedParams, (CreditManagerParams, CreditFacadeParams));
114110

115-
address creditManager = _deployCreditManager(msg.sender, pool, priceOracle, params);
111+
address creditManager = _deployCreditManager(msg.sender, pool, params);
116112
address creditConfigurator = _deployCreditConfigurator(msg.sender, creditManager);
117113
address creditFacade = _deployCreditFacade(msg.sender, creditManager, facadeParams);
118114

@@ -124,7 +120,6 @@ contract CreditFactory is AbstractFactory, ICreditFactory {
124120
_authorizeFactory(msg.sender, creditManager, creditConfigurator),
125121
_authorizeFactory(msg.sender, creditManager, creditFacade),
126122
_setCreditFacade(creditConfigurator, creditFacade, false),
127-
_setLossLiquidator(creditConfigurator, lossLiquidator),
128123
_setDebtLimits(creditConfigurator, params.minDebt, params.maxDebt)
129124
)
130125
});
@@ -143,13 +138,13 @@ contract CreditFactory is AbstractFactory, ICreditFactory {
143138
return CallBuilder.build(_setPriceOracle(_creditConfigurator(creditManager), newPriceOracle));
144139
}
145140

146-
function onUpdateLossLiquidator(address creditManager, address newLossLiquidator, address)
141+
function onUpdateLossPolicy(address creditManager, address newLossPolicy, address)
147142
external
148143
view
149144
override
150145
returns (Call[] memory)
151146
{
152-
return CallBuilder.build(_setLossLiquidator(_creditConfigurator(creditManager), newLossLiquidator));
147+
return CallBuilder.build(_setLossPolicy(_creditConfigurator(creditManager), newLossPolicy));
153148
}
154149

155150
// ------------- //
@@ -238,12 +233,13 @@ contract CreditFactory is AbstractFactory, ICreditFactory {
238233
// INTERNALS //
239234
// --------- //
240235

241-
function _deployCreditManager(
242-
address marketConfigurator,
243-
address pool,
244-
address priceOracle,
245-
CreditManagerParams memory params
246-
) internal returns (address) {
236+
function _deployCreditManager(address marketConfigurator, address pool, CreditManagerParams memory params)
237+
internal
238+
returns (address)
239+
{
240+
address contractsRegister = IMarketConfigurator(marketConfigurator).contractsRegister();
241+
address priceOracle = IContractsRegister(contractsRegister).getPriceOracle(pool);
242+
247243
bytes32 postfix = _getTokenSpecificPostfix(IPoolV3(pool).asset());
248244

249245
// TODO: ensure that account factory is registered, add manager to it
@@ -269,7 +265,8 @@ contract CreditFactory is AbstractFactory, ICreditFactory {
269265
}
270266

271267
function _deployCreditConfigurator(address marketConfigurator, address creditManager) internal returns (address) {
272-
bytes memory constructorParams = abi.encode(creditManager);
268+
address acl = IMarketConfigurator(marketConfigurator).acl();
269+
bytes memory constructorParams = abi.encode(acl, creditManager);
273270

274271
return _deployLatestPatch({
275272
contractType: AP_CREDIT_CONFIGURATOR,
@@ -284,10 +281,13 @@ contract CreditFactory is AbstractFactory, ICreditFactory {
284281
returns (address)
285282
{
286283
address acl = IMarketConfigurator(marketConfigurator).acl();
284+
address contractsRegister = IMarketConfigurator(marketConfigurator).contractsRegister();
285+
address lossPolicy = IContractsRegister(contractsRegister).getLossPolicy(ICreditManagerV3(creditManager).pool());
286+
287287
// TODO: ensure that botList is registered, coincides with the previous one, add manager to it
288288
// TODO: ensure that degenNFT is registered, add facade to it
289289
bytes memory constructorParams =
290-
abi.encode(acl, creditManager, params.botList, weth, params.degenNFT, params.expirable);
290+
abi.encode(acl, creditManager, lossPolicy, params.botList, weth, params.degenNFT, params.expirable);
291291

292292
return _deployLatestPatch({
293293
contractType: AP_CREDIT_FACADE,
@@ -304,7 +304,7 @@ contract CreditFactory is AbstractFactory, ICreditFactory {
304304
address decodedCreditManager = address(bytes20(bytes32(params.constructorParams)));
305305
if (decodedCreditManager != creditManager) revert InvalidConstructorParamsException();
306306

307-
// NOTE: unlike other contracts, this might be deployed multiple times, so using the same salt
307+
// FIXME: unlike other contracts, this might be deployed multiple times, so using the same salt
308308
// can be an issue. Same thing can happen to rate keepers, IRMs, etc.
309309
return _deployLatestPatch({
310310
contractType: _getContractType(DOMAIN_ADAPTER, params.postfix),
@@ -346,12 +346,8 @@ contract CreditFactory is AbstractFactory, ICreditFactory {
346346
return Call(creditConfigurator, abi.encodeCall(ICreditConfiguratorV3.setPriceOracle, priceOracle));
347347
}
348348

349-
function _setLossLiquidator(address creditConfigurator, address lossLiquidator)
350-
internal
351-
pure
352-
returns (Call memory)
353-
{
354-
return Call(creditConfigurator, abi.encodeCall(ICreditConfiguratorV3.setLossLiquidator, lossLiquidator));
349+
function _setLossPolicy(address creditConfigurator, address lossPolicy) internal pure returns (Call memory) {
350+
return Call(creditConfigurator, abi.encodeCall(ICreditConfiguratorV3.setLossPolicy, lossPolicy));
355351
}
356352

357353
function _allowAdapter(address creditConfigurator, address adapter) internal pure returns (Call memory) {

contracts/factories/LossLiquidatorFactory.sol renamed to contracts/factories/LossPolicyFactory.sol

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,25 @@
33
// (c) Gearbox Foundation, 2024.
44
pragma solidity ^0.8.23;
55

6+
import {ILossPolicy} from "@gearbox-protocol/core-v3/contracts/interfaces/base/ILossPolicy.sol";
7+
68
import {IFactory} from "../interfaces/factories/IFactory.sol";
79
import {IMarketFactory} from "../interfaces/factories/IMarketFactory.sol";
8-
import {ILossLiquidatorFactory} from "../interfaces/factories/ILossLiquidatorFactory.sol";
10+
import {ILossPolicyFactory} from "../interfaces/factories/ILossPolicyFactory.sol";
911
import {Call, DeployParams, DeployResult} from "../interfaces/Types.sol";
1012

1113
import {CallBuilder} from "../libraries/CallBuilder.sol";
12-
import {AP_LOSS_LIQUIDATOR_FACTORY, DOMAIN_LOSS_LIQUIDATOR} from "../libraries/ContractLiterals.sol";
14+
import {AP_LOSS_POLICY_FACTORY, DOMAIN_LOSS_POLICY} from "../libraries/ContractLiterals.sol";
1315

1416
import {AbstractFactory} from "./AbstractFactory.sol";
1517
import {AbstractMarketFactory} from "./AbstractMarketFactory.sol";
1618

17-
contract LossLiquidatorFactory is AbstractMarketFactory, ILossLiquidatorFactory {
19+
contract LossPolicyFactory is AbstractMarketFactory, ILossPolicyFactory {
1820
/// @notice Contract version
1921
uint256 public constant override version = 3_10;
2022

2123
/// @notice Contract type
22-
bytes32 public constant override contractType = AP_LOSS_LIQUIDATOR_FACTORY;
24+
bytes32 public constant override contractType = AP_LOSS_POLICY_FACTORY;
2325

2426
/// @notice Constructor
2527
/// @param addressProvider_ Address provider contract address
@@ -29,43 +31,43 @@ contract LossLiquidatorFactory is AbstractMarketFactory, ILossLiquidatorFactory
2931
// DEPLOYMENT //
3032
// ---------- //
3133

32-
function deployLossLiquidator(address pool, DeployParams calldata params)
34+
function deployLossPolicy(address pool, DeployParams calldata params)
3335
external
3436
override
3537
onlyMarketConfigurators
3638
returns (DeployResult memory)
3739
{
3840
if (params.postfix == "ALIASED") {
39-
address decodedACL = abi.decode(params.constructorParams, (address));
40-
if (decodedACL != _acl(pool)) revert InvalidConstructorParamsException();
41+
address decodedPool = abi.decode(params.constructorParams, (address));
42+
if (decodedPool != pool) revert InvalidConstructorParamsException();
4143
} else {
4244
_validateDefaultConstructorParams(pool, params.constructorParams);
4345
}
4446

45-
address lossLiquidator = _deployLatestPatch({
46-
contractType: _getContractType(DOMAIN_LOSS_LIQUIDATOR, params.postfix),
47+
address lossPolicy = _deployLatestPatch({
48+
contractType: _getContractType(DOMAIN_LOSS_POLICY, params.postfix),
4749
minorVersion: version,
4850
constructorParams: params.constructorParams,
4951
salt: bytes32(bytes20(msg.sender))
5052
});
5153

5254
return DeployResult({
53-
newContract: lossLiquidator,
54-
onInstallOps: CallBuilder.build(_authorizeFactory(msg.sender, pool, lossLiquidator))
55+
newContract: lossPolicy,
56+
onInstallOps: CallBuilder.build(_authorizeFactory(msg.sender, pool, lossPolicy))
5557
});
5658
}
5759

5860
// ------------ //
5961
// MARKET HOOKS //
6062
// ------------ //
6163

62-
function onUpdateLossLiquidator(address pool, address, address oldLossLiquidator)
64+
function onUpdateLossPolicy(address pool, address, address oldLossPolicy)
6365
external
6466
view
6567
override(AbstractMarketFactory, IMarketFactory)
6668
returns (Call[] memory calls)
6769
{
68-
calls = CallBuilder.build(_unauthorizeFactory(msg.sender, pool, oldLossLiquidator));
70+
calls = CallBuilder.build(_unauthorizeFactory(msg.sender, pool, oldLossPolicy));
6971
}
7072

7173
// ------------- //
@@ -78,7 +80,7 @@ contract LossLiquidatorFactory is AbstractMarketFactory, ILossLiquidatorFactory
7880
override(AbstractFactory, IFactory)
7981
returns (Call[] memory)
8082
{
81-
return CallBuilder.build(Call(_lossLiquidator(pool), callData));
83+
return CallBuilder.build(Call(_lossPolicy(pool), callData));
8284
}
8385

8486
function emergencyConfigure(address pool, bytes calldata callData)
@@ -87,7 +89,10 @@ contract LossLiquidatorFactory is AbstractMarketFactory, ILossLiquidatorFactory
8789
override(AbstractFactory, IFactory)
8890
returns (Call[] memory)
8991
{
90-
// TODO: only allow to disable and pause
91-
return CallBuilder.build(Call(_lossLiquidator(pool), callData));
92+
bytes4 selector = bytes4(callData);
93+
if (selector != ILossPolicy.enable.selector && selector != ILossPolicy.disable.selector) {
94+
revert ForbiddenEmergencyConfigurationCallException(selector);
95+
}
96+
return CallBuilder.build(Call(_lossPolicy(pool), callData));
9297
}
9398
}

contracts/factories/PoolFactory.sol

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {IPoolQuotaKeeperV3} from "@gearbox-protocol/core-v3/contracts/interfaces
1111
import {IPoolV3} from "@gearbox-protocol/core-v3/contracts/interfaces/IPoolV3.sol";
1212
import {IPriceOracleV3} from "@gearbox-protocol/core-v3/contracts/interfaces/IPriceOracleV3.sol";
1313

14+
import {DefaultIRM} from "../helpers/DefaultIRM.sol";
15+
1416
import {IFactory} from "../interfaces/factories/IFactory.sol";
1517
import {IMarketFactory} from "../interfaces/factories/IMarketFactory.sol";
1618
import {IPoolFactory} from "../interfaces/factories/IPoolFactory.sol";
@@ -20,11 +22,7 @@ import {Call, DeployResult} from "../interfaces/Types.sol";
2022

2123
import {CallBuilder} from "../libraries/CallBuilder.sol";
2224
import {
23-
AP_DEFAULT_IRM,
24-
AP_POOL_FACTORY,
25-
AP_POOL_QUOTA_KEEPER,
26-
DOMAIN_POOL,
27-
NO_VERSION_CONTROL
25+
AP_POOL_FACTORY, AP_POOL_QUOTA_KEEPER, DOMAIN_POOL, NO_VERSION_CONTROL
2826
} from "../libraries/ContractLiterals.sol";
2927

3028
import {AbstractFactory} from "./AbstractFactory.sol";
@@ -73,7 +71,7 @@ contract PoolFactory is AbstractMarketFactory, IPoolFactory {
7371
/// @notice Constructor
7472
/// @param addressProvider_ Address provider contract address
7573
constructor(address addressProvider_) AbstractFactory(addressProvider_) {
76-
defaultInterestRateModel = _getAddressOrRevert(AP_DEFAULT_IRM, NO_VERSION_CONTROL);
74+
defaultInterestRateModel = address(new DefaultIRM());
7775
}
7876

7977
// ---------- //

contracts/factories/PriceOracleFactory.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ contract PriceOracleFactory is AbstractMarketFactory, IPriceOracleFactory {
7070
contractType: AP_PRICE_ORACLE,
7171
minorVersion: version,
7272
constructorParams: abi.encode(_acl(pool)),
73-
salt: bytes32(bytes20(msg.sender))
73+
salt: bytes32(bytes20(pool))
7474
});
7575

7676
return DeployResult({

contracts/global/MarketConfiguratorFactory.sol

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import {
2626
import {MarketConfiguratorLegacy} from "../market/legacy/MarketConfiguratorLegacy.sol";
2727
import {MarketConfigurator} from "../market/MarketConfigurator.sol";
2828

29+
// TODO: somehow need to add MC Legacy to MC Factory. maybe we can deploy it from here?
30+
2931
contract MarketConfiguratorFactory is Ownable2Step, AbstractDeployer, IMarketConfiguratorFactory {
3032
using Address for address;
3133
using EnumerableSet for EnumerableSet.AddressSet;
@@ -88,9 +90,9 @@ contract MarketConfiguratorFactory is Ownable2Step, AbstractDeployer, IMarketCon
8890

8991
function createMarketConfigurator(string calldata name) external override returns (address marketConfigurator) {
9092
// TODO: deploy timelocks
91-
marketConfigurator = _deploy({
93+
marketConfigurator = _deployLatestPatch({
9294
contractType: AP_MARKET_CONFIGURATOR,
93-
version: version,
95+
minorVersion: version,
9496
constructorParams: abi.encode(name, msg.sender, msg.sender, addressProvider),
9597
salt: bytes32(bytes20(msg.sender))
9698
});
@@ -113,23 +115,20 @@ contract MarketConfiguratorFactory is Ownable2Step, AbstractDeployer, IMarketCon
113115
emit ShutdownMarketConfigurator(marketConfigurator);
114116
}
115117

118+
// TODO: probably move these two to the instance manager, don't forget to change it in the MC
119+
116120
function setVotingContractStatus(address votingContract, VotingContractStatus status)
117121
external
118122
override
119123
onlyMarketConfigurators
120124
{
121-
// QUESTION: do we need to check that votingContract's voter is indeed the GEAR staking contract?
122125
_configureGearStaking(abi.encodeCall(IGearStakingV3.setVotingContractStatus, (votingContract, status)));
123126
}
124127

125128
function configureGearStaking(bytes calldata data) external override onlyOwner {
126129
_configureGearStaking(data);
127130
}
128131

129-
// --------- //
130-
// INTERNALS //
131-
// --------- //
132-
133132
function _configureGearStaking(bytes memory data) internal {
134133
address gearStaking = _getAddressOrRevert(AP_GEAR_STAKING, NO_VERSION_CONTROL);
135134
if (IGearStakingV3(gearStaking).version() < 3_10) {

contracts/helpers/AbstractDeployer.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ abstract contract AbstractDeployer {
5555
bytes memory constructorParams,
5656
bytes32 salt
5757
) internal returns (address) {
58+
// NOTE: it's best to add a check that deployed contract's version matches the expected one in the governor
5859
return _deploy(
5960
contractType,
6061
IBytecodeRepository(bytecodeRepository).getLatestPatchVersion(contractType, minorVersion),

contracts/helpers/DefaultIRM.sol

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-License-Identifier: BUSL-1.1
2+
// Gearbox Protocol. Generalized leverage for DeFi protocols
3+
// (c) Gearbox Foundation, 2024.
4+
pragma solidity ^0.8.23;
5+
6+
import {IInterestRateModel} from "@gearbox-protocol/core-v3/contracts/interfaces/base/IInterestRateModel.sol";
7+
8+
contract DefaultIRM is IInterestRateModel {
9+
uint256 public constant override version = 3_10;
10+
bytes32 public constant override contractType = "IRM_DEFAULT";
11+
12+
function calcBorrowRate(uint256, uint256, bool) external pure override returns (uint256) {
13+
return 0;
14+
}
15+
16+
function availableToBorrow(uint256, uint256) external pure override returns (uint256) {
17+
return 0;
18+
}
19+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-License-Identifier: BUSL-1.1
2+
// Gearbox Protocol. Generalized leverage for DeFi protocols
3+
// (c) Gearbox Foundation, 2024.
4+
pragma solidity ^0.8.23;
5+
6+
import {ACLTrait} from "@gearbox-protocol/core-v3/contracts/traits/ACLTrait.sol";
7+
8+
contract DefaultLossPolicy is ACLTrait {
9+
uint256 public constant version = 3_10;
10+
bytes32 public constant contractType = "LOSS_POLICY_DEFAULT";
11+
12+
bool public enabled;
13+
14+
constructor(address acl_) ACLTrait(acl_) {}
15+
16+
function isLiquidatable(address, address, bytes calldata) external view returns (bool) {
17+
return enabled;
18+
}
19+
20+
function enable() external configuratorOnly {
21+
enabled = true;
22+
}
23+
24+
function disable() external configuratorOnly {
25+
enabled = false;
26+
}
27+
}

0 commit comments

Comments
 (0)