Expose BroadcastMode in CosmWasmClient

This commit is contained in:
Simon Warta 2020-02-18 12:08:11 +01:00
parent a9c605de51
commit af2bfbef1c
8 changed files with 39 additions and 13 deletions

View File

@ -2,7 +2,7 @@ import { Sha256 } from "@iov/crypto";
import { Encoding } from "@iov/encoding";
import { Log, parseLogs } from "./logs";
import { BlockResponse, RestClient, TxsResponse } from "./restclient";
import { BlockResponse, BroadcastMode, RestClient, TxsResponse } from "./restclient";
import { CosmosSdkAccount, CosmosSdkTx } from "./types";
export interface GetNonceResult {
@ -46,8 +46,8 @@ function isSearchBySentFromOrToQuery(query: SearchTxQuery): query is SearchBySen
export class CosmWasmClient {
protected readonly restClient: RestClient;
public constructor(url: string) {
this.restClient = new RestClient(url);
public constructor(url: string, broadcastMode = BroadcastMode.Block) {
this.restClient = new RestClient(url, broadcastMode);
}
public async chainId(): Promise<string> {

View File

@ -5,7 +5,7 @@ export { logs, types };
export { CosmosAddressBech32Prefix, encodeAddress, isValidAddress } from "./address";
export { unmarshalTx } from "./decoding";
export { makeSignBytes, marshalTx } from "./encoding";
export { RestClient, TxsResponse } from "./restclient";
export { BroadcastMode, RestClient, TxsResponse } from "./restclient";
export {
CosmWasmClient,
GetNonceResult,

View File

@ -152,7 +152,19 @@ type RestClientResponse =
| WasmResponse<string>
| WasmResponse<GetCodeResult>;
type BroadcastMode = "block" | "sync" | "async";
/**
* The mode used to send transaction
*
* @see https://cosmos.network/rpc/#/Transactions/post_txs
*/
export enum BroadcastMode {
/** Return after tx commit */
Block = "block",
/** Return afer CheckTx */
Sync = "sync",
/** Return right away */
Async = "async",
}
function isWasmError<T>(resp: WasmResponse<T>): resp is WasmError {
return (resp as WasmError).error !== undefined;
@ -194,11 +206,9 @@ function parseAxios500error(err: AxiosError): never {
export class RestClient {
private readonly client: AxiosInstance;
// From https://cosmos.network/rpc/#/ICS0/post_txs
// The supported broadcast modes include "block"(return after tx commit), "sync"(return afer CheckTx) and "async"(return right away).
private readonly mode: BroadcastMode;
public constructor(url: string, mode: BroadcastMode = "block") {
public constructor(url: string, mode = BroadcastMode.Block) {
const headers = {
post: { "Content-Type": "application/json" },
};

View File

@ -3,6 +3,7 @@ import { Encoding } from "@iov/encoding";
import { CosmWasmClient, GetNonceResult, PostTxResult } from "./cosmwasmclient";
import { makeSignBytes, marshalTx } from "./encoding";
import { findAttribute, Log } from "./logs";
import { BroadcastMode } from "./restclient";
import {
Coin,
CosmosSdkAccount,
@ -63,8 +64,9 @@ export class SigningCosmWasmClient extends CosmWasmClient {
senderAddress: string,
signCallback: SigningCallback,
customFees?: Partial<FeeTable>,
broadcastMode = BroadcastMode.Block,
) {
super(url);
super(url, broadcastMode);
this.senderAddress = senderAddress;
this.signCallback = signCallback;
this.fees = { ...defaultFees, ...(customFees || {}) };

View File

@ -1,5 +1,5 @@
import { Log } from "./logs";
import { BlockResponse, RestClient, TxsResponse } from "./restclient";
import { BlockResponse, BroadcastMode, RestClient, TxsResponse } from "./restclient";
import { CosmosSdkAccount, CosmosSdkTx } from "./types";
export interface GetNonceResult {
readonly accountNumber: number;
@ -23,7 +23,7 @@ export interface SearchBySentFromOrToQuery {
export declare type SearchTxQuery = SearchByIdQuery | SearchByHeightQuery | SearchBySentFromOrToQuery;
export declare class CosmWasmClient {
protected readonly restClient: RestClient;
constructor(url: string);
constructor(url: string, broadcastMode?: BroadcastMode);
chainId(): Promise<string>;
/**
* Returns a 32 byte upper-case hex transaction hash (typically used as the transaction ID)

View File

@ -4,7 +4,7 @@ export { logs, types };
export { CosmosAddressBech32Prefix, encodeAddress, isValidAddress } from "./address";
export { unmarshalTx } from "./decoding";
export { makeSignBytes, marshalTx } from "./encoding";
export { RestClient, TxsResponse } from "./restclient";
export { BroadcastMode, RestClient, TxsResponse } from "./restclient";
export {
CosmWasmClient,
GetNonceResult,

View File

@ -105,7 +105,19 @@ declare type RestClientResponse =
| EncodeTxResponse
| WasmResponse<string>
| WasmResponse<GetCodeResult>;
declare type BroadcastMode = "block" | "sync" | "async";
/**
* The mode used to send transaction
*
* @see https://cosmos.network/rpc/#/Transactions/post_txs
*/
export declare enum BroadcastMode {
/** Return after tx commit */
Block = "block",
/** Return afer CheckTx */
Sync = "sync",
/** Return right away */
Async = "async",
}
export declare class RestClient {
private readonly client;
private readonly mode;

View File

@ -1,5 +1,6 @@
import { CosmWasmClient, GetNonceResult, PostTxResult } from "./cosmwasmclient";
import { Log } from "./logs";
import { BroadcastMode } from "./restclient";
import { Coin, CosmosSdkAccount, StdFee, StdSignature } from "./types";
export interface SigningCallback {
(signBytes: Uint8Array): Promise<StdSignature>;
@ -22,6 +23,7 @@ export declare class SigningCosmWasmClient extends CosmWasmClient {
senderAddress: string,
signCallback: SigningCallback,
customFees?: Partial<FeeTable>,
broadcastMode?: BroadcastMode,
);
getNonce(address?: string): Promise<GetNonceResult>;
getAccount(address?: string): Promise<CosmosSdkAccount | undefined>;