Skip to content

Commit

Permalink
[Plugin] factory list
Browse files Browse the repository at this point in the history
  • Loading branch information
fourlen committed May 14, 2024
1 parent 39e8ec9 commit 0e33b6f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
37 changes: 20 additions & 17 deletions src/plugin/contracts/BasePluginV1Factory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ contract BasePluginV1Factory is IBasePluginV1Factory {
/// @inheritdoc IBasePluginV1Factory
mapping(address poolAddress => address pluginAddress) public override pluginByPool;

address public dynamicFeeModuleFactory;
address public farmingModuleFactory;
address public oracleModuleFactory;
mapping(uint256 factoryIndex => address factoryAddress) public factoryByIndex;
uint256 factoriesCounter;

mapping(bytes4 => uint256) public moduleCounterByHookSelector;

// address public dynamicFeeModuleFactory;
// address public farmingModuleFactory;
// address public oracleModuleFactory;

modifier onlyAdministrator() {
require(IAlgebraFactory(algebraFactory).hasRoleOrOwner(ALGEBRA_BASE_PLUGIN_FACTORY_ADMINISTRATOR, msg.sender), 'Only administrator');
Expand All @@ -46,9 +51,11 @@ contract BasePluginV1Factory is IBasePluginV1Factory {
algebraFactory = _algebraFactory;
defaultFeeConfiguration = AdaptiveFee.initialFeeConfiguration();

dynamicFeeModuleFactory = _dynamicFeeModuleFactory;
farmingModuleFactory = _farmingModuleFactory;
oracleModuleFactory = _oracleModuleFactory;
factoryByIndex[0] = _oracleModuleFactory; // oracle module must be at 0 index
factoryByIndex[1] = _dynamicFeeModuleFactory;
factoryByIndex[2] = _farmingModuleFactory;

factoriesCounter = 3;

emit DefaultFeeConfiguration(defaultFeeConfiguration);
}
Expand Down Expand Up @@ -79,20 +86,16 @@ contract BasePluginV1Factory is IBasePluginV1Factory {

AlgebraModularHub modularHub = new AlgebraModularHub(pool, algebraFactory);

DeployModuleParams memory deployParams;
deployParams.modularHub = address(modularHub);

address oracleModule = OracleModuleFactory(oracleModuleFactory).deploy(deployParams);
for (uint256 i = 0; i < factoriesCounter; ++i) {
address moduleFactoryAddress = factoryByIndex[i];
address moduleAddress = IAlgebraModuleFactory(moduleFactoryAddress).deploy();

deployParams.oracleModule = oracleModule;
uint256 globalModuleIndex = modularHub.registerModule(moduleAddress);
InsertModuleParams[] memory insertModuleParams = IAlgebraModuleFactory(moduleFactoryAddress).getInsertModuleParams(globalModuleIndex);

address dynamicFeeModule = DynamicFeeModuleFactory(dynamicFeeModuleFactory).deploy(deployParams);
address farmingModule = FarmingModuleFactory(farmingModuleFactory).deploy(deployParams);

modularHub.insertModulesToHookLists(insertModuleParams);
}

modularHub.registerModule(oracleModule);
modularHub.registerModule(farmingModule);
modularHub.registerModule(dynamicFeeModule);

pluginByPool[pool] = address(modularHub);
return address(modularHub);
Expand Down
10 changes: 4 additions & 6 deletions src/plugin/contracts/interfaces/IAlgebraModuleFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
pragma solidity ^0.8.20;

import '@cryptoalgebra/integral-core/contracts/interfaces/IAlgebraFactory.sol';

struct DeployModuleParams {
address oracleModule;
address modularHub;
};
import '@cryptoalgebra/algebra-modular-hub/contracts/types/InsertModuleParams.sol';

interface IAlgebraModuleFactory {
function algebraFactory() external returns (address);

function deploy(DeployModuleParams calldata params) external returns (address);
function deploy(address modularHub) external returns (address);

function getInsertModuleParams(uint256 indexInHookList, uint256 moduleGlobalIndex) external pure returns (InsertModuleParams[] memory);
}
27 changes: 25 additions & 2 deletions src/plugin/contracts/modules/DynamicFeeModuleFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity ^0.8.20;
import '../libraries/AdaptiveFee.sol';
import '../base/AlgebraModuleFactory.sol';
import './DynamicFeeModule.sol';
import '@cryptoalgebra/integral-core/contracts/interfaces/plugin/IAlgebraPlugin.sol';


contract DynamicFeeModuleFactory is AlgebraModuleFactory {
Expand All @@ -22,10 +23,32 @@ contract DynamicFeeModuleFactory is AlgebraModuleFactory {
emit DefaultFeeConfiguration(newConfig);
}

function deploy(DeployModuleParams calldata params) external override returns (address) {
DynamicFeeModule dynamicFeeModule = new DynamicFeeModule(algebraFactory, address(this), params.oracleModule, params.modularHub);
function deploy(address modularHub) external override returns (address) {
DynamicFeeModule dynamicFeeModule = new DynamicFeeModule(algebraFactory, address(this), IAlgebraModularHub(modularHub).modules(0), modularHub);
dynamicFeeModule.changeFeeConfiguration(defaultFeeConfiguration);

return address(dynamicFeeModule);
}

function getInsertModuleParams(uint256 moduleGlobalIndex) external override pure returns (InsertModuleParams[] memory) {
InsertModuleParams[] memory insertModuleParams = new InsertModuleParams[](2);

insertModuleParams[0] = InsertModuleParams({
selector: IAlgebraPlugin.afterInitialize.selector,
indexInHookList: 0,
moduleGlobalIndex: moduleGlobalIndex,
useDelegate: false,
useDynamicFee: true
});

insertModuleParams[1] = InsertModuleParams({
selector: IAlgebraPlugin.beforeSwap.selector,
indexInHookList: 0,
moduleGlobalIndex: moduleGlobalIndex,
useDelegate: false,
useDynamicFee: true
});

return insertModuleParams;
}
}

0 comments on commit 0e33b6f

Please sign in to comment.