|
| 1 | +// SPDX-License-Identifier: LicenseRef-DCL-1.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright (c) 2020 Rain Open Source Software Ltd |
| 3 | +pragma solidity =0.8.25; |
| 4 | + |
| 5 | +import {RaindexV6ExternalRealTest} from "test/util/abstract/RaindexV6ExternalRealTest.sol"; |
| 6 | +import {LibRaindexDeploy} from "src/lib/deploy/LibRaindexDeploy.sol"; |
| 7 | +import {LibTestTakeOrder} from "test/util/lib/LibTestTakeOrder.sol"; |
| 8 | +import {MockToken} from "test/util/concrete/MockToken.sol"; |
| 9 | +import { |
| 10 | + OrderV4, |
| 11 | + TakeOrderConfigV4, |
| 12 | + TakeOrdersConfigV5, |
| 13 | + SignedContextV1, |
| 14 | + TaskV2 |
| 15 | +} from "raindex-interface-0.1.8/src/interface/IRaindexV6.sol"; |
| 16 | +import {Float, LibDecimalFloat} from "rain-math-float-0.3.10/src/lib/LibDecimalFloat.sol"; |
| 17 | + |
| 18 | +/// @title RaindexV6TakeOrderDirtyFreeMemoryTest |
| 19 | +/// @notice `takeOrders4` settles exactly the taken orders when the memory past |
| 20 | +/// the free memory pointer is nonzero on entry. |
| 21 | +contract RaindexV6TakeOrderDirtyFreeMemoryTest is RaindexV6ExternalRealTest { |
| 22 | + using LibDecimalFloat for Float; |
| 23 | + |
| 24 | + address internal owner = address(uint160(uint256(keccak256("owner.rain.test")))); |
| 25 | + address internal bob = address(uint160(uint256(keccak256("bob.rain.test")))); |
| 26 | + |
| 27 | + bytes32 internal constant OUTPUT_VAULT_ID = bytes32(uint256(0x01)); |
| 28 | + |
| 29 | + MockToken internal token0; |
| 30 | + MockToken internal token1; |
| 31 | + |
| 32 | + constructor() { |
| 33 | + vm.etch( |
| 34 | + LibRaindexDeploy.RAINDEX_DEPLOYED_ADDRESS, |
| 35 | + vm.getDeployedCode("RaindexV6DirtyFreeMemoryHarness.sol:RaindexV6DirtyFreeMemoryHarness") |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + function setUp() public { |
| 40 | + token0 = new MockToken("Token Zero", "TK0", 18); |
| 41 | + token1 = new MockToken("Token One", "TK1", 18); |
| 42 | + } |
| 43 | + |
| 44 | + function _deposit(address who, MockToken token, bytes32 vaultId, uint256 amount) internal { |
| 45 | + token.mint(who, amount * 1e18); |
| 46 | + vm.startPrank(who); |
| 47 | + token.approve(address(iRaindex), amount * 1e18); |
| 48 | + // forge-lint: disable-next-line(unsafe-typecast) |
| 49 | + iRaindex.deposit4(address(token), vaultId, LibDecimalFloat.packLossless(int256(amount), 0), new TaskV2[](0)); |
| 50 | + vm.stopPrank(); |
| 51 | + } |
| 52 | + |
| 53 | + function testTakeOrdersDirtyFreeMemorySettlesOnlyTakenOrders() external { |
| 54 | + _deposit(owner, token1, OUTPUT_VAULT_ID, 10); |
| 55 | + OrderV4 memory taken = LibTestTakeOrder.addOrderWithExpression( |
| 56 | + vm, owner, "_ _:3 1;:;", address(token0), bytes32(0), address(token1), OUTPUT_VAULT_ID |
| 57 | + ); |
| 58 | + OrderV4 memory zeroAmount = LibTestTakeOrder.addOrderWithExpression( |
| 59 | + vm, owner, "_ _:0 1;:;", address(token0), bytes32(0), address(token1), OUTPUT_VAULT_ID |
| 60 | + ); |
| 61 | + |
| 62 | + token0.mint(bob, 3e18); |
| 63 | + vm.prank(bob); |
| 64 | + token0.approve(address(iRaindex), 3e18); |
| 65 | + |
| 66 | + TakeOrderConfigV4[] memory orders = new TakeOrderConfigV4[](2); |
| 67 | + orders[0] = TakeOrderConfigV4(taken, 0, 0, new SignedContextV1[](0)); |
| 68 | + orders[1] = TakeOrderConfigV4(zeroAmount, 0, 0, new SignedContextV1[](0)); |
| 69 | + TakeOrdersConfigV5 memory config = LibTestTakeOrder.defaultTakeConfig(orders); |
| 70 | + config.maximumIO = LibDecimalFloat.packLossless(10, 0); |
| 71 | + |
| 72 | + vm.prank(bob); |
| 73 | + (Float totalTakerInput, Float totalTakerOutput) = iRaindex.takeOrders4(config); |
| 74 | + |
| 75 | + assertTrue(totalTakerInput.eq(LibDecimalFloat.packLossless(3, 0)), "taker input"); |
| 76 | + assertTrue(totalTakerOutput.eq(LibDecimalFloat.packLossless(3, 0)), "taker output"); |
| 77 | + assertEq(token0.balanceOf(owner), 3e18, "owner wallet"); |
| 78 | + assertEq(token0.balanceOf(bob), 0, "taker token0"); |
| 79 | + assertEq(token1.balanceOf(bob), 3e18, "taker token1"); |
| 80 | + assertEq(token0.balanceOf(address(iRaindex)), 0, "orderbook token0"); |
| 81 | + } |
| 82 | +} |
0 commit comments