stargate: Separate StargateClient.searchTx and .getTx

This commit is contained in:
willclarktech 2020-12-15 14:07:08 +00:00
parent 33c525ff75
commit 90f2e765f7
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
4 changed files with 24 additions and 48 deletions

View File

@ -201,8 +201,9 @@ describe("SigningStargateClient", () => {
await sleep(1000);
const searchResult = await client.searchTx({ id: result.transactionHash });
const tx = Tx.decode(searchResult[0].tx);
const searchResult = await client.getTx(result.transactionHash);
assert(searchResult, "Must find transaction");
const tx = Tx.decode(searchResult.tx);
// From ModifyingDirectSecp256k1HdWallet
expect(tx.body!.memo).toEqual("This was modified");
expect({ ...tx.authInfo!.fee!.amount![0] }).toEqual(coin(3000, "ucosm"));
@ -277,8 +278,9 @@ describe("SigningStargateClient", () => {
await sleep(1000);
const searchResult = await client.searchTx({ id: result.transactionHash });
const tx = Tx.decode(searchResult[0].tx);
const searchResult = await client.getTx(result.transactionHash);
assert(searchResult, "Must find transaction");
const tx = Tx.decode(searchResult.tx);
// From ModifyingSecp256k1HdWallet
expect(tx.body!.memo).toEqual("This was modified");
expect({ ...tx.authInfo!.fee!.amount![0] }).toEqual(coin(3000, "ucosm"));

View File

@ -95,7 +95,7 @@ async function sendTokens(
};
}
describe("StargateClient.searchTx", () => {
describe("StargateClient.getTx and .searchTx", () => {
const registry = new Registry();
let sendUnsuccessful: TestTxSend | undefined;
@ -147,14 +147,13 @@ describe("StargateClient.searchTx", () => {
}
});
describe("with SearchByIdQuery", () => {
it("can search successful tx by ID", async () => {
describe("getTx", () => {
it("can get successful tx by ID", async () => {
pendingWithoutSimapp();
assert(sendSuccessful, "value must be set in beforeAll()");
const client = await StargateClient.connect(simapp.tendermintUrl);
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,
@ -164,13 +163,12 @@ describe("StargateClient.searchTx", () => {
);
});
it("can search unsuccessful tx by ID", async () => {
it("can get unsuccessful tx by ID", async () => {
pendingWithoutSimapp();
assert(sendUnsuccessful, "value must be set in beforeAll()");
const client = await StargateClient.connect(simapp.tendermintUrl);
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,
@ -180,39 +178,12 @@ describe("StargateClient.searchTx", () => {
);
});
it("can search by ID (non existent)", async () => {
it("can get by ID (non existent)", async () => {
pendingWithoutSimapp();
const client = await StargateClient.connect(simapp.tendermintUrl);
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 () => {
pendingWithoutSimapp();
assert(sendSuccessful, "value must be set in beforeAll()");
const client = await StargateClient.connect(simapp.tendermintUrl);
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

@ -4,7 +4,6 @@ import {
Block,
Coin,
isSearchByHeightQuery,
isSearchByIdQuery,
isSearchBySentFromOrToQuery,
isSearchByTagsQuery,
PubKey,
@ -207,6 +206,11 @@ export class StargateClient {
return balances.map(coinFromProto);
}
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;
@ -219,9 +223,7 @@ export class StargateClient {
let txs: readonly IndexedTx[];
if (isSearchByIdQuery(query)) {
txs = await this.txsQuery(`tx.hash='${query.id}'`);
} else if (isSearchByHeightQuery(query)) {
if (isSearchByHeightQuery(query)) {
txs =
query.height >= minHeight && query.height <= maxHeight
? await this.txsQuery(`tx.height=${query.height}`)

View File

@ -72,6 +72,7 @@ export declare class StargateClient {
* proofs from such a method.
*/
getAllBalancesUnverified(address: string): Promise<readonly Coin[]>;
getTx(id: string): Promise<IndexedTx | null>;
searchTx(query: SearchTxQuery, filter?: SearchTxFilter): Promise<readonly IndexedTx[]>;
disconnect(): void;
broadcastTx(tx: Uint8Array): Promise<BroadcastTxResponse>;