Skip to content

Commit

Permalink
handle io revert test
Browse files Browse the repository at this point in the history
  • Loading branch information
thedavidmeister committed Nov 9, 2023
1 parent f28bac5 commit 5d187a0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
51 changes: 51 additions & 0 deletions test/concrete/OrderBook.takeOrder.handleIO.revert.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: CAL
pragma solidity =0.8.19;

import {Vm} from "forge-std/Vm.sol";
import {OrderBookExternalRealTest, OrderConfigV2, IO, IParserV1, EvaluableConfigV2, Order, TakeOrderConfig, SignedContextV1, TakeOrdersConfigV2} from "test/util/abstract/OrderBookExternalRealTest.sol";
import {EnsureFailed} from "rain.interpreter/src/lib/op/logic/LibOpEnsureNP.sol";

/// @title OrderBookTakeOrderHandleIORevertTest
/// @notice A test harness for testing the OrderBook takeOrder function will run
/// handle IO and revert if it fails.
contract OrderBookTakeOrderHandleIORevertTest is OrderBookExternalRealTest {
function testTakeOrderHandleIORevert() public {
uint256 vaultId = 0;
address inputToken = address(0x100);
address outputToken = address(0x101);
OrderConfigV2 memory config;
{
IO[] memory validInputs = new IO[](1);
validInputs[0] = IO(inputToken, 18, vaultId);
IO[] memory validOutputs = new IO[](1);
validOutputs[0] = IO(outputToken, 18, vaultId);
(bytes memory bytecode, uint256[] memory constants) = IParserV1(address(iDeployer)).parse("_ _:max-int-value() 1e18;:ensure<1>(0);");
EvaluableConfigV2 memory evaluableConfig = EvaluableConfigV2(iDeployer, bytecode, constants);
config = OrderConfigV2(validInputs, validOutputs, evaluableConfig, "");
// Etch with invalid.
vm.etch(outputToken, hex"fe");
vm.etch(inputToken, hex"fe");
// Mock every call to output as a success, so the orderbook thinks it
// is transferring tokens.
vm.mockCall(outputToken, "", abi.encode(true));
vm.mockCall(inputToken, "", abi.encode(true));
}
iOrderbook.deposit(outputToken, vaultId, type(uint256).max);
assertEq(iOrderbook.vaultBalance(address(this), outputToken, vaultId), type(uint256).max);

vm.recordLogs();
iOrderbook.addOrder(config);
Vm.Log[] memory entries = vm.getRecordedLogs();
assertEq(entries.length, 3);
(,, Order memory order,) = abi.decode(entries[2].data, (address, address, Order, bytes32));

TakeOrderConfig[] memory orders = new TakeOrderConfig[](1);
orders[0] = TakeOrderConfig(order, 0, 0, new SignedContextV1[](0));
TakeOrdersConfigV2 memory takeOrdersConfig =
TakeOrdersConfigV2(0, type(uint256).max, type(uint256).max, orders, "");

vm.expectRevert(abi.encodeWithSelector(EnsureFailed.selector, 1, 0));
(uint256 totalTakerInput, uint256 totalTakerOutput) = iOrderbook.takeOrders(takeOrdersConfig);
(totalTakerInput, totalTakerOutput);
}
}
2 changes: 1 addition & 1 deletion test/concrete/OrderBook.takeOrder.precision.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: CAL
pragma solidity =0.8.19;

import "test/util/abstract/OrderBookExternalRealTest.sol";
import {OrderBookExternalRealTest, OrderConfigV2, IO, EvaluableConfigV2, IParserV1, Vm, Order, TakeOrderConfig, SignedContextV1, TakeOrdersConfigV2} from "test/util/abstract/OrderBookExternalRealTest.sol";

/// @title OrderBookTakeOrderPrecisionTest
/// @notice A test harness for testing the OrderBook takeOrder function.
Expand Down
5 changes: 0 additions & 5 deletions test/concrete/OrderBookV3FlashLender.griefRecipient.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,4 @@ contract OrderBookClearTest is OrderBookExternalMockTest {
vm.expectRevert();
iOrderbook.flashLoan(IERC3156FlashBorrower(receiver), address(iToken0), amount, data);
}

/// Tests that if the receiver is some contract that returns
/// `ON_FLASH_LOAN_CALLBACK_SUCCESS`, and the token movements do not error,
/// then the flash loan will succeed.

}

0 comments on commit 5d187a0

Please sign in to comment.