Skip to content

Commit

Permalink
Update no-op tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cairoeth committed Jan 5, 2025
1 parent 72bd290 commit fb473ef
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 108 deletions.
65 changes: 10 additions & 55 deletions src/base/BaseCustomAccounting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,11 @@ import {BaseHook} from "src/base/BaseHook.sol";
import {Hooks} from "v4-core/src/libraries/Hooks.sol";
import {PoolKey} from "v4-core/src/types/PoolKey.sol";
import {IPoolManager} from "v4-core/src/interfaces/IPoolManager.sol";
import {BeforeSwapDelta, toBeforeSwapDelta, BeforeSwapDeltaLibrary} from "v4-core/src/types/BeforeSwapDelta.sol";
import {Currency} from "v4-core/src/types/Currency.sol";
import {SafeCast} from "v4-core/src/libraries/SafeCast.sol";
import {CurrencySettler} from "v4-core/test/utils/CurrencySettler.sol";
import {ERC6909} from "v4-core/src/ERC6909.sol";
import {BalanceDelta} from "v4-core/src/types/BalanceDelta.sol";
import {PoolId} from "v4-core/src/types/PoolId.sol";
import {TickMath} from "v4-core/src/libraries/TickMath.sol";
import {LiquidityAmounts} from "v4-periphery/src/libraries/LiquidityAmounts.sol";
import {StateLibrary} from "v4-core/src/libraries/StateLibrary.sol";
import {ERC20} from "openzeppelin/token/ERC20/ERC20.sol";
import {FullMath} from "v4-core/src/libraries/FullMath.sol";
import {TransientStateLibrary} from "v4-core/src/libraries/TransientStateLibrary.sol";

/**
* @dev Base implementation for custom accounting and hook-owned liquidity, which must be deposited
Expand All @@ -38,11 +30,9 @@ import {TransientStateLibrary} from "v4-core/src/libraries/TransientStateLibrary
*
* _Available since v0.1.0_
*/
abstract contract BaseCustomAccounting is BaseHook, ERC20 {
abstract contract BaseCustomAccounting is BaseHook {
using CurrencySettler for Currency;
using SafeCast for uint256;
using StateLibrary for IPoolManager;
using TransientStateLibrary for IPoolManager;

error ExpiredPastDeadline();
error PoolNotInitialized();
Expand Down Expand Up @@ -82,12 +72,9 @@ abstract contract BaseCustomAccounting is BaseHook, ERC20 {
}

/**
* @dev Set the pool poolManager and hook's token parameters.
* @dev Set the pool `PoolManager` address.
*/
constructor(IPoolManager _poolManager, string memory _name, string memory _symbol)
BaseHook(_poolManager)
ERC20(_name, _symbol)
{}
constructor(IPoolManager _poolManager) BaseHook(_poolManager) {}

/**
* @notice Adds liquidity to the hook's pool.
Expand All @@ -111,7 +98,7 @@ abstract contract BaseCustomAccounting is BaseHook, ERC20 {

delta = _modifyLiquidity(modifyParams);

_mint(params.to, liquidity);
_mint(params, delta, liquidity);

if (uint128(-delta.amount0()) < params.amount0Min || uint128(-delta.amount1()) < params.amount1Min) {
revert TooMuchSlippage();
Expand All @@ -137,7 +124,7 @@ abstract contract BaseCustomAccounting is BaseHook, ERC20 {

delta = _modifyLiquidity(modifyParams);

_burn(msg.sender, liquidity);
_burn(params, delta, liquidity);
}

/**
Expand Down Expand Up @@ -193,48 +180,16 @@ abstract contract BaseCustomAccounting is BaseHook, ERC20 {
function _getAddLiquidity(uint160 sqrtPriceX96, AddLiquidityParams memory params)
internal
virtual
returns (bytes memory modify, uint256 liquidity)
{
liquidity = LiquidityAmounts.getLiquidityForAmounts(
sqrtPriceX96,
TickMath.getSqrtPriceAtTick(params.tickLower),
TickMath.getSqrtPriceAtTick(params.tickUpper),
params.amount0Desired,
params.amount1Desired
);

return (
abi.encode(
IPoolManager.ModifyLiquidityParams({
tickLower: params.tickLower,
tickUpper: params.tickUpper,
liquidityDelta: liquidity.toInt256(),
salt: 0
})
),
liquidity
);
}
returns (bytes memory modify, uint256 liquidity);

function _getRemoveLiquidity(RemoveLiquidityParams memory params)
internal
virtual
returns (bytes memory, uint256 liquidity)
{
liquidity = FullMath.mulDiv(params.liquidity, poolManager.getLiquidity(poolKey.toId()), totalSupply());
returns (bytes memory modify, uint256 liquidity);

return (
abi.encode(
IPoolManager.ModifyLiquidityParams({
tickLower: params.tickLower,
tickUpper: params.tickUpper,
liquidityDelta: -liquidity.toInt256(),
salt: 0
})
),
liquidity
);
}
function _mint(AddLiquidityParams memory params, BalanceDelta delta, uint256 liquidity) internal virtual;

function _burn(RemoveLiquidityParams memory params, BalanceDelta delta, uint256 liquidity) internal virtual;

/**
* @dev Set the hook permissions, specifically `beforeSwap` and `beforeSwapReturnDelta`.
Expand Down
14 changes: 6 additions & 8 deletions src/base/BaseCustomCurve.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ abstract contract BaseCustomCurve is BaseCustomAccounting {
error OnlyDirectLiquidity();

/**
* @dev Set the pool manager.
* @dev Set the PoolManager address.
*/
constructor(IPoolManager _poolManager, string memory _name, string memory _symbol)
BaseCustomAccounting(_poolManager, _name, _symbol)
{}
constructor(IPoolManager _poolManager) BaseCustomAccounting(_poolManager) {}

/**
* @dev Force liquidity to only be added directly to the hook.
Expand All @@ -75,7 +73,7 @@ abstract contract BaseCustomCurve is BaseCustomAccounting {
override
returns (bytes memory, uint256)
{
(uint256 amount0, uint256 amount1, uint256 liquidity) = _calculateIn(params);
(uint256 amount0, uint256 amount1, uint256 liquidity) = _getAmountIn(params);
return (abi.encode(amount0.toInt128(), amount1.toInt128()), liquidity);
}

Expand All @@ -85,7 +83,7 @@ abstract contract BaseCustomCurve is BaseCustomAccounting {
override
returns (bytes memory, uint256)
{
(uint256 amount0, uint256 amount1, uint256 liquidity) = _calculateOut(params);
(uint256 amount0, uint256 amount1, uint256 liquidity) = _getAmountOut(params);
return (abi.encode(-amount0.toInt128(), -amount1.toInt128()), liquidity);
}

Expand Down Expand Up @@ -205,12 +203,12 @@ abstract contract BaseCustomCurve is BaseCustomAccounting {
virtual
returns (uint256 amountIn);

function _calculateOut(RemoveLiquidityParams memory params)
function _getAmountOut(RemoveLiquidityParams memory params)
internal
virtual
returns (uint256 amount0, uint256 amount1, uint256 liquidity);

function _calculateIn(AddLiquidityParams memory params)
function _getAmountIn(AddLiquidityParams memory params)
internal
virtual
returns (uint256 amount0, uint256 amount1, uint256 liquidity);
Expand Down
18 changes: 10 additions & 8 deletions src/base/BaseHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ abstract contract BaseHook is IHooks {
function beforeInitialize(address sender, PoolKey calldata key, uint160 sqrtPriceX96)
external
virtual
onlyPoolManager
returns (bytes4)
{
return _beforeInitialize(sender, key, sqrtPriceX96);
Expand All @@ -134,6 +135,7 @@ abstract contract BaseHook is IHooks {
function afterInitialize(address sender, PoolKey calldata key, uint160 sqrtPriceX96, int24 tick)
external
virtual
onlyPoolManager
returns (bytes4)
{
return _afterInitialize(sender, key, sqrtPriceX96, tick);
Expand All @@ -151,7 +153,7 @@ abstract contract BaseHook is IHooks {
PoolKey calldata key,
IPoolManager.ModifyLiquidityParams calldata params,
bytes calldata hookData
) external virtual returns (bytes4) {
) external virtual onlyPoolManager returns (bytes4) {
return _beforeAddLiquidity(sender, key, params, hookData);
}

Expand All @@ -171,7 +173,7 @@ abstract contract BaseHook is IHooks {
PoolKey calldata key,
IPoolManager.ModifyLiquidityParams calldata params,
bytes calldata hookData
) external virtual returns (bytes4) {
) external virtual onlyPoolManager returns (bytes4) {
return _beforeRemoveLiquidity(sender, key, params, hookData);
}

Expand All @@ -194,7 +196,7 @@ abstract contract BaseHook is IHooks {
BalanceDelta delta0,
BalanceDelta delta1,
bytes calldata hookData
) external virtual returns (bytes4, BalanceDelta) {
) external virtual onlyPoolManager returns (bytes4, BalanceDelta) {
return _afterAddLiquidity(sender, key, params, delta0, delta1, hookData);
}

Expand All @@ -219,7 +221,7 @@ abstract contract BaseHook is IHooks {
BalanceDelta delta0,
BalanceDelta delta1,
bytes calldata hookData
) external virtual returns (bytes4, BalanceDelta) {
) external virtual onlyPoolManager returns (bytes4, BalanceDelta) {
return _afterRemoveLiquidity(sender, key, params, delta0, delta1, hookData);
}

Expand All @@ -242,7 +244,7 @@ abstract contract BaseHook is IHooks {
PoolKey calldata key,
IPoolManager.SwapParams calldata params,
bytes calldata hookData
) external virtual returns (bytes4, BeforeSwapDelta, uint24) {
) external virtual onlyPoolManager returns (bytes4, BeforeSwapDelta, uint24) {
return _beforeSwap(sender, key, params, hookData);
}

Expand All @@ -263,7 +265,7 @@ abstract contract BaseHook is IHooks {
IPoolManager.SwapParams calldata params,
BalanceDelta delta,
bytes calldata hookData
) external virtual returns (bytes4, int128) {
) external virtual onlyPoolManager returns (bytes4, int128) {
return _afterSwap(sender, key, params, delta, hookData);
}

Expand All @@ -284,7 +286,7 @@ abstract contract BaseHook is IHooks {
uint256 amount0,
uint256 amount1,
bytes calldata hookData
) external virtual returns (bytes4) {
) external virtual onlyPoolManager returns (bytes4) {
return _beforeDonate(sender, key, amount0, amount1, hookData);
}

Expand All @@ -305,7 +307,7 @@ abstract contract BaseHook is IHooks {
uint256 amount0,
uint256 amount1,
bytes calldata hookData
) external virtual returns (bytes4) {
) external virtual onlyPoolManager returns (bytes4) {
return _afterDonate(sender, key, amount0, amount1, hookData);
}

Expand Down
Loading

0 comments on commit fb473ef

Please sign in to comment.