Skip to content

Commit

Permalink
dry arb
Browse files Browse the repository at this point in the history
  • Loading branch information
thedavidmeister committed Sep 7, 2023
1 parent b29e9fb commit 98658e8
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 92 deletions.
12 changes: 6 additions & 6 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GenericPoolOrderBookV3ArbOrderTakerTest:testMinimumOutput((address,bool,(address,address,address),(address,uint8,uint256)[],(address,uint8,uint256)[]),uint256,uint256,uint256,uint256) (runs: 5096, μ: 1118510, ~: 1118456)
GenericPoolOrderBookV3ArbOrderTakerTest:testTakeOrdersSender((address,bool,(address,address,address),(address,uint8,uint256)[],(address,uint8,uint256)[]),uint256,uint256) (runs: 5096, μ: 1062543, ~: 1063098)
GenericPoolOrderBookV3FlashBorrowerTest:testMinimumOutput((address,bool,(address,address,address),(address,uint8,uint256)[],(address,uint8,uint256)[]),uint256,uint256,uint256,uint256) (runs: 5096, μ: 1394297, ~: 1393645)
GenericPoolOrderBookV3FlashBorrowerTest:testTakeOrdersSender((address,bool,(address,address,address),(address,uint8,uint256)[],(address,uint8,uint256)[]),uint256,uint256) (runs: 5096, μ: 1319200, ~: 1319647)
GenericPoolOrderBookV3ArbOrderTakerTest:testMinimumOutput((address,bool,(address,address,address),(address,uint8,uint256)[],(address,uint8,uint256)[]),uint256,uint256,uint256,uint256) (runs: 5096, μ: 1009609, ~: 1008341)
GenericPoolOrderBookV3ArbOrderTakerTest:testTakeOrdersSender((address,bool,(address,address,address),(address,uint8,uint256)[],(address,uint8,uint256)[]),uint256,uint256) (runs: 5096, μ: 953258, ~: 953814)
GenericPoolOrderBookV3FlashBorrowerTest:testMinimumOutput((address,bool,(address,address,address),(address,uint8,uint256)[],(address,uint8,uint256)[]),uint256,uint256,uint256,uint256) (runs: 5096, μ: 1290560, ~: 1288607)
GenericPoolOrderBookV3FlashBorrowerTest:testTakeOrdersSender((address,bool,(address,address,address),(address,uint8,uint256)[],(address,uint8,uint256)[]),uint256,uint256) (runs: 5096, μ: 1212419, ~: 1212863)
LibOrderTest:testHashEqual((address,bool,(address,address,address),(address,uint8,uint256)[],(address,uint8,uint256)[])) (runs: 5096, μ: 194389, ~: 191140)
LibOrderTest:testHashNotEqual((address,bool,(address,address,address),(address,uint8,uint256)[],(address,uint8,uint256)[]),(address,bool,(address,address,address),(address,uint8,uint256)[],(address,uint8,uint256)[])) (runs: 5096, μ: 298734, ~: 298554)
OrderBookAddOrderMockTest:testAddOrderSameAccountWithDifferentConfig(address,((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes,uint256[]),bytes),((address,uint8,uint256)[],(address,uint8,uint256)[],(address,bytes,uint256[]),bytes),address,address) (runs: 5096, μ: 2771808, ~: 2761475)
Expand Down Expand Up @@ -51,5 +51,5 @@ OrderBookWithdrawTest:testWithdrawMany((bool,address,address,uint256,uint248)[])
OrderBookWithdrawTest:testWithdrawPartialVault(address,uint256,uint256,uint256) (runs: 5096, μ: 51929, ~: 51929)
OrderBookWithdrawTest:testWithdrawReentrant(address,uint256,uint256,address,address,uint256) (runs: 5096, μ: 506228, ~: 507997)
OrderBookWithdrawTest:testWithdrawZero(address,address,uint256) (runs: 5096, μ: 12809, ~: 12809)
RouteProcessorOrderBookV3ArbOrderTakerTest:testMinimumOutput((address,bool,(address,address,address),(address,uint8,uint256)[],(address,uint8,uint256)[]),uint256,uint256,uint256,uint256) (runs: 5096, μ: 1140756, ~: 1140707)
RouteProcessorOrderBookV3ArbOrderTakerTest:testTakeOrdersSender((address,bool,(address,address,address),(address,uint8,uint256)[],(address,uint8,uint256)[]),uint256,uint256) (runs: 5096, μ: 1085625, ~: 1086117)
RouteProcessorOrderBookV3ArbOrderTakerTest:testMinimumOutput((address,bool,(address,address,address),(address,uint8,uint256)[],(address,uint8,uint256)[]),uint256,uint256,uint256,uint256) (runs: 5096, μ: 1033065, ~: 1032882)
RouteProcessorOrderBookV3ArbOrderTakerTest:testTakeOrdersSender((address,bool,(address,address,address),(address,uint8,uint256)[],(address,uint8,uint256)[]),uint256,uint256) (runs: 5096, μ: 976343, ~: 976833)
39 changes: 12 additions & 27 deletions test/concrete/GenericPoolOrderBookV3ArbOrderTaker.sender.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,36 @@ import "test/util/lib/LibGenericPoolOrderBookV3ArbOrderTakerConstants.sol";
import "src/concrete/GenericPoolOrderBookV3ArbOrderTaker.sol";
import "src/interface/unstable/IOrderBookV3.sol";

import "test/util/concrete/FlashLendingMockOrderBook.sol";

contract GenericPoolOrderBookV3ArbOrderTakerTest is ArbTest {
function buildArbTestConstructorConfig() internal returns (ArbTestConstructorConfig memory) {
(address deployer, DeployerDiscoverableMetaV2ConstructionConfig memory config) =
buildConstructorConfig(GENERIC_POOL_ORDER_BOOK_V3_ARB_ORDER_TAKER_META_PATH);
return ArbTestConstructorConfig(deployer, address(new GenericPoolOrderBookV3ArbOrderTaker(config)));
}

constructor() ArbTest(buildArbTestConstructorConfig()) {}
constructor() ArbTest(buildArbTestConstructorConfig()) {
ICloneableV2(iArb).initialize(
abi.encode(
OrderBookV3ArbOrderTakerConfigV1(
address(iOrderBook), EvaluableConfigV2(IExpressionDeployerV2(address(0)), "", new uint256[](0)), ""
)
)
);
}

function testTakeOrdersSender(Order memory order, uint256 inputIOIndex, uint256 outputIOIndex) public {
vm.assume(order.validInputs.length > 0);
inputIOIndex = bound(inputIOIndex, 0, order.validInputs.length - 1);
vm.assume(order.validOutputs.length > 0);
outputIOIndex = bound(outputIOIndex, 0, order.validOutputs.length - 1);

FlashLendingMockOrderBook ob = new FlashLendingMockOrderBook();

GenericPoolOrderBookV3ArbOrderTaker arb = GenericPoolOrderBookV3ArbOrderTaker(Clones.clone(iImplementation));
arb.initialize(
abi.encode(
OrderBookV3ArbOrderTakerConfigV1(
address(ob), EvaluableConfigV2(IExpressionDeployerV2(address(0)), "", new uint256[](0)), ""
)
)
);

order.validInputs[inputIOIndex].token = address(iTakerOutput);
order.validOutputs[outputIOIndex].token = address(iTakerInput);

TakeOrderConfig[] memory orders = new TakeOrderConfig[](1);
orders[0] = TakeOrderConfig(order, inputIOIndex, outputIOIndex, new SignedContextV1[](0));

arb.arb(
GenericPoolOrderBookV3ArbOrderTaker(iArb).arb(
TakeOrdersConfigV2(0, type(uint256).max, type(uint256).max, orders, abi.encode(iRefundoor, iRefundoor, "")),
0
);
Expand All @@ -63,18 +58,8 @@ contract GenericPoolOrderBookV3ArbOrderTakerTest is ArbTest {
outputIOIndex = bound(outputIOIndex, 0, order.validOutputs.length - 1);

vm.assume(minimumOutput > mintAmount);
FlashLendingMockOrderBook ob = new FlashLendingMockOrderBook();

GenericPoolOrderBookV3ArbOrderTaker arb = GenericPoolOrderBookV3ArbOrderTaker(Clones.clone(iImplementation));
arb.initialize(
abi.encode(
OrderBookV3ArbOrderTakerConfigV1(
address(ob), EvaluableConfigV2(IExpressionDeployerV2(address(0)), "", new uint256[](0)), ""
)
)
);

iTakerOutput.mint(address(arb), mintAmount);
iTakerOutput.mint(iArb, mintAmount);

order.validInputs[inputIOIndex].token = address(iTakerOutput);
order.validOutputs[outputIOIndex].token = address(iTakerInput);
Expand All @@ -83,7 +68,7 @@ contract GenericPoolOrderBookV3ArbOrderTakerTest is ArbTest {
orders[0] = TakeOrderConfig(order, inputIOIndex, outputIOIndex, new SignedContextV1[](0));

vm.expectRevert(abi.encodeWithSelector(MinimumOutput.selector, minimumOutput, mintAmount));
arb.arb(
GenericPoolOrderBookV3ArbOrderTaker(iArb).arb(
TakeOrdersConfigV2(0, type(uint256).max, type(uint256).max, orders, abi.encode(iRefundoor, iRefundoor, "")),
minimumOutput
);
Expand Down
38 changes: 12 additions & 26 deletions test/concrete/GenericPoolOrderBookV3FlashBorrower.sender.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import "lib/openzeppelin-contracts/contracts/proxy/Clones.sol";

import "test/util/lib/LibTestConstants.sol";
import "test/util/lib/LibGenericPoolOrderBookV3FlashBorrowerConstants.sol";
import "test/util/concrete/FlashLendingMockOrderBook.sol";

import "src/concrete/GenericPoolOrderBookV3FlashBorrower.sol";
import "src/interface/unstable/IOrderBookV3.sol";
Expand All @@ -18,32 +17,29 @@ contract GenericPoolOrderBookV3FlashBorrowerTest is ArbTest {
return ArbTestConstructorConfig(deployer, address(new GenericPoolOrderBookV3FlashBorrower(config)));
}

constructor() ArbTest(buildArbTestConstructorConfig()) {}
constructor() ArbTest(buildArbTestConstructorConfig()) {
ICloneableV2(iArb).initialize(
abi.encode(
OrderBookV3FlashBorrowerConfigV2(
address(iOrderBook), EvaluableConfigV2(IExpressionDeployerV2(address(0)), "", new uint256[](0)), ""
)
)
);
}

function testTakeOrdersSender(Order memory order, uint256 inputIOIndex, uint256 outputIOIndex) public {
vm.assume(order.validInputs.length > 0);
inputIOIndex = bound(inputIOIndex, 0, order.validInputs.length - 1);
vm.assume(order.validOutputs.length > 0);
outputIOIndex = bound(outputIOIndex, 0, order.validOutputs.length - 1);

FlashLendingMockOrderBook ob = new FlashLendingMockOrderBook();

GenericPoolOrderBookV3FlashBorrower arb = GenericPoolOrderBookV3FlashBorrower(Clones.clone(iImplementation));
arb.initialize(
abi.encode(
OrderBookV3FlashBorrowerConfigV2(
address(ob), EvaluableConfigV2(IExpressionDeployerV2(address(0)), "", new uint256[](0)), ""
)
)
);

order.validInputs[inputIOIndex].token = address(iTakerOutput);
order.validOutputs[outputIOIndex].token = address(iTakerInput);

TakeOrderConfig[] memory orders = new TakeOrderConfig[](1);
orders[0] = TakeOrderConfig(order, inputIOIndex, outputIOIndex, new SignedContextV1[](0));

arb.arb(
GenericPoolOrderBookV3FlashBorrower(iArb).arb(
TakeOrdersConfigV2(0, type(uint256).max, type(uint256).max, orders, ""),
0,
abi.encode(iRefundoor, iRefundoor, "")
Expand All @@ -63,18 +59,8 @@ contract GenericPoolOrderBookV3FlashBorrowerTest is ArbTest {
outputIOIndex = bound(outputIOIndex, 0, order.validOutputs.length - 1);

vm.assume(minimumOutput > mintAmount);
FlashLendingMockOrderBook ob = new FlashLendingMockOrderBook();

GenericPoolOrderBookV3FlashBorrower arb = GenericPoolOrderBookV3FlashBorrower(Clones.clone(iImplementation));
arb.initialize(
abi.encode(
OrderBookV3FlashBorrowerConfigV2(
address(ob), EvaluableConfigV2(IExpressionDeployerV2(address(0)), "", new uint256[](0)), ""
)
)
);

iTakerOutput.mint(address(arb), mintAmount);
iTakerOutput.mint(iArb, mintAmount);

order.validInputs[inputIOIndex].token = address(iTakerOutput);
order.validOutputs[outputIOIndex].token = address(iTakerInput);
Expand All @@ -83,7 +69,7 @@ contract GenericPoolOrderBookV3FlashBorrowerTest is ArbTest {
orders[0] = TakeOrderConfig(order, inputIOIndex, outputIOIndex, new SignedContextV1[](0));

vm.expectRevert(abi.encodeWithSelector(MinimumOutput.selector, minimumOutput, mintAmount));
arb.arb(
GenericPoolOrderBookV3FlashBorrower(iArb).arb(
TakeOrdersConfigV2(0, type(uint256).max, type(uint256).max, orders, ""),
minimumOutput,
abi.encode(iRefundoor, iRefundoor, "")
Expand Down
50 changes: 17 additions & 33 deletions test/concrete/RouteProcessorOrderBookV3ArbOrderTaker.sender.t.sol
Original file line number Diff line number Diff line change
@@ -1,53 +1,50 @@
// SPDX-License-Identifier: CAL
pragma solidity =0.8.19;

import "openzeppelin-contracts/contracts/proxy/Clones.sol";

import "test/util/lib/LibTestConstants.sol";
import "test/util/lib/LibRouteProcessorOrderBookV3ArbOrderTakerConstants.sol";

import {ArbTest, ArbTestConstructorConfig} from "test/util/abstract/ArbTest.sol";
import "test/util/concrete/FlashLendingMockOrderBook.sol";

import "src/concrete/RouteProcessorOrderBookV3ArbOrderTaker.sol";
import "src/interface/unstable/IOrderBookV3.sol";

import "rain.factory/src/interface/ICloneableV2.sol";

contract RouteProcessorOrderBookV3ArbOrderTakerTest is ArbTest {
function buildArbTestConstructorConfig() internal returns (ArbTestConstructorConfig memory) {
(address deployer, DeployerDiscoverableMetaV2ConstructionConfig memory config) =
buildConstructorConfig(ROUTE_PROCESSOR_ORDER_BOOK_V3_ARB_ORDER_TAKER_META_PATH);
return ArbTestConstructorConfig(deployer, address(new RouteProcessorOrderBookV3ArbOrderTaker(config)));
}

constructor() ArbTest(buildArbTestConstructorConfig()) {}

function testTakeOrdersSender(Order memory order, uint256 inputIOIndex, uint256 outputIOIndex) public {
vm.assume(order.validInputs.length > 0);
inputIOIndex = bound(inputIOIndex, 0, order.validInputs.length - 1);
vm.assume(order.validOutputs.length > 0);
outputIOIndex = bound(outputIOIndex, 0, order.validOutputs.length - 1);

FlashLendingMockOrderBook ob = new FlashLendingMockOrderBook();

RouteProcessorOrderBookV3ArbOrderTaker arb =
RouteProcessorOrderBookV3ArbOrderTaker(Clones.clone(iImplementation));
arb.initialize(
constructor() ArbTest(buildArbTestConstructorConfig()) {
ICloneableV2(iArb).initialize(
abi.encode(
OrderBookV3ArbOrderTakerConfigV1(
address(ob),
address(iOrderBook),
EvaluableConfigV2(IExpressionDeployerV2(address(0)), "", new uint256[](0)),
abi.encode(iRefundoor)
)
)
);
}

function testTakeOrdersSender(Order memory order, uint256 inputIOIndex, uint256 outputIOIndex) public {
vm.assume(order.validInputs.length > 0);
inputIOIndex = bound(inputIOIndex, 0, order.validInputs.length - 1);
vm.assume(order.validOutputs.length > 0);
outputIOIndex = bound(outputIOIndex, 0, order.validOutputs.length - 1);

order.validInputs[inputIOIndex].token = address(iTakerOutput);
order.validOutputs[outputIOIndex].token = address(iTakerInput);

TakeOrderConfig[] memory orders = new TakeOrderConfig[](1);
orders[0] = TakeOrderConfig(order, inputIOIndex, outputIOIndex, new SignedContextV1[](0));

arb.arb(TakeOrdersConfigV2(0, type(uint256).max, type(uint256).max, orders, abi.encode(bytes("0x00"))), 0);
RouteProcessorOrderBookV3ArbOrderTaker(iArb).arb(
TakeOrdersConfigV2(0, type(uint256).max, type(uint256).max, orders, abi.encode(bytes("0x00"))), 0
);
}

function testMinimumOutput(
Expand All @@ -63,21 +60,8 @@ contract RouteProcessorOrderBookV3ArbOrderTakerTest is ArbTest {
outputIOIndex = bound(outputIOIndex, 0, order.validOutputs.length - 1);

vm.assume(minimumOutput > mintAmount);
FlashLendingMockOrderBook ob = new FlashLendingMockOrderBook();

RouteProcessorOrderBookV3ArbOrderTaker arb =
RouteProcessorOrderBookV3ArbOrderTaker(Clones.clone(iImplementation));
arb.initialize(
abi.encode(
OrderBookV3ArbOrderTakerConfigV1(
address(ob),
EvaluableConfigV2(IExpressionDeployerV2(address(0)), "", new uint256[](0)),
abi.encode(iRefundoor)
)
)
);

iTakerOutput.mint(address(arb), mintAmount);
iTakerOutput.mint(iArb, mintAmount);

order.validInputs[inputIOIndex].token = address(iTakerOutput);
order.validOutputs[outputIOIndex].token = address(iTakerInput);
Expand All @@ -86,7 +70,7 @@ contract RouteProcessorOrderBookV3ArbOrderTakerTest is ArbTest {
orders[0] = TakeOrderConfig(order, inputIOIndex, outputIOIndex, new SignedContextV1[](0));

vm.expectRevert(abi.encodeWithSelector(MinimumOutput.selector, minimumOutput, mintAmount));
arb.arb(
RouteProcessorOrderBookV3ArbOrderTaker(iArb).arb(
TakeOrdersConfigV2(0, type(uint256).max, type(uint256).max, orders, abi.encode(bytes("0x00"))),
minimumOutput
);
Expand Down
6 changes: 6 additions & 0 deletions test/util/abstract/ArbTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
pragma solidity =0.8.19;

import "forge-std/Test.sol";
import "openzeppelin-contracts/contracts/proxy/Clones.sol";

import "test/util/lib/LibTestConstants.sol";
import {DeployerDiscoverableMetaV2ConstructionConfig} from
"rain.interpreter/src/abstract/DeployerDiscoverableMetaV2.sol";
import {IExpressionDeployerV2} from "rain.interpreter/src/interface/unstable/IExpressionDeployerV2.sol";
import "openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
import "test/util/concrete/Refundoor.sol";
import "test/util/concrete/FlashLendingMockOrderBook.sol";

contract Token is ERC20 {
constructor() ERC20("Token", "TKN") {}
Expand All @@ -29,13 +31,17 @@ abstract contract ArbTest is Test {
Token immutable iTakerInput;
Token immutable iTakerOutput;
address immutable iRefundoor;
FlashLendingMockOrderBook immutable iOrderBook;
address iArb;

constructor(ArbTestConstructorConfig memory config) {
iDeployer = config.deployer;
iImplementation = config.implementation;
iArb = Clones.clone(iImplementation);
iTakerInput = new Token();
iTakerOutput = new Token();
iRefundoor = address(new Refundoor());
iOrderBook = new FlashLendingMockOrderBook();
}

function buildConstructorConfig(string memory metaPath)
Expand Down

0 comments on commit 98658e8

Please sign in to comment.