Skip to content

Commit 5de58d9

Browse files
authored
Merge pull request #127 from hyperweb-io/search-blocks
add searchBlock and searchTx
2 parents 3b04fd9 + b5bc44d commit 5de58d9

File tree

2 files changed

+109
-48
lines changed

2 files changed

+109
-48
lines changed

networks/cosmos/src/signing-client.ts

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,19 @@ import {
2424
Block,
2525
BlockResponse,
2626
IndexedTx,
27+
SearchBlockQuery,
2728
SearchTxQuery,
28-
SearchTxResponse,
29+
SearchTxQueryObj,
2930
TxResponse,
31+
isSearchBlockQueryObj,
32+
isSearchTxQueryObj,
3033
} from './types/query';
3134
import {
3235
EncodeObject,
3336
SigningOptions,
3437
} from './types/signing-client';
3538

39+
3640
/**
3741
* SigningClient is a client that can sign and broadcast transactions.
3842
*/
@@ -319,38 +323,81 @@ export class SigningClient {
319323
};
320324
}
321325

322-
async searchTx(query: SearchTxQuery): Promise<IndexedTx[]> {
326+
async searchTx(query: SearchTxQuery | SearchTxQueryObj): Promise<any> {
323327
let rawQuery: string;
328+
let prove = false;
329+
let page = 1;
330+
let perPage = 100;
331+
let orderBy: 'asc' | 'desc' = 'asc';
332+
324333
if (typeof query === 'string') {
325334
rawQuery = query;
326335
} else if (Array.isArray(query)) {
327336
rawQuery = query.map((t) => `${t.key}=${t.value}`).join(' AND ');
337+
} else if (isSearchTxQueryObj(query)) {
338+
if (typeof query.query === 'string') {
339+
rawQuery = query.query;
340+
} else if (Array.isArray(query.query)) {
341+
rawQuery = query.query.map((t) => `${t.key}=${t.value}`).join(' AND ');
342+
} else {
343+
throw new Error('Need to provide a valid query.');
344+
}
345+
prove = query.prove ?? false;
346+
page = query.page ?? 1;
347+
perPage = query.perPage ?? 100;
348+
orderBy = query.orderBy ?? 'asc';
328349
} else {
329350
throw new Error('Got unsupported query type.');
330351
}
331-
const orderBy: 'asc' | 'desc' = 'asc';
332-
const data = await fetch(
333-
`${this.endpoint.url}/tx_search?query="${rawQuery}"&order_by="${orderBy}"`
334-
// `${this.endpoint.url}/tx_search?query="${rawQuery}"&order_by="${orderBy}"&page=1&per_page=100`
335-
);
352+
353+
const params = new URLSearchParams({
354+
query: `"${rawQuery}"`,
355+
prove: prove.toString(),
356+
page: page.toString(),
357+
per_page: perPage.toString(),
358+
order_by: `"${orderBy}"`,
359+
});
360+
361+
const data = await fetch(`${this.endpoint.url}/tx_search?${params.toString()}`);
336362
const json = await data.json();
363+
return json['result'];
364+
}
365+
366+
async searchBlock(query: SearchBlockQuery): Promise<any> {
367+
let rawQuery: string;
368+
let page = 1;
369+
let perPage = 100;
370+
let orderBy: 'asc' | 'desc' = 'asc';
337371

338-
const { txs }: SearchTxResponse = json['result'];
339-
return txs.map((tx) => {
340-
return {
341-
height: Number.parseInt(tx.height),
342-
txIndex: tx.index,
343-
hash: tx.hash,
344-
code: 0,
345-
// events: tx.tx_result.tags,
346-
events: [],
347-
rawLog: tx.tx_result.log || '',
348-
tx: fromBase64(tx.tx),
349-
msgResponses: [],
350-
gasUsed: tx?.tx_result?.gas_used ? BigInt(tx.tx_result.gas_used) : 0n,
351-
gasWanted: tx?.tx_result?.gas_wanted ? BigInt(tx.tx_result.gas_wanted) : 0n,
352-
} as IndexedTx;
372+
if (typeof query === 'string') {
373+
rawQuery = query;
374+
} else if (Array.isArray(query)) {
375+
rawQuery = query.map((t) => `${t.key}=${t.value}`).join(' AND ');
376+
} else if (isSearchBlockQueryObj(query)) {
377+
if (typeof query.query === 'string') {
378+
rawQuery = query.query;
379+
} else if (Array.isArray(query.query)) {
380+
rawQuery = query.query.map((t) => `${t.key}=${t.value}`).join(' AND ');
381+
} else {
382+
throw new Error('Need to provide a valid query.');
383+
}
384+
page = query.page ?? 1;
385+
perPage = query.perPage ?? 100;
386+
orderBy = query.orderBy ?? 'asc';
387+
} else {
388+
throw new Error('Got unsupported query type.');
389+
}
390+
391+
const params = new URLSearchParams({
392+
query: `"${rawQuery}"`,
393+
page: page.toString(),
394+
per_page: perPage.toString(),
395+
order_by: `"${orderBy}"`,
353396
});
397+
398+
const data = await fetch(`${this.endpoint.url}/block_search?${params.toString()}`);
399+
const json = await data.json();
400+
return json['result'];
354401
}
355402

356403
async getBlock(height?: number): Promise<Block> {

networks/cosmos/src/types/query.ts

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,39 +66,45 @@ export type SearchTxQuery =
6666
readonly value: string;
6767
}>;
6868

69+
export interface SearchTxQueryObj {
70+
query:
71+
| string
72+
| ReadonlyArray<{
73+
readonly key: string;
74+
readonly value: string;
75+
}>;
76+
prove?: boolean;
77+
page?: number;
78+
perPage?: number;
79+
orderBy?: 'asc' | 'desc';
80+
}
81+
82+
export type SearchBlockQuery =
83+
| string
84+
| ReadonlyArray<{
85+
readonly key: string;
86+
readonly value: string;
87+
}>;
88+
89+
export interface SearchBlockQueryObj {
90+
query:
91+
| string
92+
| ReadonlyArray<{
93+
readonly key: string;
94+
readonly value: string;
95+
}>;
96+
page?: number;
97+
perPage?: number;
98+
orderBy?: 'asc' | 'desc';
99+
}
100+
69101
interface EventAttribute {
70102
key: string;
71103
value: string;
72104
/** nondeterministic */
73105
index: boolean;
74106
}
75107

76-
export interface SearchTxResponse {
77-
txs: {
78-
hash: string;
79-
height: string;
80-
index: number;
81-
tx_result: {
82-
log: string;
83-
gas_wanted: string;
84-
gas_used: string;
85-
tags: EventAttribute[];
86-
};
87-
tx: string;
88-
proof: {
89-
RootHash: string;
90-
Data: string;
91-
Proof: {
92-
total: string;
93-
index: string;
94-
leaf_hash: string;
95-
aunts: string[];
96-
};
97-
};
98-
}[];
99-
total_count: string;
100-
}
101-
102108
interface BlockHeader {
103109
version: {
104110
block: string;
@@ -183,3 +189,11 @@ export interface BlockResponse {
183189
last_commit?: any;
184190
};
185191
}
192+
193+
export function isSearchTxQueryObj(query: any): query is SearchTxQueryObj {
194+
return typeof query === 'object' && 'query' in query;
195+
}
196+
197+
export function isSearchBlockQueryObj(query: any): query is SearchBlockQueryObj {
198+
return typeof query === 'object' && 'query' in query;
199+
}

0 commit comments

Comments
 (0)