From 20bd687283ebb8048a0cc306c59830c19a359102 Mon Sep 17 00:00:00 2001 From: brock smedley <2791467+zeroXbrock@users.noreply.github.com> Date: Fri, 19 Jan 2024 01:50:21 -0800 Subject: [PATCH] sendBundle tweaks (#29) * remove unused url from bundle struct * fix failing test * fix failing test --- src/protocols/Bundle.sol | 44 ++++++++++++++++++++++++++++--------- test/protocols/Bundle.t.sol | 6 ++--- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/protocols/Bundle.sol b/src/protocols/Bundle.sol index 6675977..6494736 100644 --- a/src/protocols/Bundle.sol +++ b/src/protocols/Bundle.sol @@ -7,25 +7,38 @@ import "solady/src/utils/LibString.sol"; // https://docs.flashbots.net/flashbots-auction/advanced/rpc-endpoint#eth_sendbundle library Bundle { struct BundleObj { - string url; uint64 blockNumber; uint64 minTimestamp; uint64 maxTimestamp; bytes[] txns; } - function sendBundle(string memory url, BundleObj memory bundle) internal view { + function sendBundle( + string memory url, + BundleObj memory bundle + ) internal view returns (bytes memory) { Suave.HttpRequest memory request = encodeBundle(bundle); request.url = url; - Suave.doHTTPRequest(request); + return Suave.doHTTPRequest(request); } - function encodeBundle(BundleObj memory args) internal pure returns (Suave.HttpRequest memory) { + function encodeBundle( + BundleObj memory args + ) internal pure returns (Suave.HttpRequest memory) { require(args.txns.length > 0, "Bundle: no txns"); - bytes memory params = abi.encodePacked('{"blockNumber": "', LibString.toString(args.blockNumber), '", "txs": ['); + bytes memory params = abi.encodePacked( + '{"blockNumber": "', + LibString.toHexString(args.blockNumber), + '", "txs": [' + ); for (uint256 i = 0; i < args.txns.length; i++) { - params = abi.encodePacked(params, '"', LibString.toHexString(args.txns[i]), '"'); + params = abi.encodePacked( + params, + '"', + LibString.toHexString(args.txns[i]), + '"' + ); if (i < args.txns.length - 1) { params = abi.encodePacked(params, ","); } else { @@ -33,15 +46,26 @@ library Bundle { } } if (args.minTimestamp > 0) { - params = abi.encodePacked(params, ', "minTimestamp": ', LibString.toString(args.minTimestamp)); + params = abi.encodePacked( + params, + ', "minTimestamp": ', + LibString.toString(args.minTimestamp) + ); } if (args.maxTimestamp > 0) { - params = abi.encodePacked(params, ', "maxTimestamp": ', LibString.toString(args.maxTimestamp)); + params = abi.encodePacked( + params, + ', "maxTimestamp": ', + LibString.toString(args.maxTimestamp) + ); } params = abi.encodePacked(params, "}"); - bytes memory body = - abi.encodePacked('{"jsonrpc":"2.0","method":"eth_sendBundle","params":[', params, '],"id":1}'); + bytes memory body = abi.encodePacked( + '{"jsonrpc":"2.0","method":"eth_sendBundle","params":[', + params, + '],"id":1}' + ); Suave.HttpRequest memory request; request.method = "POST"; diff --git a/test/protocols/Bundle.t.sol b/test/protocols/Bundle.t.sol index 39dbce9..c684ff6 100644 --- a/test/protocols/Bundle.t.sol +++ b/test/protocols/Bundle.t.sol @@ -15,7 +15,7 @@ contract EthSendBundle is Test { Suave.HttpRequest memory request = Bundle.encodeBundle(bundle); assertEq( string(request.body), - '{"jsonrpc":"2.0","method":"eth_sendBundle","params":[{"blockNumber": "1", "txs": ["0x1234"]}],"id":1}' + '{"jsonrpc":"2.0","method":"eth_sendBundle","params":[{"blockNumber": "0x01", "txs": ["0x1234"]}],"id":1}' ); assertTrue(request.withFlashbotsSignature); @@ -25,7 +25,7 @@ contract EthSendBundle is Test { Suave.HttpRequest memory request2 = Bundle.encodeBundle(bundle); assertEq( string(request2.body), - '{"jsonrpc":"2.0","method":"eth_sendBundle","params":[{"blockNumber": "1", "txs": ["0x1234"], "minTimestamp": 2}],"id":1}' + '{"jsonrpc":"2.0","method":"eth_sendBundle","params":[{"blockNumber": "0x01", "txs": ["0x1234"], "minTimestamp": 2}],"id":1}' ); // encode with 'maxTimestamp' @@ -34,7 +34,7 @@ contract EthSendBundle is Test { Suave.HttpRequest memory request3 = Bundle.encodeBundle(bundle); assertEq( string(request3.body), - '{"jsonrpc":"2.0","method":"eth_sendBundle","params":[{"blockNumber": "1", "txs": ["0x1234"], "minTimestamp": 2, "maxTimestamp": 3}],"id":1}' + '{"jsonrpc":"2.0","method":"eth_sendBundle","params":[{"blockNumber": "0x01", "txs": ["0x1234"], "minTimestamp": 2, "maxTimestamp": 3}],"id":1}' ); } }