Skip to content

Commit

Permalink
feat(sidecar): Add jsonrpc request handler
Browse files Browse the repository at this point in the history
  • Loading branch information
code0xff committed Oct 15, 2024
1 parent a2dd8d2 commit fe35485
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
5 changes: 4 additions & 1 deletion sidecar/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
AbciRpcHandler,
TxRpcHandler
} from "./rpc";
import { JsonRpcHandler } from "./handlers/jsonrpc";

export class App {
config: IConfig;
Expand Down Expand Up @@ -125,6 +126,7 @@ export class App {
const txHandler = new TxHandler(this.services.get<TxService>('tx'));
const stakingHandler = new StakingHandler(this.services.get<StakingService>('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);
Expand All @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions sidecar/src/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './balance';
export * from './account';
export * from './jsonrpc';
export * from './nodeinfo';
export * from './tx';
export * from './distribution';
Expand Down
16 changes: 16 additions & 0 deletions sidecar/src/handlers/jsonrpc.ts
Original file line number Diff line number Diff line change
@@ -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<JSONRPCResponse | null> => {
return await this.jsonrpc.receive(request.body);
}
}
2 changes: 1 addition & 1 deletion sidecar/src/handlers/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion sidecar/src/services/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit fe35485

Please sign in to comment.