Skip to content

Commit d470aaf

Browse files
committed
removed encodeParams and decodeResp
1 parent 264f991 commit d470aaf

File tree

4 files changed

+16
-194
lines changed

4 files changed

+16
-194
lines changed

networks/cosmos/src/adapters/base.ts

Lines changed: 0 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,6 @@ export interface IProtocolAdapter {
230230
getVersion(): ProtocolVersion;
231231
getSupportedMethods(): Set<RpcMethod>;
232232
getCapabilities(): ProtocolCapabilities;
233-
encodeParams(method: RpcMethod, params: any): any;
234-
decodeResponse(method: RpcMethod, response: any): any;
235233
encodeBytes(data: string): Uint8Array;
236234
decodeBytes(data: Uint8Array): string;
237235
}
@@ -357,179 +355,6 @@ export abstract class BaseAdapter implements RequestEncoder, ResponseDecoder, IC
357355
return this.version;
358356
}
359357

360-
encodeParams(method: RpcMethod, params: any): any {
361-
// Convert camelCase to snake_case for Tendermint/CometBFT
362-
if (!params) return {};
363-
364-
// Special handling for ABCI query using codec
365-
if (method === RpcMethod.ABCI_QUERY) {
366-
const encoded = this.encodeAbciQuery(params as AbciQueryParams);
367-
// Convert to snake_case for RPC
368-
return {
369-
path: encoded.path,
370-
data: encoded.data,
371-
height: encoded.height,
372-
prove: encoded.prove
373-
};
374-
}
375-
376-
// Special handling for commit using codec
377-
if (method === RpcMethod.COMMIT) {
378-
const encoded = this.encodeCommit(params as CommitParams);
379-
return encoded;
380-
}
381-
382-
// Special handling for block_results using codec
383-
if (method === RpcMethod.BLOCK_RESULTS) {
384-
const encoded = this.encodeBlockResults(params as BlockResultsParams);
385-
return encoded;
386-
}
387-
388-
389-
390-
// Convert height to string for block-related methods
391-
if ((method === RpcMethod.BLOCK) &&
392-
params.height !== undefined && typeof params.height === "number") {
393-
params = { ...params, height: params.height.toString() };
394-
}
395-
396-
// Convert height to string for validator and consensus methods
397-
if ((method === RpcMethod.VALIDATORS || method === RpcMethod.CONSENSUS_PARAMS) &&
398-
params.height !== undefined && typeof params.height === "number") {
399-
params = { ...params, height: params.height.toString() };
400-
}
401-
402-
403-
404-
// Special handling for genesis (no parameters)
405-
if (method === RpcMethod.GENESIS) {
406-
return undefined;
407-
}
408-
409-
410-
411-
412-
413-
// Special handling for unconfirmed_txs using codec
414-
if (method === RpcMethod.UNCONFIRMED_TXS) {
415-
const encoded = this.encodeUnconfirmedTxs(params as UnconfirmedTxsParams);
416-
return encoded;
417-
}
418-
419-
// Special handling for validators using codec
420-
if (method === RpcMethod.VALIDATORS) {
421-
const encoded = this.encodeValidators(params as ValidatorsParams);
422-
return encoded;
423-
}
424-
425-
// Special handling for tx using codec
426-
if (method === RpcMethod.TX) {
427-
const encoded = this.encodeTx(params as TxParams);
428-
return encoded;
429-
}
430-
431-
// Special handling for tx_search using codec
432-
if (method === RpcMethod.TX_SEARCH) {
433-
const encoded = this.encodeTxSearch(params as TxSearchParams);
434-
return encoded;
435-
}
436-
437-
438-
439-
// Special handling for broadcast_tx_sync using codec
440-
if (method === RpcMethod.BROADCAST_TX_SYNC) {
441-
const encoded = this.encodeBroadcastTxSync(params as BroadcastTxParams);
442-
return encoded;
443-
}
444-
445-
// Special handling for broadcast_tx_async using codec
446-
if (method === RpcMethod.BROADCAST_TX_ASYNC) {
447-
const encoded = this.encodeBroadcastTxAsync(params as BroadcastTxParams);
448-
return encoded;
449-
}
450-
451-
// Special handling for broadcast_tx_commit using codec
452-
if (method === RpcMethod.BROADCAST_TX_COMMIT) {
453-
const encoded = this.encodeBroadcastTxCommit(params as BroadcastTxParams);
454-
return encoded;
455-
}
456-
457-
// Special handling for check_tx using codec
458-
if (method === RpcMethod.CHECK_TX) {
459-
const encoded = this.encodeCheckTx(params as CheckTxParams);
460-
return encoded;
461-
}
462-
463-
const encoded: any = {};
464-
for (const [key, value] of Object.entries(params)) {
465-
const snakeKey = this.camelToSnake(key);
466-
467-
// Handle hash parameters with 0x prefix
468-
if (key === 'hash' && typeof value === 'string' && value.startsWith('0x')) {
469-
// Convert hex to base64 for RPC
470-
const hexString = value.slice(2); // Remove 0x prefix
471-
const bytes = Buffer.from(hexString, 'hex');
472-
encoded[snakeKey] = bytes.toString('base64');
473-
}
474-
// Handle Uint8Array data (especially for ABCI queries and broadcast)
475-
else if (value instanceof Uint8Array) {
476-
// For tx field in broadcast methods, encode as base64
477-
if (key === 'tx') {
478-
encoded[snakeKey] = Buffer.from(value).toString('base64');
479-
} else {
480-
// Convert Uint8Array to hex string for RPC
481-
encoded[snakeKey] = this.decodeBytes(value);
482-
}
483-
}
484-
else {
485-
encoded[snakeKey] = value;
486-
}
487-
}
488-
return encoded;
489-
}
490-
491-
decodeResponse(method: RpcMethod, response: any): any {
492-
// Use the version-specific decoder
493-
switch (method) {
494-
case RpcMethod.ABCI_INFO:
495-
return this.decodeAbciInfo(response);
496-
case RpcMethod.ABCI_QUERY:
497-
return this.decodeAbciQuery(response);
498-
case RpcMethod.BLOCK_RESULTS:
499-
return this.decodeBlockResults(response);
500-
501-
case RpcMethod.CONSENSUS_PARAMS:
502-
return this.decodeConsensusParams(response);
503-
504-
505-
506-
case RpcMethod.STATUS:
507-
return this.decodeStatus(response);
508-
case RpcMethod.NET_INFO:
509-
return this.decodeNetInfo(response);
510-
case RpcMethod.GENESIS:
511-
return this.decodeGenesis(response);
512-
case RpcMethod.GENESIS_CHUNKED:
513-
return this.decodeGenesisChunked(response);
514-
case RpcMethod.HEALTH:
515-
return this.decodeHealth(response);
516-
517-
case RpcMethod.NUM_UNCONFIRMED_TXS:
518-
return this.decodeNumUnconfirmedTxs(response);
519-
case RpcMethod.COMMIT:
520-
return this.decodeCommit(response);
521-
case RpcMethod.BROADCAST_TX_SYNC:
522-
return this.decodeBroadcastTxSync(response);
523-
case RpcMethod.BROADCAST_TX_ASYNC:
524-
return this.decodeBroadcastTxAsync(response);
525-
case RpcMethod.BROADCAST_TX_COMMIT:
526-
return this.decodeBroadcastTxCommit(response);
527-
default:
528-
// For unsupported methods, return raw response
529-
return response;
530-
}
531-
}
532-
533358
encodeBytes(data: string): Uint8Array {
534359
// Handle hex strings and base64
535360
if (data.startsWith('0x')) {

networks/cosmos/src/client-factory.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,11 @@ export class CosmosClientFactory {
104104
endpoint: string | WebSocketEndpoint,
105105
options: WebSocketClientOptions = {}
106106
): Promise<ICosmosEventClient> {
107-
// For WebSocket, we need to convert the endpoint to HTTP for detection
108-
const httpEndpoint = this.convertToHttpEndpoint(endpoint);
109-
const protocolAdapter = await this.getProtocolAdapter(httpEndpoint, options.protocolVersion);
110107
const rpcClient = new WebSocketRpcClient(endpoint, {
111108
reconnect: options.reconnect
112109
});
113110

114-
return new CosmosEventClient(rpcClient, protocolAdapter);
111+
return new CosmosEventClient(rpcClient);
115112
}
116113

117114
/**
@@ -135,7 +132,7 @@ export class CosmosClientFactory {
135132

136133
return {
137134
queryClient: new CosmosQueryClient(httpRpcClient, protocolAdapter) as any,
138-
eventClient: new CosmosEventClient(wsRpcClient, protocolAdapter)
135+
eventClient: new CosmosEventClient(wsRpcClient)
139136
};
140137
}
141138

@@ -155,7 +152,7 @@ export class CosmosClientFactory {
155152

156153
return {
157154
queryClient: new CosmosQueryClient(rpcClient, protocolAdapter) as any,
158-
eventClient: new CosmosEventClient(rpcClient, protocolAdapter)
155+
eventClient: new CosmosEventClient(rpcClient)
159156
};
160157
}
161158
}

networks/cosmos/src/event/cosmos-event-client.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,24 @@ import { RpcMethod, EventType } from '../types/protocol';
55
import { Block } from '../types/responses/common/block/block';
66
import { BlockHeader } from '../types/responses/common/header/block-header';
77
import { TxEvent, BlockEvent } from '../types/responses/common/event';
8-
import { IProtocolAdapter } from '../adapters/base';
98

109
export class CosmosEventClient implements ICosmosEventClient {
1110
private activeSubscriptions = new Set<string>();
1211

1312
constructor(
14-
private rpcClient: IRpcClient,
15-
private protocolAdapter: IProtocolAdapter
13+
private rpcClient: IRpcClient
1614
) {}
1715

1816
async* subscribeToEvents<TEvent>(
19-
eventType: string,
17+
eventType: string,
2018
filter?: unknown
2119
): AsyncIterable<TEvent> {
2220
if (!this.rpcClient.isConnected()) {
2321
throw new SubscriptionError('RPC client not connected');
2422
}
2523

2624
const subscriptionKey = `${eventType}_${JSON.stringify(filter)}`;
27-
25+
2826
if (this.activeSubscriptions.has(subscriptionKey)) {
2927
throw new SubscriptionError(`Already subscribed to ${eventType}`);
3028
}
@@ -33,10 +31,12 @@ export class CosmosEventClient implements ICosmosEventClient {
3331

3432
try {
3533
const params = { query: this.buildQuery(eventType, filter) };
36-
const encodedParams = this.protocolAdapter.encodeParams(RpcMethod.SUBSCRIBE, params);
37-
34+
// For SUBSCRIBE, just pass params directly since they're already in the correct format
35+
const encodedParams = params;
36+
3837
for await (const event of this.rpcClient.subscribe<any>(RpcMethod.SUBSCRIBE, encodedParams)) {
39-
const decoded = this.protocolAdapter.decodeResponse(RpcMethod.SUBSCRIBE, event);
38+
// For SUBSCRIBE, return the raw response as no specific decoding is needed
39+
const decoded = event;
4040
yield decoded as TEvent;
4141
}
4242
} finally {
@@ -84,7 +84,7 @@ export class CosmosEventClient implements ICosmosEventClient {
8484

8585
private buildQuery(eventType: string, filter?: unknown): string {
8686
let query = `tm.event='${eventType}'`;
87-
87+
8888
if (filter && typeof filter === 'object') {
8989
const filterObj = filter as Record<string, any>;
9090
for (const [key, value] of Object.entries(filterObj)) {
@@ -95,7 +95,7 @@ export class CosmosEventClient implements ICosmosEventClient {
9595
query += ` AND ${key}='${value}'`;
9696
}
9797
}
98-
98+
9999
return query;
100100
}
101101
}

networks/cosmos/src/query/__tests__/broadcast.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,13 @@ describe('CosmosQueryClient Broadcast Methods', () => {
169169
it('should encode tx as base64 for broadcast methods', () => {
170170
const tx = new Uint8Array([255, 128, 64, 32, 16]);
171171

172-
const syncParams = adapter.encodeParams(RpcMethod.BROADCAST_TX_SYNC, { tx });
172+
const syncParams = adapter.encodeBroadcastTxSync({ tx });
173173
expect(syncParams.tx).toBe('/4BAIBA=');
174174

175-
const asyncParams = adapter.encodeParams(RpcMethod.BROADCAST_TX_ASYNC, { tx });
175+
const asyncParams = adapter.encodeBroadcastTxAsync({ tx });
176176
expect(asyncParams.tx).toBe('/4BAIBA=');
177177

178-
const commitParams = adapter.encodeParams(RpcMethod.BROADCAST_TX_COMMIT, { tx });
178+
const commitParams = adapter.encodeBroadcastTxCommit({ tx });
179179
expect(commitParams.tx).toBe('/4BAIBA=');
180180
});
181181
});

0 commit comments

Comments
 (0)