From b121eabca4b17672d457e3b25361ee919a684354 Mon Sep 17 00:00:00 2001 From: willclarktech Date: Wed, 10 Jun 2020 10:42:05 +0100 Subject: [PATCH] Update sdk38 error handling for review comments --- packages/sdk38/src/cosmosclient.searchtx.spec.ts | 7 +++---- packages/sdk38/src/cosmosclient.spec.ts | 8 +++----- packages/sdk38/src/cosmosclient.ts | 15 ++++++++------- packages/sdk38/src/restclient.spec.ts | 6 ++---- packages/sdk38/src/signingcosmosclient.spec.ts | 6 ++---- packages/sdk38/types/cosmosclient.d.ts | 12 +++++------- 6 files changed, 23 insertions(+), 31 deletions(-) diff --git a/packages/sdk38/src/cosmosclient.searchtx.spec.ts b/packages/sdk38/src/cosmosclient.searchtx.spec.ts index 8f32d20b..b1c75d60 100644 --- a/packages/sdk38/src/cosmosclient.searchtx.spec.ts +++ b/packages/sdk38/src/cosmosclient.searchtx.spec.ts @@ -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, }; } diff --git a/packages/sdk38/src/cosmosclient.spec.ts b/packages/sdk38/src/cosmosclient.spec.ts index d64b5880..47433ee9 100644 --- a/packages/sdk38/src/cosmosclient.spec.ts +++ b/packages/sdk38/src/cosmosclient.spec.ts @@ -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"); diff --git a/packages/sdk38/src/cosmosclient.ts b/packages/sdk38/src/cosmosclient.ts index 1da17f74..5b43dc97 100644 --- a/packages/sdk38/src/cosmosclient.ts +++ b/packages/sdk38/src/cosmosclient.ts @@ -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 || "", diff --git a/packages/sdk38/src/restclient.spec.ts b/packages/sdk38/src/restclient.spec.ts index 478d6ed2..bdaea11a 100644 --- a/packages/sdk38/src/restclient.spec.ts +++ b/packages/sdk38/src/restclient.spec.ts @@ -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, diff --git a/packages/sdk38/src/signingcosmosclient.spec.ts b/packages/sdk38/src/signingcosmosclient.spec.ts index cc9600e2..258c4f76 100644 --- a/packages/sdk38/src/signingcosmosclient.spec.ts +++ b/packages/sdk38/src/signingcosmosclient.spec.ts @@ -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(); diff --git a/packages/sdk38/types/cosmosclient.d.ts b/packages/sdk38/types/cosmosclient.d.ts index db2cf20b..4749f6b9 100644 --- a/packages/sdk38/types/cosmosclient.d.ts +++ b/packages/sdk38/types/cosmosclient.d.ts @@ -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; }