Skip to content

Commit

Permalink
Add Gateway contract to make calls to external contracts (#72)
Browse files Browse the repository at this point in the history
* Add jsonrpc call() method

* Introduce the Gateway contract
  • Loading branch information
ferranbt authored Mar 27, 2024
1 parent 783736c commit c474f35
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
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);
}

0 comments on commit c474f35

Please sign in to comment.