Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
bc0e535
temp
fourlen Mar 25, 2025
8b489c1
+reblaanceManager
fourlen Mar 25, 2025
79c02d4
sorry
fourlen Mar 25, 2025
0851a13
Merge remote-tracking branch 'origin/integral-v1.2.1' into alm-plugin
debych Mar 26, 2025
c2643ae
[Plugin] add authorize in initializeALM
debych Mar 26, 2025
9af2a9b
[Plugin] add setters
debych Mar 26, 2025
2f87ae1
[Plugin] add alm-vault package in npm
debych Mar 26, 2025
d4c4502
[Plugin] create BaseRebalanceManager.sol
debych Mar 26, 2025
4d5e7ea
[Plugin] add ALM tests
debych Mar 26, 2025
1ca0116
bug fix
fourlen Mar 26, 2025
360959f
fix alm plugin factory
IliaAzhel Mar 26, 2025
30ca550
+fixtures
fourlen Mar 26, 2025
1b56985
add base testnet to hardhat config & deploy script fixes
IliaAzhel Mar 26, 2025
0314abc
first alm test
fourlen Mar 27, 2025
b4f2429
change comment
fourlen Mar 27, 2025
8243c09
fix unreachable code
fourlen Mar 27, 2025
2b6f8f0
-logs
fourlen Mar 27, 2025
c23f14e
-failed obtain twap
fourlen Mar 27, 2025
171ff0b
top fix
fourlen Mar 28, 2025
db2beac
top2 fix
fourlen Mar 28, 2025
17d8de0
[Plugin] swapAmount -> 0
debych Mar 28, 2025
94ac0f3
issue fixes
fourlen Mar 31, 2025
231a232
if -> require
fourlen Mar 31, 2025
021873b
add alm custom pool deployer
IliaAzhel Apr 1, 2025
5ff8b1e
add custom plugin deployer test
IliaAzhel Apr 1, 2025
b21a794
remove farming from alm custom pool deployer
IliaAzhel Apr 1, 2025
ff4bcef
-comments
fourlen Apr 2, 2025
021efe2
-more comments
fourlen Apr 2, 2025
1ae46a7
token1Decimals -> pairedTokenDecimals
fourlen Apr 2, 2025
2824012
-logs
fourlen Apr 2, 2025
b63cf0b
+test
fourlen Apr 3, 2025
88bf37d
only plugin
fourlen Apr 3, 2025
7cb21a2
Merge branch 'alm-plugin-custom' into alm-plugin
IliaAzhel Apr 4, 2025
6d5be5f
add base chain to hh config
IliaAzhel Apr 4, 2025
55014ef
Add deploy script
debych Apr 4, 2025
4d691fb
add factory address to the constructor
debych Apr 4, 2025
4024746
fix state, add some tests
fourlen Apr 10, 2025
9970bab
-comments
fourlen Apr 10, 2025
82c0778
-failedToObtainTWAPs, +tests
fourlen Apr 11, 2025
72ab616
some fixes, +tests
fourlen Apr 21, 2025
d07f460
fix tests
fourlen Apr 22, 2025
7de653b
+tests
fourlen Apr 30, 2025
4e2fe00
??
fourlen May 27, 2025
b95d2c4
+tests
fourlen May 27, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions src/plugin/contracts/AlgebraBasePluginALM.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.20;

import '@cryptoalgebra/integral-core/contracts/libraries/Plugins.sol';

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

import './plugins/DynamicFeePlugin.sol';
import './plugins/AlmPlugin.sol';
import './plugins/SlidingFeePlugin.sol';
import './plugins/VolatilityOraclePlugin.sol';

/// @title Algebra Integral 1.2.1 adaptive fee plugin
Comment thread
fourlen marked this conversation as resolved.
Outdated
contract AlgebraBasePluginALM is AlmPlugin, DynamicFeePlugin, VolatilityOraclePlugin {
using Plugins for uint8;

/// @inheritdoc IAlgebraPlugin
uint8 public constant override defaultPluginConfig =
uint8(Plugins.AFTER_INIT_FLAG | Plugins.BEFORE_SWAP_FLAG | Plugins.AFTER_SWAP_FLAG | Plugins.DYNAMIC_FEE);

constructor(
address _pool,
address _factory,
address _pluginFactory,
AlgebraFeeConfiguration memory _config
) AlgebraBasePlugin(_pool, _factory, _pluginFactory) DynamicFeePlugin(_config) {}

// ###### HOOKS ######

function beforeInitialize(address, uint160) external override onlyPool returns (bytes4) {
_updatePluginConfigInPool(defaultPluginConfig);
return IAlgebraPlugin.beforeInitialize.selector;
}

function afterInitialize(address, uint160, int24 tick) external override onlyPool returns (bytes4) {
_initialize_TWAP(tick);
return IAlgebraPlugin.afterInitialize.selector;
}

/// @dev unused
function beforeModifyPosition(address, address, int24, int24, int128, bytes calldata) external override onlyPool returns (bytes4, uint24) {
_updatePluginConfigInPool(defaultPluginConfig); // should not be called, reset config
return (IAlgebraPlugin.beforeModifyPosition.selector, 0);
}

/// @dev unused
function afterModifyPosition(address, address, int24, int24, int128, uint256, uint256, bytes calldata) external override onlyPool returns (bytes4) {
_updatePluginConfigInPool(defaultPluginConfig); // should not be called, reset config
return IAlgebraPlugin.afterModifyPosition.selector;
}

function beforeSwap(address, address, bool, int256, uint160, bool, bytes calldata) external override onlyPool returns (bytes4, uint24, uint24) {
_writeTimepoint();
uint88 volatilityAverage = _getAverageVolatilityLast();
uint24 fee = _getCurrentFee(volatilityAverage);
return (IAlgebraPlugin.beforeSwap.selector, fee, 0);
}

function afterSwap(address, address, bool, int256, uint160, int256, int256, bytes calldata) external override onlyPool returns (bytes4) {
( , int24 currentTick, , ) = _getPoolState();
uint32 lastBlockTimestamp = _getLastBlockTimestamp();

bool failedToObtainTWAP;
int24 slowTwapTick;
int24 fastTwapTick;

if (_ableToGetTimepoints(slowTwapPeriod)) {
slowTwapTick = _getTwapTick(slowTwapPeriod);
fastTwapTick = _getTwapTick(fastTwapPeriod);
} else {
failedToObtainTWAP = true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO filedToObtainTWAP handling should be completely removed then. If the only case when it is true is before slowTwapPeriod then we perhaps should just skip rebalance and not set pause and Special state

}

_obtainTWAPAndRebalance(currentTick, slowTwapTick, fastTwapTick, lastBlockTimestamp, failedToObtainTWAP);

return IAlgebraPlugin.afterSwap.selector;
}

/// @dev unused
function beforeFlash(address, address, uint256, uint256, bytes calldata) external override onlyPool returns (bytes4) {
_updatePluginConfigInPool(defaultPluginConfig); // should not be called, reset config
return IAlgebraPlugin.beforeFlash.selector;
}

/// @dev unused
function afterFlash(address, address, uint256, uint256, uint256, uint256, bytes calldata) external override onlyPool returns (bytes4) {
_updatePluginConfigInPool(defaultPluginConfig); // should not be called, reset config
return IAlgebraPlugin.afterFlash.selector;
}

function getCurrentFee() external view override returns (uint16 fee) {
uint88 volatilityAverage = _getAverageVolatilityLast();
fee = _getCurrentFee(volatilityAverage);
}
}
80 changes: 80 additions & 0 deletions src/plugin/contracts/AlgebraBasePluginALMFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.20;

import './interfaces/IBasePluginV1Factory.sol';
import './libraries/AdaptiveFee.sol';
import './AlgebraBasePluginV1.sol';

/// @title Algebra Integral 1.2.1 default plugin factory
/// @notice This contract creates Algebra adaptive fee plugins for Algebra liquidity pools
/// @dev This plugin factory can only be used for Algebra base pools
contract AlgebraBasePluginALMFactory is IBasePluginV1Factory {
Comment thread
fourlen marked this conversation as resolved.
/// @inheritdoc IBasePluginV1Factory
bytes32 public constant override ALGEBRA_BASE_PLUGIN_FACTORY_ADMINISTRATOR = keccak256('ALGEBRA_BASE_PLUGIN_FACTORY_ADMINISTRATOR');

/// @inheritdoc IBasePluginV1Factory
address public immutable override algebraFactory;

/// @inheritdoc IBasePluginV1Factory
AlgebraFeeConfiguration public override defaultFeeConfiguration; // values of constants for sigmoids in fee calculation formula

/// @inheritdoc IBasePluginV1Factory
address public override farmingAddress;

/// @inheritdoc IBasePluginV1Factory
mapping(address poolAddress => address pluginAddress) public override pluginByPool;

modifier onlyAdministrator() {
require(IAlgebraFactory(algebraFactory).hasRoleOrOwner(ALGEBRA_BASE_PLUGIN_FACTORY_ADMINISTRATOR, msg.sender), 'Only administrator');
_;
}

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

/// @inheritdoc IAlgebraPluginFactory
function beforeCreatePoolHook(address pool, address, address, address, address, bytes calldata) external override returns (address) {
require(msg.sender == algebraFactory);
return _createPlugin(pool);
}

/// @inheritdoc IAlgebraPluginFactory
function afterCreatePoolHook(address, address, address) external view override {
require(msg.sender == algebraFactory);
}

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

address pool = factory.poolByPair(token0, token1);
require(pool != address(0), 'Pool not exist');

return _createPlugin(pool);
}

function _createPlugin(address pool) internal returns (address) {
require(pluginByPool[pool] == address(0), 'Already created');
IDynamicFeeManager volatilityOracle = new AlgebraBasePluginV1(pool, algebraFactory, address(this), defaultFeeConfiguration);
pluginByPool[pool] = address(volatilityOracle);
return address(volatilityOracle);
}

/// @inheritdoc IBasePluginV1Factory
function setDefaultFeeConfiguration(AlgebraFeeConfiguration calldata newConfig) external override onlyAdministrator {
AdaptiveFee.validateFeeConfiguration(newConfig);
defaultFeeConfiguration = newConfig;
emit DefaultFeeConfiguration(newConfig);
}

/// @inheritdoc IBasePluginV1Factory
function setFarmingAddress(address newFarmingAddress) external override onlyAdministrator {
require(farmingAddress != newFarmingAddress);
farmingAddress = newFarmingAddress;
emit FarmingAddress(newFarmingAddress);
}
}
Loading