diff --git a/packages/bcp/src/decode.ts b/packages/bcp/src/decode.ts index 2835b0fa..af83599a 100644 --- a/packages/bcp/src/decode.ts +++ b/packages/bcp/src/decode.ts @@ -73,13 +73,10 @@ export function decodeAmount(tokens: TokenInfos, coin: types.Coin): Amount { } export function parseMsg(msg: types.Msg, chainId: ChainId, tokens: TokenInfos): SendTransaction { - if (msg.type !== "cosmos-sdk/MsgSend") { + if (!types.isMsgSend(msg)) { throw new Error("Unknown message type in transaction"); } - if (!(msg.value as types.MsgSend).from_address) { - throw new Error("Only MsgSend is supported"); - } - const msgValue = msg.value as types.MsgSend; + const msgValue = msg.value; if (msgValue.amount.length !== 1) { throw new Error("Only MsgSend with one amount is supported"); } diff --git a/packages/sdk/src/restclient.spec.ts b/packages/sdk/src/restclient.spec.ts index f6febb93..e753904b 100644 --- a/packages/sdk/src/restclient.spec.ts +++ b/packages/sdk/src/restclient.spec.ts @@ -7,7 +7,7 @@ import { encodeSecp256k1Signature, marshalTx, sortJson } from "./encoding"; import { RestClient } from "./restclient"; import contract from "./testdata/contract.json"; import data from "./testdata/cosmoshub.json"; -import { MsgStoreCodeWrapped, StdTx } from "./types"; +import { MsgStoreCode, StdTx } from "./types"; const { fromBase64, toUtf8 } = Encoding; @@ -66,7 +66,7 @@ describe("RestClient", () => { const signer = await wallet.createIdentity("abc" as ChainId, faucetPath); const memo = "My first contract on chain"; - const theMsg: MsgStoreCodeWrapped = { + const theMsg: MsgStoreCode = { type: "wasm/store-code", value: { sender: faucetAddress, diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index badfd978..07066b29 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -22,12 +22,12 @@ export function isAminoStdTx(txValue: unknown): txValue is StdTx { ); } -interface MsgUnknownWrapped { +interface MsgTemplate { readonly type: string; readonly value: object; } -export interface MsgSend { +export interface ValueSend { /** Bech32 account address */ readonly from_address: string; /** Bech32 account address */ @@ -35,12 +35,12 @@ export interface MsgSend { readonly amount: ReadonlyArray; } -export interface MsgSendWrapped extends MsgUnknownWrapped { - readonly type: "cosmos-sdk/StdTx"; - readonly value: MsgSend; +export interface MsgSend extends MsgTemplate { + readonly type: "cosmos-sdk/MsgSend"; + readonly value: ValueSend; } -export interface MsgStoreCode { +export interface ValueStoreCode { /** Bech32 account address */ readonly sender: string; /** Base64 encoded Wasm */ @@ -51,12 +51,20 @@ export interface MsgStoreCode { readonly builder?: string; } -export interface MsgStoreCodeWrapped extends MsgUnknownWrapped { +export interface MsgStoreCode extends MsgTemplate { readonly type: "wasm/store-code"; - readonly value: MsgStoreCode; + readonly value: ValueStoreCode; } -export type Msg = MsgSendWrapped | MsgStoreCodeWrapped | MsgUnknownWrapped; +export type Msg = MsgSend | MsgStoreCode | MsgTemplate; + +export function isMsgSend(msg: Msg): msg is MsgSend { + return (msg as MsgSend).type === "cosmos-sdk/MsgSend"; +} + +export function isMsgStoreCode(msg: Msg): msg is MsgStoreCode { + return (msg as MsgStoreCode).type === "wasm/store-code"; +} export interface StdFee { readonly amount: ReadonlyArray; diff --git a/packages/sdk/types/types.d.ts b/packages/sdk/types/types.d.ts index 7104e3a3..b1b43a0d 100644 --- a/packages/sdk/types/types.d.ts +++ b/packages/sdk/types/types.d.ts @@ -12,22 +12,22 @@ export declare type AminoTx = Tx & { readonly value: StdTx; }; export declare function isAminoStdTx(txValue: unknown): txValue is StdTx; -interface MsgUnknownWrapped { +interface MsgTemplate { readonly type: string; readonly value: object; } -export interface MsgSend { +export interface ValueSend { /** Bech32 account address */ readonly from_address: string; /** Bech32 account address */ readonly to_address: string; readonly amount: ReadonlyArray; } -export interface MsgSendWrapped extends MsgUnknownWrapped { - readonly type: "cosmos-sdk/StdTx"; - readonly value: MsgSend; +export interface MsgSend extends MsgTemplate { + readonly type: "cosmos-sdk/MsgSend"; + readonly value: ValueSend; } -export interface MsgStoreCode { +export interface ValueStoreCode { /** Bech32 account address */ readonly sender: string; /** Base64 encoded Wasm */ @@ -37,11 +37,13 @@ export interface MsgStoreCode { /** A docker tag, optional */ readonly builder?: string; } -export interface MsgStoreCodeWrapped extends MsgUnknownWrapped { +export interface MsgStoreCode extends MsgTemplate { readonly type: "wasm/store-code"; - readonly value: MsgStoreCode; + readonly value: ValueStoreCode; } -export declare type Msg = MsgSendWrapped | MsgStoreCodeWrapped | MsgUnknownWrapped; +export declare type Msg = MsgSend | MsgStoreCode | MsgTemplate; +export declare function isMsgSend(msg: Msg): msg is MsgSend; +export declare function isMsgStoreCode(msg: Msg): msg is MsgStoreCode; export interface StdFee { readonly amount: ReadonlyArray; readonly gas: string;