-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Mevshare protocol * Update README.md * Add flashbots signature * Merge final version
- Loading branch information
Showing
3 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// SPDX-License-Identifier: Unlicense | ||
pragma solidity ^0.8.13; | ||
|
||
import "../suavelib/Suave.sol"; | ||
import "solady/src/utils/LibString.sol"; | ||
import "solady/src/utils/JSONParserLib.sol"; | ||
|
||
// https://github.com/flashbots/mev-share/blob/main/specs/bundles/v0.1.md#json-rpc-request-scheme | ||
library MevShare { | ||
struct Bundle { | ||
uint64 inclusionBlock; | ||
bytes[] bodies; | ||
bool[] canRevert; | ||
uint8[] refundPercents; | ||
} | ||
|
||
function encodeBundle(Bundle memory bundle) internal pure returns (Suave.HttpRequest memory) { | ||
require(bundle.bodies.length == bundle.canRevert.length, "MevShare: bodies and canRevert length mismatch"); | ||
bytes memory body = abi.encodePacked('{"jsonrpc":"2.0","method":"mev_sendBundle","params":[{"version":"v0.1",'); | ||
|
||
// -> inclusion | ||
body = | ||
abi.encodePacked(body, '"inclusion":{"block":"', LibString.toMinimalHexString(bundle.inclusionBlock), '"},'); | ||
|
||
// -> body | ||
body = abi.encodePacked(body, '"body":['); | ||
|
||
for (uint256 i = 0; i < bundle.bodies.length; i++) { | ||
body = abi.encodePacked( | ||
body, | ||
'{"tx":"', | ||
LibString.toHexString(bundle.bodies[i]), | ||
'","canRevert":', | ||
bundle.canRevert[i] ? "true" : "false", | ||
"}" | ||
); | ||
|
||
if (i < bundle.bodies.length - 1) { | ||
body = abi.encodePacked(body, ","); | ||
} | ||
} | ||
|
||
body = abi.encodePacked(body, "],"); | ||
|
||
// -> validity | ||
body = abi.encodePacked(body, '"validity":{"refund":['); | ||
|
||
for (uint256 i = 0; i < bundle.refundPercents.length; i++) { | ||
body = abi.encodePacked( | ||
body, | ||
'{"bodyIdx":', | ||
LibString.toString(i), | ||
',"percent":', | ||
LibString.toString(bundle.refundPercents[i]), | ||
"}" | ||
); | ||
|
||
if (i < bundle.refundPercents.length - 1) { | ||
body = abi.encodePacked(body, ","); | ||
} | ||
} | ||
|
||
body = abi.encodePacked(body, "]}"); | ||
|
||
Suave.HttpRequest memory request; | ||
request.headers = new string[](1); | ||
request.headers[0] = "Content-Type:application/json"; | ||
request.body = body; | ||
request.withFlashbotsSignature = true; | ||
|
||
return request; | ||
} | ||
|
||
function sendBundle(string memory url, Bundle memory bundle) internal view { | ||
Suave.HttpRequest memory request = encodeBundle(bundle); | ||
request.url = url; | ||
Suave.doHTTPRequest(request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.13; | ||
|
||
import "forge-std/Test.sol"; | ||
import "src/protocols/MevShare.sol"; | ||
import "src/suavelib/Suave.sol"; | ||
|
||
contract MevShareTest is Test { | ||
function testEncodeMevShare() public { | ||
MevShare.Bundle memory bundle; | ||
bundle.inclusionBlock = 1; | ||
|
||
bundle.bodies = new bytes[](1); | ||
bundle.bodies[0] = hex"1234"; | ||
|
||
bundle.canRevert = new bool[](1); | ||
bundle.canRevert[0] = true; | ||
|
||
bundle.refundPercents = new uint8[](1); | ||
bundle.refundPercents[0] = 10; | ||
|
||
Suave.HttpRequest memory request = MevShare.encodeBundle(bundle); | ||
assertEq( | ||
string(request.body), | ||
'{"jsonrpc":"2.0","method":"mev_sendBundle","params":[{"version":"v0.1","inclusion":{"block":"0x1"},"body":[{"tx":"0x1234","canRevert":true}],"validity":{"refund":[{"bodyIdx":0,"percent":10}]}' | ||
); | ||
assertTrue(request.withFlashbotsSignature); | ||
} | ||
} |