Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump ob dep #16

Merged
merged 5 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 33 additions & 28 deletions src/StrategyTests.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,32 @@ contract StrategyTests is OrderBookStrategyTest {
internal
returns (OrderV3 memory order)
{
bytes memory addOrderCallData;
{
addOrderCallData = LibComposeOrders.getOrderCalldata(
vm, strategy.strategyFile, strategy.settingsFile, strategy.deploymentKey, strategy.buildPath, strategy.manifestPath
);
}

vm.startPrank(ORDER_OWNER);
vm.recordLogs();
(, bytes memory result) = address(iOrderBook).call(addOrderCallData);
(bool stateChanged) = abi.decode(result, (bool));
assertEq(stateChanged, true);
Vm.Log[] memory entries = vm.getRecordedLogs();
(,, order) = abi.decode(entries[0].data, (address, bytes32, OrderV3));
assertEq(order.owner, ORDER_OWNER);

address inputToken;
address outputToken;
uint256 inputTokenVaultId;
uint256 outputTokenVaultId;

{
inputToken = strategy.inputVaults[strategy.inputTokenIndex].token;
outputToken = strategy.outputVaults[strategy.outputTokenIndex].token;
inputTokenVaultId = strategy.inputVaults[strategy.inputTokenIndex].vaultId;
outputTokenVaultId = strategy.outputVaults[strategy.outputTokenIndex].vaultId;
inputToken = order.validInputs[strategy.inputTokenIndex].token;
outputToken = order.validOutputs[strategy.outputTokenIndex].token;
inputTokenVaultId = order.validInputs[strategy.inputTokenIndex].vaultId;
outputTokenVaultId = order.validOutputs[strategy.outputTokenIndex].vaultId;
deal(address(inputToken), EXTERNAL_EOA, 1e30);
deal(address(outputToken), EXTERNAL_EOA, 1e30);
deal(address(inputToken), APPROVED_EOA, 1e30);
Expand All @@ -36,26 +52,6 @@ contract StrategyTests is OrderBookStrategyTest {
{
depositTokens(ORDER_OWNER, outputToken, outputTokenVaultId, strategy.takerAmount, new TaskV1[](0));
}
{
TaskV1[] memory postAddOrderTasks;
{
bytes memory postOrderCompose = iParser.parse2(
LibComposeOrders.getComposedPostAddOrder(
vm, strategy.strategyFile, strategy.strategyScenario, strategy.buildPath, strategy.manifestPath
)
);
EvaluableV3 memory postOrderEvaluable = EvaluableV3(iInterpreter, iStore, postOrderCompose);
TaskV1 memory postOrderAction = TaskV1(postOrderEvaluable,strategy.postAddOrderTaskSignedContext);
postAddOrderTasks = new TaskV1[](1);
postAddOrderTasks[0] = postOrderAction;
}
bytes memory bytecode = iParser.parse2(
LibComposeOrders.getComposedOrder(
vm, strategy.strategyFile, strategy.strategyScenario, strategy.buildPath, strategy.manifestPath
)
);
order = placeOrder(ORDER_OWNER, bytecode, strategy.inputVaults, strategy.outputVaults, postAddOrderTasks);
}
}

// Function to assert OrderBook calculations context by calling 'takeOrders' function
Expand Down Expand Up @@ -84,8 +80,8 @@ contract StrategyTests is OrderBookStrategyTest {
// Move external pool price in opposite direction that of the order
{
moveExternalPrice(
strategy.inputVaults[strategy.inputTokenIndex].token,
strategy.outputVaults[strategy.outputTokenIndex].token,
order.validInputs[strategy.inputTokenIndex].token,
order.validOutputs[strategy.outputTokenIndex].token,
strategy.makerAmount,
strategy.makerRoute
);
Expand Down Expand Up @@ -114,7 +110,7 @@ contract StrategyTests is OrderBookStrategyTest {
) internal {
bytes memory bytecode = iParser.parse2(
LibComposeOrders.getComposedOrder(
vm, strategy.strategyFile, strategy.strategyScenario, strategy.buildPath, strategy.manifestPath
vm, strategy.strategyFile, strategy.settingsFile, strategy.strategyScenario, strategy.buildPath, strategy.manifestPath
)
);

Expand All @@ -132,6 +128,15 @@ contract StrategyTests is OrderBookStrategyTest {
}
}

function toAddress(bytes memory b) internal pure returns (address) {
require(b.length >= 20, "Insufficient length");
address addr;
assembly {
addr := mload(add(b, 0x14)) // Load 20 bytes starting from the 20th byte
}
return addr;
}

function getBounty(Vm.Log[] memory entries)
public
view
Expand All @@ -153,5 +158,5 @@ contract StrategyTests is OrderBookStrategyTest {
}
}
return (bounties[0], bounties[1]);
}
}
}
86 changes: 79 additions & 7 deletions src/lib/LibComposeOrder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ library LibComposeOrders {
function getComposedOrder(
hardyjosh marked this conversation as resolved.
Show resolved Hide resolved
Vm vm,
string memory filePath,
string memory settingsPath,
string memory scenario,
string memory buildPath,
string memory manifestPath
) internal returns (bytes memory composedOrder) {
string[] memory ffi = new string[](16);
string[] memory ffi = new string[](18);
ffi[0] = "nix";
ffi[1] = "develop";
ffi[2] = buildPath;
Expand All @@ -30,20 +31,23 @@ library LibComposeOrders {
ffi[11] = "compose";
ffi[12] = "-f";
ffi[13] = filePath;
ffi[14] = "-s";
ffi[15] = scenario;
ffi[14] = "-c";
ffi[15] = settingsPath;
ffi[16] = "-s";
ffi[17] = scenario;

composedOrder = vm.ffi(ffi);
}

function getComposedPostAddOrder(
hardyjosh marked this conversation as resolved.
Show resolved Hide resolved
Vm vm,
string memory filePath,
string memory settingsPath,
string memory scenario,
string memory buildPath,
string memory manifestPath
) internal returns (bytes memory composedSource) {
string[] memory ffi = new string[](17);
string[] memory ffi = new string[](19);
ffi[0] = "nix";
ffi[1] = "develop";
ffi[2] = buildPath;
Expand All @@ -58,9 +62,77 @@ library LibComposeOrders {
ffi[11] = "compose";
ffi[12] = "-f";
ffi[13] = filePath;
ffi[14] = "-s";
ffi[15] = scenario;
ffi[16] = "-p";
ffi[14] = "-c";
ffi[15] = settingsPath;
ffi[16] = "-s";
ffi[17] = scenario;
ffi[18] = "-p";

composedSource = vm.ffi(ffi);
}

function getOrderCalldata(
Vm vm,
string memory filePath,
string memory settingsPath,
string memory deploymentKey,
string memory buildPath,
string memory manifestPath
) internal returns (bytes memory composedOrder) {
string[] memory ffi = new string[](20);
ffi[0] = "nix";
ffi[1] = "develop";
ffi[2] = buildPath;
ffi[3] = "--command";
ffi[4] = "cargo";
ffi[5] = "run";
ffi[6] = "--manifest-path";
ffi[7] = manifestPath;
ffi[8] = "--package";
ffi[9] = "rain_orderbook_cli";
ffi[10] = "order";
ffi[11] = "calldata";
ffi[12] = "-f";
ffi[13] = filePath;
ffi[14] = "-c";
ffi[15] = settingsPath;
ffi[16] = "-e";
ffi[17] = deploymentKey;
ffi[18] = "-o";
ffi[19] = "hex";

composedOrder = vm.ffi(ffi);
}

function getOrderOrderBook(
Vm vm,
string memory filePath,
string memory settingsPath,
string memory deploymentKey,
string memory buildPath,
string memory manifestPath
) internal returns (bytes memory composedSource) {
string[] memory ffi = new string[](20);
ffi[0] = "nix";
ffi[1] = "develop";
ffi[2] = buildPath;
ffi[3] = "--command";
ffi[4] = "cargo";
ffi[5] = "run";
ffi[6] = "--manifest-path";
ffi[7] = manifestPath;
ffi[8] = "--package";
ffi[9] = "rain_orderbook_cli";
ffi[10] = "order";
ffi[11] = "ob-addr";
ffi[12] = "-f";
ffi[13] = filePath;
ffi[14] = "-c";
ffi[15] = settingsPath;
ffi[16] = "-e";
ffi[17] = deploymentKey;
ffi[18] = "-o";
ffi[19] = "hex";

composedSource = vm.ffi(ffi);
}
Expand Down
5 changes: 2 additions & 3 deletions src/lib/LibStrategyDeployment.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ library LibStrategyDeployment {
uint256 expectedRatio;
uint256 expectedAmount;
string strategyFile;
string settingsFile;
string strategyScenario;
string deploymentKey;
string buildPath;
string manifestPath;
IO[] inputVaults;
IO[] outputVaults;
SignedContextV1[] postAddOrderTaskSignedContext;
}
}
64 changes: 19 additions & 45 deletions test/DeployedStrategyTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ contract DeployedStrategyTest is StrategyTests {
// Inheriting contract defines the fork block number.
uint256 constant FORK_BLOCK_NUMBER = 18690236;

// https://basescan.org/address/0x222789334d44bb5b2364939477e15a6c981ca165
address constant RED_TOKEN = address(0x222789334D44bB5b2364939477E15A6c981Ca165);

// https://basescan.org/address/0x6d3abb80c3cbae0f60ba274f36137298d8571fbe
address constant BLUE_TOKEN = address(0x6d3AbB80c3CBAe0f60ba274F36137298D8571Fbe);

// Inheriting contract defines fork selection.
function selectFork() internal {
uint256 fork = vm.createFork(vm.envString("RPC_URL_BASE"));
Expand All @@ -30,33 +24,26 @@ contract DeployedStrategyTest is StrategyTests {
function setUp() public {
selectFork();

iParser = IParserV2(0x56394785a22b3BE25470a0e03eD9E0a939C47b9b);
iStore = IInterpreterStoreV2(0x6E4b01603edBDa617002A077420E98C86595748E);
iInterpreter = IInterpreterV3(0x379b966DC6B117dD47b5Fc5308534256a4Ab1BCC);
iExpressionDeployer = IExpressionDeployerV3(0x56394785a22b3BE25470a0e03eD9E0a939C47b9b);

iOrderBook = IOrderBookV4(0x80DE00e3cA96AE0569426A1bb1Ae22CD4181dE6F);
iArbInstance = IOrderBookV4ArbOrderTakerV2(0x40D44abeC30288BFcd400200BA65FBD05daA5321);
iRouteProcessor = IRouteProcessor(address(0x0389879e0156033202C44BF784ac18fC02edeE4f));

EXTERNAL_EOA = address(0x654FEf5Fb8A1C91ad47Ba192F7AA81dd3C821427);
APPROVED_EOA = address(0x669845c29D9B1A64FFF66a55aA13EB4adB889a88);
ORDER_OWNER = address(0x5e01e44aE1969e16B9160d903B6F2aa991a37B21);
ORDER_OWNER = address(0x5e01e44aE1969e16B9160d903B6F2aa991a37B21);

bytes memory orderBook = LibComposeOrders.getOrderOrderBook(
vm,
"test/strategies/base-order.rain",
"test/strategies/base-settings.yml",
"buy-order",
"./lib/rain.orderbook",
"./lib/rain.orderbook/Cargo.toml"
);
iOrderBook = IOrderBookV4(address(uint160(bytes20(orderBook))));
}

// Inheriting contract tests OrderBook strategy with test suite.
function testDeployedStrategy() public {
// Input vaults
IO[] memory inputVaults = new IO[](1);
IO memory inputVault = IO(BLUE_TOKEN, 18, 1);
inputVaults[0] = inputVault;

// Output vaults
IO[] memory outputVaults = new IO[](1);
IO memory outputVault = IO(RED_TOKEN, 18, 1);
outputVaults[0] = outputVault;

// Expected calculations context
uint256 expectedRatio = 0.001e18;
uint256 expectedAmountOutputMax = 0.0000000000000001e18;
Expand All @@ -72,12 +59,11 @@ contract DeployedStrategyTest is StrategyTests {
expectedRatio,
expectedAmountOutputMax,
"test/strategies/base-order.rain",
"base-order-simulations",
"test/strategies/base-settings.yml",
"",
"buy-order",
"./lib/rain.orderbook",
"./lib/rain.orderbook/Cargo.toml",
inputVaults,
outputVaults,
new SignedContextV1[](0)
"./lib/rain.orderbook/Cargo.toml"
);

// Assert strategy calculations by executing order by directly calling 'takeOrder' function
Expand All @@ -87,16 +73,6 @@ contract DeployedStrategyTest is StrategyTests {

// Inheriting contract tests OrderBook strategy with test suite.
function testDeployedStrategyArbOrder() public {
// Input vaults
IO[] memory inputVaults = new IO[](1);
IO memory inputVault = IO(BLUE_TOKEN, 18, 1);
inputVaults[0] = inputVault;

// Output vaults
IO[] memory outputVaults = new IO[](1);
IO memory outputVault = IO(RED_TOKEN, 18, 1);
outputVaults[0] = outputVault;

// Expected calculations context
uint256 expectedRatio = 0.001e18;
uint256 expectedAmountOutputMax = 0.0000000000000001e18;
Expand All @@ -112,17 +88,15 @@ contract DeployedStrategyTest is StrategyTests {
expectedRatio,
expectedAmountOutputMax,
"test/strategies/base-order.rain",
"base-order-simulations",
"test/strategies/base-settings.yml",
"",
"buy-order",
"./lib/rain.orderbook",
"./lib/rain.orderbook/Cargo.toml",
inputVaults,
outputVaults,
new SignedContextV1[](0)
"./lib/rain.orderbook/Cargo.toml"
);

// Assert strategy calculations by executing order by calling 'arb' function
// on the OrderBookV3ArbOrderTaker contract.
// checkStrategyCalculationsArbOrder(strategy);
OrderV3 memory order = addOrderDepositOutputTokens(strategy);

{
Expand Down
25 changes: 2 additions & 23 deletions test/strategies/base-order.rain
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
networks:
base-test:
rpc: https://mainnet.base.org
chain-id: 8453
network-id: 8453
currency: ETH

subgraphs:
base-test: https://api.goldsky.com/api/public/project_clv14x04y9kzi01saerx7bxpg/subgraphs/ob4-base/0.3/gn

orderbooks:
base-test:
address: 0x80DE00e3cA96AE0569426A1bb1Ae22CD4181dE6F
network: base-test
subgraph: base-test

deployers:
base-test:
address: 0x56394785a22b3BE25470a0e03eD9E0a939C47b9b
network: base-test

tokens:
base-red:
network: base-test
Expand All @@ -31,10 +10,10 @@ tokens:
orders:
base-order:
inputs:
- token: base-red
- token: base-blue
vault-id: 0x58f0bb181e38b0bfecbf503744e46ee3fe517425c56655288f640ad008930ca5
outputs:
- token: base-blue
- token: base-red
vault-id: 0x58f0bb181e38b0bfecbf503744e46ee3fe517425c56655288f640ad008930ca5

scenarios:
Expand Down
Loading
Loading