Skip to content

Commit

Permalink
get working with latest core commit
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahZinsmeister committed Mar 20, 2023
1 parent e6880d0 commit 0430625
Show file tree
Hide file tree
Showing 19 changed files with 237 additions and 163 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ node_modules/
typechain/
cache/
foundry-out/
lib/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
57 changes: 36 additions & 21 deletions contracts/hooks/BaseHook.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity =0.8.13;
pragma solidity =0.8.19;

import {IPoolManager} from "@uniswap/core-next/contracts/interfaces/IPoolManager.sol";
import {IHooks} from "@uniswap/core-next/contracts/interfaces/IHooks.sol";
import {IPoolManager} from '@uniswap/core-next/contracts/interfaces/IPoolManager.sol';
import {IHooks} from '@uniswap/core-next/contracts/interfaces/IHooks.sol';

abstract contract BaseHook is IHooks {
error NotPoolManager();
Expand Down Expand Up @@ -47,23 +47,28 @@ abstract contract BaseHook is IHooks {
}
}

function beforeInitialize(address, IPoolManager.PoolKey calldata, uint160) external virtual returns (bytes4) {
function beforeInitialize(
address,
IPoolManager.PoolKey calldata,
uint160
) external virtual returns (bytes4) {
revert HookNotImplemented();
}

function afterInitialize(address, IPoolManager.PoolKey calldata, uint160, int24)
external
virtual
returns (bytes4)
{
function afterInitialize(
address,
IPoolManager.PoolKey calldata,
uint160,
int24
) external virtual returns (bytes4) {
revert HookNotImplemented();
}

function beforeModifyPosition(address, IPoolManager.PoolKey calldata, IPoolManager.ModifyPositionParams calldata)
external
virtual
returns (bytes4)
{
function beforeModifyPosition(
address,
IPoolManager.PoolKey calldata,
IPoolManager.ModifyPositionParams calldata
) external virtual returns (bytes4) {
revert HookNotImplemented();
}

Expand All @@ -76,11 +81,11 @@ abstract contract BaseHook is IHooks {
revert HookNotImplemented();
}

function beforeSwap(address, IPoolManager.PoolKey calldata, IPoolManager.SwapParams calldata)
external
virtual
returns (bytes4)
{
function beforeSwap(
address,
IPoolManager.PoolKey calldata,
IPoolManager.SwapParams calldata
) external virtual returns (bytes4) {
revert HookNotImplemented();
}

Expand All @@ -93,11 +98,21 @@ abstract contract BaseHook is IHooks {
revert HookNotImplemented();
}

function beforeDonate(address, IPoolManager.PoolKey calldata, uint256, uint256) external virtual returns (bytes4) {
function beforeDonate(
address,
IPoolManager.PoolKey calldata,
uint256,
uint256
) external virtual returns (bytes4) {
revert HookNotImplemented();
}

function afterDonate(address, IPoolManager.PoolKey calldata, uint256, uint256) external virtual returns (bytes4) {
function afterDonate(
address,
IPoolManager.PoolKey calldata,
uint256,
uint256
) external virtual returns (bytes4) {
revert HookNotImplemented();
}
}
67 changes: 35 additions & 32 deletions contracts/hooks/GeomeanOracle.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity =0.8.13;
pragma solidity =0.8.19;

import {IPoolManager} from "@uniswap/core-next/contracts/interfaces/IPoolManager.sol";
import {PoolId} from "@uniswap/core-next/contracts/libraries/PoolId.sol";
import {Hooks} from "@uniswap/core-next/contracts/libraries/Hooks.sol";
import {TickMath} from "@uniswap/core-next/contracts/libraries/TickMath.sol";
import {Oracle} from "@uniswap/core-next/contracts/libraries/Oracle.sol";
import {BaseHook} from "./BaseHook.sol";
import {IPoolManager} from '@uniswap/core-next/contracts/interfaces/IPoolManager.sol';
import {PoolId} from '@uniswap/core-next/contracts/libraries/PoolId.sol';
import {Hooks} from '@uniswap/core-next/contracts/libraries/Hooks.sol';
import {TickMath} from '@uniswap/core-next/contracts/libraries/TickMath.sol';
import {Oracle} from '@uniswap/core-next/contracts/libraries/Oracle.sol';
import {BaseHook} from './BaseHook.sol';

/// @notice A hook for a pool that allows a Uniswap pool to act as an oracle. Pools that use this hook must have full range
/// tick spacing and liquidity is always permanently locked in these pools. This is the suggested configuration
Expand Down Expand Up @@ -73,26 +73,24 @@ contract GeomeanOracle is BaseHook {
);
}

function beforeInitialize(address, IPoolManager.PoolKey calldata key, uint160)
external
view
override
poolManagerOnly
returns (bytes4)
{
function beforeInitialize(
address,
IPoolManager.PoolKey calldata key,
uint160
) external view override poolManagerOnly returns (bytes4) {
// This is to limit the fragmentation of pools using this oracle hook. In other words,
// there may only be one pool per pair of tokens that use this hook. The tick spacing is set to the maximum
// because we only allow max range liquidity in this pool.
if (key.fee != 0 || key.tickSpacing != poolManager.MAX_TICK_SPACING()) revert OnlyOneOraclePoolAllowed();
return GeomeanOracle.beforeInitialize.selector;
}

function afterInitialize(address, IPoolManager.PoolKey calldata key, uint160, int24)
external
override
poolManagerOnly
returns (bytes4)
{
function afterInitialize(
address,
IPoolManager.PoolKey calldata key,
uint160,
int24
) external override poolManagerOnly returns (bytes4) {
bytes32 id = key.toId();
(states[id].cardinality, states[id].cardinalityNext) = observations[id].initialize(_blockTimestamp());
return GeomeanOracle.afterInitialize.selector;
Expand All @@ -101,12 +99,17 @@ contract GeomeanOracle is BaseHook {
/// @dev Called before any action that potentially modifies pool price or liquidity, such as swap or modify position
function _updatePool(IPoolManager.PoolKey calldata key) private {
bytes32 id = key.toId();
(, int24 tick,) = poolManager.getSlot0(id);
(, int24 tick, ) = poolManager.getSlot0(id);

uint128 liquidity = poolManager.getLiquidity(id);

(states[id].index, states[id].cardinality) = observations[id].write(
states[id].index, _blockTimestamp(), tick, liquidity, states[id].cardinality, states[id].cardinalityNext
states[id].index,
_blockTimestamp(),
tick,
liquidity,
states[id].cardinality,
states[id].cardinalityNext
);
}

Expand All @@ -118,19 +121,18 @@ contract GeomeanOracle is BaseHook {
if (params.liquidityDelta < 0) revert OraclePoolMustLockLiquidity();
int24 maxTickSpacing = poolManager.MAX_TICK_SPACING();
if (
params.tickLower != TickMath.minUsableTick(maxTickSpacing)
|| params.tickUpper != TickMath.maxUsableTick(maxTickSpacing)
params.tickLower != TickMath.minUsableTick(maxTickSpacing) ||
params.tickUpper != TickMath.maxUsableTick(maxTickSpacing)
) revert OraclePositionsMustBeFullRange();
_updatePool(key);
return GeomeanOracle.beforeModifyPosition.selector;
}

function beforeSwap(address, IPoolManager.PoolKey calldata key, IPoolManager.SwapParams calldata)
external
override
poolManagerOnly
returns (bytes4)
{
function beforeSwap(
address,
IPoolManager.PoolKey calldata key,
IPoolManager.SwapParams calldata
) external override poolManagerOnly returns (bytes4) {
_updatePool(key);
return GeomeanOracle.beforeSwap.selector;
}
Expand All @@ -145,11 +147,12 @@ contract GeomeanOracle is BaseHook {

ObservationState memory state = states[id];

(, int24 tick,) = poolManager.getSlot0(id);
(, int24 tick, ) = poolManager.getSlot0(id);

uint128 liquidity = poolManager.getLiquidity(id);

return observations[id].observe(_blockTimestamp(), secondsAgos, tick, state.index, liquidity, state.cardinality);
return
observations[id].observe(_blockTimestamp(), secondsAgos, tick, state.index, liquidity, state.cardinality);
}

/// @notice Increase the cardinality target for the given pool
Expand Down
Loading

0 comments on commit 0430625

Please sign in to comment.