Skip to content

Commit

Permalink
Merge pull request #1284 from cosmos/fix-cosmwasm-msg-types-simon
Browse files Browse the repository at this point in the history
Fix the cosmwasm msg types
  • Loading branch information
webmaster128 committed Oct 10, 2022
2 parents e51ef11 + f0ac2f1 commit e4aa7f8
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 9 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,17 @@ and this project adheres to
- @cosmjs/utils: Add `isDefined` which checks for `undefined` in a
TypeScript-friendly way.

### Fixed

- @cosmjs/cosmwasm-stargate: Use type `JsonObject = any` for smart query
requests and messages (in `WasmExtension.wasm.queryContractSmart`,
`CosmWasmClient.queryContractSmart`, `SigningCosmWasmClient.instantiate`,
`SigningCosmWasmClient.migrate`, `SigningCosmWasmClient.execute`). This
reverts the type change done in CosmJS 0.23.0. ([#1281], [#1284])

[#1281]: https://github.com/cosmos/cosmjs/pull/1281
[#1284]: https://github.com/cosmos/cosmjs/pull/1284

## [0.29.1] - 2022-10-09

### Changed
Expand Down
8 changes: 8 additions & 0 deletions packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts
Expand Up @@ -418,6 +418,14 @@ describe("CosmWasmClient", () => {
const client = await CosmWasmClient.connect(wasmd.endpoint);
const result = await client.queryContractSmart(contract.address, { verifier: {} });
expect(result).toEqual({ verifier: contract.instantiateMsg.verifier });

// Typed request (https://github.com/cosmos/cosmjs/pull/1281)
interface VerifierQuery {
verifier: Record<string, never>;
}
const request: VerifierQuery = { verifier: {} };
const result2 = await client.queryContractSmart(contract.address, request);
expect(result2).toEqual({ verifier: contract.instantiateMsg.verifier });
});

it("errors for malformed query message", async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/cosmwasm-stargate/src/cosmwasmclient.ts
Expand Up @@ -71,7 +71,7 @@ export interface ContractCodeHistoryEntry {
/** The source of this history entry */
readonly operation: "Genesis" | "Init" | "Migrate";
readonly codeId: number;
readonly msg: Record<string, unknown>;
readonly msg: JsonObject;
}

/** Use for testing only */
Expand Down Expand Up @@ -439,7 +439,7 @@ export class CosmWasmClient {
* Promise is rejected for invalid query format.
* Promise is rejected for invalid response format.
*/
public async queryContractSmart(address: string, queryMsg: Record<string, unknown>): Promise<JsonObject> {
public async queryContractSmart(address: string, queryMsg: JsonObject): Promise<JsonObject> {
try {
return await this.forceGetQueryClient().wasm.queryContractSmart(address, queryMsg);
} catch (error) {
Expand Down
3 changes: 2 additions & 1 deletion packages/cosmwasm-stargate/src/modules/wasm/queries.spec.ts
Expand Up @@ -35,6 +35,7 @@ import {
MsgStoreCodeEncodeObject,
wasmTypes,
} from "./messages";
import { JsonObject } from "./queries";

const registry = new Registry(wasmTypes);

Expand Down Expand Up @@ -100,7 +101,7 @@ async function instantiateContract(
async function executeContract(
signer: OfflineDirectSigner,
contractAddress: string,
msg: Record<string, unknown>,
msg: JsonObject,
): Promise<DeliverTxResponse> {
const memo = "Time for action";
const theMsg: MsgExecuteContractEncodeObject = {
Expand Down
4 changes: 2 additions & 2 deletions packages/cosmwasm-stargate/src/modules/wasm/queries.ts
Expand Up @@ -61,7 +61,7 @@ export interface WasmExtension {
* Makes a smart query on the contract and parses the response as JSON.
* Throws error if no such contract exists, the query format is invalid or the response is invalid.
*/
readonly queryContractSmart: (address: string, query: Record<string, unknown>) => Promise<JsonObject>;
readonly queryContractSmart: (address: string, query: JsonObject) => Promise<JsonObject>;
};
}

Expand Down Expand Up @@ -116,7 +116,7 @@ export function setupWasmExtension(base: QueryClient): WasmExtension {
return queryService.RawContractState(request);
},

queryContractSmart: async (address: string, query: Record<string, unknown>) => {
queryContractSmart: async (address: string, query: JsonObject) => {
const request = { address: address, queryData: toUtf8(JSON.stringify(query)) };
const { data } = await queryService.SmartContractState(request);
// By convention, smart queries must return a valid JSON document (see https://github.com/CosmWasm/cosmwasm/issues/144)
Expand Down
9 changes: 5 additions & 4 deletions packages/cosmwasm-stargate/src/signingcosmwasmclient.ts
Expand Up @@ -50,6 +50,7 @@ import pako from "pako";
import { CosmWasmClient } from "./cosmwasmclient";
import {
createWasmAminoConverters,
JsonObject,
MsgClearAdminEncodeObject,
MsgExecuteContractEncodeObject,
MsgInstantiateContractEncodeObject,
Expand Down Expand Up @@ -137,7 +138,7 @@ export interface MigrateResult {

export interface ExecuteInstruction {
contractAddress: string;
msg: Record<string, unknown>;
msg: JsonObject;
funds?: readonly Coin[];
}

Expand Down Expand Up @@ -278,7 +279,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
public async instantiate(
senderAddress: string,
codeId: number,
msg: Record<string, unknown>,
msg: JsonObject,
label: string,
fee: StdFee | "auto" | number,
options: InstantiateOptions = {},
Expand Down Expand Up @@ -368,7 +369,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
senderAddress: string,
contractAddress: string,
codeId: number,
migrateMsg: Record<string, unknown>,
migrateMsg: JsonObject,
fee: StdFee | "auto" | number,
memo = "",
): Promise<MigrateResult> {
Expand Down Expand Up @@ -397,7 +398,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
public async execute(
senderAddress: string,
contractAddress: string,
msg: Record<string, unknown>,
msg: JsonObject,
fee: StdFee | "auto" | number,
memo = "",
funds?: readonly Coin[],
Expand Down

0 comments on commit e4aa7f8

Please sign in to comment.