From 619ecbf014fc02061b351b4ee771ab4ea9fb2354 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Fri, 19 Jun 2020 13:10:00 +0200 Subject: [PATCH 1/4] Deduplicate TxsResponse --- packages/cosmwasm/src/index.ts | 2 +- packages/cosmwasm/src/restclient.spec.ts | 3 ++- packages/cosmwasm/src/restclient.ts | 19 +------------------ packages/cosmwasm/types/index.d.ts | 2 +- packages/cosmwasm/types/restclient.d.ts | 18 +----------------- 5 files changed, 6 insertions(+), 38 deletions(-) diff --git a/packages/cosmwasm/src/index.ts b/packages/cosmwasm/src/index.ts index 53b22ee4..801e29f9 100644 --- a/packages/cosmwasm/src/index.ts +++ b/packages/cosmwasm/src/index.ts @@ -1,7 +1,7 @@ import * as logs from "./logs"; export { logs }; -export { RestClient, TxsResponse } from "./restclient"; +export { RestClient } from "./restclient"; export { Account, Block, diff --git a/packages/cosmwasm/src/restclient.spec.ts b/packages/cosmwasm/src/restclient.spec.ts index 39a2a765..8d6d48c6 100644 --- a/packages/cosmwasm/src/restclient.spec.ts +++ b/packages/cosmwasm/src/restclient.spec.ts @@ -15,6 +15,7 @@ import { StdFee, StdSignature, StdTx, + TxsResponse, } from "@cosmjs/sdk38"; import { assert, sleep } from "@cosmjs/utils"; import { ReadonlyDate } from "readonly-date"; @@ -28,7 +29,7 @@ import { MsgInstantiateContract, MsgStoreCode, } from "./msgs"; -import { RestClient, TxsResponse } from "./restclient"; +import { RestClient } from "./restclient"; import { SigningCosmWasmClient } from "./signingcosmwasmclient"; import cosmoshub from "./testdata/cosmoshub.json"; import { diff --git a/packages/cosmwasm/src/restclient.ts b/packages/cosmwasm/src/restclient.ts index 8da81232..06931bfd 100644 --- a/packages/cosmwasm/src/restclient.ts +++ b/packages/cosmwasm/src/restclient.ts @@ -1,5 +1,5 @@ import { fromBase64, fromUtf8, toHex, toUtf8 } from "@cosmjs/encoding"; -import { BroadcastMode, CosmosSdkTx, RestClient as BaseRestClient } from "@cosmjs/sdk38"; +import { BroadcastMode, RestClient as BaseRestClient } from "@cosmjs/sdk38"; import { JsonObject, Model, parseWasmData, WasmData } from "./types"; @@ -17,23 +17,6 @@ interface WasmError { readonly error: string; } -export interface TxsResponse { - readonly height: string; - readonly txhash: string; - /** 🤷‍♂️ */ - readonly codespace?: string; - /** Falsy when transaction execution succeeded. Contains error code on error. */ - readonly code?: number; - readonly raw_log: string; - readonly logs?: object; - readonly tx: CosmosSdkTx; - /** The gas limit as set by the user */ - readonly gas_wanted?: string; - /** The gas used by the execution */ - readonly gas_used?: string; - readonly timestamp: string; -} - export interface CodeInfo { readonly id: number; /** Bech32 account address */ diff --git a/packages/cosmwasm/types/index.d.ts b/packages/cosmwasm/types/index.d.ts index 047f950e..85de7876 100644 --- a/packages/cosmwasm/types/index.d.ts +++ b/packages/cosmwasm/types/index.d.ts @@ -1,6 +1,6 @@ import * as logs from "./logs"; export { logs }; -export { RestClient, TxsResponse } from "./restclient"; +export { RestClient } from "./restclient"; export { Account, Block, diff --git a/packages/cosmwasm/types/restclient.d.ts b/packages/cosmwasm/types/restclient.d.ts index 7862f463..bfee9ec9 100644 --- a/packages/cosmwasm/types/restclient.d.ts +++ b/packages/cosmwasm/types/restclient.d.ts @@ -1,21 +1,5 @@ -import { BroadcastMode, CosmosSdkTx, RestClient as BaseRestClient } from "@cosmjs/sdk38"; +import { BroadcastMode, RestClient as BaseRestClient } from "@cosmjs/sdk38"; import { JsonObject, Model } from "./types"; -export interface TxsResponse { - readonly height: string; - readonly txhash: string; - /** 🤷‍♂️ */ - readonly codespace?: string; - /** Falsy when transaction execution succeeded. Contains error code on error. */ - readonly code?: number; - readonly raw_log: string; - readonly logs?: object; - readonly tx: CosmosSdkTx; - /** The gas limit as set by the user */ - readonly gas_wanted?: string; - /** The gas used by the execution */ - readonly gas_used?: string; - readonly timestamp: string; -} export interface CodeInfo { readonly id: number; /** Bech32 account address */ From 9f49598ff47f864b9ace1a94981b2fc5c57616ba Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Fri, 19 Jun 2020 13:20:29 +0200 Subject: [PATCH 2/4] Use documented way to enable @typescript-eslint/no-unused-vars --- .eslintrc.js | 1 + 1 file changed, 1 insertion(+) diff --git a/.eslintrc.js b/.eslintrc.js index 8576e84b..ae6c05b0 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -25,6 +25,7 @@ module.exports = { "no-console": ["warn", { allow: ["error", "info", "warn"] }], "no-param-reassign": "warn", "no-shadow": "warn", + "no-unused-vars": "off", // disabled in favour of @typescript-eslint/no-unused-vars, see https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-unused-vars.md "prefer-const": "warn", "radix": ["warn", "always"], "spaced-comment": ["warn", "always", { line: { markers: ["/ Date: Fri, 19 Jun 2020 13:21:15 +0200 Subject: [PATCH 3/4] Remove Wasm types from @cosmjs/sdk38 --- packages/sdk38/src/restclient.ts | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/packages/sdk38/src/restclient.ts b/packages/sdk38/src/restclient.ts index 7fbdf8f8..7404c807 100644 --- a/packages/sdk38/src/restclient.ts +++ b/packages/sdk38/src/restclient.ts @@ -102,20 +102,6 @@ export interface AuthAccountsResponse { }; } -// Currently all wasm query responses return json-encoded strings... -// later deprecate this and use the specific types for result -// (assuming it is inlined, no second parse needed) -type WasmResponse = WasmSuccess | WasmError; - -interface WasmSuccess { - readonly height: string; - readonly result: T; -} - -interface WasmError { - readonly error: string; -} - export interface TxsResponse { readonly height: string; readonly txhash: string; From 2728f25c36d761cc561163d0984f1eb3b349de96 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Fri, 19 Jun 2020 13:29:28 +0200 Subject: [PATCH 4/4] Let encodeTx return type EncodeTxResponse unprocessed --- packages/cosmwasm/src/cosmwasmclient.ts | 4 ++-- packages/cosmwasm/src/restclient.spec.ts | 9 --------- packages/sdk38/src/cosmosclient.ts | 4 ++-- packages/sdk38/src/index.ts | 1 + packages/sdk38/src/restclient.spec.ts | 8 ++++++-- packages/sdk38/src/restclient.ts | 10 +++++----- packages/sdk38/types/index.d.ts | 1 + packages/sdk38/types/restclient.d.ts | 7 ++++++- 8 files changed, 23 insertions(+), 21 deletions(-) diff --git a/packages/cosmwasm/src/cosmwasmclient.ts b/packages/cosmwasm/src/cosmwasmclient.ts index e8a17b4d..3ab253d7 100644 --- a/packages/cosmwasm/src/cosmwasmclient.ts +++ b/packages/cosmwasm/src/cosmwasmclient.ts @@ -199,8 +199,8 @@ export class CosmWasmClient { */ public async getIdentifier(tx: CosmosSdkTx): Promise { // We consult the REST API because we don't have a local amino encoder - const bytes = await this.restClient.encodeTx(tx); - const hash = new Sha256(bytes).digest(); + const response = await this.restClient.encodeTx(tx); + const hash = new Sha256(fromBase64(response.tx)).digest(); return toHex(hash).toUpperCase(); } diff --git a/packages/cosmwasm/src/restclient.spec.ts b/packages/cosmwasm/src/restclient.spec.ts index 8d6d48c6..955565d3 100644 --- a/packages/cosmwasm/src/restclient.spec.ts +++ b/packages/cosmwasm/src/restclient.spec.ts @@ -31,7 +31,6 @@ import { } from "./msgs"; import { RestClient } from "./restclient"; import { SigningCosmWasmClient } from "./signingcosmwasmclient"; -import cosmoshub from "./testdata/cosmoshub.json"; import { alice, bech32AddressMatcher, @@ -747,14 +746,6 @@ describe("RestClient", () => { }); }); - describe("encodeTx", () => { - it("works for cosmoshub example", async () => { - pendingWithoutWasmd(); - const client = new RestClient(wasmd.endpoint); - expect(await client.encodeTx(cosmoshub.tx)).toEqual(fromBase64(cosmoshub.tx_data)); - }); - }); - describe("postTx", () => { it("can send tokens", async () => { pendingWithoutWasmd(); diff --git a/packages/sdk38/src/cosmosclient.ts b/packages/sdk38/src/cosmosclient.ts index 5b43dc97..fc6fd5b5 100644 --- a/packages/sdk38/src/cosmosclient.ts +++ b/packages/sdk38/src/cosmosclient.ts @@ -181,8 +181,8 @@ export class CosmosClient { */ public async getIdentifier(tx: CosmosSdkTx): Promise { // We consult the REST API because we don't have a local amino encoder - const bytes = await this.restClient.encodeTx(tx); - const hash = new Sha256(bytes).digest(); + const response = await this.restClient.encodeTx(tx); + const hash = new Sha256(fromBase64(response.tx)).digest(); return toHex(hash).toUpperCase(); } diff --git a/packages/sdk38/src/index.ts b/packages/sdk38/src/index.ts index 50ca53e3..332b6398 100644 --- a/packages/sdk38/src/index.ts +++ b/packages/sdk38/src/index.ts @@ -24,6 +24,7 @@ export { AuthAccountsResponse, BlockResponse, BroadcastMode, + EncodeTxResponse, PostTxsResponse, NodeInfoResponse, RestClient, diff --git a/packages/sdk38/src/restclient.spec.ts b/packages/sdk38/src/restclient.spec.ts index bdaea11a..f2544e1e 100644 --- a/packages/sdk38/src/restclient.spec.ts +++ b/packages/sdk38/src/restclient.spec.ts @@ -1,5 +1,4 @@ /* eslint-disable @typescript-eslint/camelcase */ -import { fromBase64 } from "@cosmjs/encoding"; import { assert, sleep } from "@cosmjs/utils"; import { ReadonlyDate } from "readonly-date"; @@ -523,7 +522,12 @@ describe("RestClient", () => { it("works for cosmoshub example", async () => { pendingWithoutWasmd(); const client = new RestClient(wasmd.endpoint); - expect(await client.encodeTx(cosmoshub.tx)).toEqual(fromBase64(cosmoshub.tx_data)); + const response = await client.encodeTx(cosmoshub.tx); + expect(response).toEqual( + jasmine.objectContaining({ + tx: cosmoshub.tx_data, + }), + ); }); }); diff --git a/packages/sdk38/src/restclient.ts b/packages/sdk38/src/restclient.ts index 7404c807..0cf8e62a 100644 --- a/packages/sdk38/src/restclient.ts +++ b/packages/sdk38/src/restclient.ts @@ -1,4 +1,3 @@ -import { fromBase64 } from "@cosmjs/encoding"; import { isNonNullObject } from "@cosmjs/utils"; import axios, { AxiosError, AxiosInstance } from "axios"; @@ -147,8 +146,9 @@ export interface PostTxsResponse { readonly gas_used?: string; } -interface EncodeTxResponse { - // base64-encoded amino-binary encoded representation +/** A reponse from the /txs/encode endpoint */ +export interface EncodeTxResponse { + /** base64-encoded amino-binary encoded representation */ readonly tx: string; } @@ -289,12 +289,12 @@ export class RestClient { } /** returns the amino-encoding of the transaction performed by the server */ - public async encodeTx(tx: CosmosSdkTx): Promise { + public async encodeTx(tx: CosmosSdkTx): Promise { const responseData = await this.post("/txs/encode", tx); if (!responseData.tx) { throw new Error("Unexpected response data format"); } - return fromBase64((responseData as EncodeTxResponse).tx); + return responseData as EncodeTxResponse; } /** diff --git a/packages/sdk38/types/index.d.ts b/packages/sdk38/types/index.d.ts index 7a9de872..26f37186 100644 --- a/packages/sdk38/types/index.d.ts +++ b/packages/sdk38/types/index.d.ts @@ -22,6 +22,7 @@ export { AuthAccountsResponse, BlockResponse, BroadcastMode, + EncodeTxResponse, PostTxsResponse, NodeInfoResponse, RestClient, diff --git a/packages/sdk38/types/restclient.d.ts b/packages/sdk38/types/restclient.d.ts index d27863db..830457cb 100644 --- a/packages/sdk38/types/restclient.d.ts +++ b/packages/sdk38/types/restclient.d.ts @@ -125,6 +125,11 @@ export interface PostTxsResponse { /** The gas used by the execution */ readonly gas_used?: string; } +/** A reponse from the /txs/encode endpoint */ +export interface EncodeTxResponse { + /** base64-encoded amino-binary encoded representation */ + readonly tx: string; +} /** * The mode used to send transaction * @@ -162,7 +167,7 @@ export declare class RestClient { txById(id: string): Promise; txsQuery(query: string): Promise; /** returns the amino-encoding of the transaction performed by the server */ - encodeTx(tx: CosmosSdkTx): Promise; + encodeTx(tx: CosmosSdkTx): Promise; /** * Broadcasts a signed transaction to into the transaction pool. * Depending on the RestClient's broadcast mode, this might or might