Update sdk38 error handling for review comments

This commit is contained in:
willclarktech
2020-06-10 10:42:05 +01:00
parent 84cf719229
commit b121eabca4
6 changed files with 23 additions and 31 deletions
@@ -1,9 +1,8 @@
/* eslint-disable @typescript-eslint/camelcase */
import { Uint53 } from "@cosmjs/math";
import { assert, sleep } from "@cosmjs/utils";
import { Coin } from "./coins";
import { CosmosClient, isPostTxFailureResult } from "./cosmosclient";
import { CosmosClient, isPostTxFailure } from "./cosmosclient";
import { makeSignBytes } from "./encoding";
import { Secp256k1Pen } from "./pen";
import { RestClient } from "./restclient";
@@ -106,12 +105,12 @@ describe("CosmosClient.searchTx", () => {
};
const transactionId = await client.getIdentifier(tx);
const result = await client.postTx(tx.value);
if (isPostTxFailureResult(result)) {
if (isPostTxFailure(result)) {
sendUnsuccessful = {
sender: faucet.address,
recipient: recipient,
hash: transactionId,
height: Uint53.fromString(result.height).toNumber(),
height: result.height,
tx: tx,
};
}
+3 -5
View File
@@ -1,8 +1,8 @@
/* eslint-disable @typescript-eslint/camelcase */
import { sleep } from "@cosmjs/utils";
import { assert, sleep } from "@cosmjs/utils";
import { ReadonlyDate } from "readonly-date";
import { CosmosClient, isPostTxFailureResult, PrivateCosmWasmClient } from "./cosmosclient";
import { CosmosClient, isPostTxFailure, PrivateCosmWasmClient } from "./cosmosclient";
import { makeSignBytes } from "./encoding";
import { findAttribute } from "./logs";
import { Secp256k1Pen } from "./pen";
@@ -231,9 +231,7 @@ describe("CosmosClient", () => {
signatures: [signature],
};
const txResult = await client.postTx(signedTx);
if (isPostTxFailureResult(txResult)) {
throw new Error("Post tx failed");
}
assert(!isPostTxFailure(txResult));
const { logs, transactionHash } = txResult;
const amountAttr = findAttribute(logs, "transfer", "amount");
expect(amountAttr.value).toEqual("1234567ucosm");
+8 -7
View File
@@ -1,5 +1,6 @@
import { Sha256 } from "@cosmjs/crypto";
import { fromBase64, fromHex, toHex } from "@cosmjs/encoding";
import { Uint53 } from "@cosmjs/math";
import { Coin } from "./coins";
import { Log, parseLogs } from "./logs";
@@ -21,15 +22,15 @@ export interface Account {
readonly sequence: number;
}
export interface PostTxFailureResult {
export interface PostTxFailure {
/** Transaction hash (might be used as transaction ID). Guaranteed to be non-empty upper-case hex */
readonly transactionHash: string;
readonly height: string;
readonly height: number;
readonly code: number;
readonly rawLog: string;
}
export interface PostTxSuccessResult {
export interface PostTxSuccess {
readonly logs: readonly Log[];
readonly rawLog: string;
/** Transaction hash (might be used as transaction ID). Guaranteed to be non-empty upper-case hex */
@@ -37,10 +38,10 @@ export interface PostTxSuccessResult {
readonly data?: Uint8Array;
}
export type PostTxResult = PostTxSuccessResult | PostTxFailureResult;
export type PostTxResult = PostTxSuccess | PostTxFailure;
export function isPostTxFailureResult(postTxResult: PostTxResult): postTxResult is PostTxFailureResult {
return (postTxResult as PostTxFailureResult).code !== undefined;
export function isPostTxFailure(postTxResult: PostTxResult): postTxResult is PostTxFailure {
return !!(postTxResult as PostTxFailure).code;
}
export interface SearchByIdQuery {
@@ -293,7 +294,7 @@ export class CosmosClient {
return result.code !== undefined
? {
height: result.height,
height: Uint53.fromString(result.height).toNumber(),
transactionHash: result.txhash,
code: result.code,
rawLog: result.raw_log || "",
+2 -4
View File
@@ -4,7 +4,7 @@ import { assert, sleep } from "@cosmjs/utils";
import { ReadonlyDate } from "readonly-date";
import { rawSecp256k1PubkeyToAddress } from "./address";
import { isPostTxFailureResult } from "./cosmosclient";
import { isPostTxFailure } from "./cosmosclient";
import { makeSignBytes } from "./encoding";
import { parseLogs } from "./logs";
import { makeCosmoshubPath, Secp256k1Pen } from "./pen";
@@ -277,9 +277,7 @@ describe("RestClient", () => {
};
const transactionId = await client.getIdentifier({ type: "cosmos-sdk/StdTx", value: signedTx });
const result = await client.postTx(signedTx);
if (!isPostTxFailureResult(result)) {
throw new Error("Post tx succeeded unexpectedly");
}
assert(isPostTxFailure(result));
unsuccessful = {
sender: faucet.address,
recipient: recipient,
@@ -1,7 +1,7 @@
import { assert } from "@cosmjs/utils";
import { Coin } from "./coins";
import { isPostTxFailureResult, PrivateCosmWasmClient } from "./cosmosclient";
import { isPostTxFailure, PrivateCosmWasmClient } from "./cosmosclient";
import { Secp256k1Pen } from "./pen";
import { SigningCosmosClient } from "./signingcosmosclient";
import { makeRandomAddress, pendingWithoutWasmd } from "./testutils.spec";
@@ -66,9 +66,7 @@ describe("SigningCosmosClient", () => {
// send
const result = await client.sendTokens(beneficiaryAddress, transferAmount, "for dinner");
if (isPostTxFailureResult(result)) {
throw new Error("Send tokens failed");
}
assert(!isPostTxFailure(result));
const [firstLog] = result.logs;
expect(firstLog).toBeTruthy();
+5 -7
View File
@@ -14,24 +14,22 @@ export interface Account {
readonly accountNumber: number;
readonly sequence: number;
}
export interface PostTxFailureResult {
export interface PostTxFailure {
/** Transaction hash (might be used as transaction ID). Guaranteed to be non-empty upper-case hex */
readonly transactionHash: string;
readonly height: string;
readonly height: number;
readonly code: number;
readonly rawLog: string;
}
export interface PostTxSuccessResult {
export interface PostTxSuccess {
readonly logs: readonly Log[];
readonly rawLog: string;
/** Transaction hash (might be used as transaction ID). Guaranteed to be non-empty upper-case hex */
readonly transactionHash: string;
readonly data?: Uint8Array;
}
export declare type PostTxResult = PostTxSuccessResult | PostTxFailureResult;
export declare function isPostTxFailureResult(
postTxResult: PostTxResult,
): postTxResult is PostTxFailureResult;
export declare type PostTxResult = PostTxSuccess | PostTxFailure;
export declare function isPostTxFailure(postTxResult: PostTxResult): postTxResult is PostTxFailure;
export interface SearchByIdQuery {
readonly id: string;
}