Skip to content

Commit

Permalink
move the oracle into a hook, remove time based stuff from pool manager
Browse files Browse the repository at this point in the history
fixes #53
  • Loading branch information
moodysalem committed Apr 19, 2022
1 parent ea1a16e commit 4d93842
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 623 deletions.
38 changes: 1 addition & 37 deletions contracts/PoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ contract PoolManager is IPoolManager, NoDelegateCall {

mapping(bytes32 => Pool.State) public pools;

/// @dev For mocking in unit tests
function _blockTimestamp() internal view virtual returns (uint32) {
return uint32(block.timestamp);
}

function _getPool(IPoolManager.PoolKey memory key) private view returns (Pool.State storage) {
return pools[keccak256(abi.encode(key))];
}
Expand All @@ -35,23 +30,13 @@ contract PoolManager is IPoolManager, NoDelegateCall {
key.hooks.beforeInitialize(msg.sender, key, sqrtPriceX96);
}

tick = _getPool(key).initialize(_blockTimestamp(), sqrtPriceX96);
tick = _getPool(key).initialize(sqrtPriceX96);

if (key.hooks.shouldCallAfterInitialize()) {
key.hooks.afterInitialize(msg.sender, key, sqrtPriceX96, tick);
}
}

/// @notice Increase the maximum number of stored observations for the pool's oracle
function increaseObservationCardinalityNext(IPoolManager.PoolKey memory key, uint16 observationCardinalityNext)
external
override
returns (uint16 observationCardinalityNextOld, uint16 observationCardinalityNextNew)
{
(observationCardinalityNextOld, observationCardinalityNextNew) = _getPool(key)
.increaseObservationCardinalityNext(observationCardinalityNext);
}

/// @inheritdoc IPoolManager
mapping(IERC20Minimal => uint256) public override reservesOf;

Expand Down Expand Up @@ -175,7 +160,6 @@ contract PoolManager is IPoolManager, NoDelegateCall {
tickLower: params.tickLower,
tickUpper: params.tickUpper,
liquidityDelta: params.liquidityDelta.toInt128(),
time: _blockTimestamp(),
maxLiquidityPerTick: Tick.tickSpacingToMaxLiquidityPerTick(key.tickSpacing),
tickSpacing: key.tickSpacing
})
Expand All @@ -201,7 +185,6 @@ contract PoolManager is IPoolManager, NoDelegateCall {

delta = _getPool(key).swap(
Pool.SwapParams({
time: _blockTimestamp(),
fee: key.fee,
tickSpacing: key.tickSpacing,
zeroForOne: params.zeroForOne,
Expand Down Expand Up @@ -237,23 +220,4 @@ contract PoolManager is IPoolManager, NoDelegateCall {
// subtraction must be safe
_accountDelta(token, -(paid.toInt256()));
}

/// @notice Observe a past state of a pool
function observe(IPoolManager.PoolKey calldata key, uint32[] calldata secondsAgos)
external
view
noDelegateCall
returns (int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s)
{
return _getPool(key).observe(_blockTimestamp(), secondsAgos);
}

/// @notice Get the snapshot of the cumulative values of a tick range
function snapshotCumulativesInside(
IPoolManager.PoolKey calldata key,
int24 tickLower,
int24 tickUpper
) external view override noDelegateCall returns (Pool.Snapshot memory) {
return _getPool(key).snapshotCumulativesInside(tickLower, tickUpper, _blockTimestamp());
}
}
120 changes: 120 additions & 0 deletions contracts/hooks/V3Oracle.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity =0.8.13;

import {IHooks} from '../interfaces/IHooks.sol';
import {IPoolManager} from '../interfaces/IPoolManager.sol';
import {Hooks} from '../libraries/Hooks.sol';

/// @notice Includes the Oracle feature in a similar way to V3. This is now externalized
contract V3Oracle is IHooks {
constructor() {
Hooks.validateHookAddress(
this,
Hooks.Calls({
beforeInitialize: false,
// to initialize the observations for the pool
afterInitialize: true,
// to record the tick at the start of the block for the pool
beforeModifyPosition: true,
afterModifyPosition: false,
// to record the tick at the start of the block for the pool
beforeSwap: true,
afterSwap: false
})
);
}

function beforeInitialize(
address sender,
IPoolManager.PoolKey memory key,
uint160 sqrtPriceX96
) external override {
revert();
}

function afterInitialize(
address sender,
IPoolManager.PoolKey memory key,
uint160 sqrtPriceX96,
int24 tick
) external override {
revert();
}

function beforeModifyPosition(
address sender,
IPoolManager.PoolKey calldata key,
IPoolManager.ModifyPositionParams calldata params
) external override {
revert();
}

function afterModifyPosition(
address sender,
IPoolManager.PoolKey calldata key,
IPoolManager.ModifyPositionParams calldata params,
IPoolManager.BalanceDelta calldata delta
) external override {
revert();
}

function beforeSwap(
address sender,
IPoolManager.PoolKey calldata key,
IPoolManager.SwapParams calldata params
) external override {
revert();
}

function afterSwap(
address sender,
IPoolManager.PoolKey calldata key,
IPoolManager.SwapParams calldata params,
IPoolManager.BalanceDelta calldata delta
) external override {
revert();
}


// /// @notice Observe a past state of a pool
// function observe(PoolKey calldata key, uint32[] calldata secondsAgos)
// external
// view
// returns (int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s);
//
// /// @notice Get the snapshot of the cumulative values of a tick range
// function snapshotCumulativesInside(
// PoolKey calldata key,
// int24 tickLower,
// int24 tickUpper
// ) external view returns (Pool.Snapshot memory);

/// @dev Increase the number of stored observations
// function observe(
// IPoolManager.PoolKey calldata key,
// uint32[] calldata secondsAgos
// ) external view returns (int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s) {
// return observations[key].observe(
// time,
// secondsAgos,
// self.slot0.tick,
// self.slot0.observationIndex,
// self.liquidity,
// self.slot0.observationCardinality
// );
// }
//
// /// @dev Increase the number of stored observations
// function increaseObservationCardinalityNext(State storage self, uint16 observationCardinalityNext)
// internal
// returns (uint16 observationCardinalityNextOld, uint16 observationCardinalityNextNew)
// {
// observationCardinalityNextOld = self.slot0.observationCardinalityNext;
// observationCardinalityNextNew = self.observations.grow(
// observationCardinalityNextOld,
// observationCardinalityNext
// );
// self.slot0.observationCardinalityNext = observationCardinalityNextNew;
// }

}
18 changes: 0 additions & 18 deletions contracts/interfaces/IPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ interface IPoolManager {
/// @notice Initialize the state for a given pool ID
function initialize(PoolKey memory key, uint160 sqrtPriceX96) external returns (int24 tick);

/// @notice Increase the maximum number of stored observations for the pool's oracle
function increaseObservationCardinalityNext(PoolKey memory key, uint16 observationCardinalityNext)
external
returns (uint16 observationCardinalityNextOld, uint16 observationCardinalityNextNew);

/// @notice Represents the stack of addresses that have locked the pool. Each call to #lock pushes the address onto the stack
/// @param index The index of the locker, also known as the id of the locker
function lockedBy(uint256 index) external view returns (address);
Expand Down Expand Up @@ -110,17 +105,4 @@ interface IPoolManager {

/// @notice Called by the user to pay what is owed
function settle(IERC20Minimal token) external returns (uint256 paid);

/// @notice Observe a past state of a pool
function observe(PoolKey calldata key, uint32[] calldata secondsAgos)
external
view
returns (int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s);

/// @notice Get the snapshot of the cumulative values of a tick range
function snapshotCumulativesInside(
PoolKey calldata key,
int24 tickLower,
int24 tickUpper
) external view returns (Pool.Snapshot memory);
}
Loading

0 comments on commit 4d93842

Please sign in to comment.