-
Notifications
You must be signed in to change notification settings - Fork 11
/
Bundle.sol
79 lines (71 loc) · 2.31 KB
/
Bundle.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.13;
import "../suavelib/Suave.sol";
import "solady/src/utils/LibString.sol";
// https://docs.flashbots.net/flashbots-auction/advanced/rpc-endpoint#eth_sendbundle
library Bundle {
struct BundleObj {
uint64 blockNumber;
uint64 minTimestamp;
uint64 maxTimestamp;
bytes[] txns;
}
function sendBundle(
string memory url,
BundleObj memory bundle
) internal view returns (bytes memory) {
Suave.HttpRequest memory request = encodeBundle(bundle);
request.url = url;
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": ['
);
for (uint256 i = 0; i < args.txns.length; 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)
);
}
if (args.maxTimestamp > 0) {
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}'
);
Suave.HttpRequest memory request;
request.method = "POST";
request.body = body;
request.headers = new string[](1);
request.headers[0] = "Content-Type: application/json";
request.withFlashbotsSignature = true;
return request;
}
}