-
-
Save ohhkaneda/1f89386724c09b6ab947442e5890a318 to your computer and use it in GitHub Desktop.
RPC Multiplexer
This file contains hidden or 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
| # Import necessary modules from the Flask library | |
| from flask import Flask, request | |
| from flask_restful import Resource, Api | |
| # Import the requests module to send HTTP requests | |
| import requests | |
| # Import ThreadPoolExecutor from the concurrent.futures module for parallelizing requests | |
| from concurrent.futures import ThreadPoolExecutor | |
| # Create a Flask application instance | |
| app = Flask(__name__) | |
| # Create an Api instance to add RESTful resources to the app | |
| api = Api(app) | |
| # Define the default RPC endpoint | |
| default_rpc = "https://rpc.builder0x69.io/" | |
| # Define a list of RPC endpoints to which requests will be forwarded | |
| rpcs = ["https://rpc.builder0x69.io/", "https://rpc.flashbots.net", ...] | |
| # Define a function to send HTTP POST requests and return the JSON response | |
| def send_request(url, json): | |
| try: | |
| # Send a POST request to the specified URL with the provided JSON and return the response JSON | |
| return requests.post(url, json=json).json() | |
| except requests.RequestException as e: | |
| # Return an error message if the request fails | |
| return {"error": str(e)} | |
| # Define a Resource class for the RPC multiplexer | |
| class RpcMultiplexer(Resource): | |
| # Define a method to handle HTTP POST requests to this resource | |
| def post(self): | |
| # Validate that the request contains valid JSON, return an error message if not | |
| if not request.is_json: | |
| return {"error": "Invalid JSON"}, 400 | |
| # Parse the JSON from the request | |
| request_json = request.get_json() | |
| # Validate that the JSON contains the "method" key, return an error message if not | |
| if "method" not in request_json: | |
| return {"error": "Missing 'method' in JSON object"}, 400 | |
| # Check if the "method" in the JSON is "eth_sendRawTransaction" | |
| if request_json["method"] == "eth_sendRawTransaction": | |
| # Create a ThreadPoolExecutor to send requests in parallel | |
| with ThreadPoolExecutor() as executor: | |
| # Map the send_request function to the list of RPCs and the request JSON, and execute them in parallel | |
| responses = list(executor.map(send_request, rpcs, [request_json] * len(rpcs))) | |
| # Return the responses from all RPCs | |
| return {"responses": responses} | |
| else: | |
| # For other methods, forward the request to the default RPC and return its response | |
| return send_request(default_rpc, request_json) | |
| # Add the RpcMultiplexer resource to the Api instance at the root URL | |
| api.add_resource(RpcMultiplexer, '/') | |
| # Check if this script is the main module | |
| if __name__ == '__main__': | |
| # Run the Flask application on port 6001 with debugging enabled | |
| app.run(port=6001, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment