Skip to content
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 eth_call with account state overrides for bytecode #84

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/protocols/EthJsonRPC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ contract EthJsonRPC {

string endpoint;

struct AccountOverride {
address addr;
bytes code;
}

constructor(string memory _endpoint) {
endpoint = _endpoint;
}
Expand Down Expand Up @@ -65,6 +70,44 @@ contract EthJsonRPC {
return result;
}

/// @notice call a contract function with a state override.
/// @param to the address of the contract.
/// @param data the data of the function.
/// @param accountOverride the state override.
/// @return the result of the function call.
function call(address to, bytes memory data, AccountOverride[] memory accountOverride)
public
returns (bytes memory)
{
bytes memory body = abi.encodePacked(
'{"jsonrpc":"2.0","method":"eth_call","params":[{"to":"',
LibString.toHexStringChecksummed(to),
'","data":"',
LibString.toHexString(data),
'"},"latest",{'
);

for (uint256 i = 0; i < accountOverride.length; i++) {
body = abi.encodePacked(
body,
'"',
LibString.toHexStringChecksummed(accountOverride[i].addr),
'": {"code": "',
LibString.toHexString(accountOverride[i].code),
'"}'
);
if (i < accountOverride.length - 1) {
body = abi.encodePacked(body, ",");
}
}
body = abi.encodePacked(body, '}],"id":1}');

JSONParserLib.Item memory item = doRequest(string(body));
bytes memory result = HexStrings.fromHexString(_stripQuotesAndPrefix(item.value()));

return result;
}

function doRequest(string memory body) public returns (JSONParserLib.Item memory) {
Suave.HttpRequest memory request;
request.method = "POST";
Expand Down
27 changes: 26 additions & 1 deletion test/protocols/EthJsonRPC.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,27 @@ contract EthJsonRPCTest is Test, SuaveEnabled {

bytes memory data = abi.encodeWithSignature("get_deposit_count()");
bytes memory result = ethjsonrpc.call(address(0x00000000219ab540356cBB839Cbe05303d7705Fa), data);
uint256 resultUint = abi.decode(result, (uint256));

require(result.length > 0, "result is empty");
require(resultUint > 0, "result is empty");
}

function testEthJsonRPCCallWithOverrides() public {
EthJsonRPC ethjsonrpc = getEthJsonRPC();

EthJsonRPC.AccountOverride memory accountOverride = EthJsonRPC.AccountOverride({
addr: address(0x00000000219ab540356cBB839Cbe05303d7705Fa),
code: type(TestOverrideContract).runtimeCode
});

EthJsonRPC.AccountOverride[] memory acco = new EthJsonRPC.AccountOverride[](1);
acco[0] = accountOverride;

bytes memory data = abi.encodeWithSignature("get_deposit_count()");
bytes memory result = ethjsonrpc.call(address(0x00000000219ab540356cBB839Cbe05303d7705Fa), data, acco);
uint256 resultUint = abi.decode(result, (uint256));

require(resultUint == 10, "result is empty");
}

function getEthJsonRPC() public returns (EthJsonRPC ethjsonrpc) {
Expand All @@ -38,3 +57,9 @@ contract EthJsonRPCTest is Test, SuaveEnabled {
}
}
}

contract TestOverrideContract {
function get_deposit_count() public pure returns (uint256) {
return 10;
}
}
Loading