cosmwasm: Separate CosmWasmClient.searchTx and .getTx

This commit is contained in:
willclarktech 2020-12-15 13:59:26 +00:00
parent cb125800bd
commit 33c525ff75
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
6 changed files with 22 additions and 60 deletions

View File

@ -6,6 +6,8 @@
@cosmjs/launchpad instead.
- @cosmjs/cosmwasm: Export `JsonObject`, `ChangeAdminResult` and `WasmData`
types as well as `isValidBuilder` and `parseWasmData` functions.
- @cosmjs/cosmwasm: Add `CosmWasmClient.getTx` method for searching by ID and
remove such functionality from `CosmWasmClient.searchTx`.
- @cosmjs/cosmwasm-stargate: Add new package for CosmWasm Stargate support.
- @cosmjs/launchpad: Add `Secp256k1Wallet` to manage a single raw secp256k1
keypair.

View File

@ -44,7 +44,7 @@ interface TestTxExecute {
readonly tx: WrappedStdTx;
}
describe("CosmWasmClient.searchTx", () => {
describe("CosmWasmClient.getTx and .searchTx", () => {
let sendSuccessful: TestTxSend | undefined;
let sendSelfSuccessful: TestTxSend | undefined;
let sendUnsuccessful: TestTxSend | undefined;
@ -147,15 +147,14 @@ describe("CosmWasmClient.searchTx", () => {
}
});
describe("with SearchByIdQuery", () => {
it("can search successful tx by ID", async () => {
describe("getTx", () => {
it("can get successful tx by ID", async () => {
pendingWithoutLaunchpad();
pendingWithoutErc20();
assert(sendSuccessful, "value must be set in beforeAll()");
const client = new CosmWasmClient(launchpad.endpoint);
const result = await client.searchTx({ id: sendSuccessful.hash });
expect(result.length).toEqual(1);
expect(result[0]).toEqual(
const result = await client.getTx(sendSuccessful.hash);
expect(result).toEqual(
jasmine.objectContaining({
height: sendSuccessful.height,
hash: sendSuccessful.hash,
@ -165,14 +164,13 @@ describe("CosmWasmClient.searchTx", () => {
);
});
it("can search unsuccessful tx by ID", async () => {
it("can get unsuccessful tx by ID", async () => {
pendingWithoutLaunchpad();
pendingWithoutErc20();
assert(sendUnsuccessful, "value must be set in beforeAll()");
const client = new CosmWasmClient(launchpad.endpoint);
const result = await client.searchTx({ id: sendUnsuccessful.hash });
expect(result.length).toEqual(1);
expect(result[0]).toEqual(
const result = await client.getTx(sendUnsuccessful.hash);
expect(result).toEqual(
jasmine.objectContaining({
height: sendUnsuccessful.height,
hash: sendUnsuccessful.hash,
@ -182,41 +180,13 @@ describe("CosmWasmClient.searchTx", () => {
);
});
it("can search by ID (non existent)", async () => {
it("can get by ID (non existent)", async () => {
pendingWithoutLaunchpad();
pendingWithoutErc20();
const client = new CosmWasmClient(launchpad.endpoint);
const nonExistentId = "0000000000000000000000000000000000000000000000000000000000000000";
const result = await client.searchTx({ id: nonExistentId });
expect(result.length).toEqual(0);
});
it("can search by ID and filter by minHeight", async () => {
pendingWithoutLaunchpad();
pendingWithoutErc20();
assert(sendSuccessful);
const client = new CosmWasmClient(launchpad.endpoint);
const query = { id: sendSuccessful.hash };
{
const result = await client.searchTx(query, { minHeight: 0 });
expect(result.length).toEqual(1);
}
{
const result = await client.searchTx(query, { minHeight: sendSuccessful.height - 1 });
expect(result.length).toEqual(1);
}
{
const result = await client.searchTx(query, { minHeight: sendSuccessful.height });
expect(result.length).toEqual(1);
}
{
const result = await client.searchTx(query, { minHeight: sendSuccessful.height + 1 });
expect(result.length).toEqual(0);
}
const result = await client.getTx(nonExistentId);
expect(result).toBeNull();
});
});

View File

@ -54,15 +54,7 @@ export interface SearchByTagsQuery {
readonly tags: ReadonlyArray<{ readonly key: string; readonly value: string }>;
}
export type SearchTxQuery =
| SearchByIdQuery
| SearchByHeightQuery
| SearchBySentFromOrToQuery
| SearchByTagsQuery;
function isSearchByIdQuery(query: SearchTxQuery): query is SearchByIdQuery {
return (query as SearchByIdQuery).id !== undefined;
}
export type SearchTxQuery = SearchByHeightQuery | SearchBySentFromOrToQuery | SearchByTagsQuery;
function isSearchByHeightQuery(query: SearchTxQuery): query is SearchByHeightQuery {
return (query as SearchByHeightQuery).height !== undefined;
@ -264,6 +256,11 @@ export class CosmWasmClient {
};
}
public async getTx(id: string): Promise<IndexedTx | null> {
const results = await this.txsQuery(`tx.hash=${id}`);
return results[0] ?? null;
}
public async searchTx(query: SearchTxQuery, filter: SearchTxFilter = {}): Promise<readonly IndexedTx[]> {
const minHeight = filter.minHeight || 0;
const maxHeight = filter.maxHeight || Number.MAX_SAFE_INTEGER;
@ -275,9 +272,7 @@ export class CosmWasmClient {
}
let txs: readonly IndexedTx[];
if (isSearchByIdQuery(query)) {
txs = await this.txsQuery(`tx.hash=${query.id}`);
} else if (isSearchByHeightQuery(query)) {
if (isSearchByHeightQuery(query)) {
// optional optimization to avoid network request
if (query.height < minHeight || query.height > maxHeight) {
txs = [];

View File

@ -14,7 +14,6 @@ export {
CosmWasmClient,
GetSequenceResult,
SearchByHeightQuery,
SearchByIdQuery,
SearchBySentFromOrToQuery,
SearchByTagsQuery,
SearchTxQuery,

View File

@ -42,11 +42,7 @@ export interface SearchByTagsQuery {
readonly value: string;
}>;
}
export declare type SearchTxQuery =
| SearchByIdQuery
| SearchByHeightQuery
| SearchBySentFromOrToQuery
| SearchByTagsQuery;
export declare type SearchTxQuery = SearchByHeightQuery | SearchBySentFromOrToQuery | SearchByTagsQuery;
export interface SearchTxFilter {
readonly minHeight?: number;
readonly maxHeight?: number;
@ -148,6 +144,7 @@ export declare class CosmWasmClient {
* @param height The height of the block. If undefined, the latest height is used.
*/
getBlock(height?: number): Promise<Block>;
getTx(id: string): Promise<IndexedTx | null>;
searchTx(query: SearchTxQuery, filter?: SearchTxFilter): Promise<readonly IndexedTx[]>;
broadcastTx(tx: StdTx): Promise<BroadcastTxResult>;
getCodes(): Promise<readonly Code[]>;

View File

@ -13,7 +13,6 @@ export {
CosmWasmClient,
GetSequenceResult,
SearchByHeightQuery,
SearchByIdQuery,
SearchBySentFromOrToQuery,
SearchByTagsQuery,
SearchTxQuery,