Expose BroadcastMode in CosmWasmClient

This commit is contained in:
Simon Warta
2020-02-18 13:25:26 +01:00
parent a9c605de51
commit af2bfbef1c
8 changed files with 39 additions and 13 deletions
+3 -3
View File
@@ -2,7 +2,7 @@ import { Sha256 } from "@iov/crypto";
import { Encoding } from "@iov/encoding"; import { Encoding } from "@iov/encoding";
import { Log, parseLogs } from "./logs"; import { Log, parseLogs } from "./logs";
import { BlockResponse, RestClient, TxsResponse } from "./restclient"; import { BlockResponse, BroadcastMode, RestClient, TxsResponse } from "./restclient";
import { CosmosSdkAccount, CosmosSdkTx } from "./types"; import { CosmosSdkAccount, CosmosSdkTx } from "./types";
export interface GetNonceResult { export interface GetNonceResult {
@@ -46,8 +46,8 @@ function isSearchBySentFromOrToQuery(query: SearchTxQuery): query is SearchBySen
export class CosmWasmClient { export class CosmWasmClient {
protected readonly restClient: RestClient; protected readonly restClient: RestClient;
public constructor(url: string) { public constructor(url: string, broadcastMode = BroadcastMode.Block) {
this.restClient = new RestClient(url); this.restClient = new RestClient(url, broadcastMode);
} }
public async chainId(): Promise<string> { public async chainId(): Promise<string> {
+1 -1
View File
@@ -5,7 +5,7 @@ export { logs, types };
export { CosmosAddressBech32Prefix, encodeAddress, isValidAddress } from "./address"; export { CosmosAddressBech32Prefix, encodeAddress, isValidAddress } from "./address";
export { unmarshalTx } from "./decoding"; export { unmarshalTx } from "./decoding";
export { makeSignBytes, marshalTx } from "./encoding"; export { makeSignBytes, marshalTx } from "./encoding";
export { RestClient, TxsResponse } from "./restclient"; export { BroadcastMode, RestClient, TxsResponse } from "./restclient";
export { export {
CosmWasmClient, CosmWasmClient,
GetNonceResult, GetNonceResult,
+14 -4
View File
@@ -152,7 +152,19 @@ type RestClientResponse =
| WasmResponse<string> | WasmResponse<string>
| WasmResponse<GetCodeResult>; | 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 { function isWasmError<T>(resp: WasmResponse<T>): resp is WasmError {
return (resp as WasmError).error !== undefined; return (resp as WasmError).error !== undefined;
@@ -194,11 +206,9 @@ function parseAxios500error(err: AxiosError): never {
export class RestClient { export class RestClient {
private readonly client: AxiosInstance; 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; private readonly mode: BroadcastMode;
public constructor(url: string, mode: BroadcastMode = "block") { public constructor(url: string, mode = BroadcastMode.Block) {
const headers = { const headers = {
post: { "Content-Type": "application/json" }, post: { "Content-Type": "application/json" },
}; };
+3 -1
View File
@@ -3,6 +3,7 @@ import { Encoding } from "@iov/encoding";
import { CosmWasmClient, GetNonceResult, PostTxResult } from "./cosmwasmclient"; import { CosmWasmClient, GetNonceResult, PostTxResult } from "./cosmwasmclient";
import { makeSignBytes, marshalTx } from "./encoding"; import { makeSignBytes, marshalTx } from "./encoding";
import { findAttribute, Log } from "./logs"; import { findAttribute, Log } from "./logs";
import { BroadcastMode } from "./restclient";
import { import {
Coin, Coin,
CosmosSdkAccount, CosmosSdkAccount,
@@ -63,8 +64,9 @@ export class SigningCosmWasmClient extends CosmWasmClient {
senderAddress: string, senderAddress: string,
signCallback: SigningCallback, signCallback: SigningCallback,
customFees?: Partial<FeeTable>, customFees?: Partial<FeeTable>,
broadcastMode = BroadcastMode.Block,
) { ) {
super(url); super(url, broadcastMode);
this.senderAddress = senderAddress; this.senderAddress = senderAddress;
this.signCallback = signCallback; this.signCallback = signCallback;
this.fees = { ...defaultFees, ...(customFees || {}) }; this.fees = { ...defaultFees, ...(customFees || {}) };
+2 -2
View File
@@ -1,5 +1,5 @@
import { Log } from "./logs"; import { Log } from "./logs";
import { BlockResponse, RestClient, TxsResponse } from "./restclient"; import { BlockResponse, BroadcastMode, RestClient, TxsResponse } from "./restclient";
import { CosmosSdkAccount, CosmosSdkTx } from "./types"; import { CosmosSdkAccount, CosmosSdkTx } from "./types";
export interface GetNonceResult { export interface GetNonceResult {
readonly accountNumber: number; readonly accountNumber: number;
@@ -23,7 +23,7 @@ export interface SearchBySentFromOrToQuery {
export declare type SearchTxQuery = SearchByIdQuery | SearchByHeightQuery | SearchBySentFromOrToQuery; export declare type SearchTxQuery = SearchByIdQuery | SearchByHeightQuery | SearchBySentFromOrToQuery;
export declare class CosmWasmClient { export declare class CosmWasmClient {
protected readonly restClient: RestClient; protected readonly restClient: RestClient;
constructor(url: string); constructor(url: string, broadcastMode?: BroadcastMode);
chainId(): Promise<string>; chainId(): Promise<string>;
/** /**
* Returns a 32 byte upper-case hex transaction hash (typically used as the transaction ID) * Returns a 32 byte upper-case hex transaction hash (typically used as the transaction ID)
+1 -1
View File
@@ -4,7 +4,7 @@ export { logs, types };
export { CosmosAddressBech32Prefix, encodeAddress, isValidAddress } from "./address"; export { CosmosAddressBech32Prefix, encodeAddress, isValidAddress } from "./address";
export { unmarshalTx } from "./decoding"; export { unmarshalTx } from "./decoding";
export { makeSignBytes, marshalTx } from "./encoding"; export { makeSignBytes, marshalTx } from "./encoding";
export { RestClient, TxsResponse } from "./restclient"; export { BroadcastMode, RestClient, TxsResponse } from "./restclient";
export { export {
CosmWasmClient, CosmWasmClient,
GetNonceResult, GetNonceResult,
+13 -1
View File
@@ -105,7 +105,19 @@ declare type RestClientResponse =
| EncodeTxResponse | EncodeTxResponse
| WasmResponse<string> | WasmResponse<string>
| WasmResponse<GetCodeResult>; | 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 { export declare class RestClient {
private readonly client; private readonly client;
private readonly mode; private readonly mode;
+2
View File
@@ -1,5 +1,6 @@
import { CosmWasmClient, GetNonceResult, PostTxResult } from "./cosmwasmclient"; import { CosmWasmClient, GetNonceResult, PostTxResult } from "./cosmwasmclient";
import { Log } from "./logs"; import { Log } from "./logs";
import { BroadcastMode } from "./restclient";
import { Coin, CosmosSdkAccount, StdFee, StdSignature } from "./types"; import { Coin, CosmosSdkAccount, StdFee, StdSignature } from "./types";
export interface SigningCallback { export interface SigningCallback {
(signBytes: Uint8Array): Promise<StdSignature>; (signBytes: Uint8Array): Promise<StdSignature>;
@@ -22,6 +23,7 @@ export declare class SigningCosmWasmClient extends CosmWasmClient {
senderAddress: string, senderAddress: string,
signCallback: SigningCallback, signCallback: SigningCallback,
customFees?: Partial<FeeTable>, customFees?: Partial<FeeTable>,
broadcastMode?: BroadcastMode,
); );
getNonce(address?: string): Promise<GetNonceResult>; getNonce(address?: string): Promise<GetNonceResult>;
getAccount(address?: string): Promise<CosmosSdkAccount | undefined>; getAccount(address?: string): Promise<CosmosSdkAccount | undefined>;