Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 69 additions & 22 deletions networks/cosmos/src/signing-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@ import {
Block,
BlockResponse,
IndexedTx,
SearchBlockQuery,
SearchTxQuery,
SearchTxResponse,
SearchTxQueryObj,
TxResponse,
isSearchBlockQueryObj,
isSearchTxQueryObj,
} from './types/query';
import {
EncodeObject,
SigningOptions,
} from './types/signing-client';


/**
* SigningClient is a client that can sign and broadcast transactions.
*/
Expand Down Expand Up @@ -319,38 +323,81 @@ export class SigningClient {
};
}

async searchTx(query: SearchTxQuery): Promise<IndexedTx[]> {
async searchTx(query: SearchTxQuery | SearchTxQueryObj): Promise<any> {
let rawQuery: string;
let prove = false;
let page = 1;
let perPage = 100;
let orderBy: 'asc' | 'desc' = 'asc';

if (typeof query === 'string') {
rawQuery = query;
} else if (Array.isArray(query)) {
rawQuery = query.map((t) => `${t.key}=${t.value}`).join(' AND ');
} else if (isSearchTxQueryObj(query)) {
if (typeof query.query === 'string') {
rawQuery = query.query;
} else if (Array.isArray(query.query)) {
rawQuery = query.query.map((t) => `${t.key}=${t.value}`).join(' AND ');
} else {
throw new Error('Need to provide a valid query.');
}
prove = query.prove ?? false;
page = query.page ?? 1;
perPage = query.perPage ?? 100;
orderBy = query.orderBy ?? 'asc';
} else {
throw new Error('Got unsupported query type.');
}
const orderBy: 'asc' | 'desc' = 'asc';
const data = await fetch(
`${this.endpoint.url}/tx_search?query="${rawQuery}"&order_by="${orderBy}"`
// `${this.endpoint.url}/tx_search?query="${rawQuery}"&order_by="${orderBy}"&page=1&per_page=100`
);

const params = new URLSearchParams({
query: `"${rawQuery}"`,
prove: prove.toString(),
page: page.toString(),
per_page: perPage.toString(),
order_by: `"${orderBy}"`,
});

const data = await fetch(`${this.endpoint.url}/tx_search?${params.toString()}`);
const json = await data.json();
return json['result'];
}

async searchBlock(query: SearchBlockQuery): Promise<any> {
let rawQuery: string;
let page = 1;
let perPage = 100;
let orderBy: 'asc' | 'desc' = 'asc';

const { txs }: SearchTxResponse = json['result'];
return txs.map((tx) => {
return {
height: Number.parseInt(tx.height),
txIndex: tx.index,
hash: tx.hash,
code: 0,
// events: tx.tx_result.tags,
events: [],
rawLog: tx.tx_result.log || '',
tx: fromBase64(tx.tx),
msgResponses: [],
gasUsed: tx?.tx_result?.gas_used ? BigInt(tx.tx_result.gas_used) : 0n,
gasWanted: tx?.tx_result?.gas_wanted ? BigInt(tx.tx_result.gas_wanted) : 0n,
} as IndexedTx;
if (typeof query === 'string') {
rawQuery = query;
} else if (Array.isArray(query)) {
rawQuery = query.map((t) => `${t.key}=${t.value}`).join(' AND ');
} else if (isSearchBlockQueryObj(query)) {
if (typeof query.query === 'string') {
rawQuery = query.query;
} else if (Array.isArray(query.query)) {
rawQuery = query.query.map((t) => `${t.key}=${t.value}`).join(' AND ');
} else {
throw new Error('Need to provide a valid query.');
}
page = query.page ?? 1;
perPage = query.perPage ?? 100;
orderBy = query.orderBy ?? 'asc';
} else {
throw new Error('Got unsupported query type.');
}

const params = new URLSearchParams({
query: `"${rawQuery}"`,
page: page.toString(),
per_page: perPage.toString(),
order_by: `"${orderBy}"`,
});

const data = await fetch(`${this.endpoint.url}/block_search?${params.toString()}`);
const json = await data.json();
return json['result'];
}

async getBlock(height?: number): Promise<Block> {
Expand Down
66 changes: 40 additions & 26 deletions networks/cosmos/src/types/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,39 +66,45 @@ export type SearchTxQuery =
readonly value: string;
}>;

export interface SearchTxQueryObj {
query:
| string
| ReadonlyArray<{
readonly key: string;
readonly value: string;
}>;
prove?: boolean;
page?: number;
perPage?: number;
orderBy?: 'asc' | 'desc';
}

export type SearchBlockQuery =
| string
| ReadonlyArray<{
readonly key: string;
readonly value: string;
}>;

export interface SearchBlockQueryObj {
query:
| string
| ReadonlyArray<{
readonly key: string;
readonly value: string;
}>;
page?: number;
perPage?: number;
orderBy?: 'asc' | 'desc';
}

interface EventAttribute {
key: string;
value: string;
/** nondeterministic */
index: boolean;
}

export interface SearchTxResponse {
txs: {
hash: string;
height: string;
index: number;
tx_result: {
log: string;
gas_wanted: string;
gas_used: string;
tags: EventAttribute[];
};
tx: string;
proof: {
RootHash: string;
Data: string;
Proof: {
total: string;
index: string;
leaf_hash: string;
aunts: string[];
};
};
}[];
total_count: string;
}

interface BlockHeader {
version: {
block: string;
Expand Down Expand Up @@ -183,3 +189,11 @@ export interface BlockResponse {
last_commit?: any;
};
}

export function isSearchTxQueryObj(query: any): query is SearchTxQueryObj {
return typeof query === 'object' && 'query' in query;
}

export function isSearchBlockQueryObj(query: any): query is SearchBlockQueryObj {
return typeof query === 'object' && 'query' in query;
}
Loading