@@ -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' ;
3134import {
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 > {
0 commit comments