-
Notifications
You must be signed in to change notification settings - Fork 59
Alm plugin #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fourlen
wants to merge
44
commits into
integral-v1.2.1
Choose a base branch
from
alm-plugin
base: integral-v1.2.1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Alm plugin #143
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
bc0e535
temp
fourlen 8b489c1
+reblaanceManager
fourlen 79c02d4
sorry
fourlen 0851a13
Merge remote-tracking branch 'origin/integral-v1.2.1' into alm-plugin
debych c2643ae
[Plugin] add authorize in initializeALM
debych 9af2a9b
[Plugin] add setters
debych 2f87ae1
[Plugin] add alm-vault package in npm
debych d4c4502
[Plugin] create BaseRebalanceManager.sol
debych 4d5e7ea
[Plugin] add ALM tests
debych 1ca0116
bug fix
fourlen 360959f
fix alm plugin factory
IliaAzhel 30ca550
+fixtures
fourlen 1b56985
add base testnet to hardhat config & deploy script fixes
IliaAzhel 0314abc
first alm test
fourlen b4f2429
change comment
fourlen 8243c09
fix unreachable code
fourlen 2b6f8f0
-logs
fourlen c23f14e
-failed obtain twap
fourlen 171ff0b
top fix
fourlen db2beac
top2 fix
fourlen 17d8de0
[Plugin] swapAmount -> 0
debych 94ac0f3
issue fixes
fourlen 231a232
if -> require
fourlen 021873b
add alm custom pool deployer
IliaAzhel 5ff8b1e
add custom plugin deployer test
IliaAzhel b21a794
remove farming from alm custom pool deployer
IliaAzhel ff4bcef
-comments
fourlen 021efe2
-more comments
fourlen 1ae46a7
token1Decimals -> pairedTokenDecimals
fourlen 2824012
-logs
fourlen b63cf0b
+test
fourlen 88bf37d
only plugin
fourlen 7cb21a2
Merge branch 'alm-plugin-custom' into alm-plugin
IliaAzhel 6d5be5f
add base chain to hh config
IliaAzhel 55014ef
Add deploy script
debych 4d691fb
add factory address to the constructor
debych 4024746
fix state, add some tests
fourlen 9970bab
-comments
fourlen 82c0778
-failedToObtainTWAPs, +tests
fourlen 72ab616
some fixes, +tests
fourlen d07f460
fix tests
fourlen 7de653b
+tests
fourlen 4e2fe00
??
fourlen b95d2c4
+tests
fourlen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| {} | ||
| {"poolDeployer":"0x32e3F485696b2C6dBBc5C83A5Cb803Af72e1ecF3","factory":"0x5E4F01767A1068C5570c29fDF9bf743b0Aa637d7","vault":"0xCEeAfFE7E3DfEB7656B1E7E9c3516455A7Bb7aF9","vaultFactory":"0xe1909bcA4E528f7361b63F82330269d3001011e1","BasePluginV1Factory":"0x5dee969A86c9F99642Bdc14bdffFB6173228501c","wrapped":"0x4200000000000000000000000000000000000006","entryPoint":"0x8aD26dc9f724c9A7319E0E25b907d15626D9a056","tickLens":"0x3aA96eDb755C44F3E50C5408a36abb52f28326Ba","quoter":"0xc58874216AFe47779ADED27B8AAd77E8Bd6eBEBb","quoterV2":"0x4e73E421480a7E0C24fB3c11019254edE194f736","swapRouter":"0x4b2A38344b9aAc2F4e82130f35F1630C80ED94Bb","proxy":"0x348b694d0b6E43D6C7D9d6E2E1E2e9C739De5a74","nonfungiblePositionManager":"0x9ea4459c8DefBF561495d95414b9CF1E2242a3E2","eternal":"0xf3b57fE4d5D0927C3A5e549CB6aF1866687e2D62","fc":"0x211BD8917d433B7cC1F4497AbA906554Ab6ee479"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
| pragma solidity =0.8.20; | ||
|
|
||
| import '@cryptoalgebra/integral-periphery/contracts/interfaces/IAlgebraCustomPoolEntryPoint.sol'; | ||
| import './interfaces/IAlgebraALMCustomPoolDeployer.sol'; | ||
| import './libraries/AdaptiveFee.sol'; | ||
| import './AlgebraBasePluginALM.sol'; | ||
|
|
||
| /// @title Algebra Integral 1.2.1 ALM custom plugin deployer | ||
| contract AlgebraALMCustomPoolDeployer is IAlgebraALMCustomPoolDeployer { | ||
| /// @inheritdoc IAlgebraALMCustomPoolDeployer | ||
| bytes32 public constant override ALGEBRA_CUSTOM_PLUGIN_ADMINISTRATOR = keccak256('ALGEBRA_CUSTOM_PLUGIN_ADMINISTRATOR'); | ||
|
|
||
| /// @inheritdoc IAlgebraALMCustomPoolDeployer | ||
| address public immutable override algebraFactory; | ||
|
|
||
| /// @inheritdoc IAlgebraALMCustomPoolDeployer | ||
| address public immutable entryPoint; | ||
|
|
||
| /// @inheritdoc IAlgebraALMCustomPoolDeployer | ||
| AlgebraFeeConfiguration public override defaultFeeConfiguration; // values of constants for sigmoids in fee calculation formula | ||
|
|
||
| /// @inheritdoc IAlgebraALMCustomPoolDeployer | ||
| mapping(address poolAddress => address pluginAddress) public override pluginByPool; | ||
|
|
||
| modifier onlyAdministrator() { | ||
| require(IAlgebraFactory(algebraFactory).hasRoleOrOwner(ALGEBRA_CUSTOM_PLUGIN_ADMINISTRATOR, msg.sender), 'Only administrator'); | ||
| _; | ||
| } | ||
|
|
||
| constructor(address _algebraFactory, address _entryPoint) { | ||
| entryPoint = _entryPoint; | ||
| algebraFactory = _algebraFactory; | ||
| defaultFeeConfiguration = AdaptiveFee.initialFeeConfiguration(); | ||
| } | ||
|
|
||
| /// @inheritdoc IAlgebraPluginFactory | ||
| function beforeCreatePoolHook(address pool, address, address, address, address, bytes calldata) external override returns (address) { | ||
| require(msg.sender == entryPoint); | ||
| return _createPlugin(pool); | ||
| } | ||
|
|
||
| /// @inheritdoc IAlgebraPluginFactory | ||
| function afterCreatePoolHook(address, address, address) external view override { | ||
| require(msg.sender == entryPoint); | ||
| } | ||
|
|
||
| function _createPlugin(address pool) internal returns (address) { | ||
| require(pluginByPool[pool] == address(0), 'Already created'); | ||
| address plugin = address(new AlgebraBasePluginALM(pool, algebraFactory, address(this), defaultFeeConfiguration)); | ||
| pluginByPool[pool] = plugin; | ||
| return address(plugin); | ||
| } | ||
|
|
||
| /// @inheritdoc IAlgebraALMCustomPoolDeployer | ||
| function createCustomPool(address creator, address tokenA, address tokenB, bytes calldata data) external returns (address customPool) { | ||
| return IAlgebraCustomPoolEntryPoint(entryPoint).createCustomPool(address(this), creator, tokenA, tokenB, data); | ||
| } | ||
|
|
||
| /// @inheritdoc IAlgebraALMCustomPoolDeployer | ||
| function setDefaultFeeConfiguration(AlgebraFeeConfiguration calldata newConfig) external override onlyAdministrator { | ||
| AdaptiveFee.validateFeeConfiguration(newConfig); | ||
| defaultFeeConfiguration = newConfig; | ||
| emit DefaultFeeConfiguration(newConfig); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // 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 ALM plugin | ||
| 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) { | ||
| if (rebalanceManager != address(0)) { | ||
| if (!_ableToGetTimepoints(slowTwapPeriod)) return IAlgebraPlugin.afterSwap.selector; | ||
| (, int24 currentTick, , ) = _getPoolState(); | ||
| uint32 lastBlockTimestamp = _getLastBlockTimestamp(); | ||
|
|
||
| int24 slowTwapTick = _getTwapTick(slowTwapPeriod); | ||
| int24 fastTwapTick = _getTwapTick(fastTwapPeriod); | ||
|
|
||
| _obtainTWAPAndRebalance(currentTick, slowTwapTick, fastTwapTick, lastBlockTimestamp); | ||
| } | ||
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 './AlgebraBasePluginALM.sol'; | ||
|
|
||
| /// @title Algebra Integral 1.2.1 ALM 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 { | ||
| /// @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 AlgebraBasePluginALM(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); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
| pragma solidity =0.8.20; | ||
|
|
||
| import './base/BaseRebalanceManager.sol'; | ||
|
|
||
| contract RebalanceManager is BaseRebalanceManager { | ||
| constructor(address _vault, uint32 _minTimeBetweenRebalances, Thresholds memory _thresholds) { | ||
| require(_vault != address(0), 'Invalid vault address'); | ||
| paused = false; | ||
| vault = _vault; | ||
| pool = IAlgebraVault(vault).pool(); | ||
| factory = IAlgebraPool(pool).factory(); | ||
|
|
||
| tickSpacing = IAlgebraPool(pool).tickSpacing(); | ||
|
|
||
| bool _allowToken1 = IAlgebraVault(vault).allowToken1(); | ||
|
|
||
| minTimeBetweenRebalances = _minTimeBetweenRebalances; | ||
|
|
||
| allowToken1 = _allowToken1; | ||
| state = State.OverInventory; | ||
| lastRebalanceTimestamp = 0; | ||
| lastRebalanceCurrentPrice = 0; | ||
|
|
||
| _validateThresholds(_thresholds); | ||
| thresholds = _thresholds; | ||
|
|
||
| address token0 = IAlgebraVault(_vault).token0(); | ||
| address token1 = IAlgebraVault(_vault).token1(); | ||
|
|
||
| address _pairedToken = _allowToken1 ? token0 : token1; | ||
| pairedToken = _pairedToken; | ||
| uint8 _pairedTokenDecimals = _getPairedTokenDecimals(); | ||
| pairedTokenDecimals = _pairedTokenDecimals; | ||
|
|
||
| address _depositToken = _allowToken1 ? token1 : token0; | ||
| depositToken = _depositToken; | ||
| uint8 _depositTokenDecimals = _getDepositTokenDecimals(); | ||
| depositTokenDecimals = _depositTokenDecimals; | ||
|
|
||
| decimalsSum = _depositTokenDecimals + _pairedTokenDecimals; | ||
| tokenDecimals = _allowToken1 ? _pairedTokenDecimals : _depositTokenDecimals; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.