Skip to content

Commit

Permalink
-pool state var
Browse files Browse the repository at this point in the history
  • Loading branch information
Никита Оборин authored and Никита Оборин committed May 3, 2024
1 parent 2be73ab commit 8e1e26f
Showing 1 changed file with 40 additions and 32 deletions.
72 changes: 40 additions & 32 deletions src/plugin/contracts/modules/OracleModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ import '@cryptoalgebra/integral-core/contracts/libraries/Plugins.sol';
import '@cryptoalgebra/integral-core/contracts/interfaces/pool/IAlgebraPoolState.sol';
import '@cryptoalgebra/integral-core/contracts/interfaces/plugin/IAlgebraPlugin.sol';
import '@cryptoalgebra/integral-core/contracts/interfaces/IAlgebraPool.sol';
import '@cryptoalgebra/algebra-modular-hub/contracts/interface/IAlgebraModule.sol';
import '@cryptoalgebra/algebra-modular-hub/contracts/interface/IAlgebraModularHub.sol';
import '@cryptoalgebra/algebra-modular-hub/contracts/interfaces/IAlgebraModule.sol';
import '@cryptoalgebra/algebra-modular-hub/contracts/interfaces/IAlgebraModularHub.sol';
import '@cryptoalgebra/algebra-modular-hub/contracts/types/HookParams.sol';

import '../interfaces/IAlgebraBasePluginV1.sol';

import '../libraries/VolatilityOracle.sol';
import '../libraries/VolatilityOracle.sol';
import '../interfaces/plugins/IVolatilityOracle.sol';

contract OracleModule is IAlgebraModule, Timestamp {
contract OracleModule is IAlgebraModule, IVolatilityOracle, Timestamp {
using Plugins for uint8;

uint256 internal constant UINT16_MODULO = 65536;
using VolatilityOracle for VolatilityOracle.Timepoint[UINT16_MODULO];

uint8 public constant defaultPluginConfig = uint8(Plugins.AFTER_INIT_FLAG | Plugins.BEFORE_SWAP_FLAG | Plugins.DYNAMIC_FEE);

address public immutable override pool;

address public immutable modularHub;

/// @inheritdoc IVolatilityOracle
Expand All @@ -40,27 +40,28 @@ contract OracleModule is IAlgebraModule, Timestamp {
VolatilityOracle.Timepoint[UINT16_MODULO] public override timepoints;

constructor(address _modularHub, address _pool) {
(modularHub, pool) = (_modularHub, _pool);
modularHub = _modularHub;
}

function initialize() external override {
function initialize() external {
require(!isInitialized, 'Already initialized');
require(IAlgebraModularHub(modularHub).moduleAddressToIndex(address(this)), 'Plugin not attached');
(uint160 price, int24 tick, , ) = _getPoolState();
require(IAlgebraModularHub(modularHub).moduleAddressToIndex(address(this)) != 0, 'Plugin not attached');
address pool = IAlgebraModularHub(modularHub).pool();
(uint160 price, int24 tick, , ) = _getPoolState(pool);
require(price != 0, 'Pool is not initialized');

uint32 time = _blockTimestamp();
timepoints.initialize(time, tick);

isInitialized = true;

_updatePluginConfigInPool();
_updatePluginConfigInPool(pool);
}

/// @inheritdoc IVolatilityOracle
function getSingleTimepoint(uint32 secondsAgo) external view override returns (int56 tickCumulative, uint88 volatilityCumulative) {
// `volatilityCumulative` values for timestamps after the last timepoint _should not_ be compared: they may differ due to interpolation errors
(, int24 tick, , ) = _getPoolState();
(, int24 tick, , ) = _getPoolState(IAlgebraModularHub(modularHub).pool());
uint16 lastTimepointIndex = timepointIndex;
uint16 oldestIndex = timepoints.getOldestIndex(lastTimepointIndex);
VolatilityOracle.Timepoint memory result = timepoints.getSingleTimepoint(_blockTimestamp(), secondsAgo, tick, lastTimepointIndex, oldestIndex);
Expand All @@ -72,7 +73,7 @@ contract OracleModule is IAlgebraModule, Timestamp {
uint32[] memory secondsAgos
) external view override returns (int56[] memory tickCumulatives, uint88[] memory volatilityCumulatives) {
// `volatilityCumulative` values for timestamps after the last timepoint _should not_ be compared: they may differ due to interpolation errors
(, int24 tick, , ) = _getPoolState();
(, int24 tick, , ) = _getPoolState(IAlgebraModularHub(modularHub).pool());
return timepoints.getTimepoints(_blockTimestamp(), secondsAgos, tick, timepointIndex);
}

Expand All @@ -88,16 +89,16 @@ contract OracleModule is IAlgebraModule, Timestamp {
}
}

function _getPoolState() internal view returns (uint160 price, int24 tick, uint16 fee, uint8 pluginConfig) {
function _getPoolState(address pool) internal view returns (uint160 price, int24 tick, uint16 fee, uint8 pluginConfig) {
(price, tick, fee, pluginConfig, , ) = IAlgebraPoolState(pool).globalState();
}

function _getPluginInPool() internal view returns (address plugin) {
function _getPluginInPool(address pool) internal view returns (address plugin) {
return IAlgebraPool(pool).plugin();
}

function _updatePluginConfigInPool() internal {
(, , , uint8 currentPluginConfig) = _getPoolState();
function _updatePluginConfigInPool(address pool) internal {
(, , , uint8 currentPluginConfig) = _getPoolState(pool);
if (currentPluginConfig != defaultPluginConfig) {
IAlgebraPool(pool).setPluginConfig(defaultPluginConfig);
}
Expand Down Expand Up @@ -142,28 +143,31 @@ contract OracleModule is IAlgebraModule, Timestamp {
}

function _beforeInitialize(
bytes memory /* params */,
bytes memory params,
uint16 /* poolFeeCache */
) internal virtual {
_updatePluginConfigInPool();
BeforeFlashParams memory decodedParams = abi.decode(params, (BeforeFlashParams));
_updatePluginConfigInPool(decodedParams.pool);
}

function _afterInitialize(
bytes memory /* params */,
bytes memory params,
uint16 /* poolFeeCache */
) internal virtual {
AfterInitializeParams memory decodedParams = abi.decode(params, (AfterInitializeParams));
uint32 _timestamp = _blockTimestamp();
timepoints.initialize(_timestamp, tick);
timepoints.initialize(_timestamp, decodedParams.tick);

lastTimepointTimestamp = _timestamp;
isInitialized = true;
}

function _beforeSwap(
bytes memory /* params */,
bytes memory params,
uint16 /* poolFeeCache */
) internal virtual {
_writeTimepoints();
BeforeSwapParams memory decodedParams = abi.decode(params, (BeforeSwapParams));
_writeTimepoints(decodedParams.pool);
}

// function _afterSwap(
Expand All @@ -174,34 +178,38 @@ contract OracleModule is IAlgebraModule, Timestamp {
// }

function _beforeModifyPosition(
bytes memory /* params */,
bytes memory params,
uint16 /* poolFeeCache */
) internal virtual {
_updatePluginConfigInPool();
BeforeModifyPositionParams memory decodedParams = abi.decode(params, (BeforeModifyPositionParams));
_updatePluginConfigInPool(decodedParams.pool);
}

function _afterModifyPosition(
bytes memory /* params */,
bytes memory params,
uint16 /* poolFeeCache */
) internal virtual {
_updatePluginConfigInPool();
AfterModifyPositionParams memory decodedParams = abi.decode(params, (AfterModifyPositionParams));
_updatePluginConfigInPool(decodedParams.pool);
}

function _beforeFlash(
bytes memory /* params */,
bytes memory params,
uint16 /* poolFeeCache */
) internal virtual {
_updatePluginConfigInPool();
BeforeFlashParams memory decodedParams = abi.decode(params, (BeforeFlashParams));
_updatePluginConfigInPool(decodedParams.pool);
}

function _afterFlash(
bytes memory /* params */,
bytes memory params,
uint16 /* poolFeeCache */
) internal virtual {
_updatePluginConfigInPool();
AfterFlashParams memory decodedParams = abi.decode(params, (AfterFlashParams));
_updatePluginConfigInPool(decodedParams.pool);
}

function _writeTimepoints() internal {
function _writeTimepoints(address pool) internal {
// single SLOAD
uint16 _lastIndex = timepointIndex;
uint32 _lastTimepointTimestamp = lastTimepointTimestamp;
Expand All @@ -212,7 +220,7 @@ contract OracleModule is IAlgebraModule, Timestamp {

if (_lastTimepointTimestamp == currentTimestamp) return;

(, int24 tick, , ) = _getPoolState();
(, int24 tick, , ) = _getPoolState(pool);
(uint16 newLastIndex, uint16 newOldestIndex) = timepoints.write(_lastIndex, currentTimestamp, tick);

timepointIndex = newLastIndex;
Expand Down

0 comments on commit 8e1e26f

Please sign in to comment.