Skip to content

Commit

Permalink
[Plugin][Core] tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fourlen committed May 15, 2024
1 parent 0e33b6f commit a06e6ca
Show file tree
Hide file tree
Showing 16 changed files with 340 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ interface IAlgebraPluginFactory {
/// @param token0 First token of the pool
/// @param token1 Second token of the pool
/// @return New plugin address
function createPlugin(address pool, address token0, address token1) external returns (address);
function createPlugin(address pool, address token0, address token1) external returns (address, address[] memory);
}
23 changes: 12 additions & 11 deletions src/plugin/contracts/BasePluginV1Factory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import './modules/FarmingModuleFactory.sol';
import './modules/OracleModuleFactory.sol';

import '@cryptoalgebra/algebra-modular-hub/contracts/AlgebraModularHub.sol';
import '@cryptoalgebra/integral-core/contracts/interfaces/IAlgebraPool.sol';
import '@cryptoalgebra/integral-core/contracts/libraries/Plugins.sol';

/// @title Algebra Integral 1.0 default plugin factory
/// @notice This contract creates Algebra default plugins for Algebra liquidity pools
Expand All @@ -36,12 +38,6 @@ contract BasePluginV1Factory is IBasePluginV1Factory {
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 @@ -61,13 +57,13 @@ contract BasePluginV1Factory is IBasePluginV1Factory {
}

/// @inheritdoc IAlgebraPluginFactory
function createPlugin(address pool, address, address) external override returns (address) {
function createPlugin(address pool, address, address) external override returns (address, address[] memory) {
require(msg.sender == algebraFactory);
return _createPlugin(pool);
}

/// @inheritdoc IBasePluginV1Factory
function createPluginForExistingPool(address token0, address token1) external override returns (address) {
function createPluginForExistingPool(address token0, address token1) external override returns (address, address[] memory) {
IAlgebraFactory factory = IAlgebraFactory(algebraFactory);
require(factory.hasRoleOrOwner(factory.POOLS_ADMINISTRATOR_ROLE(), msg.sender));

Expand All @@ -77,18 +73,23 @@ contract BasePluginV1Factory is IBasePluginV1Factory {
return _createPlugin(pool);
}

function _createPlugin(address pool) internal returns (address) {
function _createPlugin(address pool) internal returns (address, address[] memory) {
require(pluginByPool[pool] == address(0), 'Already created');
// IAlgebraBasePluginV1 volatilityOracle = new AlgebraBasePluginV1(pool, algebraFactory, address(this));
// volatilityOracle.changeFeeConfiguration(defaultFeeConfiguration);
// pluginByPool[pool] = address(volatilityOracle);
// return address(volatilityOracle);

IAlgebraPool(pool).setPluginConfig(uint8(Plugins.DYNAMIC_FEE));

AlgebraModularHub modularHub = new AlgebraModularHub(pool, algebraFactory);
address[] memory modulesAddresses = new address[](factoriesCounter);

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

modulesAddresses[i] = moduleAddress;

uint256 globalModuleIndex = modularHub.registerModule(moduleAddress);
InsertModuleParams[] memory insertModuleParams = IAlgebraModuleFactory(moduleFactoryAddress).getInsertModuleParams(globalModuleIndex);
Expand All @@ -98,7 +99,7 @@ contract BasePluginV1Factory is IBasePluginV1Factory {


pluginByPool[pool] = address(modularHub);
return address(modularHub);
return (address(modularHub), modulesAddresses);
}

/// @inheritdoc IBasePluginV1Factory
Expand Down
6 changes: 5 additions & 1 deletion src/plugin/contracts/base/AlgebraModuleFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity ^0.8.20;
import '../libraries/AdaptiveFee.sol';
import '../interfaces/IAlgebraModuleFactory.sol';

contract AlgebraModuleFactory is IAlgebraModuleFactory {
abstract contract AlgebraModuleFactory is IAlgebraModuleFactory {
bytes32 public constant ALGEBRA_BASE_PLUGIN_FACTORY_ADMINISTRATOR = keccak256('ALGEBRA_BASE_PLUGIN_FACTORY_ADMINISTRATOR');

address public immutable algebraFactory;
Expand All @@ -13,4 +13,8 @@ contract AlgebraModuleFactory is IAlgebraModuleFactory {
require(IAlgebraFactory(algebraFactory).hasRoleOrOwner(ALGEBRA_BASE_PLUGIN_FACTORY_ADMINISTRATOR, msg.sender), 'Only administrator');
_;
}

constructor(address _algebraFactory) {
algebraFactory = _algebraFactory;
}
}
2 changes: 1 addition & 1 deletion src/plugin/contracts/interfaces/IAlgebraModuleFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ interface IAlgebraModuleFactory {

function deploy(address modularHub) external returns (address);

function getInsertModuleParams(uint256 indexInHookList, uint256 moduleGlobalIndex) external pure returns (InsertModuleParams[] memory);
function getInsertModuleParams(uint256 moduleGlobalIndex) external pure returns (InsertModuleParams[] memory);
}
2 changes: 1 addition & 1 deletion src/plugin/contracts/interfaces/IBasePluginV1Factory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ interface IBasePluginV1Factory is IAlgebraPluginFactory {
/// @param token0 The address of first token in pool
/// @param token1 The address of second token in pool
/// @return The address of created plugin
function createPluginForExistingPool(address token0, address token1) external returns (address);
function createPluginForExistingPool(address token0, address token1) external returns (address, address[] memory);

/// @notice Changes initial fee configuration for new pools
/// @dev changes coefficients for sigmoids: α / (1 + e^( (β-x) / γ))
Expand Down
9 changes: 5 additions & 4 deletions src/plugin/contracts/modules/DynamicFeeModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import '../interfaces/plugins/IVolatilityOracle.sol';
import '../libraries/AdaptiveFee.sol';
import '../libraries/VolatilityOracle.sol';

import 'hardhat/console.sol';

contract DynamicFeeModule is AlgebraModule, IDynamicFeeManager, Timestamp {
string public constant MODULE_NAME = 'Dynamic Fee';

Expand Down Expand Up @@ -88,11 +90,10 @@ contract DynamicFeeModule is AlgebraModule, IDynamicFeeManager, Timestamp {
}

function _afterInitialize(
bytes memory params,
bytes memory /* params */,
uint16 /* poolFeeCache */
) internal override {
AfterInitializeParams memory decodedParams = abi.decode(params, (AfterInitializeParams));
IAlgebraPool(decodedParams.pool).setFee(_feeConfig.baseFee());
) internal view override {
ModuleUtils.returnDynamicFeeResult(_feeConfig.baseFee(), false);
}

function _beforeSwap(
Expand Down
5 changes: 2 additions & 3 deletions src/plugin/contracts/modules/DynamicFeeModuleFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ contract DynamicFeeModuleFactory is AlgebraModuleFactory {

AlgebraFeeConfiguration public defaultFeeConfiguration; // values of constants for sigmoids in fee calculation formula

constructor(address _algebraFactory) {
algebraFactory = _algebraFactory;
constructor(address _algebraFactory) AlgebraModuleFactory(_algebraFactory) {
defaultFeeConfiguration = AdaptiveFee.initialFeeConfiguration();
}

function setDefaultFeeConfiguration(AlgebraFeeConfiguration calldata newConfig) external override onlyAdministrator {
function setDefaultFeeConfiguration(AlgebraFeeConfiguration calldata newConfig) external onlyAdministrator {
AdaptiveFee.validateFeeConfiguration(newConfig);
defaultFeeConfiguration = newConfig;
emit DefaultFeeConfiguration(newConfig);
Expand Down
4 changes: 4 additions & 0 deletions src/plugin/contracts/modules/FarmingModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import '../interfaces/plugins/IFarmingPlugin.sol';
import '../interfaces/IBasePluginV1Factory.sol';
import '../interfaces/IAlgebraVirtualPool.sol';

import 'hardhat/console.sol';

contract FarmingModule is AlgebraModule, IFarmingPlugin, Timestamp {
string public constant MODULE_NAME = 'Farming';

Expand Down Expand Up @@ -83,6 +85,8 @@ contract FarmingModule is AlgebraModule, IFarmingPlugin, Timestamp {
bytes memory params ,
uint16 /* poolFeeCache */
) internal override {
console.log("Farming afterInitialize");

AfterSwapParams memory decodedParams = abi.decode(params, (AfterSwapParams));

address _incentive = incentive;
Expand Down
26 changes: 21 additions & 5 deletions src/plugin/contracts/modules/FarmingModuleFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,33 @@ import './FarmingModule.sol';
contract FarmingModuleFactory is AlgebraModuleFactory {
event FarmingAddress(address newFarmingAddress);

address public override farmingAddress;
address public farmingAddress;

function setFarmingAddress(address newFarmingAddress) external override onlyAdministrator {
constructor(address _algebraFactory) AlgebraModuleFactory(_algebraFactory) {}

function setFarmingAddress(address newFarmingAddress) external onlyAdministrator {
require(farmingAddress != newFarmingAddress);
farmingAddress = newFarmingAddress;
emit FarmingAddress(newFarmingAddress);
}

function deploy(DeployModuleParams calldata params) external override returns (address) {
FarmingModule farmingModule = new FarmingModule(params.modularHub, address(this));
function deploy(address modularHub) external override returns (address) {
FarmingModule farmingModule = new FarmingModule(modularHub, address(this));

return address(farmingModule);
}

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

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

return address(dynamicFeeModule);
return insertModuleParams;
}
}
28 changes: 26 additions & 2 deletions src/plugin/contracts/modules/OracleModuleFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,33 @@ import '../base/AlgebraModuleFactory.sol';
import './OracleModule.sol';

contract OracleModuleFactory is AlgebraModuleFactory {
function deploy(DeployModuleParams calldata params) external override returns (address) {
OracleModule oracleModule = new OracleModule(params.modularHub);
constructor(address _algebraFactory) AlgebraModuleFactory(_algebraFactory) {}

function deploy(address modularHub) external override returns (address) {
OracleModule oracleModule = new OracleModule(modularHub);

return address(oracleModule);
}

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: false
});

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

return insertModuleParams;
}
}
14 changes: 12 additions & 2 deletions src/plugin/contracts/test/MockPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import '@cryptoalgebra/integral-core/contracts/interfaces/pool/IAlgebraPoolPermi
import '@cryptoalgebra/integral-core/contracts/interfaces/pool/IAlgebraPoolErrors.sol';
import '@cryptoalgebra/integral-core/contracts/interfaces/plugin/IAlgebraPlugin.sol';

import 'hardhat/console.sol';

/// @title Mock of Algebra concentrated liquidity pool for plugins testing
contract MockPool is IAlgebraPoolActions, IAlgebraPoolPermissionedActions, IAlgebraPoolState {
struct GlobalState {
Expand Down Expand Up @@ -51,6 +53,8 @@ contract MockPool is IAlgebraPoolActions, IAlgebraPoolPermissionedActions, IAlge
/// @inheritdoc IAlgebraPoolState
address public override plugin;

address public pluginFactory;

address public override communityVault;

/// @inheritdoc IAlgebraPoolState
Expand Down Expand Up @@ -209,20 +213,26 @@ contract MockPool is IAlgebraPoolActions, IAlgebraPoolPermissionedActions, IAlge
tickSpacing = newTickSpacing;
}

function setPluginFactory(address newPluginFactory) external {
require(msg.sender == owner);
pluginFactory = newPluginFactory;
}

/// @inheritdoc IAlgebraPoolPermissionedActions
function setPlugin(address newPluginAddress) external override {
require(msg.sender == owner);
require(msg.sender == owner || msg.sender == pluginFactory);
plugin = newPluginAddress;
}

/// @inheritdoc IAlgebraPoolPermissionedActions
function setPluginConfig(uint8 newConfig) external override {
require(msg.sender == owner || msg.sender == plugin);
require(msg.sender == owner || msg.sender == plugin || msg.sender == pluginFactory);
globalState.pluginConfig = newConfig;
}

/// @inheritdoc IAlgebraPoolPermissionedActions
function setFee(uint16 newFee) external override {
console.log(msg.sender, owner, plugin); // plugin
require(msg.sender == owner || msg.sender == plugin);
bool isDynamicFeeEnabled = globalState.pluginConfig & uint8(Plugins.DYNAMIC_FEE) != 0;

Expand Down
47 changes: 39 additions & 8 deletions src/plugin/contracts/test/MockTimeDSFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import '../base/AlgebraFeeConfiguration.sol';
import '../libraries/AdaptiveFee.sol';

import './MockTimeAlgebraBasePluginV1.sol';
import './MockPool.sol';

import '../interfaces/IBasePluginV1Factory.sol';
import '../interfaces/IAlgebraModuleFactory.sol';

import '@cryptoalgebra/integral-core/contracts/interfaces/plugin/IAlgebraPluginFactory.sol';
import '@cryptoalgebra/algebra-modular-hub/contracts/AlgebraModularHub.sol';

contract MockTimeDSFactory is IBasePluginV1Factory {
/// @inheritdoc IBasePluginV1Factory
Expand All @@ -25,17 +28,26 @@ contract MockTimeDSFactory is IBasePluginV1Factory {
/// @inheritdoc IBasePluginV1Factory
address public override farmingAddress;

constructor(address _algebraFactory) {
mapping(uint256 factoryIndex => address factoryAddress) public factoryByIndex;
uint256 factoriesCounter;

constructor(address _algebraFactory, address _dynamicFeeModuleFactory, address _farmingModuleFactory, address _oracleModuleFactory) {
algebraFactory = _algebraFactory;
defaultFeeConfiguration = AdaptiveFee.initialFeeConfiguration();

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

factoriesCounter = 3;
}

/// @inheritdoc IAlgebraPluginFactory
function createPlugin(address pool, address, address) external override returns (address) {
function createPlugin(address pool, address, address) external override returns (address, address[] memory) {
return _createPlugin(pool);
}

function createPluginForExistingPool(address token0, address token1) external override returns (address) {
function createPluginForExistingPool(address token0, address token1) external override returns (address, address[] memory) {
IAlgebraFactory factory = IAlgebraFactory(algebraFactory);
require(factory.hasRoleOrOwner(factory.POOLS_ADMINISTRATOR_ROLE(), msg.sender));

Expand All @@ -49,11 +61,30 @@ contract MockTimeDSFactory is IBasePluginV1Factory {
pluginByPool[pool] = plugin;
}

function _createPlugin(address pool) internal returns (address) {
MockTimeAlgebraBasePluginV1 volatilityOracle = new MockTimeAlgebraBasePluginV1(pool, algebraFactory, address(this));
volatilityOracle.changeFeeConfiguration(defaultFeeConfiguration);
pluginByPool[pool] = address(volatilityOracle);
return address(volatilityOracle);
function _createPlugin(address pool) internal returns (address, address[] memory) {
require(pluginByPool[pool] == address(0), 'Already created');
AlgebraModularHub modularHub = new AlgebraModularHub(pool, algebraFactory);
console.log("modular hub: ", address(modularHub));

MockPool(pool).setPlugin(address(modularHub));
MockPool(pool).setPluginConfig(uint8(Plugins.DYNAMIC_FEE));

address[] memory modulesAddresses = new address[](factoriesCounter);

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

modulesAddresses[i] = moduleAddress;

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

modularHub.insertModulesToHookLists(insertModuleParams);
}

pluginByPool[pool] = address(modularHub);
return (address(modularHub), modulesAddresses);
}

/// @inheritdoc IBasePluginV1Factory
Expand Down
Loading

0 comments on commit a06e6ca

Please sign in to comment.