Add assertIsPostTxSuccess
This commit is contained in:
parent
09ad90c9af
commit
31ca949a8c
@ -18,3 +18,5 @@
|
||||
`Uint64.fromNumber(1.1)` produced some result.
|
||||
- @cosmjs/sdk38: Add `SigningCosmosClient.signAndPost` as a mid-level
|
||||
abstraction between `SigningCosmosClient.sendTokens` and `.postTx`.
|
||||
- @cosmjs/sdk38: Export `PostTxFailure`/`PostTxSuccess` and type checkers
|
||||
`isPostTxFailure`/`isPostTxSuccess`; export `assertIsPostTxSuccess`.
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import { assert, sleep } from "@cosmjs/utils";
|
||||
import { sleep } from "@cosmjs/utils";
|
||||
import { ReadonlyDate } from "readonly-date";
|
||||
|
||||
import { CosmosClient, isPostTxFailure, PrivateCosmWasmClient } from "./cosmosclient";
|
||||
import { assertIsPostTxSuccess, CosmosClient, PrivateCosmWasmClient } from "./cosmosclient";
|
||||
import { makeSignBytes } from "./encoding";
|
||||
import { findAttribute } from "./logs";
|
||||
import { MsgSend } from "./msgs";
|
||||
@ -234,7 +234,7 @@ describe("CosmosClient", () => {
|
||||
signatures: [signature],
|
||||
};
|
||||
const txResult = await client.postTx(signedTx);
|
||||
assert(!isPostTxFailure(txResult));
|
||||
assertIsPostTxSuccess(txResult);
|
||||
const { logs, transactionHash } = txResult;
|
||||
const amountAttr = findAttribute(logs, "transfer", "amount");
|
||||
expect(amountAttr.value).toEqual("1234567ucosm");
|
||||
|
||||
@ -40,8 +40,23 @@ export interface PostTxSuccess {
|
||||
|
||||
export type PostTxResult = PostTxSuccess | PostTxFailure;
|
||||
|
||||
export function isPostTxFailure(postTxResult: PostTxResult): postTxResult is PostTxFailure {
|
||||
return !!(postTxResult as PostTxFailure).code;
|
||||
export function isPostTxFailure(result: PostTxResult): result is PostTxFailure {
|
||||
return !!(result as PostTxFailure).code;
|
||||
}
|
||||
|
||||
export function isPostTxSuccess(result: PostTxResult): result is PostTxSuccess {
|
||||
return !isPostTxFailure(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures the given result is a success. Throws a detailed error message otherwise.
|
||||
*/
|
||||
export function assertIsPostTxSuccess(result: PostTxResult): asserts result is PostTxSuccess {
|
||||
if (isPostTxFailure(result)) {
|
||||
throw new Error(
|
||||
`Error when posting tx ${result.transactionHash} at height ${result.height}. Code: ${result.code}; Raw log: ${result.rawLog}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export interface SearchByIdQuery {
|
||||
|
||||
@ -6,12 +6,17 @@ export { Coin, coin, coins } from "./coins";
|
||||
|
||||
export {
|
||||
Account,
|
||||
assertIsPostTxSuccess,
|
||||
Block,
|
||||
BlockHeader,
|
||||
CosmosClient,
|
||||
GetSequenceResult,
|
||||
IndexedTx,
|
||||
isPostTxFailure,
|
||||
isPostTxSuccess,
|
||||
PostTxFailure,
|
||||
PostTxResult,
|
||||
PostTxSuccess,
|
||||
SearchByHeightQuery,
|
||||
SearchByIdQuery,
|
||||
SearchBySentFromOrToQuery,
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import { Bech32 } from "@cosmjs/encoding";
|
||||
import { assert, sleep } from "@cosmjs/utils";
|
||||
import { sleep } from "@cosmjs/utils";
|
||||
|
||||
import { coin, coins } from "../coins";
|
||||
import { isPostTxFailure } from "../cosmosclient";
|
||||
import { assertIsPostTxSuccess } from "../cosmosclient";
|
||||
import { makeSignBytes } from "../encoding";
|
||||
import { MsgDelegate } from "../msgs";
|
||||
import { SigningCosmosClient } from "../signingcosmosclient";
|
||||
@ -55,8 +55,8 @@ describe("DistributionExtension", () => {
|
||||
signatures: [signature],
|
||||
};
|
||||
|
||||
const receipt = await client.postTx(tx);
|
||||
assert(!isPostTxFailure(receipt));
|
||||
const result = await client.postTx(tx);
|
||||
assertIsPostTxSuccess(result);
|
||||
|
||||
await sleep(75); // wait until transactions are indexed
|
||||
}
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import { assert, sleep } from "@cosmjs/utils";
|
||||
import { sleep } from "@cosmjs/utils";
|
||||
|
||||
import { coins } from "../coins";
|
||||
import { isPostTxFailure } from "../cosmosclient";
|
||||
import { assertIsPostTxSuccess } from "../cosmosclient";
|
||||
import { makeSignBytes } from "../encoding";
|
||||
import { SigningCosmosClient } from "../signingcosmosclient";
|
||||
import {
|
||||
@ -66,9 +66,9 @@ describe("GovExtension", () => {
|
||||
signatures: [proposalSignature],
|
||||
};
|
||||
|
||||
const proposalReceipt = await client.postTx(proposalTx);
|
||||
assert(!isPostTxFailure(proposalReceipt));
|
||||
proposalId = proposalReceipt.logs[0].events
|
||||
const proposalResult = await client.postTx(proposalTx);
|
||||
assertIsPostTxSuccess(proposalResult);
|
||||
proposalId = proposalResult.logs[0].events
|
||||
.find(({ type }) => type === "submit_proposal")!
|
||||
.attributes.find(({ key }) => key === "proposal_id")!.value;
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
import { assert, sleep } from "@cosmjs/utils";
|
||||
|
||||
import { coin, coins } from "../coins";
|
||||
import { isPostTxFailure } from "../cosmosclient";
|
||||
import { assertIsPostTxSuccess } from "../cosmosclient";
|
||||
import { makeSignBytes } from "../encoding";
|
||||
import { MsgDelegate, MsgUndelegate } from "../msgs";
|
||||
import { SigningCosmosClient } from "../signingcosmosclient";
|
||||
@ -56,8 +56,8 @@ describe("StakingExtension", () => {
|
||||
signatures: [signature],
|
||||
};
|
||||
|
||||
const receipt = await client.postTx(tx);
|
||||
assert(!isPostTxFailure(receipt));
|
||||
const result = await client.postTx(tx);
|
||||
assertIsPostTxSuccess(result);
|
||||
}
|
||||
{
|
||||
const msg: MsgUndelegate = {
|
||||
@ -79,8 +79,8 @@ describe("StakingExtension", () => {
|
||||
signatures: [signature],
|
||||
};
|
||||
|
||||
const receipt = await client.postTx(tx);
|
||||
assert(!isPostTxFailure(receipt));
|
||||
const result = await client.postTx(tx);
|
||||
assertIsPostTxSuccess(result);
|
||||
}
|
||||
|
||||
await sleep(75); // wait until transactions are indexed
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
import { assert } from "@cosmjs/utils";
|
||||
|
||||
import { Coin, coin, coins } from "./coins";
|
||||
import { isPostTxFailure, PrivateCosmWasmClient } from "./cosmosclient";
|
||||
import { assertIsPostTxSuccess, PrivateCosmWasmClient } from "./cosmosclient";
|
||||
import { MsgDelegate } from "./msgs";
|
||||
import { SigningCosmosClient } from "./signingcosmosclient";
|
||||
import { makeRandomAddress, pendingWithoutWasmd, validatorAddress } from "./testutils.spec";
|
||||
@ -68,7 +68,7 @@ describe("SigningCosmosClient", () => {
|
||||
|
||||
// send
|
||||
const result = await client.sendTokens(beneficiaryAddress, transferAmount, "for dinner");
|
||||
assert(!isPostTxFailure(result));
|
||||
assertIsPostTxSuccess(result);
|
||||
const [firstLog] = result.logs;
|
||||
expect(firstLog).toBeTruthy();
|
||||
|
||||
@ -98,7 +98,7 @@ describe("SigningCosmosClient", () => {
|
||||
gas: "120000", // 120k
|
||||
};
|
||||
const result = await client.signAndPost([msg], fee, "Use your power wisely");
|
||||
assert(!isPostTxFailure(result));
|
||||
assertIsPostTxSuccess(result);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
7
packages/sdk38/types/cosmosclient.d.ts
vendored
7
packages/sdk38/types/cosmosclient.d.ts
vendored
@ -29,7 +29,12 @@ export interface PostTxSuccess {
|
||||
readonly data?: Uint8Array;
|
||||
}
|
||||
export declare type PostTxResult = PostTxSuccess | PostTxFailure;
|
||||
export declare function isPostTxFailure(postTxResult: PostTxResult): postTxResult is PostTxFailure;
|
||||
export declare function isPostTxFailure(result: PostTxResult): result is PostTxFailure;
|
||||
export declare function isPostTxSuccess(result: PostTxResult): result is PostTxSuccess;
|
||||
/**
|
||||
* Ensures the given result is a success. Throws a detailed error message otherwise.
|
||||
*/
|
||||
export declare function assertIsPostTxSuccess(result: PostTxResult): asserts result is PostTxSuccess;
|
||||
export interface SearchByIdQuery {
|
||||
readonly id: string;
|
||||
}
|
||||
|
||||
5
packages/sdk38/types/index.d.ts
vendored
5
packages/sdk38/types/index.d.ts
vendored
@ -4,12 +4,17 @@ export { pubkeyToAddress, rawSecp256k1PubkeyToAddress } from "./address";
|
||||
export { Coin, coin, coins } from "./coins";
|
||||
export {
|
||||
Account,
|
||||
assertIsPostTxSuccess,
|
||||
Block,
|
||||
BlockHeader,
|
||||
CosmosClient,
|
||||
GetSequenceResult,
|
||||
IndexedTx,
|
||||
isPostTxFailure,
|
||||
isPostTxSuccess,
|
||||
PostTxFailure,
|
||||
PostTxResult,
|
||||
PostTxSuccess,
|
||||
SearchByHeightQuery,
|
||||
SearchByIdQuery,
|
||||
SearchBySentFromOrToQuery,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user