mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 21:49:15 +00:00
Update cosmwasm error handling for review comments
This commit is contained in:
parent
b121eabca4
commit
8c443dd134
@ -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,
|
||||
};
|
||||
}
|
||||
|
@ -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");
|
||||
|
@ -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 || "",
|
||||
|
@ -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,
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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 {
|
||||
|
12
packages/cosmwasm/types/cosmwasmclient.d.ts
vendored
12
packages/cosmwasm/types/cosmwasmclient.d.ts
vendored
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user