Skip to content

Commit

Permalink
feat: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marktoda committed Apr 18, 2024
1 parent b9c3d37 commit 56b40e7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion test/reactors/BaseDutchOrderReactor.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ abstract contract BaseDutchOrderReactorTest is PermitSignature, DeployPermit2, B
return (SignedOrder(abi.encode(request), signOrder(swapperPrivateKey, address(permit2), request)), orderHash);
}

function generateOrder(TestDutchOrderSpec memory spec) private returns (SignedOrder memory order) {
function generateOrder(TestDutchOrderSpec memory spec) internal returns (SignedOrder memory order) {
vm.warp(spec.currentTime);
tokenIn.mint(address(swapper), uint256(spec.input.endAmount));
tokenIn.forceApprove(swapper, address(permit2), spec.input.endAmount);
Expand Down
20 changes: 19 additions & 1 deletion test/reactors/DutchOrderReactor.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {MockFillContract} from "../util/mock/MockFillContract.sol";
import {MockFillContractWithOutputOverride} from "../util/mock/MockFillContractWithOutputOverride.sol";
import {PermitSignature} from "../util/PermitSignature.sol";
import {ReactorEvents} from "../../src/base/ReactorEvents.sol";
import {BaseDutchOrderReactorTest} from "./BaseDutchOrderReactor.t.sol";
import {BaseDutchOrderReactorTest, TestDutchOrderSpec} from "./BaseDutchOrderReactor.t.sol";

// This suite of tests test execution with a mock fill contract.
contract DutchOrderReactorTest is PermitSignature, DeployPermit2, BaseDutchOrderReactorTest {
Expand Down Expand Up @@ -245,6 +245,24 @@ contract DutchOrderReactorTest is PermitSignature, DeployPermit2, BaseDutchOrder
fill.executeBatch(generateSignedOrders(orders));
}

// 2nd output decays, so revert with error InputAndOutputDecay().
function testValidateInputAndOutputDecay() public {
uint256 currentTime = 100;

SignedOrder memory order = generateOrder(
TestDutchOrderSpec({
currentTime: currentTime,
startTime: currentTime,
endTime: currentTime + 100,
deadline: currentTime + 100,
input: DutchInput(tokenIn, 100, 110),
outputs: OutputsBuilder.singleDutch(tokenOut, 1000, 900, address(0))
})
);
vm.expectRevert(DutchOrderReactor.InputAndOutputDecay.selector);
quoter.quote(order.order, order.sig);
}

function generateSignedOrders(DutchOrder[] memory orders) private view returns (SignedOrder[] memory result) {
result = new SignedOrder[](orders.length);
for (uint256 i = 0; i < orders.length; i++) {
Expand Down
34 changes: 33 additions & 1 deletion test/reactors/V2DutchOrderReactor.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {MockFillContract} from "../util/mock/MockFillContract.sol";
import {MockFillContractWithOutputOverride} from "../util/mock/MockFillContractWithOutputOverride.sol";
import {PermitSignature} from "../util/PermitSignature.sol";
import {ReactorEvents} from "../../src/base/ReactorEvents.sol";
import {DutchOrder, BaseDutchOrderReactorTest} from "./BaseDutchOrderReactor.t.sol";
import {DutchOrder, BaseDutchOrderReactorTest, TestDutchOrderSpec} from "./BaseDutchOrderReactor.t.sol";

contract V2DutchOrderTest is PermitSignature, DeployPermit2, BaseDutchOrderReactorTest {
using OrderInfoBuilder for OrderInfo;
Expand Down Expand Up @@ -453,6 +453,38 @@ contract V2DutchOrderTest is PermitSignature, DeployPermit2, BaseDutchOrderReact
assertEq(tokenIn.balanceOf(address(fillContract)), inputAmount);
}

function testExecuteInputAndOutputDecay() public {
uint256 inputAmount = 1 ether;
uint256 outputAmount = 1 ether;
uint256 startTime = block.timestamp;
uint256 deadline = startTime + 1000;
// Seed both swapper and fillContract with enough tokens (important for dutch order)
tokenIn.mint(address(swapper), uint256(inputAmount) * 100);
tokenOut.mint(address(fillContract), uint256(outputAmount) * 100);
tokenIn.forceApprove(swapper, address(permit2), inputAmount);

SignedOrder memory order = generateOrder(
TestDutchOrderSpec({
currentTime: startTime,
startTime: startTime,
endTime: deadline,
deadline: deadline,
input: DutchInput(tokenIn, inputAmount, inputAmount * 110 / 100),
outputs: OutputsBuilder.singleDutch(tokenOut, outputAmount, outputAmount * 90 / 100, address(swapper))
})
);
uint256 swapperInputBalanceStart = tokenIn.balanceOf(address(swapper));
uint256 swapperOutputBalanceStart = tokenOut.balanceOf(address(swapper));
vm.expectEmit(false, true, true, false, address(reactor));
emit Fill(keccak256("not checked"), address(fillContract), swapper, 0);
vm.warp(startTime + 500);
fillContract.execute(order);
uint256 swapperInputBalanceEnd = tokenIn.balanceOf(address(swapper));
uint256 swapperOutputBalanceEnd = tokenOut.balanceOf(address(swapper));
assertEq(swapperInputBalanceStart - swapperInputBalanceEnd, inputAmount * 105 / 100);
assertEq(swapperOutputBalanceEnd - swapperOutputBalanceStart, outputAmount * 95 / 100);
}

function cosignOrder(bytes32 orderHash, CosignerData memory cosignerData) private pure returns (bytes memory sig) {
bytes32 msgHash = keccak256(abi.encodePacked(orderHash, abi.encode(cosignerData)));
(uint8 v, bytes32 r, bytes32 s) = vm.sign(cosignerPrivateKey, msgHash);
Expand Down

0 comments on commit 56b40e7

Please sign in to comment.