Skip to content

Commit

Permalink
Update v4 core (#74)
Browse files Browse the repository at this point in the history
* feat: update v4-core

This commit updates v4 core to latest and fixes integration issues

* fix: tests

* fix: update tests

* fix: test router was borked

* fix: alice comments

* update to latest core

* use prev values

* change twamm to use pool getters

* changes after merging main

* use --via-ir in cli

* fix formatting

* fix FullRange/TWAMM hook

---------

Co-authored-by: Sara Reynolds <[email protected]>
  • Loading branch information
marktoda and snreynolds authored Dec 19, 2023
1 parent 8864031 commit 0b46125
Show file tree
Hide file tree
Showing 57 changed files with 419 additions and 337 deletions.
7 changes: 7 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FOUNDRY_FUZZ_SEED=0x4444

if [[ "$OSTYPE" == "linux-gnu"* ]]; then
export FOUNDRY_SOLC="./lib/v4-core/bin/solc-static-linux"
elif [[ "$OSTYPE" == "darwin"* ]]; then
export FOUNDRY_SOLC="./lib/v4-core/bin/solc-mac"
fi
2 changes: 1 addition & 1 deletion .forge-snapshots/FullOracleObserve0After5Seconds.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2000
2771
2 changes: 1 addition & 1 deletion .forge-snapshots/FullOracleObserve200By13.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
21068
23377
2 changes: 1 addition & 1 deletion .forge-snapshots/FullOracleObserve200By13Plus5.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
21318
23624
2 changes: 1 addition & 1 deletion .forge-snapshots/FullOracleObserve5After5Seconds.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2076
2798
2 changes: 1 addition & 1 deletion .forge-snapshots/FullOracleObserveOldest.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20164
22396
2 changes: 1 addition & 1 deletion .forge-snapshots/FullOracleObserveOldestAfter5Seconds.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20458
22695
2 changes: 1 addition & 1 deletion .forge-snapshots/FullOracleObserveZero.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1525
2130
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeAddInitialLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
412696
410761
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeAddLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
206962
204683
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeFirstSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
154763
156432
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeInitialize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
879542
897565
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeRemoveLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
200095
200057
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeRemoveLiquidityAndRebalance.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
379287
386095
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeSecondSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
112303
114700
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
153038
154641
2 changes: 1 addition & 1 deletion .forge-snapshots/OracleGrow10Slots.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
233028
254711
2 changes: 1 addition & 1 deletion .forge-snapshots/OracleGrow10SlotsCardinalityGreater.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
223717
245393
2 changes: 1 addition & 1 deletion .forge-snapshots/OracleGrow1Slot.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
32886
54893
2 changes: 1 addition & 1 deletion .forge-snapshots/OracleGrow1SlotCardinalityGreater.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
23586
45575
2 changes: 1 addition & 1 deletion .forge-snapshots/OracleInitialize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
51411
72361
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5571
6618
2 changes: 1 addition & 1 deletion .forge-snapshots/OracleObserveCurrentTime.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1525
2130
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1525
2130
2 changes: 1 addition & 1 deletion .forge-snapshots/OracleObserveLast20Seconds.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
75965
88543
2 changes: 1 addition & 1 deletion .forge-snapshots/OracleObserveLatestEqual.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1525
2130
2 changes: 1 addition & 1 deletion .forge-snapshots/OracleObserveLatestTransform.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2000
2771
2 changes: 1 addition & 1 deletion .forge-snapshots/OracleObserveMiddle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5746
6807
2 changes: 1 addition & 1 deletion .forge-snapshots/OracleObserveOldest.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5277
6319
2 changes: 1 addition & 1 deletion .forge-snapshots/OracleObserveSinceMostRecent.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2615
3466
2 changes: 1 addition & 1 deletion .forge-snapshots/TWAMMSubmitOrder.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
123576
146158
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ jobs:
version: nightly

- name: Run tests
run: forge test -vvv
run: forge test -vvv --via-ir
env:
FOUNDRY_PROFILE: ci
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
cache/
foundry-out/
foundry-out/
.vscode/
21 changes: 13 additions & 8 deletions contracts/BaseHook.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;

import {Hooks} from "@uniswap/v4-core/contracts/libraries/Hooks.sol";
import {IPoolManager} from "@uniswap/v4-core/contracts/interfaces/IPoolManager.sol";
import {IHooks} from "@uniswap/v4-core/contracts/interfaces/IHooks.sol";
import {BalanceDelta} from "@uniswap/v4-core/contracts/types/BalanceDelta.sol";
import {PoolKey} from "@uniswap/v4-core/contracts/types/PoolKey.sol";
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol";
import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";

abstract contract BaseHook is IHooks {
error NotPoolManager();
Expand Down Expand Up @@ -40,16 +40,21 @@ abstract contract BaseHook is IHooks {
_;
}

function getHooksCalls() public pure virtual returns (Hooks.Calls memory);
function getHooksCalls() public pure virtual returns (Hooks.Permissions memory);

// this function is virtual so that we can override it during testing,
// which allows us to deploy an implementation to any address
// and then etch the bytecode into the correct address
function validateHookAddress(BaseHook _this) internal pure virtual {
Hooks.validateHookAddress(_this, getHooksCalls());
Hooks.validateHookPermissions(_this, getHooksCalls());
}

function lockAcquired(bytes calldata data) external virtual poolManagerOnly returns (bytes memory) {
function lockAcquired(address, /*sender*/ bytes calldata data)
external
virtual
poolManagerOnly
returns (bytes memory)
{
(bool success, bytes memory returnData) = address(this).call(data);
if (success) return returnData;
if (returnData.length == 0) revert LockFailure();
Expand Down
2 changes: 1 addition & 1 deletion contracts/base/PeripheryPayments.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity ^0.8.19;

import {ERC20} from "solmate/tokens/ERC20.sol";
import {Currency, CurrencyLibrary} from "@uniswap/v4-core/contracts/types/Currency.sol";
import {Currency, CurrencyLibrary} from "@uniswap/v4-core/src/types/Currency.sol";
import {SafeTransferLib} from "solmate/utils/SafeTransferLib.sol";
import {IPeripheryPayments} from "../interfaces/IPeripheryPayments.sol";

Expand Down
50 changes: 28 additions & 22 deletions contracts/hooks/examples/FullRange.sol
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;

import {IPoolManager} from "@uniswap/v4-core/contracts/interfaces/IPoolManager.sol";
import {PoolManager} from "@uniswap/v4-core/contracts/PoolManager.sol";
import {Hooks} from "@uniswap/v4-core/contracts/libraries/Hooks.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {PoolManager} from "@uniswap/v4-core/src/PoolManager.sol";
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {BaseHook} from "../../BaseHook.sol";
import {SafeCast} from "@uniswap/v4-core/contracts/libraries/SafeCast.sol";
import {IHooks} from "@uniswap/v4-core/contracts/interfaces/IHooks.sol";
import {CurrencyLibrary, Currency} from "@uniswap/v4-core/contracts/types/Currency.sol";
import {TickMath} from "@uniswap/v4-core/contracts/libraries/TickMath.sol";
import {BalanceDelta} from "@uniswap/v4-core/contracts/types/BalanceDelta.sol";
import {IERC20Minimal} from "@uniswap/v4-core/contracts/interfaces/external/IERC20Minimal.sol";
import {ILockCallback} from "@uniswap/v4-core/contracts/interfaces/callback/ILockCallback.sol";
import {PoolId, PoolIdLibrary} from "@uniswap/v4-core/contracts/types/PoolId.sol";
import {PoolKey} from "@uniswap/v4-core/contracts/types/PoolKey.sol";
import {FullMath} from "@uniswap/v4-core/contracts/libraries/FullMath.sol";
import {SafeCast} from "@uniswap/v4-core/src/libraries/SafeCast.sol";
import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol";
import {CurrencyLibrary, Currency} from "@uniswap/v4-core/src/types/Currency.sol";
import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol";
import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol";
import {IERC20Minimal} from "@uniswap/v4-core/src/interfaces/external/IERC20Minimal.sol";
import {ILockCallback} from "@uniswap/v4-core/src/interfaces/callback/ILockCallback.sol";
import {PoolId, PoolIdLibrary} from "@uniswap/v4-core/src/types/PoolId.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {FullMath} from "@uniswap/v4-core/src/libraries/FullMath.sol";
import {UniswapV4ERC20} from "../../libraries/UniswapV4ERC20.sol";
import {FixedPoint96} from "@uniswap/v4-core/contracts/libraries/FixedPoint96.sol";
import {FixedPoint96} from "@uniswap/v4-core/src/libraries/FixedPoint96.sol";
import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol";
import {IERC20Metadata} from "@openzeppelin/contracts/interfaces/IERC20Metadata.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
Expand Down Expand Up @@ -87,16 +87,18 @@ contract FullRange is BaseHook, ILockCallback {
_;
}

function getHooksCalls() public pure override returns (Hooks.Calls memory) {
return Hooks.Calls({
function getHooksCalls() public pure override returns (Hooks.Permissions memory) {
return Hooks.Permissions({
beforeInitialize: true,
afterInitialize: false,
beforeModifyPosition: true,
afterModifyPosition: false,
beforeSwap: true,
afterSwap: false,
beforeDonate: false,
afterDonate: false
afterDonate: false,
noOp: false,
accessLock: false
});
}

Expand All @@ -115,7 +117,7 @@ contract FullRange is BaseHook, ILockCallback {

PoolId poolId = key.toId();

(uint160 sqrtPriceX96,,,) = poolManager.getSlot0(poolId);
(uint160 sqrtPriceX96,,) = poolManager.getSlot0(poolId);

if (sqrtPriceX96 == 0) revert PoolNotInitialized();

Expand Down Expand Up @@ -172,7 +174,7 @@ contract FullRange is BaseHook, ILockCallback {

PoolId poolId = key.toId();

(uint160 sqrtPriceX96,,,) = poolManager.getSlot0(poolId);
(uint160 sqrtPriceX96,,) = poolManager.getSlot0(poolId);

if (sqrtPriceX96 == 0) revert PoolNotInitialized();

Expand Down Expand Up @@ -247,7 +249,9 @@ contract FullRange is BaseHook, ILockCallback {
internal
returns (BalanceDelta delta)
{
delta = abi.decode(poolManager.lock(abi.encode(CallbackData(msg.sender, key, params))), (BalanceDelta));
delta = abi.decode(
poolManager.lock(address(this), abi.encode(CallbackData(msg.sender, key, params))), (BalanceDelta)
);
}

function _settleDeltas(address sender, PoolKey memory key, BalanceDelta delta) internal {
Expand Down Expand Up @@ -295,12 +299,14 @@ contract FullRange is BaseHook, ILockCallback {
pool.hasAccruedFees = false;
}

function lockAcquired(bytes calldata rawData)
function lockAcquired(address sender, bytes calldata rawData)
external
override(ILockCallback, BaseHook)
poolManagerOnly
returns (bytes memory)
{
// Now that manager can be called by EOAs with a lock target, it's necessary for lockAcquired to check the original sender if it wants to trust the data passed through.
if (sender != address(this)) revert SenderMustBeHook();
CallbackData memory data = abi.decode(rawData, (CallbackData));
BalanceDelta delta;

Expand Down Expand Up @@ -332,7 +338,7 @@ contract FullRange is BaseHook, ILockCallback {
) * FixedPointMathLib.sqrt(FixedPoint96.Q96)
).toUint160();

(uint160 sqrtPriceX96,,,) = poolManager.getSlot0(poolId);
(uint160 sqrtPriceX96,,) = poolManager.getSlot0(poolId);

poolManager.swap(
key,
Expand Down
22 changes: 12 additions & 10 deletions contracts/hooks/examples/GeomeanOracle.sol
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;

import {IPoolManager} from "@uniswap/v4-core/contracts/interfaces/IPoolManager.sol";
import {PoolId, PoolIdLibrary} from "@uniswap/v4-core/contracts/types/PoolId.sol";
import {Hooks} from "@uniswap/v4-core/contracts/libraries/Hooks.sol";
import {TickMath} from "@uniswap/v4-core/contracts/libraries/TickMath.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {PoolId, PoolIdLibrary} from "@uniswap/v4-core/src/types/PoolId.sol";
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol";
import {Oracle} from "../../libraries/Oracle.sol";
import {BaseHook} from "../../BaseHook.sol";
import {PoolKey} from "@uniswap/v4-core/contracts/types/PoolKey.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.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 @@ -60,16 +60,18 @@ contract GeomeanOracle is BaseHook {

constructor(IPoolManager _poolManager) BaseHook(_poolManager) {}

function getHooksCalls() public pure override returns (Hooks.Calls memory) {
return Hooks.Calls({
function getHooksCalls() public pure override returns (Hooks.Permissions memory) {
return Hooks.Permissions({
beforeInitialize: true,
afterInitialize: true,
beforeModifyPosition: true,
afterModifyPosition: false,
beforeSwap: true,
afterSwap: false,
beforeDonate: false,
afterDonate: false
afterDonate: false,
noOp: false,
accessLock: false
});
}

Expand Down Expand Up @@ -101,7 +103,7 @@ contract GeomeanOracle is BaseHook {
/// @dev Called before any action that potentially modifies pool price or liquidity, such as swap or modify position
function _updatePool(PoolKey calldata key) private {
PoolId id = key.toId();
(, int24 tick,,) = poolManager.getSlot0(id);
(, int24 tick,) = poolManager.getSlot0(id);

uint128 liquidity = poolManager.getLiquidity(id);

Expand Down Expand Up @@ -146,7 +148,7 @@ contract GeomeanOracle is BaseHook {

ObservationState memory state = states[id];

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

uint128 liquidity = poolManager.getLiquidity(id);

Expand Down
Loading

0 comments on commit 0b46125

Please sign in to comment.