-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Mevshare protocol #8
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d350fd4
Add Mevshare protocol
ferranbt 0b1dbf0
Update README.md
ferranbt eb3c4b7
Merge conflicts
ferranbt 029ab90
Merge branch 'main' into feature/mev-share-protocol
ferranbt e2ae565
Add flashbots signature
ferranbt e27ac99
Merge final version
ferranbt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,78 @@ | ||
// 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 { | ||
string version; | ||
uint64 inclusionBlock; | ||
bytes[] bodies; | ||
bool[] canRevert; | ||
uint8[] refundPercents; | ||
} | ||
|
||
function encodeBundle(Bundle memory bundle) internal pure returns (bytes 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":[{'); | ||
|
||
// -> 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, "]}"); | ||
return body; | ||
} | ||
|
||
function sendBundle(string memory url, Bundle memory bundle) internal view { | ||
Suave.HttpRequest memory request; | ||
request.url = url; | ||
request.headers = new string[](1); | ||
request.headers[0] = "Content-Type:application/json"; | ||
request.body = encodeBundle(bundle); | ||
// TODO: Add Flashbots signature | ||
|
||
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,28 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.13; | ||
|
||
import "forge-std/Test.sol"; | ||
import "src/protocols/MevShare.sol"; | ||
|
||
contract MevShareTest is Test { | ||
function testEncodeMevShare() public { | ||
MevShare.Bundle memory bundle; | ||
bundle.version = ""; | ||
bundle.inclusionBlock = 1; | ||
|
||
bundle.bodies = new bytes[](1); | ||
bundle.bodies[0] = abi.encode(1234); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just a nit but might change |
||
|
||
bundle.canRevert = new bool[](1); | ||
bundle.canRevert[0] = true; | ||
|
||
bundle.refundPercents = new uint8[](1); | ||
bundle.refundPercents[0] = 10; | ||
|
||
bytes memory res = MevShare.encodeBundle(bundle); | ||
assertEq( | ||
string(res), | ||
'{"jsonrpc":"2.0","method":"mev_sendBundle","params":[{"inclusion":{"block":"0x1"},"body":[{"tx":"0x00000000000000000000000000000000000000000000000000000000000004d2","canRevert":true}],"validity":{"refund":[{"bodyIdx":0,"percent":10}]}' | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's needed to add the FB signature? I think we can just add the private key as a param to this function, then it's basically copypasta from the ethers lib