Skip to content

changes src/test/ with src/mocks/ #949

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
108 changes: 108 additions & 0 deletions src/mocks/BaseTestHooks.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

import {IHooks} from "../interfaces/IHooks.sol";
import {PoolKey} from "../types/PoolKey.sol";
import {BalanceDelta} from "../types/BalanceDelta.sol";
import {BeforeSwapDelta} from "../types/BeforeSwapDelta.sol";
import {IPoolManager} from "../interfaces/IPoolManager.sol";

contract BaseTestHooks is IHooks {
error HookNotImplemented();

function beforeInitialize(address, /* sender **/ PoolKey calldata, /* key **/ uint160 /* sqrtPriceX96 **/ )
external
virtual
returns (bytes4)
{
revert HookNotImplemented();
}

function afterInitialize(
address, /* sender **/
PoolKey calldata, /* key **/
uint160, /* sqrtPriceX96 **/
int24 /* tick **/
) external virtual returns (bytes4) {
revert HookNotImplemented();
}

function beforeAddLiquidity(
address, /* sender **/
PoolKey calldata, /* key **/
IPoolManager.ModifyLiquidityParams calldata, /* params **/
bytes calldata /* hookData **/
) external virtual returns (bytes4) {
revert HookNotImplemented();
}

function afterAddLiquidity(
address, /* sender **/
PoolKey calldata, /* key **/
IPoolManager.ModifyLiquidityParams calldata, /* params **/
BalanceDelta, /* delta **/
BalanceDelta, /* feeDelta **/
bytes calldata /* hookData **/
) external virtual returns (bytes4, BalanceDelta) {
revert HookNotImplemented();
}

function beforeRemoveLiquidity(
address, /* sender **/
PoolKey calldata, /* key **/
IPoolManager.ModifyLiquidityParams calldata, /* params **/
bytes calldata /* hookData **/
) external virtual returns (bytes4) {
revert HookNotImplemented();
}

function afterRemoveLiquidity(
address, /* sender **/
PoolKey calldata, /* key **/
IPoolManager.ModifyLiquidityParams calldata, /* params **/
BalanceDelta, /* delta **/
BalanceDelta, /* feeDelta **/
bytes calldata /* hookData **/
) external virtual returns (bytes4, BalanceDelta) {
revert HookNotImplemented();
}

function beforeSwap(
address, /* sender **/
PoolKey calldata, /* key **/
IPoolManager.SwapParams calldata, /* params **/
bytes calldata /* hookData **/
) external virtual returns (bytes4, BeforeSwapDelta, uint24) {
revert HookNotImplemented();
}

function afterSwap(
address, /* sender **/
PoolKey calldata, /* key **/
IPoolManager.SwapParams calldata, /* params **/
BalanceDelta, /* delta **/
bytes calldata /* hookData **/
) external virtual returns (bytes4, int128) {
revert HookNotImplemented();
}

function beforeDonate(
address, /* sender **/
PoolKey calldata, /* key **/
uint256, /* amount0 **/
uint256, /* amount1 **/
bytes calldata /* hookData **/
) external virtual returns (bytes4) {
revert HookNotImplemented();
}

function afterDonate(
address, /* sender **/
PoolKey calldata, /* key **/
uint256, /* amount0 **/
uint256, /* amount1 **/
bytes calldata /* hookData **/
) external virtual returns (bytes4) {
revert HookNotImplemented();
}
}
30 changes: 30 additions & 0 deletions src/mocks/CurrencyTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;

import {Currency, CurrencyLibrary} from "../types/Currency.sol";

contract CurrencyTest {
function transfer(Currency currency, address to, uint256 amount) external {
currency.transfer(to, amount);
}

function balanceOfSelf(Currency currency) external view returns (uint256) {
return currency.balanceOfSelf();
}

function balanceOf(Currency currency, address owner) external view returns (uint256) {
return currency.balanceOf(owner);
}

function isAddressZero(Currency currency) external pure returns (bool) {
return currency.isAddressZero();
}

function toId(Currency currency) external pure returns (uint256) {
return currency.toId();
}

function fromId(uint256 id) external pure returns (Currency) {
return CurrencyLibrary.fromId(id);
}
}
70 changes: 70 additions & 0 deletions src/mocks/CustomCurveHook.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;

import {Hooks} from "../libraries/Hooks.sol";
import {IHooks} from "../interfaces/IHooks.sol";
import {IPoolManager} from "../interfaces/IPoolManager.sol";
import {PoolKey} from "../types/PoolKey.sol";
import {BeforeSwapDelta, toBeforeSwapDelta} from "../types/BeforeSwapDelta.sol";
import {BalanceDelta} from "../types/BalanceDelta.sol";
import {Currency} from "../types/Currency.sol";
import {CurrencySettler} from "../../test/utils/CurrencySettler.sol";
import {BaseTestHooks} from "./BaseTestHooks.sol";
import {Currency} from "../types/Currency.sol";

contract CustomCurveHook is BaseTestHooks {
using Hooks for IHooks;
using CurrencySettler for Currency;

error AddLiquidityDirectToHook();

IPoolManager immutable manager;

constructor(IPoolManager _manager) {
manager = _manager;
}

modifier onlyPoolManager() {
require(msg.sender == address(manager));
_;
}

function beforeSwap(
address, /* sender **/
PoolKey calldata key,
IPoolManager.SwapParams calldata params,
bytes calldata /* hookData **/
) external override onlyPoolManager returns (bytes4, BeforeSwapDelta, uint24) {
(Currency inputCurrency, Currency outputCurrency, uint256 amount) = _getInputOutputAndAmount(key, params);

// this "custom curve" is a line, 1-1
// take the full input amount, and give the full output amount
manager.take(inputCurrency, address(this), amount);
outputCurrency.settle(manager, address(this), amount, false);

// return -amountSpecified as specified to no-op the concentrated liquidity swap
BeforeSwapDelta hookDelta = toBeforeSwapDelta(int128(-params.amountSpecified), int128(params.amountSpecified));
return (IHooks.beforeSwap.selector, hookDelta, 0);
}

function afterAddLiquidity(
address, /* sender **/
PoolKey calldata, /* key **/
IPoolManager.ModifyLiquidityParams calldata, /* params **/
BalanceDelta, /* delta **/
BalanceDelta, /* feeDelta **/
bytes calldata /* hookData **/
) external view override onlyPoolManager returns (bytes4, BalanceDelta) {
revert AddLiquidityDirectToHook();
}

function _getInputOutputAndAmount(PoolKey calldata key, IPoolManager.SwapParams calldata params)
internal
pure
returns (Currency input, Currency output, uint256 amount)
{
(input, output) = params.zeroForOne ? (key.currency0, key.currency1) : (key.currency1, key.currency0);

amount = params.amountSpecified < 0 ? uint256(-params.amountSpecified) : uint256(params.amountSpecified);
}
}
99 changes: 99 additions & 0 deletions src/mocks/DeltaReturningHook.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;

import {Hooks} from "../libraries/Hooks.sol";
import {IHooks} from "../interfaces/IHooks.sol";
import {IPoolManager} from "../interfaces/IPoolManager.sol";
import {CurrencySettler} from "../../test/utils/CurrencySettler.sol";
import {PoolKey} from "../types/PoolKey.sol";
import {BalanceDelta} from "../types/BalanceDelta.sol";
import {Currency} from "../types/Currency.sol";
import {BaseTestHooks} from "./BaseTestHooks.sol";
import {Currency} from "../types/Currency.sol";
import {BeforeSwapDelta, toBeforeSwapDelta} from "../types/BeforeSwapDelta.sol";

contract DeltaReturningHook is BaseTestHooks {
using Hooks for IHooks;
using CurrencySettler for Currency;

IPoolManager immutable manager;

int128 deltaSpecified;
int128 deltaUnspecifiedBeforeSwap;
int128 deltaUnspecifiedAfterSwap;

constructor(IPoolManager _manager) {
manager = _manager;
}

modifier onlyPoolManager() {
require(msg.sender == address(manager));
_;
}

function setDeltaSpecified(int128 delta) external {
deltaSpecified = delta;
}

function setDeltaUnspecifiedBeforeSwap(int128 delta) external {
deltaUnspecifiedBeforeSwap = delta;
}

function setDeltaUnspecifiedAfterSwap(int128 delta) external {
deltaUnspecifiedAfterSwap = delta;
}

function beforeSwap(
address, /* sender **/
PoolKey calldata key,
IPoolManager.SwapParams calldata params,
bytes calldata /* hookData **/
) external override onlyPoolManager returns (bytes4, BeforeSwapDelta, uint24) {
(Currency specifiedCurrency, Currency unspecifiedCurrency) = _sortCurrencies(key, params);

if (deltaSpecified != 0) _settleOrTake(specifiedCurrency, deltaSpecified);
if (deltaUnspecifiedBeforeSwap != 0) _settleOrTake(unspecifiedCurrency, deltaUnspecifiedBeforeSwap);

BeforeSwapDelta beforeSwapDelta = toBeforeSwapDelta(deltaSpecified, deltaUnspecifiedBeforeSwap);

return (IHooks.beforeSwap.selector, beforeSwapDelta, 0);
}

function afterSwap(
address, /* sender **/
PoolKey calldata key,
IPoolManager.SwapParams calldata params,
BalanceDelta, /* delta **/
bytes calldata /* hookData **/
) external override onlyPoolManager returns (bytes4, int128) {
(, Currency unspecifiedCurrency) = _sortCurrencies(key, params);
_settleOrTake(unspecifiedCurrency, deltaUnspecifiedAfterSwap);

return (IHooks.afterSwap.selector, deltaUnspecifiedAfterSwap);
}

function _sortCurrencies(PoolKey calldata key, IPoolManager.SwapParams calldata params)
internal
pure
returns (Currency specified, Currency unspecified)
{
(specified, unspecified) = (params.zeroForOne == (params.amountSpecified < 0))
? (key.currency0, key.currency1)
: (key.currency1, key.currency0);
}

function _settleOrTake(Currency currency, int128 delta) internal {
// positive amount means positive delta for the hook, so it can take
// negative it should settle
if (delta > 0) {
currency.take(manager, address(this), uint128(delta), false);
} else {
uint256 amount = uint256(-int256(delta));
if (currency.isAddressZero()) {
manager.settle{value: amount}();
} else {
currency.settle(manager, address(this), amount, false);
}
}
}
}
39 changes: 39 additions & 0 deletions src/mocks/DynamicFeesTestHook.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

import {BaseTestHooks} from "./BaseTestHooks.sol";
import {PoolKey} from "../types/PoolKey.sol";
import {IPoolManager} from "../interfaces/IPoolManager.sol";
import {IHooks} from "../interfaces/IHooks.sol";
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "../types/BeforeSwapDelta.sol";

contract DynamicFeesTestHook is BaseTestHooks {
uint24 internal fee;
IPoolManager manager;

function setManager(IPoolManager _manager) external {
manager = _manager;
}

function setFee(uint24 _fee) external {
fee = _fee;
}

function afterInitialize(address, PoolKey calldata key, uint160, int24) external override returns (bytes4) {
manager.updateDynamicLPFee(key, fee);
return IHooks.afterInitialize.selector;
}

function beforeSwap(address, PoolKey calldata key, IPoolManager.SwapParams calldata, bytes calldata)
external
override
returns (bytes4, BeforeSwapDelta, uint24)
{
manager.updateDynamicLPFee(key, fee);
return (IHooks.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, 0);
}

function forcePoolFeeUpdate(PoolKey calldata _key, uint24 _fee) external {
manager.updateDynamicLPFee(_key, _fee);
}
}
38 changes: 38 additions & 0 deletions src/mocks/DynamicReturnFeeTestHook.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

import {BaseTestHooks} from "./BaseTestHooks.sol";
import {PoolKey} from "../types/PoolKey.sol";
import {IPoolManager} from "../interfaces/IPoolManager.sol";
import {IHooks} from "../interfaces/IHooks.sol";
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "../types/BeforeSwapDelta.sol";
import {LPFeeLibrary} from "../libraries/LPFeeLibrary.sol";

contract DynamicReturnFeeTestHook is BaseTestHooks {
using LPFeeLibrary for uint24;

uint24 internal fee;
IPoolManager manager;

function setManager(IPoolManager _manager) external {
manager = _manager;
}

function setFee(uint24 _fee) external {
fee = _fee;
}

function beforeSwap(address, PoolKey calldata, IPoolManager.SwapParams calldata, bytes calldata)
external
view
override
returns (bytes4, BeforeSwapDelta, uint24)
{
// attach the fee flag to `fee` to enable overriding the pool's stored fee
return (IHooks.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, fee | LPFeeLibrary.OVERRIDE_FEE_FLAG);
}

function forcePoolFeeUpdate(PoolKey calldata _key, uint24 _fee) external {
manager.updateDynamicLPFee(_key, _fee);
}
}
9 changes: 9 additions & 0 deletions src/mocks/EmptyRevertContract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

contract EmptyRevertContract {
// a contract to simulate reverting with no returndata, to test that our error catching works
fallback() external {
revert();
}
}
Loading