Skip to content
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

[Chore] Update v4-core:latest #100

Merged
merged 19 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeAddInitialLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
392772
384735
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeAddLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
187139
179102
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeFirstSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
136542
128152
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeInitialize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1041060
1017530
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeRemoveLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
175903
169304
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeRemoveLiquidityAndRebalance.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
363995
345919
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeSecondSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
97295
89081
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134817
126954
2 changes: 1 addition & 1 deletion .forge-snapshots/TWAMMSubmitOrder.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
122753
122845
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ There are many ways to contribute, but here are a few if you want a place to sta

## Opening an Issue

When opening an [issue](https://github.com/Uniswap/periphery-next/issues/new/choose), choose a template to start from: Bug Report or Feature Improvement. For bug reports, you should be able to reproduce the bug through tests or proof of concept integrations. For feature improvements, please title it with a concise problem statement and check that a similar request is not already open or already in progress. Not all issues may be deemed worth resolving, so please follow through with responding to any questions or comments that others may have regarding the issue.
When opening an [issue](https://github.com/Uniswap/v4-periphery/issues/new/choose), choose a template to start from: Bug Report or Feature Improvement. For bug reports, you should be able to reproduce the bug through tests or proof of concept integrations. For feature improvements, please title it with a concise problem statement and check that a similar request is not already open or already in progress. Not all issues may be deemed worth resolving, so please follow through with responding to any questions or comments that others may have regarding the issue.

Feel free to tag the issue as a “good first issue” for any clean-up related issues, or small scoped changes to help encourage pull requests from first time contributors!

Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Uniswap v4 is a new automated market maker protocol that provides extensibility

## Contributing

If you’re interested in contributing please see the [contribution guidelines](https://github.com/Uniswap/periphery-next/blob/main/CONTRIBUTING.md)!
If you’re interested in contributing please see the [contribution guidelines](https://github.com/Uniswap/v4-periphery/blob/main/CONTRIBUTING.md)!

## Repository Structure

Expand All @@ -31,29 +31,29 @@ Eventually, some hooks that have been audited and are considered production-read
To utilize the contracts and deploy to a local testnet, you can install the code in your repo with forge:

```solidity
forge install https://github.com/Uniswap/periphery-next
forge install https://github.com/Uniswap/v4-periphery
```

If you are building hooks, it may be useful to inherit from the `BaseHook` contract:

```solidity

import {BaseHook} from 'periphery-next/contracts/BaseHook.sol';
import {BaseHook} from 'v4-periphery/contracts/BaseHook.sol';

contract CoolHook is BaseHook {
// Override the hook callbacks you want on your hook
function beforeModifyPosition(
function beforeAddLiquidity(
address,
IPoolManager.PoolKey calldata key,
IPoolManager.ModifyLiquidityParams calldata params
) external override poolManagerOnly returns (bytes4) {
// hook logic
return BaseHook.beforeModifyPosition.selector;
return BaseHook.beforeAddLiquidity.selector;
}
}

```

## License

The license for Uniswap V4 Periphery is the GNU General Public License (GPL 2.0), see [LICENSE](https://github.com/Uniswap/periphery-next/blob/main/LICENSE).
The license for Uniswap V4 Periphery is the GNU General Public License (GPL 2.0), see [LICENSE](https://github.com/Uniswap/v4-periphery/blob/main/LICENSE).
7 changes: 1 addition & 6 deletions contracts/BaseHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ abstract contract BaseHook is IHooks {
Hooks.validateHookPermissions(_this, getHookPermissions());
}

function lockAcquired(address, /*sender*/ bytes calldata data)
external
virtual
poolManagerOnly
returns (bytes memory)
{
function lockAcquired(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
46 changes: 20 additions & 26 deletions contracts/hooks/examples/FullRange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ contract FullRange is BaseHook, ILockCallback {
beforeSwap: true,
afterSwap: false,
beforeDonate: false,
afterDonate: false,
noOp: false,
accessLock: false
afterDonate: false
});
}

Expand All @@ -119,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 All @@ -138,7 +136,7 @@ contract FullRange is BaseHook, ILockCallback {
if (poolLiquidity == 0 && liquidity <= MINIMUM_LIQUIDITY) {
revert LiquidityDoesntMeetMinimum();
}
BalanceDelta addedDelta = modifyPosition(
BalanceDelta addedDelta = modifyLiquidity(
key,
IPoolManager.ModifyLiquidityParams({
tickLower: MIN_TICK,
Expand All @@ -155,7 +153,7 @@ contract FullRange is BaseHook, ILockCallback {

UniswapV4ERC20(pool.liquidityToken).mint(params.to, liquidity);

if (uint128(addedDelta.amount0()) < params.amount0Min || uint128(addedDelta.amount1()) < params.amount1Min) {
if (uint128(-addedDelta.amount0()) < params.amount0Min || uint128(-addedDelta.amount1()) < params.amount1Min) {
revert TooMuchSlippage();
}
}
Expand All @@ -176,13 +174,13 @@ contract FullRange is BaseHook, ILockCallback {

PoolId poolId = key.toId();

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

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

UniswapV4ERC20 erc20 = UniswapV4ERC20(poolInfo[poolId].liquidityToken);

delta = modifyPosition(
delta = modifyLiquidity(
key,
IPoolManager.ModifyLiquidityParams({
tickLower: MIN_TICK,
Expand Down Expand Up @@ -247,18 +245,16 @@ contract FullRange is BaseHook, ILockCallback {
return IHooks.beforeSwap.selector;
}

function modifyPosition(PoolKey memory key, IPoolManager.ModifyLiquidityParams memory params)
function modifyLiquidity(PoolKey memory key, IPoolManager.ModifyLiquidityParams memory params)
internal
returns (BalanceDelta delta)
{
delta = abi.decode(
poolManager.lock(address(this), abi.encode(CallbackData(msg.sender, key, params))), (BalanceDelta)
);
delta = abi.decode(poolManager.lock(abi.encode(CallbackData(msg.sender, key, params))), (BalanceDelta));
}

function _settleDeltas(address sender, PoolKey memory key, BalanceDelta delta) internal {
_settleDelta(sender, key.currency0, uint128(delta.amount0()));
_settleDelta(sender, key.currency1, uint128(delta.amount1()));
_settleDelta(sender, key.currency0, uint128(-delta.amount0()));
_settleDelta(sender, key.currency1, uint128(-delta.amount1()));
}

function _settleDelta(address sender, Currency currency, uint128 amount) internal {
Expand All @@ -275,8 +271,8 @@ contract FullRange is BaseHook, ILockCallback {
}

function _takeDeltas(address sender, PoolKey memory key, BalanceDelta delta) internal {
poolManager.take(key.currency0, sender, uint256(uint128(-delta.amount0())));
poolManager.take(key.currency1, sender, uint256(uint128(-delta.amount1())));
poolManager.take(key.currency0, sender, uint256(uint128(delta.amount0())));
poolManager.take(key.currency1, sender, uint256(uint128(delta.amount1())));
}

function _removeLiquidity(PoolKey memory key, IPoolManager.ModifyLiquidityParams memory params)
Expand All @@ -301,14 +297,12 @@ contract FullRange is BaseHook, ILockCallback {
pool.hasAccruedFees = false;
}

function lockAcquired(address sender, bytes calldata rawData)
function lockAcquired(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 @@ -336,17 +330,17 @@ contract FullRange is BaseHook, ILockCallback {

uint160 newSqrtPriceX96 = (
FixedPointMathLib.sqrt(
FullMath.mulDiv(uint128(-balanceDelta.amount1()), FixedPoint96.Q96, uint128(-balanceDelta.amount0()))
FullMath.mulDiv(uint128(balanceDelta.amount1()), FixedPoint96.Q96, uint128(balanceDelta.amount0()))
) * FixedPointMathLib.sqrt(FixedPoint96.Q96)
).toUint160();

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

poolManager.swap(
key,
IPoolManager.SwapParams({
zeroForOne: newSqrtPriceX96 < sqrtPriceX96,
amountSpecified: MAX_INT,
amountSpecified: -MAX_INT - 1, // equivalent of type(int256).min
sqrtPriceLimitX96: newSqrtPriceX96
}),
ZERO_BYTES
Expand All @@ -356,8 +350,8 @@ contract FullRange is BaseHook, ILockCallback {
newSqrtPriceX96,
TickMath.getSqrtRatioAtTick(MIN_TICK),
TickMath.getSqrtRatioAtTick(MAX_TICK),
uint256(uint128(-balanceDelta.amount0())),
uint256(uint128(-balanceDelta.amount1()))
uint256(uint128(balanceDelta.amount0())),
uint256(uint128(balanceDelta.amount1()))
);

BalanceDelta balanceDeltaAfter = poolManager.modifyLiquidity(
Expand All @@ -371,8 +365,8 @@ contract FullRange is BaseHook, ILockCallback {
);

// Donate any "dust" from the sqrtRatio change as fees
uint128 donateAmount0 = uint128(-balanceDelta.amount0() - balanceDeltaAfter.amount0());
uint128 donateAmount1 = uint128(-balanceDelta.amount1() - balanceDeltaAfter.amount1());
uint128 donateAmount0 = uint128(balanceDelta.amount0() + balanceDeltaAfter.amount0());
uint128 donateAmount1 = uint128(balanceDelta.amount1() + balanceDeltaAfter.amount1());

poolManager.donate(key, donateAmount0, donateAmount1, ZERO_BYTES);
}
Expand Down
8 changes: 3 additions & 5 deletions contracts/hooks/examples/GeomeanOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ contract GeomeanOracle is BaseHook {
beforeSwap: true,
afterSwap: false,
beforeDonate: false,
afterDonate: false,
noOp: false,
accessLock: false
afterDonate: false
});
}

Expand Down Expand Up @@ -105,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 @@ -158,7 +156,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
Loading