diff --git a/sidecar/src/app.ts b/sidecar/src/app.ts index aee8ebad..fbd9f696 100644 --- a/sidecar/src/app.ts +++ b/sidecar/src/app.ts @@ -22,6 +22,7 @@ import { BalanceHandler, AccountHandler, DistributionHandler, + JsonRpcHandler, NodeInfoHandler, TxHandler, StakingHandler, @@ -125,6 +126,7 @@ export class App { const txHandler = new TxHandler(this.services.get('tx')); const stakingHandler = new StakingHandler(this.services.get('staking')); const wsHandler = new WebsocketHandler(this.jsonrpc); + const jsonrpcHandler = new JsonRpcHandler(this.jsonrpc); this.server.get('/cosmos/bank/v1beta1/balances/:address', balanceHandler.handleGetBalance); this.server.get('/cosmos/auth/v1beta1/accounts/:address', accountHandler.handleGetAccount); @@ -134,7 +136,8 @@ export class App { this.server.post('/cosmos/tx/v1beta1/simulate', txHandler.handlePostSimulate); this.server.get('/cosmos/staking/v1beta1/delegations/:delegatorAddr', stakingHandler.handleGetStaking); this.server.get('/cosmos/staking/v1beta1/delegators/:delegatorAddr/unbonding_delegations', stakingHandler.handleGetUnbondingDelegations); - this.server.get('/websocket', { websocket: true }, wsHandler.handlerMessage); + this.server.get('/websocket', { websocket: true }, wsHandler.handleMessage); + this.server.post('/', jsonrpcHandler.handleRequest) } async initJsonRpcServer() { diff --git a/sidecar/src/handlers/index.ts b/sidecar/src/handlers/index.ts index 96c621e2..0de63c85 100644 --- a/sidecar/src/handlers/index.ts +++ b/sidecar/src/handlers/index.ts @@ -1,5 +1,6 @@ export * from './balance'; export * from './account'; +export * from './jsonrpc'; export * from './nodeinfo'; export * from './tx'; export * from './distribution'; diff --git a/sidecar/src/handlers/jsonrpc.ts b/sidecar/src/handlers/jsonrpc.ts new file mode 100644 index 00000000..ca4bc00e --- /dev/null +++ b/sidecar/src/handlers/jsonrpc.ts @@ -0,0 +1,16 @@ +import { FastifyRequest } from "fastify"; +import { JSONRPCRequest, JSONRPCResponse, JSONRPCServer } from "json-rpc-2.0"; + +export class JsonRpcHandler { + jsonrpc: JSONRPCServer; + + constructor(jsonrpc: JSONRPCServer) { + this.jsonrpc = jsonrpc; + } + + handleRequest = async (request: FastifyRequest<{ + Body: JSONRPCRequest; + }>): Promise => { + return await this.jsonrpc.receive(request.body); + } +} diff --git a/sidecar/src/handlers/ws.ts b/sidecar/src/handlers/ws.ts index d8970dce..a1e27bb4 100644 --- a/sidecar/src/handlers/ws.ts +++ b/sidecar/src/handlers/ws.ts @@ -8,7 +8,7 @@ export class WebsocketHandler { this.jsonrpc = jsonrpc; } - handlerMessage = (connection: SocketStream) => { + handleMessage = (connection: SocketStream) => { connection.socket.on('message', async (message) => { const request = JSON.parse(message.toString()); const response = await this.jsonrpc.receive(request); diff --git a/sidecar/src/services/tx.ts b/sidecar/src/services/tx.ts index bb89e727..071551b7 100644 --- a/sidecar/src/services/tx.ts +++ b/sidecar/src/services/tx.ts @@ -8,7 +8,7 @@ import { import Long from "long"; import { createHash } from "crypto"; import { convertToCodespace } from "../constants/codespace"; -import { encodeTo, sleep } from "../utils"; +import { encodeTo } from "../utils"; import { Event as CosmosEvent } from "cosmjs-types/tendermint/abci/types"; import { Header } from "@polkadot/types/interfaces"; import { ChainService } from "./chain";