Create and use BroadcastTxError

This commit is contained in:
Simon Warta 2022-08-10 18:45:03 +02:00
parent 40686bc105
commit 775e433c2d
5 changed files with 38 additions and 8 deletions

View File

@ -24,8 +24,11 @@ and this project adheres to
- @cosmjs/stargate: Add support for `MsgVoteWeighted` (register by default and
create Amino JSON converters) ([#1224]).
- @cosmjs/stargate: Add Amino JSON support for `MsgCreateVestingAccount`.
- @cosmjs/stargate and @cosmjs/cosmwasm-stargate: Create and use
BroadcastTxError ([#1096]).
[#1072]: https://github.com/cosmos/cosmjs/issues/1072
[#1096]: https://github.com/cosmos/cosmjs/issues/1096
[#1154]: https://github.com/cosmos/cosmjs/issues/1154
[#1176]: https://github.com/cosmos/cosmjs/pull/1176
[#1224]: https://github.com/cosmos/cosmjs/pull/1224

View File

@ -7,6 +7,7 @@ import {
AuthExtension,
BankExtension,
Block,
BroadcastTxError,
Coin,
DeliverTxResponse,
IndexedTx,
@ -290,8 +291,8 @@ export class CosmWasmClient {
const broadcasted = await this.forceGetTmClient().broadcastTxSync({ tx });
if (broadcasted.code) {
throw new Error(
`Broadcasting transaction failed with code ${broadcasted.code} (codespace: ${broadcasted.codeSpace}). Log: ${broadcasted.log}`,
return Promise.reject(
new BroadcastTxError(broadcasted.code, broadcasted.codeSpace ?? "", broadcasted.log),
);
}
const transactionId = toHex(broadcasted.hash).toUpperCase();

View File

@ -119,6 +119,7 @@ export {
assertIsDeliverTxSuccess,
Block,
BlockHeader,
BroadcastTxError,
DeliverTxResponse,
IndexedTx,
isDeliverTxFailure,

View File

@ -15,6 +15,7 @@ import { ReadonlyDate } from "readonly-date";
import {
assertIsDeliverTxSuccess,
BroadcastTxError,
isDeliverTxFailure,
isDeliverTxSuccess,
PrivateStargateClient,
@ -432,9 +433,17 @@ describe("StargateClient", () => {
});
const txRawBytes = Uint8Array.from(TxRaw.encode(txRaw).finish());
await expectAsync(client.broadcastTx(txRawBytes)).toBeRejectedWithError(
simapp44Enabled() ? /invalid recipient address/i : /Broadcasting transaction failed with code 7/i,
);
try {
await client.broadcastTx(txRawBytes);
assert(false, "Expected broadcastTx to throw");
} catch (error: any) {
expect(error).toMatch(
simapp44Enabled() ? /invalid recipient address/i : /Broadcasting transaction failed with code 7/i,
);
assert(error instanceof BroadcastTxError);
expect(error.code).toEqual(7);
expect(error.codespace).toEqual("sdk");
}
client.disconnect();
});

View File

@ -134,6 +134,24 @@ export function assertIsDeliverTxFailure(result: DeliverTxResponse): void {
}
}
/**
* An error when broadcasting the transaction. This contains the CheckTx errors
* from the blockchain. Once a transaction is included in a block no BroadcastTxError
* is thrown, even if the execution fails (DeliverTx errors).
*/
export class BroadcastTxError extends Error {
public readonly code: number;
public readonly codespace: string;
public readonly log: string | undefined;
public constructor(code: number, codespace: string, log: string | undefined) {
super(`Broadcasting transaction failed with code ${code} (codespace: ${codespace}). Log: ${log}`);
this.code = code;
this.codespace = codespace;
this.log = log;
}
}
/** Use for testing only */
export interface PrivateStargateClient {
readonly tmClient: Tendermint34Client | undefined;
@ -410,9 +428,7 @@ export class StargateClient {
const broadcasted = await this.forceGetTmClient().broadcastTxSync({ tx });
if (broadcasted.code) {
return Promise.reject(
new Error(
`Broadcasting transaction failed with code ${broadcasted.code} (codespace: ${broadcasted.codeSpace}). Log: ${broadcasted.log}`,
),
new BroadcastTxError(broadcasted.code, broadcasted.codeSpace ?? "", broadcasted.log),
);
}
const transactionId = toHex(broadcasted.hash).toUpperCase();