From 981c3ac919370225a77e2f7e16abe8ee93a3843b Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 11 Mar 2021 12:16:01 +0100 Subject: [PATCH] Streamline Amino JSON type definitions and fix tests --- .../cosmwasm-stargate/src/aminotypes.spec.ts | 50 +++--- packages/cosmwasm-stargate/src/aminotypes.ts | 162 +++++++++++++----- 2 files changed, 144 insertions(+), 68 deletions(-) diff --git a/packages/cosmwasm-stargate/src/aminotypes.spec.ts b/packages/cosmwasm-stargate/src/aminotypes.spec.ts index c73bfec7..c182fe8f 100644 --- a/packages/cosmwasm-stargate/src/aminotypes.spec.ts +++ b/packages/cosmwasm-stargate/src/aminotypes.spec.ts @@ -1,18 +1,18 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { - MsgClearAdmin as LaunchpadMsgClearAdmin, - MsgExecuteContract as LaunchpadMsgExecuteContract, - MsgInstantiateContract as LaunchpadMsgInstantiateContract, - MsgMigrateContract as LaunchpadMsgMigrateContract, - MsgStoreCode as LaunchpadMsgStoreCode, - MsgUpdateAdmin as LaunchpadMsgUpdateAdmin, -} from "@cosmjs/cosmwasm-launchpad"; import { fromBase64, toUtf8 } from "@cosmjs/encoding"; import { coins } from "@cosmjs/launchpad"; import { AminoTypes } from "@cosmjs/stargate"; import Long from "long"; -import { cosmWasmTypes } from "./aminotypes"; +import { + AminoMsgClearAdmin, + AminoMsgExecuteContract, + AminoMsgInstantiateContract, + AminoMsgMigrateContract, + AminoMsgStoreCode, + AminoMsgUpdateAdmin, + cosmWasmTypes, +} from "./aminotypes"; import { MsgClearAdmin, MsgExecuteContract, @@ -36,7 +36,7 @@ describe("AminoTypes", () => { typeUrl: "/cosmwasm.wasm.v1beta1.MsgStoreCode", value: msg, }); - const expected: LaunchpadMsgStoreCode = { + const expected: AminoMsgStoreCode = { type: "wasm/MsgStoreCode", value: { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", @@ -65,7 +65,7 @@ describe("AminoTypes", () => { typeUrl: "/cosmwasm.wasm.v1beta1.MsgInstantiateContract", value: msg, }); - const expected: LaunchpadMsgInstantiateContract = { + const expected: AminoMsgInstantiateContract = { type: "wasm/MsgInstantiateContract", value: { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", @@ -74,7 +74,7 @@ describe("AminoTypes", () => { init_msg: { foo: "bar", }, - init_funds: coins(1234, "ucosm"), + funds: coins(1234, "ucosm"), admin: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", }, }; @@ -91,7 +91,7 @@ describe("AminoTypes", () => { typeUrl: "/cosmwasm.wasm.v1beta1.MsgUpdateAdmin", value: msg, }); - const expected: LaunchpadMsgUpdateAdmin = { + const expected: AminoMsgUpdateAdmin = { type: "wasm/MsgUpdateAdmin", value: { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", @@ -111,7 +111,7 @@ describe("AminoTypes", () => { typeUrl: "/cosmwasm.wasm.v1beta1.MsgClearAdmin", value: msg, }); - const expected: LaunchpadMsgClearAdmin = { + const expected: AminoMsgClearAdmin = { type: "wasm/MsgClearAdmin", value: { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", @@ -136,7 +136,7 @@ describe("AminoTypes", () => { typeUrl: "/cosmwasm.wasm.v1beta1.MsgExecuteContract", value: msg, }); - const expected: LaunchpadMsgExecuteContract = { + const expected: AminoMsgExecuteContract = { type: "wasm/MsgExecuteContract", value: { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", @@ -144,7 +144,7 @@ describe("AminoTypes", () => { msg: { foo: "bar", }, - sent_funds: coins(1234, "ucosm"), + funds: coins(1234, "ucosm"), }, }; expect(aminoMsg).toEqual(expected); @@ -165,7 +165,7 @@ describe("AminoTypes", () => { typeUrl: "/cosmwasm.wasm.v1beta1.MsgMigrateContract", value: msg, }); - const expected: LaunchpadMsgMigrateContract = { + const expected: AminoMsgMigrateContract = { type: "wasm/MsgMigrateContract", value: { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", @@ -182,7 +182,7 @@ describe("AminoTypes", () => { describe("fromAmino", () => { it("works for MsgStoreCode", () => { - const aminoMsg: LaunchpadMsgStoreCode = { + const aminoMsg: AminoMsgStoreCode = { type: "wasm/MsgStoreCode", value: { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", @@ -206,7 +206,7 @@ describe("AminoTypes", () => { }); it("works for MsgInstantiateContract", () => { - const aminoMsg: LaunchpadMsgInstantiateContract = { + const aminoMsg: AminoMsgInstantiateContract = { type: "wasm/MsgInstantiateContract", value: { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", @@ -215,7 +215,7 @@ describe("AminoTypes", () => { init_msg: { foo: "bar", }, - init_funds: coins(1234, "ucosm"), + funds: coins(1234, "ucosm"), admin: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", }, }; @@ -239,7 +239,7 @@ describe("AminoTypes", () => { }); it("works for MsgUpdateAdmin", () => { - const aminoMsg: LaunchpadMsgUpdateAdmin = { + const aminoMsg: AminoMsgUpdateAdmin = { type: "wasm/MsgUpdateAdmin", value: { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", @@ -260,7 +260,7 @@ describe("AminoTypes", () => { }); it("works for MsgClearAdmin", () => { - const aminoMsg: LaunchpadMsgClearAdmin = { + const aminoMsg: AminoMsgClearAdmin = { type: "wasm/MsgClearAdmin", value: { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", @@ -279,7 +279,7 @@ describe("AminoTypes", () => { }); it("works for MsgExecuteContract", () => { - const aminoMsg: LaunchpadMsgExecuteContract = { + const aminoMsg: AminoMsgExecuteContract = { type: "wasm/MsgExecuteContract", value: { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", @@ -287,7 +287,7 @@ describe("AminoTypes", () => { msg: { foo: "bar", }, - sent_funds: coins(1234, "ucosm"), + funds: coins(1234, "ucosm"), }, }; const msg = new AminoTypes({ additions: cosmWasmTypes }).fromAmino(aminoMsg); @@ -308,7 +308,7 @@ describe("AminoTypes", () => { }); it("works for MsgMigrateContract", () => { - const aminoMsg: LaunchpadMsgMigrateContract = { + const aminoMsg: AminoMsgMigrateContract = { type: "wasm/MsgMigrateContract", value: { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", diff --git a/packages/cosmwasm-stargate/src/aminotypes.ts b/packages/cosmwasm-stargate/src/aminotypes.ts index 36a9928f..55553fc7 100644 --- a/packages/cosmwasm-stargate/src/aminotypes.ts +++ b/packages/cosmwasm-stargate/src/aminotypes.ts @@ -1,10 +1,4 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { - MsgClearAdmin as LaunchpadMsgClearAdmin, - MsgMigrateContract as LaunchpadMsgMigrateContract, - MsgStoreCode as LaunchpadMsgStoreCode, - MsgUpdateAdmin as LaunchpadMsgUpdateAdmin, -} from "@cosmjs/cosmwasm-launchpad"; import { fromBase64, fromUtf8, toBase64, toUtf8 } from "@cosmjs/encoding"; import { Coin } from "@cosmjs/launchpad"; import { AminoConverter, coinFromProto } from "@cosmjs/stargate"; @@ -20,34 +14,121 @@ import { MsgUpdateAdmin, } from "./codec/x/wasm/internal/types/tx"; -interface MsgExecuteContractValueAmino { - /** Bech32 account address */ - readonly sender: string; - /** Bech32 account address */ - readonly contract: string; - /** Handle message as JavaScript object */ - readonly msg: any; - readonly funds: readonly Coin[]; +// TODO: implement +/** + * @see https://github.com/CosmWasm/wasmd/blob/v0.16.0-alpha1/x/wasm/internal/types/types.proto#L30-L35 + */ +type AccessConfig = never; + +/** + * The Amino JSON representation of + * https://github.com/CosmWasm/wasmd/blob/v0.16.0-alpha1/x/wasm/internal/types/tx.proto#L28-L40 + */ +export interface AminoMsgStoreCode { + type: "wasm/MsgStoreCode"; + value: { + /** Bech32 account address */ + readonly sender: string; + /** Base64 encoded Wasm */ + readonly wasm_byte_code: string; + /** A valid URI reference to the contract's source code. Can be empty. */ + readonly source: string; + /** A docker tag. Can be empty. */ + readonly builder: string; + readonly instantiate_permission?: AccessConfig; + }; } -interface MsgInstantiateContractValueAmino { - /** Bech32 account address */ - readonly sender: string; - /** ID of the Wasm code that was uploaded before */ - readonly code_id: string; - /** Human-readable label for this contract */ - readonly label: string; - /** Init message as JavaScript object */ - readonly init_msg: any; - readonly funds: readonly Coin[]; - /** Bech32-encoded admin address */ - readonly admin?: string; +/** + * The Amino JSON representation of + * https://github.com/CosmWasm/wasmd/blob/v0.16.0-alpha1/x/wasm/internal/types/tx.proto#L70-L80 + */ +export interface AminoMsgExecuteContract { + type: "wasm/MsgExecuteContract"; + value: { + /** Bech32 account address */ + readonly sender: string; + /** Bech32 account address */ + readonly contract: string; + /** Handle message as JavaScript object */ + readonly msg: any; + readonly funds: readonly Coin[]; + }; +} + +/** + * The Amino JSON representation of + * https://github.com/CosmWasm/wasmd/blob/v0.16.0-alpha1/x/wasm/internal/types/tx.proto#L47-L61 + */ +export interface AminoMsgInstantiateContract { + type: "wasm/MsgInstantiateContract"; + value: { + /** Bech32 account address */ + readonly sender: string; + /** ID of the Wasm code that was uploaded before */ + readonly code_id: string; + /** Human-readable label for this contract */ + readonly label: string; + /** Init message as JavaScript object */ + readonly init_msg: any; + readonly funds: readonly Coin[]; + /** Bech32-encoded admin address */ + readonly admin?: string; + }; +} + +/** + * The Amino JSON representation of + * https://github.com/CosmWasm/wasmd/blob/v0.16.0-alpha1/x/wasm/internal/types/tx.proto#L88-L98 + */ +export interface AminoMsgMigrateContract { + type: "wasm/MsgMigrateContract"; + value: { + /** Bech32 account address */ + readonly sender: string; + /** Bech32 account address */ + readonly contract: string; + /** The new code */ + readonly code_id: string; + /** Migrate message as JavaScript object */ + readonly msg: any; + }; +} + +/** + * The Amino JSON representation of + * https://github.com/CosmWasm/wasmd/blob/v0.16.0-alpha1/x/wasm/internal/types/tx.proto#L107-L115 + */ +export interface AminoMsgUpdateAdmin { + type: "wasm/MsgUpdateAdmin"; + value: { + /** Bech32-encoded sender address. This must be the old admin. */ + readonly sender: string; + /** Bech32-encoded contract address to be updated */ + readonly contract: string; + /** Bech32-encoded address of the new admin */ + readonly new_admin: string; + }; +} + +/** + * The Amino JSON representation of + * https://github.com/CosmWasm/wasmd/blob/v0.16.0-alpha1/x/wasm/internal/types/tx.proto#L120-L126 + */ +export interface AminoMsgClearAdmin { + type: "wasm/MsgClearAdmin"; + value: { + /** Bech32-encoded sender address. This must be the old admin. */ + readonly sender: string; + /** Bech32-encoded contract address to be updated */ + readonly contract: string; + }; } export const cosmWasmTypes: Record = { "/cosmwasm.wasm.v1beta1.MsgStoreCode": { aminoType: "wasm/MsgStoreCode", - toAmino: ({ sender, wasmByteCode, source, builder }: MsgStoreCode): LaunchpadMsgStoreCode["value"] => { + toAmino: ({ sender, wasmByteCode, source, builder }: MsgStoreCode): AminoMsgStoreCode["value"] => { assertDefinedAndNotNull(sender, "missing sender"); assertDefinedAndNotNull(wasmByteCode, "missing wasmByteCode"); assertDefinedAndNotNull(source, "missing source"); @@ -59,12 +140,7 @@ export const cosmWasmTypes: Record = { builder: builder, }; }, - fromAmino: ({ - sender, - wasm_byte_code, - source, - builder, - }: LaunchpadMsgStoreCode["value"]): MsgStoreCode => ({ + fromAmino: ({ sender, wasm_byte_code, source, builder }: AminoMsgStoreCode["value"]): MsgStoreCode => ({ sender: sender, wasmByteCode: fromBase64(wasm_byte_code), source: source, @@ -81,7 +157,7 @@ export const cosmWasmTypes: Record = { initMsg, funds, admin, - }: MsgInstantiateContract): MsgInstantiateContractValueAmino => { + }: MsgInstantiateContract): AminoMsgInstantiateContract["value"] => { assertDefinedAndNotNull(sender, "missing sender"); assertDefinedAndNotNull(codeId, "missing codeId"); assertDefinedAndNotNull(label, "missing label"); @@ -103,7 +179,7 @@ export const cosmWasmTypes: Record = { init_msg, funds, admin, - }: MsgInstantiateContractValueAmino): MsgInstantiateContract => ({ + }: AminoMsgInstantiateContract["value"]): MsgInstantiateContract => ({ sender: sender, codeId: Long.fromString(code_id), label: label, @@ -114,7 +190,7 @@ export const cosmWasmTypes: Record = { }, "/cosmwasm.wasm.v1beta1.MsgUpdateAdmin": { aminoType: "wasm/MsgUpdateAdmin", - toAmino: ({ sender, newAdmin, contract }: MsgUpdateAdmin): LaunchpadMsgUpdateAdmin["value"] => { + toAmino: ({ sender, newAdmin, contract }: MsgUpdateAdmin): AminoMsgUpdateAdmin["value"] => { assertDefinedAndNotNull(sender, "missing sender"); assertDefinedAndNotNull(newAdmin, "missing newAdmin"); assertDefinedAndNotNull(contract, "missing contract"); @@ -124,7 +200,7 @@ export const cosmWasmTypes: Record = { contract: contract, }; }, - fromAmino: ({ sender, new_admin, contract }: LaunchpadMsgUpdateAdmin["value"]): MsgUpdateAdmin => ({ + fromAmino: ({ sender, new_admin, contract }: AminoMsgUpdateAdmin["value"]): MsgUpdateAdmin => ({ sender: sender, newAdmin: new_admin, contract: contract, @@ -132,7 +208,7 @@ export const cosmWasmTypes: Record = { }, "/cosmwasm.wasm.v1beta1.MsgClearAdmin": { aminoType: "wasm/MsgClearAdmin", - toAmino: ({ sender, contract }: MsgClearAdmin): LaunchpadMsgClearAdmin["value"] => { + toAmino: ({ sender, contract }: MsgClearAdmin): AminoMsgClearAdmin["value"] => { assertDefinedAndNotNull(sender, "missing sender"); assertDefinedAndNotNull(contract, "missing contract"); return { @@ -140,14 +216,14 @@ export const cosmWasmTypes: Record = { contract: contract, }; }, - fromAmino: ({ sender, contract }: LaunchpadMsgClearAdmin["value"]): MsgClearAdmin => ({ + fromAmino: ({ sender, contract }: AminoMsgClearAdmin["value"]): MsgClearAdmin => ({ sender: sender, contract: contract, }), }, "/cosmwasm.wasm.v1beta1.MsgExecuteContract": { aminoType: "wasm/MsgExecuteContract", - toAmino: ({ sender, contract, msg, funds }: MsgExecuteContract): MsgExecuteContractValueAmino => { + toAmino: ({ sender, contract, msg, funds }: MsgExecuteContract): AminoMsgExecuteContract["value"] => { assertDefinedAndNotNull(sender, "missing sender"); assertDefinedAndNotNull(contract, "missing contract"); assertDefinedAndNotNull(msg, "missing msg"); @@ -159,7 +235,7 @@ export const cosmWasmTypes: Record = { funds: funds.map(coinFromProto), }; }, - fromAmino: ({ sender, contract, msg, funds }: MsgExecuteContractValueAmino): MsgExecuteContract => ({ + fromAmino: ({ sender, contract, msg, funds }: AminoMsgExecuteContract["value"]): MsgExecuteContract => ({ sender: sender, contract: contract, msg: toUtf8(JSON.stringify(msg)), @@ -173,7 +249,7 @@ export const cosmWasmTypes: Record = { contract, codeId, migrateMsg, - }: MsgMigrateContract): LaunchpadMsgMigrateContract["value"] => { + }: MsgMigrateContract): AminoMsgMigrateContract["value"] => { assertDefinedAndNotNull(sender, "missing sender"); assertDefinedAndNotNull(contract, "missing contract"); assertDefinedAndNotNull(codeId, "missing codeId"); @@ -190,7 +266,7 @@ export const cosmWasmTypes: Record = { contract, code_id, msg, - }: LaunchpadMsgMigrateContract["value"]): MsgMigrateContract => ({ + }: AminoMsgMigrateContract["value"]): MsgMigrateContract => ({ sender: sender, contract: contract, codeId: Long.fromString(code_id),