From 8c443dd134b7a0f9f44765389318a4b70badb418 Mon Sep 17 00:00:00 2001 From: willclarktech Date: Wed, 10 Jun 2020 10:42:30 +0100 Subject: [PATCH] Update cosmwasm error handling for review comments --- .../cosmwasm/src/cosmwasmclient.searchtx.spec.ts | 7 +++---- packages/cosmwasm/src/cosmwasmclient.spec.ts | 6 ++---- packages/cosmwasm/src/cosmwasmclient.ts | 15 ++++++++------- packages/cosmwasm/src/restclient.spec.ts | 6 ++---- .../cosmwasm/src/signingcosmwasmclient.spec.ts | 6 ++---- packages/cosmwasm/src/signingcosmwasmclient.ts | 12 ++++++------ packages/cosmwasm/types/cosmwasmclient.d.ts | 12 +++++------- 7 files changed, 28 insertions(+), 36 deletions(-) diff --git a/packages/cosmwasm/src/cosmwasmclient.searchtx.spec.ts b/packages/cosmwasm/src/cosmwasmclient.searchtx.spec.ts index 66e3d76d..9ed9a979 100644 --- a/packages/cosmwasm/src/cosmwasmclient.searchtx.spec.ts +++ b/packages/cosmwasm/src/cosmwasmclient.searchtx.spec.ts @@ -1,9 +1,8 @@ /* eslint-disable @typescript-eslint/camelcase */ -import { Uint53 } from "@cosmjs/math"; import { Coin, CosmosSdkTx, isMsgSend, makeSignBytes, MsgSend, Secp256k1Pen } from "@cosmjs/sdk38"; import { assert, sleep } from "@cosmjs/utils"; -import { CosmWasmClient, isPostTxFailureResult } from "./cosmwasmclient"; +import { CosmWasmClient, isPostTxFailure } from "./cosmwasmclient"; import { isMsgExecuteContract, isMsgInstantiateContract } from "./msgs"; import { RestClient } from "./restclient"; import { SigningCosmWasmClient } from "./signingcosmwasmclient"; @@ -114,12 +113,12 @@ describe("CosmWasmClient.searchTx", () => { }; const transactionId = await client.getIdentifier(tx); const result = await client.postTx(tx.value); - if (isPostTxFailureResult(result)) { + if (isPostTxFailure(result)) { sendUnsuccessful = { sender: alice.address0, recipient: recipient, hash: transactionId, - height: Uint53.fromString(result.height).toNumber(), + height: result.height, tx: tx, }; } diff --git a/packages/cosmwasm/src/cosmwasmclient.spec.ts b/packages/cosmwasm/src/cosmwasmclient.spec.ts index 9cea0772..1898982e 100644 --- a/packages/cosmwasm/src/cosmwasmclient.spec.ts +++ b/packages/cosmwasm/src/cosmwasmclient.spec.ts @@ -5,7 +5,7 @@ import { makeSignBytes, MsgSend, Secp256k1Pen, StdFee } from "@cosmjs/sdk38"; import { assert, sleep } from "@cosmjs/utils"; import { ReadonlyDate } from "readonly-date"; -import { Code, CosmWasmClient, isPostTxFailureResult, PrivateCosmWasmClient } from "./cosmwasmclient"; +import { Code, CosmWasmClient, isPostTxFailure, PrivateCosmWasmClient } from "./cosmwasmclient"; import { findAttribute } from "./logs"; import { SigningCosmWasmClient } from "./signingcosmwasmclient"; import cosmoshub from "./testdata/cosmoshub.json"; @@ -243,9 +243,7 @@ describe("CosmWasmClient", () => { signatures: [signature], }; const result = await client.postTx(signedTx); - if (isPostTxFailureResult(result)) { - throw new Error("Post tx failed"); - } + assert(!isPostTxFailure(result)); const { logs, transactionHash } = result; const amountAttr = findAttribute(logs, "transfer", "amount"); expect(amountAttr.value).toEqual("1234567ucosm"); diff --git a/packages/cosmwasm/src/cosmwasmclient.ts b/packages/cosmwasm/src/cosmwasmclient.ts index f7d16715..45f88de7 100644 --- a/packages/cosmwasm/src/cosmwasmclient.ts +++ b/packages/cosmwasm/src/cosmwasmclient.ts @@ -1,5 +1,6 @@ import { Sha256 } from "@cosmjs/crypto"; import { fromBase64, fromHex, toHex } from "@cosmjs/encoding"; +import { Uint53 } from "@cosmjs/math"; import { BroadcastMode, Coin, @@ -28,15 +29,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 */ @@ -44,10 +45,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 { @@ -311,7 +312,7 @@ export class CosmWasmClient { 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/cosmwasm/src/restclient.spec.ts b/packages/cosmwasm/src/restclient.spec.ts index 16a64076..39a2a765 100644 --- a/packages/cosmwasm/src/restclient.spec.ts +++ b/packages/cosmwasm/src/restclient.spec.ts @@ -19,7 +19,7 @@ import { import { assert, sleep } from "@cosmjs/utils"; import { ReadonlyDate } from "readonly-date"; -import { isPostTxFailureResult } from "./cosmwasmclient"; +import { isPostTxFailure } from "./cosmwasmclient"; import { findAttribute, parseLogs } from "./logs"; import { isMsgInstantiateContract, @@ -402,9 +402,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: alice.address0, recipient: recipient, diff --git a/packages/cosmwasm/src/signingcosmwasmclient.spec.ts b/packages/cosmwasm/src/signingcosmwasmclient.spec.ts index ad8d9330..92a1489a 100644 --- a/packages/cosmwasm/src/signingcosmwasmclient.spec.ts +++ b/packages/cosmwasm/src/signingcosmwasmclient.spec.ts @@ -3,7 +3,7 @@ import { toHex } from "@cosmjs/encoding"; import { Coin, Secp256k1Pen } from "@cosmjs/sdk38"; import { assert } from "@cosmjs/utils"; -import { isPostTxFailureResult, PrivateCosmWasmClient } from "./cosmwasmclient"; +import { isPostTxFailure, PrivateCosmWasmClient } from "./cosmwasmclient"; import { RestClient } from "./restclient"; import { SigningCosmWasmClient, UploadMeta } from "./signingcosmwasmclient"; import { alice, getHackatom, makeRandomAddress, pendingWithoutWasmd } from "./testutils.spec"; @@ -205,9 +205,7 @@ describe("SigningCosmWasmClient", () => { // 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/cosmwasm/src/signingcosmwasmclient.ts b/packages/cosmwasm/src/signingcosmwasmclient.ts index 92690c19..3e52b236 100644 --- a/packages/cosmwasm/src/signingcosmwasmclient.ts +++ b/packages/cosmwasm/src/signingcosmwasmclient.ts @@ -8,8 +8,8 @@ import { Account, CosmWasmClient, GetNonceResult, - isPostTxFailureResult, - PostTxFailureResult, + isPostTxFailure, + PostTxFailure, PostTxResult, } from "./cosmwasmclient"; import { findAttribute, Log } from "./logs"; @@ -91,7 +91,7 @@ export interface ExecuteResult { readonly transactionHash: string; } -function createPostTxErrorMessage(result: PostTxFailureResult): string { +function createPostTxErrorMessage(result: PostTxFailure): string { return `Error when posting tx ${result.transactionHash} at height ${result.height}. Code: ${result.code}; Raw log: ${result.rawLog}`; } @@ -165,7 +165,7 @@ export class SigningCosmWasmClient extends CosmWasmClient { }; const result = await this.postTx(signedTx); - if (isPostTxFailureResult(result)) { + if (isPostTxFailure(result)) { throw new Error(createPostTxErrorMessage(result)); } const codeIdAttr = findAttribute(result.logs, "message", "code_id"); @@ -214,7 +214,7 @@ export class SigningCosmWasmClient extends CosmWasmClient { }; const result = await this.postTx(signedTx); - if (isPostTxFailureResult(result)) { + if (isPostTxFailure(result)) { throw new Error(createPostTxErrorMessage(result)); } const contractAddressAttr = findAttribute(result.logs, "message", "contract_address"); @@ -254,7 +254,7 @@ export class SigningCosmWasmClient extends CosmWasmClient { }; const result = await this.postTx(signedTx); - if (isPostTxFailureResult(result)) { + if (isPostTxFailure(result)) { throw new Error(createPostTxErrorMessage(result)); } return { diff --git a/packages/cosmwasm/types/cosmwasmclient.d.ts b/packages/cosmwasm/types/cosmwasmclient.d.ts index 6ce66de0..0dad3fa0 100644 --- a/packages/cosmwasm/types/cosmwasmclient.d.ts +++ b/packages/cosmwasm/types/cosmwasmclient.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; }