Skip to content

Commit

Permalink
sendBundle tweaks (#29)
Browse files Browse the repository at this point in the history
* remove unused url from bundle struct

* fix failing test

* fix failing test
  • Loading branch information
zeroXbrock authored Jan 19, 2024
1 parent 46bafe1 commit 20bd687
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
44 changes: 34 additions & 10 deletions src/protocols/Bundle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,65 @@ 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 {
params = abi.encodePacked(params, "]");
}
}
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";
Expand Down
6 changes: 3 additions & 3 deletions test/protocols/Bundle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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'
Expand All @@ -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}'
);
}
}

0 comments on commit 20bd687

Please sign in to comment.