-
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.
Merge branch 'main' into feature/mevm-context
- Loading branch information
Showing
4 changed files
with
114 additions
and
17 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
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,57 @@ | ||
// SPDX-License-Identifier: Unlicense | ||
pragma solidity ^0.8.13; | ||
|
||
import "src/suavelib/Suave.sol"; | ||
import "solady/src/utils/JSONParserLib.sol"; | ||
import "solady/src/utils/LibString.sol"; | ||
|
||
contract EthJsonRPC { | ||
using JSONParserLib for *; | ||
|
||
string endpoint; | ||
|
||
constructor(string memory _endpoint) { | ||
endpoint = _endpoint; | ||
} | ||
|
||
function nonce(address addr) public returns (uint256) { | ||
bytes memory body = abi.encodePacked( | ||
'{"jsonrpc":"2.0","method":"eth_getTransactionCount","params":["', | ||
LibString.toHexStringChecksummed(addr), | ||
'","latest"],"id":1}' | ||
); | ||
|
||
JSONParserLib.Item memory item = doRequest(string(body)); | ||
uint256 val = JSONParserLib.parseUintFromHex(trimQuotes(item.value())); | ||
return val; | ||
} | ||
|
||
function doRequest(string memory body) public returns (JSONParserLib.Item memory) { | ||
Suave.HttpRequest memory request; | ||
request.method = "POST"; | ||
request.url = endpoint; | ||
request.headers = new string[](1); | ||
request.headers[0] = "Content-Type: application/json"; | ||
request.body = bytes(body); | ||
|
||
bytes memory output = Suave.doHTTPRequest(request); | ||
|
||
JSONParserLib.Item memory item = string(output).parse(); | ||
return item.at('"result"'); | ||
} | ||
|
||
function trimQuotes(string memory input) private pure returns (string memory) { | ||
bytes memory inputBytes = bytes(input); | ||
require( | ||
inputBytes.length >= 2 && inputBytes[0] == '"' && inputBytes[inputBytes.length - 1] == '"', "Invalid input" | ||
); | ||
|
||
bytes memory result = new bytes(inputBytes.length - 2); | ||
|
||
for (uint256 i = 1; i < inputBytes.length - 1; i++) { | ||
result[i - 1] = inputBytes[i]; | ||
} | ||
|
||
return string(result); | ||
} | ||
} |
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,24 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.13; | ||
|
||
import "forge-std/Test.sol"; | ||
import "src/Test.sol"; | ||
import "solady/src/utils/LibString.sol"; | ||
import "src/protocols/EthJsonRPC.sol"; | ||
|
||
contract EthJsonRPCTest is Test, SuaveEnabled { | ||
function testEthJsonRPCGetNonce() public { | ||
EthJsonRPC ethjsonrpc = getEthJsonRPC(); | ||
|
||
uint256 nonce = ethjsonrpc.nonce(address(this)); | ||
assertEq(nonce, 0); | ||
} | ||
|
||
function getEthJsonRPC() public returns (EthJsonRPC ethjsonrpc) { | ||
try vm.envString("JSONRPC_ENDPOINT") returns (string memory endpoint) { | ||
ethjsonrpc = new EthJsonRPC(endpoint); | ||
} catch { | ||
vm.skip(true); | ||
} | ||
} | ||
} |