Skip to content

Commit

Permalink
fix: test router was borked
Browse files Browse the repository at this point in the history
  • Loading branch information
marktoda committed Nov 17, 2023
1 parent 4fe142c commit be6aa94
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeFirstSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133445
133339
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeSecondSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
93791
93685
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131720
131614
1 change: 1 addition & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ cancun = true

[profile.ci]
fuzz_runs = 100000
solc = "./bin/solc-static-linux"

# See more config options https://github.com/foundry-rs/foundry/tree/master/config
4 changes: 1 addition & 3 deletions test/FullRange.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ contract TestFullRange is Test, Deployers, GasSnapshot {
token1.approve(address(router), type(uint256).max);
token2.approve(address(router), type(uint256).max);

initPool(
keyWithLiq.currency0, keyWithLiq.currency1, fullRange, 3000, SQRT_RATIO_1_1, ZERO_BYTES
);
initPool(keyWithLiq.currency0, keyWithLiq.currency1, fullRange, 3000, SQRT_RATIO_1_1, ZERO_BYTES);
fullRange.addLiquidity(
FullRange.AddLiquidityParams(
keyWithLiq.currency0,
Expand Down
9 changes: 5 additions & 4 deletions test/LimitOrder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ contract TestLimitOrder is Test, Deployers {
}

// key = PoolKey(currency0, currency1, 3000, 60, limitOrder);
(key, id) = initPoolAndAddLiquidity(
currency0, currency1, limitOrder, 3000, SQRT_RATIO_1_1, ZERO_BYTES
);
(key, id) = initPoolAndAddLiquidity(currency0, currency1, limitOrder, 3000, SQRT_RATIO_1_1, ZERO_BYTES);

token0.approve(address(limitOrder), type(uint256).max);
token1.approve(address(limitOrder), type(uint256).max);
Expand Down Expand Up @@ -130,7 +128,10 @@ contract TestLimitOrder is Test, Deployers {
function testNotZeroForOneInRangeRevert() public {
// swapping is free, there's no liquidity in the pool, so we only need to specify 1 wei
router.swap(
key, IPoolManager.SwapParams(true, 1, SQRT_RATIO_1_1 - 1), HookEnabledSwapRouter.TestSettings(true, true), ZERO_BYTES
key,
IPoolManager.SwapParams(true, 1 ether, SQRT_RATIO_1_1 - 1),
HookEnabledSwapRouter.TestSettings(true, true),
ZERO_BYTES
);
vm.expectRevert(LimitOrder.InRange.selector);
limitOrder.place(key, -60, false, 1000000);
Expand Down
10 changes: 7 additions & 3 deletions test/TWAMM.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ contract TWAMMTest is Test, Deployers, GasSnapshot {
token0 = MockERC20(Currency.unwrap(currency0));
token1 = MockERC20(Currency.unwrap(currency1));

TWAMMImplementation impl = new TWAMMImplementation(manager, 10_000, twamm);
TWAMMImplementation impl = new TWAMMImplementation(
manager,
10_000,
twamm
);
(, bytes32[] memory writes) = vm.accesses(address(impl));
vm.etch(address(twamm), address(impl).code);
// for each storage key that was written during the hook implementation, copy the value over
Expand Down Expand Up @@ -236,7 +240,7 @@ contract TWAMMTest is Test, Deployers, GasSnapshot {
uint256 token1Owed = twamm.tokensOwed(poolKey.currency1, orderKey1.owner);

// takes 10% off the remaining half (so 80% of original sellrate)
assertEq(updatedSellRate, originalSellRate * 80 / 100);
assertEq(updatedSellRate, (originalSellRate * 80) / 100);
assertEq(token0Owed, uint256(-amountDelta));
assertEq(token1Owed, orderAmount / 2);
}
Expand All @@ -261,7 +265,7 @@ contract TWAMMTest is Test, Deployers, GasSnapshot {
uint256 token1Owed = twamm.tokensOwed(poolKey.currency1, orderKey1.owner);

// takes 10% off the remaining half (so 80% of original sellrate)
assertEq(updatedSellRate, originalSellRate * 80 / 100);
assertEq(updatedSellRate, (originalSellRate * 80) / 100);
assertEq(token0Owed, orderAmount / 2);
assertEq(token1Owed, uint256(-amountDelta));
}
Expand Down
15 changes: 10 additions & 5 deletions test/utils/HookEnabledSwapRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
pragma solidity ^0.8.20;

import {CurrencyLibrary, Currency} from "@uniswap/v4-core/src/types/Currency.sol";
import {IERC20Minimal} from "@uniswap/v4-core/src/interfaces/external/IERC20Minimal.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {PoolTestBase} from "@uniswap/v4-core/src/test/PoolTestBase.sol";
import {Test} from "forge-std/Test.sol";

contract HookEnabledSwapRouter is Test, PoolTestBase {
contract HookEnabledSwapRouter is PoolTestBase {
using CurrencyLibrary for Currency;

constructor(IPoolManager _manager) PoolTestBase(_manager) {}

error NoSwapOccurred();

constructor(IPoolManager _manager) PoolTestBase(_manager) {}

struct CallbackData {
address sender;
TestSettings testSettings;
Expand Down Expand Up @@ -54,10 +55,14 @@ contract HookEnabledSwapRouter is Test, PoolTestBase {

if (data.params.zeroForOne) {
_settle(data.key.currency0, data.sender, delta.amount0(), data.testSettings.settleUsingTransfer);
_take(data.key.currency1, data.sender, delta.amount1(), data.testSettings.withdrawTokens);
if (delta.amount1() < 0) {
_take(data.key.currency1, data.sender, delta.amount1(), data.testSettings.withdrawTokens);
}
} else {
_settle(data.key.currency1, data.sender, delta.amount1(), data.testSettings.settleUsingTransfer);
_take(data.key.currency0, data.sender, delta.amount0(), data.testSettings.withdrawTokens);
if (delta.amount0() < 0) {
_take(data.key.currency0, data.sender, delta.amount0(), data.testSettings.withdrawTokens);
}
}

return abi.encode(delta);
Expand Down

0 comments on commit be6aa94

Please sign in to comment.