Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dianakocsis committed Mar 22, 2024
1 parent cd9bff9 commit 4bf9249
Show file tree
Hide file tree
Showing 5 changed files with 343 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/donate gas with 1 token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131119
131041
2 changes: 1 addition & 1 deletion .forge-snapshots/donate gas with 2 tokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
175942
175864
12 changes: 4 additions & 8 deletions src/test/PoolDonateTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,17 @@ contract PoolDonateTest is PoolTestBase {

CallbackData memory data = abi.decode(rawData, (CallbackData));

(,, uint256 reserveBefore0, int256 deltaBefore0) =
_fetchBalances(data.key.currency0, data.sender, address(this));
(,, uint256 reserveBefore1, int256 deltaBefore1) =
_fetchBalances(data.key.currency1, data.sender, address(this));
(,,, int256 deltaBefore0) = _fetchBalances(data.key.currency0, data.sender, address(this));
(,,, int256 deltaBefore1) = _fetchBalances(data.key.currency1, data.sender, address(this));

require(deltaBefore0 == 0, "deltaBefore0 is not 0");
require(deltaBefore1 == 0, "deltaBefore1 is not 0");

BalanceDelta delta = manager.donate(data.key, data.amount0, data.amount1, data.hookData);

(,, uint256 reserveAfter0, int256 deltaAfter0) = _fetchBalances(data.key.currency0, data.sender, address(this));
(,, uint256 reserveAfter1, int256 deltaAfter1) = _fetchBalances(data.key.currency1, data.sender, address(this));
(,,, int256 deltaAfter0) = _fetchBalances(data.key.currency0, data.sender, address(this));
(,,, int256 deltaAfter1) = _fetchBalances(data.key.currency1, data.sender, address(this));

require(reserveBefore0 == reserveAfter0, "reserveBefore0 is not equal to reserveAfter0");
require(reserveBefore1 == reserveAfter1, "reserveBefore1 is not equal to reserveAfter1");
require(deltaAfter0 == -int256(data.amount0), "deltaAfter0 is not equal to -int256(data.amount0)");
require(deltaAfter1 == -int256(data.amount1), "deltaAfter1 is not equal to -int256(data.amount1)");

Expand Down
146 changes: 143 additions & 3 deletions src/test/SkipCallsTestHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {PoolId, PoolIdLibrary} from "../types/PoolId.sol";
import {IERC20Minimal} from "../interfaces/external/IERC20Minimal.sol";
import {CurrencyLibrary, Currency} from "../types/Currency.sol";
import {PoolTestBase} from "./PoolTestBase.sol";
import {Constants} from "../../test/utils/Constants.sol";
import {Test} from "forge-std/Test.sol";

contract SkipCallsTestHook is BaseTestHooks, Test {
Expand All @@ -19,23 +20,129 @@ contract SkipCallsTestHook is BaseTestHooks, Test {

uint256 public counter;
IPoolManager manager;
uint24 internal fee;

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

function beforeInitialize(address, PoolKey calldata key, uint160 sqrtPriceX96, bytes calldata hookData)
external
override
returns (bytes4)
{
counter++;
_initialize(key, sqrtPriceX96, hookData);
return IHooks.beforeInitialize.selector;
}

function afterInitialize(address, PoolKey calldata key, uint160 sqrtPriceX96, int24, bytes calldata hookData)
external
override
returns (bytes4)
{
counter++;
_initialize(key, sqrtPriceX96, hookData);
return IHooks.afterInitialize.selector;
}

function beforeAddLiquidity(
address,
PoolKey calldata key,
IPoolManager.ModifyLiquidityParams calldata,
bytes calldata hookData
) external override returns (bytes4) {
counter++;
_initialize(key, Constants.SQRT_RATIO_1_1, hookData);
return IHooks.beforeAddLiquidity.selector;
}

function afterAddLiquidity(
address,
PoolKey calldata key,
IPoolManager.ModifyLiquidityParams calldata,
BalanceDelta,
bytes calldata hookData
) external override returns (bytes4) {
counter++;
_initialize(key, Constants.SQRT_RATIO_1_1, hookData);
return IHooks.afterAddLiquidity.selector;
}

function beforeRemoveLiquidity(
address,
PoolKey calldata key,
IPoolManager.ModifyLiquidityParams calldata,
bytes calldata hookData
) external override returns (bytes4) {
counter++;
IPoolManager.ModifyLiquidityParams memory newParams =
IPoolManager.ModifyLiquidityParams({tickLower: -120, tickUpper: 120, liquidityDelta: 0.1e18});
_modifyLiquidity(key, newParams, hookData);
return IHooks.beforeRemoveLiquidity.selector;
}

function afterRemoveLiquidity(
address,
PoolKey calldata key,
IPoolManager.ModifyLiquidityParams calldata,
BalanceDelta,
bytes calldata hookData
) external override returns (bytes4) {
counter++;
IPoolManager.ModifyLiquidityParams memory newParams =
IPoolManager.ModifyLiquidityParams({tickLower: -120, tickUpper: 120, liquidityDelta: 0.1e18});
_modifyLiquidity(key, newParams, hookData);
return IHooks.afterRemoveLiquidity.selector;
}

function beforeSwap(address, PoolKey calldata key, IPoolManager.SwapParams calldata params, bytes calldata hookData)
external
override
returns (bytes4)
{
counter++;
callSwap(key, params, hookData);
_swap(key, params, hookData);
return IHooks.beforeSwap.selector;
}

function callSwap(PoolKey calldata key, IPoolManager.SwapParams calldata params, bytes calldata hookData) public {
function afterSwap(
address,
PoolKey calldata key,
IPoolManager.SwapParams calldata params,
BalanceDelta,
bytes calldata hookData
) external override returns (bytes4) {
counter++;
_swap(key, params, hookData);
return IHooks.afterSwap.selector;
}

function beforeDonate(address, PoolKey calldata key, uint256 amt0, uint256 amt1, bytes calldata hookData)
external
override
returns (bytes4)
{
counter++;
_donate(key, amt0, amt1, hookData);
return IHooks.beforeDonate.selector;
}

function afterDonate(address, PoolKey calldata key, uint256 amt0, uint256 amt1, bytes calldata hookData)
external
override
returns (bytes4)
{
counter++;
_donate(key, amt0, amt1, hookData);
return IHooks.afterDonate.selector;
}

function _initialize(PoolKey memory key, uint160 sqrtPriceX96, bytes calldata hookData) public {
key.fee = 2000;
IPoolManager(manager).initialize(key, sqrtPriceX96, hookData);
}

function _swap(PoolKey calldata key, IPoolManager.SwapParams memory params, bytes calldata hookData) public {
IPoolManager(manager).swap(key, params, hookData);
address payer = abi.decode(hookData, (address));
int256 delta0 = IPoolManager(manager).currencyDelta(address(this), key.currency0);
Expand All @@ -46,4 +153,37 @@ contract SkipCallsTestHook is BaseTestHooks, Test {
manager.settle(key.currency0);
manager.take(key.currency1, payer, uint256(delta1));
}

function _modifyLiquidity(
PoolKey calldata key,
IPoolManager.ModifyLiquidityParams memory params,
bytes calldata hookData
) public {
IPoolManager(manager).modifyLiquidity(key, params, hookData);
address payer = abi.decode(hookData, (address));
int256 delta0 = IPoolManager(manager).currencyDelta(address(this), key.currency0);
int256 delta1 = IPoolManager(manager).currencyDelta(address(this), key.currency1);
if (params.liquidityDelta < 0) {
assert(delta0 > 0 || delta1 > 0);
assert(!(delta0 < 0 || delta1 < 0));
} else if (params.liquidityDelta > 0) {
assert(delta0 < 0 || delta1 < 0);
assert(!(delta0 > 0 || delta1 > 0));
}
IERC20Minimal(Currency.unwrap(key.currency0)).transferFrom(payer, address(manager), uint256(-delta0));
manager.settle(key.currency0);
IERC20Minimal(Currency.unwrap(key.currency1)).transferFrom(payer, address(manager), uint256(-delta1));
manager.settle(key.currency1);
}

function _donate(PoolKey calldata key, uint256 amt0, uint256 amt1, bytes calldata hookData) public {
IPoolManager(manager).donate(key, amt0, amt1, hookData);
address payer = abi.decode(hookData, (address));
int256 delta0 = IPoolManager(manager).currencyDelta(address(this), key.currency0);
int256 delta1 = IPoolManager(manager).currencyDelta(address(this), key.currency1);
IERC20Minimal(Currency.unwrap(key.currency0)).transferFrom(payer, address(manager), uint256(-delta0));
IERC20Minimal(Currency.unwrap(key.currency1)).transferFrom(payer, address(manager), uint256(-delta1));
manager.settle(key.currency0);
manager.settle(key.currency1);
}
}
Loading

0 comments on commit 4bf9249

Please sign in to comment.