Add a flag to enable ETH RPC server

This commit is contained in:
Prathamesh Musale 2024-09-13 09:40:18 +05:30
parent 7869b72aef
commit 36c56da467
2 changed files with 20 additions and 10 deletions

View File

@ -252,6 +252,9 @@ export interface ServerConfig {
// Flag to specify whether RPC endpoint supports block hash as block tag parameter // Flag to specify whether RPC endpoint supports block hash as block tag parameter
// https://ethereum.org/en/developers/docs/apis/json-rpc/#default-block // https://ethereum.org/en/developers/docs/apis/json-rpc/#default-block
rpcSupportsBlockHashParam: boolean; rpcSupportsBlockHashParam: boolean;
// Enable ETH JSON RPC server at /rpc
enableEthRPCServer: boolean;
} }
export interface FundingAmountsConfig { export interface FundingAmountsConfig {

View File

@ -24,6 +24,7 @@ import { PaymentsManager, paymentsPlugin } from './payments';
const log = debug('vulcanize:server'); const log = debug('vulcanize:server');
const DEFAULT_GQL_PATH = '/graphql'; const DEFAULT_GQL_PATH = '/graphql';
const ETH_RPC_PATH = '/rpc';
export const createAndStartServer = async ( export const createAndStartServer = async (
app: Application, app: Application,
@ -101,19 +102,25 @@ export const createAndStartServer = async (
path: gqlPath path: gqlPath
}); });
// Create a JSON-RPC server to handle ETH RPC calls at /rpc if (serverConfig.enableEthRPCServer) {
const rpcServer = jayson.Server(ethRPCHandlers); // Create a JSON-RPC server to handle ETH RPC calls
const rpcServer = jayson.Server(ethRPCHandlers);
// Mount the JSON-RPC server to /rpc path // Mount the JSON-RPC server to ETH_RPC_PATH
app.use( app.use(
'/rpc', ETH_RPC_PATH,
jsonParser(), jsonParser(),
// TODO: Handle GET requests as well to match Geth's behaviour // TODO: Handle GET requests as well to match Geth's behaviour
rpcServer.middleware() rpcServer.middleware()
); );
}
httpServer.listen(port, host, () => { httpServer.listen(port, host, () => {
log(`Server is listening on ${host}:${port}${server.graphqlPath}`); log(`GQL server is listening on http://${host}:${port}${server.graphqlPath}`);
if (serverConfig.enableEthRPCServer) {
log(`ETH JSON RPC server is listening on http://${host}:${port}${ETH_RPC_PATH}`);
}
}); });
return server; return server;