Skip to content

Commit

Permalink
break out bundle params encoding into a new function encodeBundlePara…
Browse files Browse the repository at this point in the history
…ms (from Bundle.encodeBundle) (#90)

* break out bundle params encoding into a new function encodeBundleParams (from Bundle.encodeBundle)

* fix blockNumber encoding in tests

* ignore build-info in docs-gen reading forge artifacts
  • Loading branch information
zeroXbrock authored Jul 1, 2024
1 parent 269e283 commit 28afe4b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
18 changes: 13 additions & 5 deletions src/protocols/Bundle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ library Bundle {
return Suave.doHTTPRequest(request);
}

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.toHexString(args.blockNumber), '", "txs": [');
/// @notice encode a bundle in json format.
/// @param args the bundle to encode.
/// @return params JSON-encoded bytes: `{blockNumber, txs, minTimestamp, maxTimestamp}`
function encodeBundleParams(BundleObj memory args) internal pure returns (bytes memory params) {
params = abi.encodePacked('{"blockNumber": "', LibString.toMinimalHexString(args.blockNumber), '", "txs": [');
for (uint256 i = 0; i < args.txns.length; i++) {
params = abi.encodePacked(params, '"', LibString.toHexString(args.txns[i]), '"');
if (i < args.txns.length - 1) {
Expand All @@ -53,7 +53,15 @@ library Bundle {
params = abi.encodePacked(params, ', "maxTimestamp": ', LibString.toString(args.maxTimestamp));
}
params = abi.encodePacked(params, "}");
}

/// @notice encode a call to `eth_sendBundle` as an HttpRequest.
/// @param args the bundle to encode.
/// @return request the HttpRequest to send the bundle.
function encodeBundle(BundleObj memory args) internal pure returns (Suave.HttpRequest memory) {
require(args.txns.length > 0, "Bundle: no txns");

bytes memory params = encodeBundleParams(args);
bytes memory body =
abi.encodePacked('{"jsonrpc":"2.0","method":"eth_sendBundle","params":[', params, '],"id":1}');

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 @@ -18,7 +18,7 @@ contract EthSendBundle is Test {
Suave.HttpRequest memory request = Bundle.encodeBundle(bundle);
assertEq(
string(request.body),
'{"jsonrpc":"2.0","method":"eth_sendBundle","params":[{"blockNumber": "0x01", "txs": ["0x1234"]}],"id":1}'
'{"jsonrpc":"2.0","method":"eth_sendBundle","params":[{"blockNumber": "0x1", "txs": ["0x1234"]}],"id":1}'
);
assertTrue(request.withFlashbotsSignature);

Expand All @@ -28,7 +28,7 @@ contract EthSendBundle is Test {
Suave.HttpRequest memory request2 = Bundle.encodeBundle(bundle);
assertEq(
string(request2.body),
'{"jsonrpc":"2.0","method":"eth_sendBundle","params":[{"blockNumber": "0x01", "txs": ["0x1234"], "minTimestamp": 2}],"id":1}'
'{"jsonrpc":"2.0","method":"eth_sendBundle","params":[{"blockNumber": "0x1", "txs": ["0x1234"], "minTimestamp": 2}],"id":1}'
);

// encode with 'maxTimestamp'
Expand All @@ -37,7 +37,7 @@ contract EthSendBundle is Test {
Suave.HttpRequest memory request3 = Bundle.encodeBundle(bundle);
assertEq(
string(request3.body),
'{"jsonrpc":"2.0","method":"eth_sendBundle","params":[{"blockNumber": "0x01", "txs": ["0x1234"], "minTimestamp": 2, "maxTimestamp": 3}],"id":1}'
'{"jsonrpc":"2.0","method":"eth_sendBundle","params":[{"blockNumber": "0x1", "txs": ["0x1234"], "minTimestamp": 2, "maxTimestamp": 3}],"id":1}'
);
}

Expand Down
7 changes: 7 additions & 0 deletions tools/docs-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@ func readForgeArtifacts(path string) ([]*artifact, error) {
if d.IsDir() {
return nil
}

// ignore if parent directory doesn't end w/ ".sol"
parentDir := filepath.Base(filepath.Dir(path))
if !strings.HasSuffix(parentDir, ".sol") {
return nil
}

ext := filepath.Ext(d.Name())
if ext != ".json" {
return nil
Expand Down

0 comments on commit 28afe4b

Please sign in to comment.