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 Gateway contract to make calls to external contracts #72

Merged
merged 4 commits into from
Mar 27, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,30 @@ contract Example {
}
```

### Gateway

Helper library to interact with contracts from other chains.

#### Example usage

```solidity
import "suave-std/Gateway.sol";
contract Example {
function example() public {
// query the beacon chain deposit contract
Gateway gateway = new Gateway("http://<jsonrpc endpoint>", address(0x00000000219ab540356cBB839Cbe05303d7705Fa));
DepositContract depositContract = DepositContract(address(gateway));
bytes memory count = depositContract.get_deposit_count();
}
}
interface DepositContract {
function get_deposit_count() external view returns (bytes memory);
}
```

### protocols/MevShare.sol

Helper library to send bundle requests with the Mev-Share protocol.
Expand Down
25 changes: 25 additions & 0 deletions src/Gateway.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.8;

import "./protocols/EthJsonRPC.sol";
import "forge-std/console.sol";

contract Gateway {
EthJsonRPC ethjsonrpc;
address target;

constructor(string memory _jsonrpc, address _target) {
ethjsonrpc = new EthJsonRPC(_jsonrpc);
target = _target;
}

fallback() external {
bytes memory ret = ethjsonrpc.call(target, msg.data);

assembly {
let location := ret
let length := mload(ret)
return(add(location, 0x20), length)
}
}
}
29 changes: 29 additions & 0 deletions test/Gateway.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.13;

import "forge-std/Test.sol";
import "src/Gateway.sol";
import "src/Test.sol";

contract TestGateway is Test, SuaveEnabled {
function testGateway() public {
string memory endpoint = getEthJsonRPC();
Gateway gateway = new Gateway(endpoint, address(0x00000000219ab540356cBB839Cbe05303d7705Fa));
DepositContract depositContract = DepositContract(address(gateway));

bytes memory count = depositContract.get_deposit_count();
require(count.length > 0, "count is empty");
}

function getEthJsonRPC() public returns (string memory) {
try vm.envString("JSONRPC_ENDPOINT") returns (string memory endpoint) {
return endpoint;
} catch {
vm.skip(true);
}
}
}

interface DepositContract {
function get_deposit_count() external view returns (bytes memory);
}