-
Notifications
You must be signed in to change notification settings - Fork 60
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
base: integral-v1.2.1
Are you sure you want to change the base?
Alm plugin #143
Changes from 15 commits
bc0e535
8b489c1
79c02d4
0851a13
c2643ae
9af2a9b
2f87ae1
d4c4502
4d5e7ea
1ca0116
360959f
30ca550
1b56985
0314abc
b4f2429
8243c09
2b6f8f0
c23f14e
171ff0b
db2beac
17d8de0
94ac0f3
231a232
021873b
5ff8b1e
b21a794
ff4bcef
021efe2
1ae46a7
2824012
b63cf0b
88bf37d
7cb21a2
6d5be5f
55014ef
4d691fb
4024746
9970bab
82c0778
72ab616
d07f460
7de653b
4e2fe00
b95d2c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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"} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // 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'; | ||
|
|
||
| import 'hardhat/console.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) { | ||
| console.log('entered after swap'); | ||
| ( , int24 currentTick, , ) = _getPoolState(); | ||
| uint32 lastBlockTimestamp = _getLastBlockTimestamp(); | ||
|
|
||
| bool failedToObtainTWAP; | ||
| int24 slowTwapTick; | ||
| int24 fastTwapTick; | ||
|
|
||
| if (_ableToGetTimepoints(slowTwapPeriod)) { | ||
| slowTwapTick = _getTwapTick(slowTwapPeriod); | ||
| fastTwapTick = _getTwapTick(fastTwapPeriod); | ||
| } else { | ||
| failedToObtainTWAP = true; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| } | ||
|
|
||
| _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); | ||
| } | ||
| } | ||
| 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 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 { | ||
|
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 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); | ||
| } | ||
| } | ||
| 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, Thresholds memory _thresholds) { | ||
| require(!isAlmInitialized, 'Already initialized'); | ||
| isAlmInitialized = true; | ||
|
fourlen marked this conversation as resolved.
Outdated
|
||
| paused = false; | ||
| // TODO: добавить require'ов | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add require statements:
|
||
| vault = _vault; | ||
| pool = IAlgebraVault(vault).pool(); | ||
|
|
||
| tickSpacing = IAlgebraPool(pool).tickSpacing(); | ||
|
|
||
| bool _allowToken1 = IAlgebraVault(vault).allowToken1(); | ||
|
|
||
| allowToken1 = _allowToken1; | ||
| state = State.OverInventory; // поч overinventory? | ||
| lastRebalanceTimestamp = 0; | ||
| lastRebalanceCurrentPrice = 0; | ||
| thresholds = _thresholds; | ||
|
|
||
| address token0 = IAlgebraVault(_vault).token0(); | ||
| address token1 = IAlgebraVault(_vault).token1(); | ||
|
|
||
| address _pairedToken = _allowToken1 ? token0 : token1; | ||
| pairedToken = _pairedToken; | ||
| uint8 _pairedTokenDecimals = _getPairedTokenDecimals(); | ||
| // console.log('_pairedTokenDecimals: ', _pairedTokenDecimals); | ||
| pairedTokenDecimals = _pairedTokenDecimals; | ||
|
|
||
| address _depositToken = _allowToken1 ? token1 : token0; | ||
| depositToken = _depositToken; | ||
| uint8 _depositTokenDecimals = _getDepositTokenDecimals(); | ||
| depositTokenDecimals = _depositTokenDecimals; | ||
| // console.log('_depositTokenDecimals: ', _depositTokenDecimals); | ||
|
|
||
| decimalsSum = _depositTokenDecimals + _pairedTokenDecimals; | ||
| // console.log('decimals sum: ', decimalsSum); | ||
| tokenDecimals = _allowToken1 ? _pairedTokenDecimals : _depositTokenDecimals; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.