Skip to content

Commit

Permalink
refactor: renamed limit order to slippage protection
Browse files Browse the repository at this point in the history
  • Loading branch information
TokenTitan committed Jul 29, 2024
1 parent 960c4dd commit dc73c7d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 46 deletions.
1 change: 0 additions & 1 deletion test/FlashLiquidity.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity 0.8.26;

import "forge-std/Test.sol";
import "../src/timetravel/CallBreaker.sol";
import "../test/examples/LimitOrder.sol";
import "../test/solve-lib/FlashLiquidityLib.sol";

contract FlashLiquidityTest is Test, FlashLiquidityLib {
Expand Down
31 changes: 24 additions & 7 deletions test/LimitOrder.t.sol → test/SlippageProtection.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
pragma solidity 0.8.26;

import "forge-std/Test.sol";
import "../src/timetravel/CallBreaker.sol";
import "../test/examples/LimitOrder.sol";
import "../test/solve-lib/LimitOrderLib.sol";
import "src/timetravel/CallBreaker.sol";
import "test/solve-lib/SlippageProtectionLib.sol";

contract LimitOrderTest is Test, LimitOrderLib {
contract SlippageProtectionTest is Test, SlippageProtectionLib {
address deployer;
address pusher;
address filler;
Expand All @@ -31,18 +30,19 @@ contract LimitOrderTest is Test, LimitOrderLib {
vm.label(filler, "filler");
}

function testLimitOrder() external {
function testSlippageProtection() external {
uint256 laminatorSequenceNumber;
uint256 maxSlippage = 10;

vm.startPrank(pusher);
laminatorSequenceNumber = userLand();
laminatorSequenceNumber = userLand(maxSlippage);
vm.stopPrank();

// go forward in time
vm.roll(block.number + 1);

vm.startPrank(filler);
solverLand(laminatorSequenceNumber, filler);
solverLand(laminatorSequenceNumber, filler, maxSlippage);
vm.stopPrank();

assertFalse(callbreaker.isPortalOpen());
Expand All @@ -52,4 +52,21 @@ contract LimitOrderTest is Test, LimitOrderLib {
assertTrue(init);
assertTrue(exec);
}

function testSlippageProtectionRevert() external {
uint256 laminatorSequenceNumber;
uint256 maxSlippage = 1;

vm.startPrank(pusher);
laminatorSequenceNumber = userLand(maxSlippage);
vm.stopPrank();

// go forward in time
vm.roll(block.number + 1);

vm.startPrank(filler);
vm.expectRevert();
solverLand(laminatorSequenceNumber, filler, maxSlippage);
vm.stopPrank();
}
}
3 changes: 0 additions & 3 deletions test/examples/FlashLiquidity.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ address constant SwapRouter = 0xE592427A0AEce92De3Edee1F18E0157C05861564;
// pool fee, 0.3%.
uint24 constant poolFee = 3000;

// This example uses fork test:
// FORK_URL=https://eth-mainnet.g.alchemy.com/v2/613t3mfjTevdrCwDl28CVvuk6wSIxRPi
// forge test -vv --gas-report --fork-url $FORK_URL --match-path test/LimitOrder.t.sol
contract FlashLiquidity is SmarterContract {
address owner;
address callbreakerAddress;
Expand Down
13 changes: 5 additions & 8 deletions test/examples/LimitOrder.sol → test/examples/SwapPool.sol
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.26;

import "../utils/interfaces/ISwapRouter.sol";
import "../../src/timetravel/CallBreaker.sol";
import "../../src/timetravel/SmarterContract.sol";
import "../../src/TimeTypes.sol";
import "src/timetravel/CallBreaker.sol";
import "src/timetravel/SmarterContract.sol";
import "src/TimeTypes.sol";
import "test/utils/interfaces/ISwapRouter.sol";

import {IWETH, IERC20} from "../utils/interfaces/IWeth.sol";
import {IPositionManager} from "../utils/interfaces/IPositionManager.sol";

// pool fee, 0.3%.
uint24 constant poolFee = 3000;

// This example uses fork test:
// FORK_URL=https://eth-mainnet.g.alchemy.com/v2/613t3mfjTevdrCwDl28CVvuk6wSIxRPi
// forge test -vv --gas-report --fork-url $FORK_URL --match-path test/LimitOrder.t.sol
contract LimitOrder is SmarterContract {
contract SwapPool is SmarterContract {
ISwapRouter private immutable router;

address owner;
Expand Down
26 changes: 13 additions & 13 deletions test/solve-lib/FlashLiquidityLib.sol
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.26;

import "../../src/lamination/Laminator.sol";
import "../../src/timetravel/CallBreaker.sol";
import "../../test/examples/LimitOrder.sol";
import "../../src/timetravel/SmarterContract.sol";
import "../utils/MockERC20Token.sol";
import "../utils/MockSwapRouter.sol";
import "../utils/MockPositionManager.sol";
import "src/lamination/Laminator.sol";
import "src/timetravel/CallBreaker.sol";
import "src/timetravel/SmarterContract.sol";
import "test/examples/SwapPool.sol";
import "test/utils/MockERC20Token.sol";
import "test/utils/MockSwapRouter.sol";
import "test/utils/MockPositionManager.sol";

contract FlashLiquidityLib {
address payable public pusherLaminated;
MockERC20Token public aToken;
MockERC20Token public bToken;
MockSwapRouter public swapRouter;
MockPositionManager public positionManager;
LimitOrder public limitOrder;
SwapPool public pool;
Laminator public laminator;
CallBreaker public callbreaker;
uint256 _tipWei = 33;
Expand All @@ -28,7 +28,7 @@ contract FlashLiquidityLib {
bToken = new MockERC20Token("BToken", "BT");
swapRouter = new MockSwapRouter(address(aToken), address(bToken));
positionManager = new MockPositionManager(address(swapRouter));
limitOrder = new LimitOrder(
pool = new SwapPool(
address(swapRouter), address(callbreaker), address(positionManager), address(aToken), address(bToken)
);
pusherLaminated = payable(laminator.computeProxyAddress(pusher));
Expand All @@ -51,11 +51,11 @@ contract FlashLiquidityLib {
amount: 0,
addr: address(aToken),
gas: 1000000,
callvalue: abi.encodeWithSignature("approve(address,uint256)", limitOrder, tokenToApprove)
callvalue: abi.encodeWithSignature("approve(address,uint256)", pool, tokenToApprove)
});
pusherCallObjs[3] = CallObject({
amount: 0,
addr: address(limitOrder),
addr: address(pool),
gas: 1000000,
callvalue: abi.encodeWithSignature("swapDAIForWETH(uint256,uint256)", amountIn, slippagePercent)
});
Expand All @@ -74,7 +74,7 @@ contract FlashLiquidityLib {

callObjs[0] = CallObject({
amount: 0,
addr: address(limitOrder),
addr: address(pool),
gas: 1000000,
callvalue: abi.encodeWithSignature("provideLiquidityToDAIETHPool(uint256,uint256)", liquidity, liquidity)
});
Expand All @@ -95,7 +95,7 @@ contract FlashLiquidityLib {

callObjs[3] = CallObject({
amount: 0,
addr: address(limitOrder),
addr: address(pool),
gas: 1000000,
callvalue: abi.encodeWithSignature("withdrawLiquidityFromDAIETHPool()")
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@ pragma solidity 0.8.26;

import "src/lamination/Laminator.sol";
import "src/timetravel/CallBreaker.sol";
import "test/examples/LimitOrder.sol";
import "src/timetravel/SmarterContract.sol";
import "../utils/MockERC20Token.sol";
import "../utils/MockSwapRouter.sol";
import "../utils/MockPositionManager.sol";
import "test/examples/SwapPool.sol";
import "test/utils/MockERC20Token.sol";
import "test/utils/MockSwapRouter.sol";
import "test/utils/MockPositionManager.sol";

contract LimitOrderLib {
contract SlippageProtectionLib {
address payable public pusherLaminated;
MockERC20Token public aToken;
MockERC20Token public bToken;
MockSwapRouter public swapRouter;
MockPositionManager public positionManager;
LimitOrder public limitOrder;
SwapPool public pool;
Laminator public laminator;
CallBreaker public callbreaker;
uint256 maxSlippage = 10;
uint256 _tipWei = 33;
uint256 private _tipWei = 33;

function deployerLand(address pusher) public {
// Initializing contracts
Expand All @@ -29,15 +27,15 @@ contract LimitOrderLib {
bToken = new MockERC20Token("BToken", "BT");
swapRouter = new MockSwapRouter(address(aToken), address(bToken));
positionManager = new MockPositionManager(address(swapRouter));
limitOrder = new LimitOrder(
pool = new SwapPool(
address(swapRouter), address(callbreaker), address(positionManager), address(aToken), address(bToken)
);
pusherLaminated = payable(laminator.computeProxyAddress(pusher));
aToken.mint(100e18, pusherLaminated);
bToken.mint(100e18, address(callbreaker));
}

function userLand() public returns (uint256) {
function userLand(uint256 maxSlippage) public returns (uint256) {
// send proxy some eth
pusherLaminated.transfer(1 ether);

Expand All @@ -49,11 +47,11 @@ contract LimitOrderLib {
amount: 0,
addr: address(aToken),
gas: 1000000,
callvalue: abi.encodeWithSignature("approve(address,uint256)", limitOrder, 100e18)
callvalue: abi.encodeWithSignature("approve(address,uint256)", pool, 100e18)
});
pusherCallObjs[1] = CallObject({
amount: 0,
addr: address(limitOrder),
addr: address(pool),
gas: 1000000,
callvalue: abi.encodeWithSignature("swapDAIForWETH(uint256,uint256)", 100, maxSlippage)
});
Expand All @@ -62,7 +60,7 @@ contract LimitOrderLib {
return laminator.pushToProxy(abi.encode(pusherCallObjs), 1);
}

function solverLand(uint256 laminatorSequenceNumber, address filler) public {
function solverLand(uint256 laminatorSequenceNumber, address filler, uint256 maxSlippage) public {
CallObject[] memory callObjs = new CallObject[](2);
ReturnObject[] memory returnObjs = new ReturnObject[](2);

Expand Down

0 comments on commit dc73c7d

Please sign in to comment.