diff --git a/CHANGELOG.md b/CHANGELOG.md index 54849eabff..cc7e910bec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ and this project adheres to dependencies. This should also reduce the bundle size as only the English wordlist is shipped. ([#966]) - @cosmjs/cli: Rename binary `cosmwasm-cli` to `cosmjs-cli` ([#1033]). +- @cosmjs/stargate & @cosmjs/cosmwasm-stargate: Removed default types from AminoTypes. ([1079]) [#927]: https://github.com/cosmos/cosmjs/issues/927 [#955]: https://github.com/cosmos/cosmjs/issues/955 @@ -55,6 +56,7 @@ and this project adheres to [#1026]: https://github.com/cosmos/cosmjs/issues/1026 [#1033]: https://github.com/cosmos/cosmjs/issues/1033 [#1053]: https://github.com/cosmos/cosmjs/issues/1053 +[#1079]: https://github.com/cosmos/cosmjs/issues/1079 ### Removed diff --git a/packages/cosmwasm-stargate/src/modules/wasm/aminomessages.spec.ts b/packages/cosmwasm-stargate/src/modules/wasm/aminomessages.spec.ts index 476519b756..4f75c472aa 100644 --- a/packages/cosmwasm-stargate/src/modules/wasm/aminomessages.spec.ts +++ b/packages/cosmwasm-stargate/src/modules/wasm/aminomessages.spec.ts @@ -29,7 +29,7 @@ describe("AminoTypes", () => { wasmByteCode: fromBase64("WUVMTE9XIFNVQk1BUklORQ=="), instantiatePermission: undefined, }; - const aminoMsg = new AminoTypes({ prefix: "cosmos", additions: createWasmAminoConverters() }).toAmino({ + const aminoMsg = new AminoTypes(createWasmAminoConverters()).toAmino({ typeUrl: "/cosmwasm.wasm.v1.MsgStoreCode", value: msg, }); @@ -54,12 +54,10 @@ describe("AminoTypes", () => { funds: coins(1234, "ucosm"), admin: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", }; - const aminoMsg = new AminoTypes({ prefix: "cosmos", additions: createWasmAminoConverters() }).toAmino( - { - typeUrl: "/cosmwasm.wasm.v1.MsgInstantiateContract", - value: msg, - }, - ); + const aminoMsg = new AminoTypes(createWasmAminoConverters()).toAmino({ + typeUrl: "/cosmwasm.wasm.v1.MsgInstantiateContract", + value: msg, + }); const expected: AminoMsgInstantiateContract = { type: "wasm/MsgInstantiateContract", value: { @@ -84,12 +82,10 @@ describe("AminoTypes", () => { funds: coins(1234, "ucosm"), admin: "", }; - const aminoMsg = new AminoTypes({ prefix: "cosmos", additions: createWasmAminoConverters() }).toAmino( - { - typeUrl: "/cosmwasm.wasm.v1.MsgInstantiateContract", - value: msg, - }, - ); + const aminoMsg = new AminoTypes(createWasmAminoConverters()).toAmino({ + typeUrl: "/cosmwasm.wasm.v1.MsgInstantiateContract", + value: msg, + }); const expected: AminoMsgInstantiateContract = { type: "wasm/MsgInstantiateContract", value: { @@ -111,7 +107,7 @@ describe("AminoTypes", () => { newAdmin: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", contract: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", }; - const aminoMsg = new AminoTypes({ prefix: "cosmos", additions: createWasmAminoConverters() }).toAmino({ + const aminoMsg = new AminoTypes(createWasmAminoConverters()).toAmino({ typeUrl: "/cosmwasm.wasm.v1.MsgUpdateAdmin", value: msg, }); @@ -131,7 +127,7 @@ describe("AminoTypes", () => { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", contract: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", }; - const aminoMsg = new AminoTypes({ prefix: "cosmos", additions: createWasmAminoConverters() }).toAmino({ + const aminoMsg = new AminoTypes(createWasmAminoConverters()).toAmino({ typeUrl: "/cosmwasm.wasm.v1.MsgClearAdmin", value: msg, }); @@ -152,7 +148,7 @@ describe("AminoTypes", () => { msg: toUtf8(`{"foo":"bar"}`), funds: coins(1234, "ucosm"), }; - const aminoMsg = new AminoTypes({ prefix: "cosmos", additions: createWasmAminoConverters() }).toAmino({ + const aminoMsg = new AminoTypes(createWasmAminoConverters()).toAmino({ typeUrl: "/cosmwasm.wasm.v1.MsgExecuteContract", value: msg, }); @@ -175,7 +171,7 @@ describe("AminoTypes", () => { codeId: Long.fromString("98765"), msg: toUtf8(`{"foo":"bar"}`), }; - const aminoMsg = new AminoTypes({ prefix: "cosmos", additions: createWasmAminoConverters() }).toAmino({ + const aminoMsg = new AminoTypes(createWasmAminoConverters()).toAmino({ typeUrl: "/cosmwasm.wasm.v1.MsgMigrateContract", value: msg, }); @@ -201,9 +197,7 @@ describe("AminoTypes", () => { wasm_byte_code: "WUVMTE9XIFNVQk1BUklORQ==", }, }; - const msg = new AminoTypes({ prefix: "cosmos", additions: createWasmAminoConverters() }).fromAmino( - aminoMsg, - ); + const msg = new AminoTypes(createWasmAminoConverters()).fromAmino(aminoMsg); const expectedValue: MsgStoreCode = { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", wasmByteCode: fromBase64("WUVMTE9XIFNVQk1BUklORQ=="), @@ -229,9 +223,7 @@ describe("AminoTypes", () => { admin: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", }, }; - const msg = new AminoTypes({ prefix: "cosmos", additions: createWasmAminoConverters() }).fromAmino( - aminoMsg, - ); + const msg = new AminoTypes(createWasmAminoConverters()).fromAmino(aminoMsg); const expectedValue: MsgInstantiateContract = { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", codeId: Long.fromString("12345"), @@ -258,9 +250,7 @@ describe("AminoTypes", () => { funds: coins(1234, "ucosm"), }, }; - const msg = new AminoTypes({ prefix: "cosmos", additions: createWasmAminoConverters() }).fromAmino( - aminoMsg, - ); + const msg = new AminoTypes(createWasmAminoConverters()).fromAmino(aminoMsg); const expectedValue: MsgInstantiateContract = { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", codeId: Long.fromString("12345"), @@ -285,9 +275,7 @@ describe("AminoTypes", () => { contract: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", }, }; - const msg = new AminoTypes({ prefix: "cosmos", additions: createWasmAminoConverters() }).fromAmino( - aminoMsg, - ); + const msg = new AminoTypes(createWasmAminoConverters()).fromAmino(aminoMsg); const expectedValue: MsgUpdateAdmin = { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", newAdmin: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", @@ -307,9 +295,7 @@ describe("AminoTypes", () => { contract: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", }, }; - const msg = new AminoTypes({ prefix: "cosmos", additions: createWasmAminoConverters() }).fromAmino( - aminoMsg, - ); + const msg = new AminoTypes(createWasmAminoConverters()).fromAmino(aminoMsg); const expectedValue: MsgClearAdmin = { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", contract: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", @@ -330,9 +316,7 @@ describe("AminoTypes", () => { funds: coins(1234, "ucosm"), }, }; - const msg = new AminoTypes({ prefix: "cosmos", additions: createWasmAminoConverters() }).fromAmino( - aminoMsg, - ); + const msg = new AminoTypes(createWasmAminoConverters()).fromAmino(aminoMsg); const expectedValue: MsgExecuteContract = { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", contract: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", @@ -355,9 +339,7 @@ describe("AminoTypes", () => { msg: { foo: "bar" }, }, }; - const msg = new AminoTypes({ prefix: "cosmos", additions: createWasmAminoConverters() }).fromAmino( - aminoMsg, - ); + const msg = new AminoTypes(createWasmAminoConverters()).fromAmino(aminoMsg); const expectedValue: MsgMigrateContract = { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", contract: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", diff --git a/packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts b/packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts index 3b61b55a8e..3093b71813 100644 --- a/packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts +++ b/packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts @@ -9,6 +9,7 @@ import { assertIsDeliverTxSuccess, coin, coins, + createStakingAminoConverters, MsgDelegateEncodeObject, MsgSendEncodeObject, } from "@cosmjs/stargate"; @@ -736,6 +737,7 @@ describe("SigningCosmWasmClient", () => { const wallet = await Secp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix }); const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, { ...defaultSigningClientOptions, + aminoTypes: new AminoTypes(createStakingAminoConverters(wasmd.prefix)), prefix: wasmd.prefix, }); @@ -851,39 +853,36 @@ describe("SigningCosmWasmClient", () => { }; customRegistry.register(msgDelegateTypeUrl, CustomMsgDelegate); const customAminoTypes = new AminoTypes({ - prefix: "cosmos", - additions: { - "/cosmos.staking.v1beta1.MsgDelegate": { - aminoType: "cosmos-sdk/MsgDelegate", - toAmino: ({ - customDelegatorAddress, - customValidatorAddress, - customAmount, - }: CustomMsgDelegate): AminoMsgDelegate["value"] => { - assert(customDelegatorAddress, "missing customDelegatorAddress"); - assert(customValidatorAddress, "missing validatorAddress"); - assert(customAmount, "missing amount"); - assert(customAmount.amount, "missing amount.amount"); - assert(customAmount.denom, "missing amount.denom"); - return { - delegator_address: customDelegatorAddress, - validator_address: customValidatorAddress, - amount: { - amount: customAmount.amount, - denom: customAmount.denom, - }, - }; - }, - fromAmino: ({ - delegator_address, - validator_address, - amount, - }: AminoMsgDelegate["value"]): CustomMsgDelegate => ({ - customDelegatorAddress: delegator_address, - customValidatorAddress: validator_address, - customAmount: Coin.fromPartial(amount), - }), + "/cosmos.staking.v1beta1.MsgDelegate": { + aminoType: "cosmos-sdk/MsgDelegate", + toAmino: ({ + customDelegatorAddress, + customValidatorAddress, + customAmount, + }: CustomMsgDelegate): AminoMsgDelegate["value"] => { + assert(customDelegatorAddress, "missing customDelegatorAddress"); + assert(customValidatorAddress, "missing validatorAddress"); + assert(customAmount, "missing amount"); + assert(customAmount.amount, "missing amount.amount"); + assert(customAmount.denom, "missing amount.denom"); + return { + delegator_address: customDelegatorAddress, + validator_address: customValidatorAddress, + amount: { + amount: customAmount.amount, + denom: customAmount.denom, + }, + }; }, + fromAmino: ({ + delegator_address, + validator_address, + amount, + }: AminoMsgDelegate["value"]): CustomMsgDelegate => ({ + customDelegatorAddress: delegator_address, + customValidatorAddress: validator_address, + customAmount: Coin.fromPartial(amount), + }), }, }); const options = { @@ -921,6 +920,7 @@ describe("SigningCosmWasmClient", () => { }); const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, { ...defaultSigningClientOptions, + aminoTypes: new AminoTypes(createStakingAminoConverters(wasmd.prefix)), prefix: wasmd.prefix, }); @@ -1067,6 +1067,7 @@ describe("SigningCosmWasmClient", () => { const wallet = await Secp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix }); const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, { ...defaultSigningClientOptions, + aminoTypes: new AminoTypes(createStakingAminoConverters(wasmd.prefix)), prefix: wasmd.prefix, }); @@ -1156,37 +1157,34 @@ describe("SigningCosmWasmClient", () => { }; customRegistry.register(msgDelegateTypeUrl, CustomMsgDelegate); const customAminoTypes = new AminoTypes({ - prefix: "cosmos", - additions: { - "/cosmos.staking.v1beta1.MsgDelegate": { - aminoType: "cosmos-sdk/MsgDelegate", - toAmino: ({ - customDelegatorAddress, - customValidatorAddress, - customAmount, - }: CustomMsgDelegate): AminoMsgDelegate["value"] => { - assert(customDelegatorAddress, "missing customDelegatorAddress"); - assert(customValidatorAddress, "missing validatorAddress"); - assert(customAmount, "missing amount"); - return { - delegator_address: customDelegatorAddress, - validator_address: customValidatorAddress, - amount: { - amount: customAmount.amount, - denom: customAmount.denom, - }, - }; - }, - fromAmino: ({ - delegator_address, - validator_address, - amount, - }: AminoMsgDelegate["value"]): CustomMsgDelegate => ({ - customDelegatorAddress: delegator_address, - customValidatorAddress: validator_address, - customAmount: Coin.fromPartial(amount), - }), + "/cosmos.staking.v1beta1.MsgDelegate": { + aminoType: "cosmos-sdk/MsgDelegate", + toAmino: ({ + customDelegatorAddress, + customValidatorAddress, + customAmount, + }: CustomMsgDelegate): AminoMsgDelegate["value"] => { + assert(customDelegatorAddress, "missing customDelegatorAddress"); + assert(customValidatorAddress, "missing validatorAddress"); + assert(customAmount, "missing amount"); + return { + delegator_address: customDelegatorAddress, + validator_address: customValidatorAddress, + amount: { + amount: customAmount.amount, + denom: customAmount.denom, + }, + }; }, + fromAmino: ({ + delegator_address, + validator_address, + amount, + }: AminoMsgDelegate["value"]): CustomMsgDelegate => ({ + customDelegatorAddress: delegator_address, + customValidatorAddress: validator_address, + customAmount: Coin.fromPartial(amount), + }), }, }); const options = { @@ -1226,6 +1224,7 @@ describe("SigningCosmWasmClient", () => { }); const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, { ...defaultSigningClientOptions, + aminoTypes: new AminoTypes(createStakingAminoConverters(wasmd.prefix)), prefix: wasmd.prefix, }); diff --git a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts index 884e9f1ac6..0fab41719f 100644 --- a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts @@ -17,6 +17,7 @@ import { AminoTypes, calculateFee, Coin, + createBankAminoConverters, defaultRegistryTypes as defaultStargateTypes, DeliverTxResponse, GasPrice, @@ -201,11 +202,9 @@ export class SigningCosmWasmClient extends CosmWasmClient { options: SigningCosmWasmClientOptions, ) { super(tmClient); - // TODO: do we really want to set a default here? Ideally we could get it from the signer such that users only have to set it once. - const prefix = options.prefix ?? "cosmos"; const { registry = createDefaultRegistry(), - aminoTypes = new AminoTypes({ prefix, additions: createWasmAminoConverters() }), + aminoTypes = new AminoTypes({ ...createWasmAminoConverters(), ...createBankAminoConverters() }), } = options; this.registry = registry; this.aminoTypes = aminoTypes; diff --git a/packages/stargate/src/aminoconverters.ts b/packages/stargate/src/aminoconverters.ts deleted file mode 100644 index 8657992b59..0000000000 --- a/packages/stargate/src/aminoconverters.ts +++ /dev/null @@ -1,8 +0,0 @@ -export interface AminoConverter { - readonly aminoType: string; - readonly toAmino: (value: any) => any; - readonly fromAmino: (value: any) => any; -} - -/** A map from protobuf type URL to the AminoConverter implementation if supported on chain */ -export type AminoConverters = Record; diff --git a/packages/stargate/src/aminotypes.spec.ts b/packages/stargate/src/aminotypes.spec.ts index aef2468639..e727453f01 100644 --- a/packages/stargate/src/aminotypes.spec.ts +++ b/packages/stargate/src/aminotypes.spec.ts @@ -1,44 +1,8 @@ -/* eslint-disable @typescript-eslint/naming-convention */ -import { encodeBech32Pubkey } from "@cosmjs/amino"; -import { fromBase64 } from "@cosmjs/encoding"; -import { coin, coins } from "@cosmjs/proto-signing"; -import { MsgMultiSend, MsgSend } from "cosmjs-types/cosmos/bank/v1beta1/tx"; -import { - MsgFundCommunityPool, - MsgSetWithdrawAddress, - MsgWithdrawDelegatorReward, - MsgWithdrawValidatorCommission, -} from "cosmjs-types/cosmos/distribution/v1beta1/tx"; -import { TextProposal, VoteOption } from "cosmjs-types/cosmos/gov/v1beta1/gov"; -import { MsgDeposit, MsgSubmitProposal, MsgVote } from "cosmjs-types/cosmos/gov/v1beta1/tx"; -import { - MsgBeginRedelegate, - MsgCreateValidator, - MsgDelegate, - MsgEditValidator, - MsgUndelegate, -} from "cosmjs-types/cosmos/staking/v1beta1/tx"; -import { MsgTransfer } from "cosmjs-types/ibc/applications/transfer/v1/tx"; -import Long from "long"; +import { coin } from "@cosmjs/proto-signing"; +import { MsgDelegate } from "cosmjs-types/cosmos/staking/v1beta1/tx"; import { AminoTypes } from "./aminotypes"; -import { - AminoMsgBeginRedelegate, - AminoMsgCreateValidator, - AminoMsgDelegate, - AminoMsgDeposit, - AminoMsgEditValidator, - AminoMsgFundCommunityPool, - AminoMsgMultiSend, - AminoMsgSend, - AminoMsgSetWithdrawAddress, - AminoMsgSubmitProposal, - AminoMsgTransfer, - AminoMsgUndelegate, - AminoMsgVote, - AminoMsgWithdrawDelegatorReward, - AminoMsgWithdrawValidatorCommission, -} from "./modules"; +import { createBankAminoConverters, createStakingAminoConverters } from "./modules"; describe("AminoTypes", () => { describe("constructor", () => { @@ -50,17 +14,14 @@ describe("AminoTypes", () => { it("can override type by type URL", () => { const types = new AminoTypes({ - prefix: "cosmos", - additions: { - "/cosmos.staking.v1beta1.MsgDelegate": { - aminoType: "my-override/MsgDelegate", - toAmino: (m: MsgDelegate): { readonly foo: string } => ({ - foo: m.delegatorAddress ?? "", - }), - fromAmino: () => ({ - bar: 123, - }), - }, + "/cosmos.staking.v1beta1.MsgDelegate": { + aminoType: "my-override/MsgDelegate", + toAmino: (m: MsgDelegate): { readonly foo: string } => ({ + foo: m.delegatorAddress ?? "", + }), + fromAmino: () => ({ + bar: 123, + }), }, }); @@ -84,17 +45,15 @@ describe("AminoTypes", () => { it("can override type with Amino type collision", () => { const types = new AminoTypes({ - prefix: "cosmos", - additions: { - "/cosmos.staking.otherVersion456.MsgDelegate": { - aminoType: "cosmos-sdk/MsgDelegate", - toAmino: (m: MsgDelegate): { readonly foo: string } => ({ - foo: m.delegatorAddress ?? "", - }), - fromAmino: () => ({ - bar: 123, - }), - }, + ...createStakingAminoConverters("cosmos"), + "/cosmos.staking.otherVersion456.MsgDelegate": { + aminoType: "cosmos-sdk/MsgDelegate", + toAmino: (m: MsgDelegate): { readonly foo: string } => ({ + foo: m.delegatorAddress ?? "", + }), + fromAmino: () => ({ + bar: 123, + }), }, }); @@ -115,492 +74,22 @@ describe("AminoTypes", () => { }); describe("toAmino", () => { - // bank - - it("works for MsgSend", () => { - const msg: MsgSend = { - fromAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - toAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - amount: coins(1234, "ucosm"), - }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ - typeUrl: "/cosmos.bank.v1beta1.MsgSend", - value: msg, - }); - const expected: AminoMsgSend = { - type: "cosmos-sdk/MsgSend", - value: { - from_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - to_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - amount: coins(1234, "ucosm"), - }, - }; - expect(aminoMsg).toEqual(expected); - }); - - it("works for MsgMultiSend", () => { - const msg: MsgMultiSend = { - inputs: [ - { address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", coins: coins(1234, "ucosm") }, - { address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", coins: coins(5678, "ucosm") }, - ], - outputs: [ - { address: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", coins: coins(6000, "ucosm") }, - { address: "cosmos142u9fgcjdlycfcez3lw8x6x5h7rfjlnfhpw2lx", coins: coins(912, "ucosm") }, - ], - }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ - typeUrl: "/cosmos.bank.v1beta1.MsgMultiSend", - value: msg, - }); - const expected: AminoMsgMultiSend = { - type: "cosmos-sdk/MsgMultiSend", - value: { - inputs: [ - { address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", coins: coins(1234, "ucosm") }, - { address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", coins: coins(5678, "ucosm") }, - ], - outputs: [ - { address: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", coins: coins(6000, "ucosm") }, - { address: "cosmos142u9fgcjdlycfcez3lw8x6x5h7rfjlnfhpw2lx", coins: coins(912, "ucosm") }, - ], - }, - }; - expect(aminoMsg).toEqual(expected); - }); - - // gov - - it("works for MsgDeposit", () => { - const msg: MsgDeposit = { - amount: [{ amount: "12300000", denom: "ustake" }], - depositor: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - proposalId: Long.fromNumber(5), - }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ - typeUrl: "/cosmos.gov.v1beta1.MsgDeposit", - value: msg, - }); - const expected: AminoMsgDeposit = { - type: "cosmos-sdk/MsgDeposit", - value: { - amount: [{ amount: "12300000", denom: "ustake" }], - depositor: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - proposal_id: "5", - }, - }; - expect(aminoMsg).toEqual(expected); - }); - - it("works for MsgSubmitProposal", () => { - const msg: MsgSubmitProposal = { - initialDeposit: [{ amount: "12300000", denom: "ustake" }], - proposer: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - content: { - typeUrl: "/cosmos.gov.v1beta1.TextProposal", - value: TextProposal.encode({ - description: "This proposal proposes to test whether this proposal passes", - title: "Test Proposal", - }).finish(), - }, - }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ - typeUrl: "/cosmos.gov.v1beta1.MsgSubmitProposal", - value: msg, - }); - const expected: AminoMsgSubmitProposal = { - type: "cosmos-sdk/MsgSubmitProposal", - value: { - initial_deposit: [{ amount: "12300000", denom: "ustake" }], - proposer: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - content: { - type: "cosmos-sdk/TextProposal", - value: { - description: "This proposal proposes to test whether this proposal passes", - title: "Test Proposal", - }, - }, - }, - }; - expect(aminoMsg).toEqual(expected); - }); - - it("works for MsgVote", () => { - const msg: MsgVote = { - option: VoteOption.VOTE_OPTION_NO_WITH_VETO, - proposalId: Long.fromNumber(5), - voter: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", - }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ - typeUrl: "/cosmos.gov.v1beta1.MsgVote", - value: msg, - }); - const expected: AminoMsgVote = { - type: "cosmos-sdk/MsgVote", - value: { - option: 4, - proposal_id: "5", - voter: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", - }, - }; - expect(aminoMsg).toEqual(expected); - }); - - // distribution - - it("works for MsgFundCommunityPool", async () => { - const msg: MsgFundCommunityPool = { - amount: coins(1234, "ucosm"), - depositor: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ - typeUrl: "/cosmos.distribution.v1beta1.MsgFundCommunityPool", - value: msg, - }); - const expected: AminoMsgFundCommunityPool = { - type: "cosmos-sdk/MsgFundCommunityPool", - value: { - amount: coins(1234, "ucosm"), - depositor: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - }, - }; - expect(aminoMsg).toEqual(expected); - }); - - it("works for MsgSetWithdrawAddress", async () => { - const msg: MsgSetWithdrawAddress = { - delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - withdrawAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ - typeUrl: "/cosmos.distribution.v1beta1.MsgSetWithdrawAddress", - value: msg, - }); - const expected: AminoMsgSetWithdrawAddress = { - type: "cosmos-sdk/MsgModifyWithdrawAddress", - value: { - delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - withdraw_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - }, - }; - expect(aminoMsg).toEqual(expected); - }); - - it("works for MsgWithdrawDelegatorReward", async () => { - const msg: MsgWithdrawDelegatorReward = { - delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ - typeUrl: "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward", - value: msg, - }); - const expected: AminoMsgWithdrawDelegatorReward = { - type: "cosmos-sdk/MsgWithdrawDelegationReward", - value: { - delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - }, - }; - expect(aminoMsg).toEqual(expected); - }); - - it("works for MsgWithdrawValidatorCommission", async () => { - const msg: MsgWithdrawValidatorCommission = { - validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ - typeUrl: "/cosmos.distribution.v1beta1.MsgWithdrawValidatorCommission", - value: msg, - }); - const expected: AminoMsgWithdrawValidatorCommission = { - type: "cosmos-sdk/MsgWithdrawValidatorCommission", - value: { - validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - }, - }; - expect(aminoMsg).toEqual(expected); - }); - - // staking - - it("works for MsgBeginRedelegate", () => { - const msg: MsgBeginRedelegate = { - delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - validatorSrcAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - validatorDstAddress: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", - amount: coin(1234, "ucosm"), - }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ - typeUrl: "/cosmos.staking.v1beta1.MsgBeginRedelegate", - value: msg, - }); - const expected: AminoMsgBeginRedelegate = { - type: "cosmos-sdk/MsgBeginRedelegate", - value: { - delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - validator_src_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - validator_dst_address: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", - amount: coin(1234, "ucosm"), - }, - }; - expect(aminoMsg).toEqual(expected); - }); - - it("works for MsgCreateValidator", () => { - const msg: MsgCreateValidator = { - description: { - moniker: "validator", - identity: "me", - website: "valid.com", - securityContact: "Hamburglar", - details: "...", - }, - commission: { - rate: "0.2", - maxRate: "0.3", - maxChangeRate: "0.1", - }, - minSelfDelegation: "123", - delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - pubkey: { - typeUrl: "/cosmos.crypto.secp256k1.PubKey", - value: fromBase64("A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ"), - }, - value: coin(1234, "ucosm"), - }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ - typeUrl: "/cosmos.staking.v1beta1.MsgCreateValidator", - value: msg, - }); - const expected: AminoMsgCreateValidator = { - type: "cosmos-sdk/MsgCreateValidator", - value: { - description: { - moniker: "validator", - identity: "me", - website: "valid.com", - security_contact: "Hamburglar", - details: "...", - }, - commission: { - rate: "0.2", - max_rate: "0.3", - max_change_rate: "0.1", - }, - min_self_delegation: "123", - delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - pubkey: encodeBech32Pubkey( - { type: "tendermint/PubKeySecp256k1", value: "A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ" }, - "cosmos", - ), - value: coin(1234, "ucosm"), - }, - }; - expect(aminoMsg).toEqual(expected); - }); - - it("works for MsgDelegate", () => { - const msg: MsgDelegate = { - delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - amount: coin(1234, "ucosm"), - }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ - typeUrl: "/cosmos.staking.v1beta1.MsgDelegate", - value: msg, - }); - const expected: AminoMsgDelegate = { - type: "cosmos-sdk/MsgDelegate", - value: { - delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - amount: coin(1234, "ucosm"), - }, - }; - expect(aminoMsg).toEqual(expected); - }); - - it("works for MsgEditValidator", () => { - const msg: MsgEditValidator = { - description: { - moniker: "validator", - identity: "me", - website: "valid.com", - securityContact: "Hamburglar", - details: "...", - }, - commissionRate: "0.2", - minSelfDelegation: "123", - validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ - typeUrl: "/cosmos.staking.v1beta1.MsgEditValidator", - value: msg, - }); - const expected: AminoMsgEditValidator = { - type: "cosmos-sdk/MsgEditValidator", - value: { - description: { - moniker: "validator", - identity: "me", - website: "valid.com", - security_contact: "Hamburglar", - details: "...", - }, - commission_rate: "0.2", - min_self_delegation: "123", - validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - }, - }; - expect(aminoMsg).toEqual(expected); - }); - - it("works for MsgUndelegate", () => { - const msg: MsgUndelegate = { - delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - amount: coin(1234, "ucosm"), - }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ - typeUrl: "/cosmos.staking.v1beta1.MsgUndelegate", - value: msg, - }); - const expected: AminoMsgUndelegate = { - type: "cosmos-sdk/MsgUndelegate", - value: { - delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - amount: coin(1234, "ucosm"), - }, - }; - expect(aminoMsg).toEqual(expected); - }); - - // ibc - - it("works for MsgTransfer", () => { - const msg: MsgTransfer = { - sourcePort: "testport", - sourceChannel: "testchannel", - token: coin(1234, "utest"), - sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - receiver: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - timeoutHeight: { - revisionHeight: Long.fromString("123", true), - revisionNumber: Long.fromString("456", true), - }, - timeoutTimestamp: Long.fromString("789", true), - }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ - typeUrl: "/ibc.applications.transfer.v1.MsgTransfer", - value: msg, - }); - const expected: AminoMsgTransfer = { - type: "cosmos-sdk/MsgTransfer", - value: { - source_port: "testport", - source_channel: "testchannel", - token: coin(1234, "utest"), - sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - receiver: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - timeout_height: { - revision_height: "123", - revision_number: "456", - }, - timeout_timestamp: "789", - }, - }; - expect(aminoMsg).toEqual(expected); - }); - - it("works for MsgTransfer with empty values", () => { - const msg: MsgTransfer = { - sourcePort: "testport", - sourceChannel: "testchannel", - token: coin(1234, "utest"), - sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - receiver: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - timeoutHeight: { - revisionHeight: Long.UZERO, - revisionNumber: Long.UZERO, - }, - timeoutTimestamp: Long.UZERO, - }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ - typeUrl: "/ibc.applications.transfer.v1.MsgTransfer", - value: msg, - }); - const expected: AminoMsgTransfer = { - type: "cosmos-sdk/MsgTransfer", - value: { - source_port: "testport", - source_channel: "testchannel", - token: coin(1234, "utest"), - sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - receiver: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - timeout_height: { - revision_height: undefined, - revision_number: undefined, - }, - timeout_timestamp: undefined, - }, - }; - expect(aminoMsg).toEqual(expected); - }); - - it("works for MsgTransfer with no height timeout", () => { - const msg: MsgTransfer = { - sourcePort: "testport", - sourceChannel: "testchannel", - token: coin(1234, "utest"), - sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - receiver: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - timeoutHeight: undefined, - timeoutTimestamp: Long.UZERO, - }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ - typeUrl: "/ibc.applications.transfer.v1.MsgTransfer", - value: msg, - }); - const expected: AminoMsgTransfer = { - type: "cosmos-sdk/MsgTransfer", - value: { - source_port: "testport", - source_channel: "testchannel", - token: coin(1234, "utest"), - sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - receiver: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - timeout_height: {}, - timeout_timestamp: undefined, - }, - }; - expect(aminoMsg).toEqual(expected); - }); - - // other - it("works with custom type url", () => { const msg = { foo: "bar", }; const aminoMsg = new AminoTypes({ - prefix: "cosmos", - additions: { - "/my.CustomType": { - aminoType: "my-sdk/CustomType", - toAmino: ({ - foo, - }: { - readonly foo: string; - }): { readonly foo: string; readonly constant: string } => ({ - foo: `amino-prefix-${foo}`, - constant: "something-for-amino", - }), - fromAmino: () => {}, - }, + "/my.CustomType": { + aminoType: "my-sdk/CustomType", + toAmino: ({ + foo, + }: { + readonly foo: string; + }): { readonly foo: string; readonly constant: string } => ({ + foo: `amino-prefix-${foo}`, + constant: "something-for-amino", + }), + fromAmino: () => {}, }, }).toAmino({ typeUrl: "/my.CustomType", value: msg }); expect(aminoMsg).toEqual({ @@ -619,15 +108,12 @@ describe("AminoTypes", () => { amount: coin(1234, "ucosm"), }; const aminoMsg = new AminoTypes({ - prefix: "cosmos", - additions: { - "/cosmos.staking.v1beta1.MsgDelegate": { - aminoType: "my-override/MsgDelegate", - toAmino: (m: MsgDelegate): { readonly foo: string } => ({ - foo: m.delegatorAddress ?? "", - }), - fromAmino: () => {}, - }, + "/cosmos.staking.v1beta1.MsgDelegate": { + aminoType: "my-override/MsgDelegate", + toAmino: (m: MsgDelegate): { readonly foo: string } => ({ + foo: m.delegatorAddress ?? "", + }), + fromAmino: () => {}, }, }).toAmino({ typeUrl: "/cosmos.staking.v1beta1.MsgDelegate", @@ -644,381 +130,15 @@ describe("AminoTypes", () => { it("throws for unknown type url", () => { expect(() => - new AminoTypes({ prefix: "cosmos" }).toAmino({ typeUrl: "/xxx.Unknown", value: { foo: "bar" } }), + new AminoTypes(createBankAminoConverters()).toAmino({ + typeUrl: "/xxx.Unknown", + value: { foo: "bar" }, + }), ).toThrowError(/Type URL '\/xxx\.Unknown' does not exist in the Amino message type register./i); }); }); describe("fromAmino", () => { - // bank - - it("works for MsgSend", () => { - const aminoMsg: AminoMsgSend = { - type: "cosmos-sdk/MsgSend", - value: { - from_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - to_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - amount: coins(1234, "ucosm"), - }, - }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); - const expectedValue: MsgSend = { - fromAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - toAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - amount: coins(1234, "ucosm"), - }; - expect(msg).toEqual({ - typeUrl: "/cosmos.bank.v1beta1.MsgSend", - value: expectedValue, - }); - }); - - it("works for MsgMultiSend", () => { - const aminoMsg: AminoMsgMultiSend = { - type: "cosmos-sdk/MsgMultiSend", - value: { - inputs: [ - { address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", coins: coins(1234, "ucosm") }, - { address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", coins: coins(5678, "ucosm") }, - ], - outputs: [ - { address: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", coins: coins(6000, "ucosm") }, - { address: "cosmos142u9fgcjdlycfcez3lw8x6x5h7rfjlnfhpw2lx", coins: coins(912, "ucosm") }, - ], - }, - }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); - const expectedValue: MsgMultiSend = { - inputs: [ - { address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", coins: coins(1234, "ucosm") }, - { address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", coins: coins(5678, "ucosm") }, - ], - outputs: [ - { address: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", coins: coins(6000, "ucosm") }, - { address: "cosmos142u9fgcjdlycfcez3lw8x6x5h7rfjlnfhpw2lx", coins: coins(912, "ucosm") }, - ], - }; - expect(msg).toEqual({ - typeUrl: "/cosmos.bank.v1beta1.MsgMultiSend", - value: expectedValue, - }); - }); - - // gov - - it("works for MsgDeposit", () => { - const aminoMsg: AminoMsgDeposit = { - type: "cosmos-sdk/MsgDeposit", - value: { - amount: [{ amount: "12300000", denom: "ustake" }], - depositor: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - proposal_id: "5", - }, - }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); - const expectedValue: MsgDeposit = { - amount: [{ amount: "12300000", denom: "ustake" }], - depositor: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - proposalId: Long.fromNumber(5), - }; - expect(msg).toEqual({ - typeUrl: "/cosmos.gov.v1beta1.MsgDeposit", - value: expectedValue, - }); - }); - - it("works for MsgSubmitProposal", () => { - const aminoMsg: AminoMsgSubmitProposal = { - type: "cosmos-sdk/MsgSubmitProposal", - value: { - initial_deposit: [{ amount: "12300000", denom: "ustake" }], - proposer: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - content: { - type: "cosmos-sdk/TextProposal", - value: { - description: "This proposal proposes to test whether this proposal passes", - title: "Test Proposal", - }, - }, - }, - }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); - const expectedValue: MsgSubmitProposal = { - initialDeposit: [{ amount: "12300000", denom: "ustake" }], - proposer: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - content: { - typeUrl: "/cosmos.gov.v1beta1.TextProposal", - value: TextProposal.encode({ - description: "This proposal proposes to test whether this proposal passes", - title: "Test Proposal", - }).finish(), - }, - }; - expect(msg).toEqual({ - typeUrl: "/cosmos.gov.v1beta1.MsgSubmitProposal", - value: expectedValue, - }); - }); - - it("works for MsgVote", () => { - const aminoMsg: AminoMsgVote = { - type: "cosmos-sdk/MsgVote", - value: { - option: 4, - proposal_id: "5", - voter: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", - }, - }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); - const expectedValue: MsgVote = { - option: VoteOption.VOTE_OPTION_NO_WITH_VETO, - proposalId: Long.fromNumber(5), - voter: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", - }; - expect(msg).toEqual({ - typeUrl: "/cosmos.gov.v1beta1.MsgVote", - value: expectedValue, - }); - }); - - // distribution - - // TODO: MsgFundCommunityPool - // TODO: MsgSetWithdrawAddress - // TODO: MsgWithdrawDelegatorReward - // TODO: MsgWithdrawValidatorCommission - - // staking - - it("works for MsgBeginRedelegate", () => { - const aminoMsg: AminoMsgBeginRedelegate = { - type: "cosmos-sdk/MsgBeginRedelegate", - value: { - delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - validator_src_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - validator_dst_address: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", - amount: coin(1234, "ucosm"), - }, - }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); - const expectedValue: MsgBeginRedelegate = { - delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - validatorSrcAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - validatorDstAddress: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", - amount: coin(1234, "ucosm"), - }; - expect(msg).toEqual({ - typeUrl: "/cosmos.staking.v1beta1.MsgBeginRedelegate", - value: expectedValue, - }); - }); - - it("works for MsgCreateValidator", () => { - const aminoMsg: AminoMsgCreateValidator = { - type: "cosmos-sdk/MsgCreateValidator", - value: { - description: { - moniker: "validator", - identity: "me", - website: "valid.com", - security_contact: "Hamburglar", - details: "...", - }, - commission: { - rate: "0.2", - max_rate: "0.3", - max_change_rate: "0.1", - }, - min_self_delegation: "123", - delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - pubkey: encodeBech32Pubkey( - { type: "tendermint/PubKeySecp256k1", value: "A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ" }, - "cosmos", - ), - value: coin(1234, "ucosm"), - }, - }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); - const expectedValue: MsgCreateValidator = { - description: { - moniker: "validator", - identity: "me", - website: "valid.com", - securityContact: "Hamburglar", - details: "...", - }, - commission: { - rate: "0.2", - maxRate: "0.3", - maxChangeRate: "0.1", - }, - minSelfDelegation: "123", - delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - pubkey: { - typeUrl: "/cosmos.crypto.secp256k1.PubKey", - value: fromBase64("A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ"), - }, - value: coin(1234, "ucosm"), - }; - expect(msg).toEqual({ - typeUrl: "/cosmos.staking.v1beta1.MsgCreateValidator", - value: expectedValue, - }); - }); - - it("works for MsgDelegate", () => { - const aminoMsg: AminoMsgDelegate = { - type: "cosmos-sdk/MsgDelegate", - value: { - delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - amount: coin(1234, "ucosm"), - }, - }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); - const expectedValue: MsgDelegate = { - delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - amount: coin(1234, "ucosm"), - }; - expect(msg).toEqual({ - typeUrl: "/cosmos.staking.v1beta1.MsgDelegate", - value: expectedValue, - }); - }); - - it("works for MsgEditValidator", () => { - const aminoMsg: AminoMsgEditValidator = { - type: "cosmos-sdk/MsgEditValidator", - value: { - description: { - moniker: "validator", - identity: "me", - website: "valid.com", - security_contact: "Hamburglar", - details: "...", - }, - commission_rate: "0.2", - min_self_delegation: "123", - validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - }, - }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); - const expectedValue: MsgEditValidator = { - description: { - moniker: "validator", - identity: "me", - website: "valid.com", - securityContact: "Hamburglar", - details: "...", - }, - commissionRate: "0.2", - minSelfDelegation: "123", - validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - }; - expect(msg).toEqual({ - typeUrl: "/cosmos.staking.v1beta1.MsgEditValidator", - value: expectedValue, - }); - }); - - it("works for MsgUndelegate", () => { - const aminoMsg: AminoMsgUndelegate = { - type: "cosmos-sdk/MsgUndelegate", - value: { - delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - amount: coin(1234, "ucosm"), - }, - }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); - const expectedValue: MsgUndelegate = { - delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - amount: coin(1234, "ucosm"), - }; - expect(msg).toEqual({ - typeUrl: "/cosmos.staking.v1beta1.MsgUndelegate", - value: expectedValue, - }); - }); - - // ibc - - it("works for MsgTransfer", () => { - const aminoMsg: AminoMsgTransfer = { - type: "cosmos-sdk/MsgTransfer", - value: { - source_port: "testport", - source_channel: "testchannel", - token: coin(1234, "utest"), - sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - receiver: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - timeout_height: { - revision_height: "123", - revision_number: "456", - }, - timeout_timestamp: "789", - }, - }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); - const expectedValue: MsgTransfer = { - sourcePort: "testport", - sourceChannel: "testchannel", - token: coin(1234, "utest"), - sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - receiver: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - timeoutHeight: { - revisionHeight: Long.fromString("123", true), - revisionNumber: Long.fromString("456", true), - }, - timeoutTimestamp: Long.fromString("789", true), - }; - expect(msg).toEqual({ - typeUrl: "/ibc.applications.transfer.v1.MsgTransfer", - value: expectedValue, - }); - }); - - it("works for MsgTransfer with default values", () => { - const aminoMsg: AminoMsgTransfer = { - type: "cosmos-sdk/MsgTransfer", - value: { - source_port: "testport", - source_channel: "testchannel", - token: coin(1234, "utest"), - sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - receiver: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - timeout_height: { - // revision_height omitted - // revision_number omitted - }, - // timeout_timestamp omitted - }, - }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); - const expectedValue: MsgTransfer = { - sourcePort: "testport", - sourceChannel: "testchannel", - token: coin(1234, "utest"), - sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", - receiver: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - timeoutHeight: { - revisionHeight: Long.UZERO, - revisionNumber: Long.UZERO, - }, - timeoutTimestamp: Long.UZERO, - }; - expect(msg).toEqual({ - typeUrl: "/ibc.applications.transfer.v1.MsgTransfer", - value: expectedValue, - }); - }); - - // other - it("works for custom type url", () => { const aminoMsg = { type: "my-sdk/CustomType", @@ -1028,15 +148,12 @@ describe("AminoTypes", () => { }, }; const msg = new AminoTypes({ - prefix: "cosmos", - additions: { - "/my.CustomType": { - aminoType: "my-sdk/CustomType", - toAmino: () => {}, - fromAmino: ({ foo }: { readonly foo: string; readonly constant: string }): any => ({ - foo: foo.slice(13), - }), - }, + "/my.CustomType": { + aminoType: "my-sdk/CustomType", + toAmino: () => {}, + fromAmino: ({ foo }: { readonly foo: string; readonly constant: string }): any => ({ + foo: foo.slice(13), + }), }, }).fromAmino(aminoMsg); const expectedValue = { @@ -1050,17 +167,14 @@ describe("AminoTypes", () => { it("works with overridden type URL", () => { const msg = new AminoTypes({ - prefix: "cosmos", - additions: { - "/cosmos.staking.v1beta1.MsgDelegate": { - aminoType: "cosmos-sdk/MsgDelegate2", - toAmino: () => {}, - fromAmino: ({ foo }: { readonly foo: string }): MsgDelegate => ({ - delegatorAddress: foo, - validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", - amount: coin(1234, "ucosm"), - }), - }, + "/cosmos.staking.v1beta1.MsgDelegate": { + aminoType: "cosmos-sdk/MsgDelegate2", + toAmino: () => {}, + fromAmino: ({ foo }: { readonly foo: string }): MsgDelegate => ({ + delegatorAddress: foo, + validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + amount: coin(1234, "ucosm"), + }), }, }).fromAmino({ type: "cosmos-sdk/MsgDelegate2", @@ -1080,7 +194,7 @@ describe("AminoTypes", () => { it("throws for types which are not on chain yet", () => { expect(() => { - new AminoTypes({ prefix: "cosmos" }).toAmino({ + new AminoTypes({ "/cosmos.feegrant.v1beta1.MsgRevokeAllowance": "not_supported_by_chain" }).toAmino({ typeUrl: "/cosmos.feegrant.v1beta1.MsgRevokeAllowance", value: 0, }); @@ -1091,7 +205,7 @@ describe("AminoTypes", () => { it("throws for unknown type url", () => { expect(() => - new AminoTypes({ prefix: "cosmos" }).fromAmino({ + new AminoTypes(createBankAminoConverters()).fromAmino({ type: "cosmos-sdk/MsgUnknown", value: { foo: "bar" }, }), diff --git a/packages/stargate/src/aminotypes.ts b/packages/stargate/src/aminotypes.ts index 236705babd..cd7f83b0d8 100644 --- a/packages/stargate/src/aminotypes.ts +++ b/packages/stargate/src/aminotypes.ts @@ -2,36 +2,14 @@ import { AminoMsg } from "@cosmjs/amino"; import { EncodeObject } from "@cosmjs/proto-signing"; -import { AminoConverter, AminoConverters } from "./aminoconverters"; -import { - createAuthzAminoConverters, - createBankAminoConverters, - createDistributionAminoConverters, - createFreegrantAminoConverters, - createGovAminoConverters, - createIbcAminoConverters, - createStakingAminoConverters, -} from "./modules"; - -function createDefaultTypes(prefix: string): AminoConverters { - return { - ...createAuthzAminoConverters(), - ...createBankAminoConverters(), - ...createDistributionAminoConverters(), - ...createGovAminoConverters(), - ...createStakingAminoConverters(prefix), - ...createIbcAminoConverters(), - ...createFreegrantAminoConverters(), - }; +export interface AminoConverter { + readonly aminoType: string; + readonly toAmino: (value: any) => any; + readonly fromAmino: (value: any) => any; } -export interface AminoTypesOptions { - /** - * The Bech32 address prefix of the chain you work with (also called Bech32 human-readable part). - */ - readonly prefix: string; - readonly additions?: AminoConverters; -} +/** A map from protobuf type URL to the AminoConverter implementation if supported on chain */ +export type AminoConverters = Record; function isAminoConverter( converter: [string, AminoConverter | "not_supported_by_chain"], @@ -50,9 +28,8 @@ export class AminoTypes { // there is no overlap when fromAmino is called. private readonly register: Record; - public constructor({ prefix, additions = {} }: AminoTypesOptions) { - const defaultTypes = createDefaultTypes(prefix); - this.register = { ...defaultTypes, ...additions }; + public constructor(types: AminoConverters) { + this.register = types; } public toAmino({ typeUrl, value }: EncodeObject): AminoMsg { diff --git a/packages/stargate/src/index.ts b/packages/stargate/src/index.ts index 5cf0c01dae..c85a447c3f 100644 --- a/packages/stargate/src/index.ts +++ b/packages/stargate/src/index.ts @@ -1,6 +1,5 @@ export { Account, accountFromAny } from "./accounts"; -export { AminoConverter, AminoConverters } from "./aminoconverters"; -export { AminoTypes, AminoTypesOptions } from "./aminotypes"; +export { AminoConverter, AminoConverters, AminoTypes } from "./aminotypes"; export { calculateFee, GasPrice } from "./fee"; export * as logs from "./logs"; export { @@ -74,6 +73,15 @@ export { StakingExtension, TxExtension, } from "./modules"; +export { + createAuthzAminoConverters, + createBankAminoConverters, + createDistributionAminoConverters, + createFreegrantAminoConverters, + createGovAminoConverters, + createIbcAminoConverters, + createStakingAminoConverters, +} from "./modules"; export { makeMultisignedTx } from "./multisignature"; export { createPagination, diff --git a/packages/stargate/src/modules/authz/aminomessages.ts b/packages/stargate/src/modules/authz/aminomessages.ts index 149fdf6dd0..f5d706a838 100644 --- a/packages/stargate/src/modules/authz/aminomessages.ts +++ b/packages/stargate/src/modules/authz/aminomessages.ts @@ -1,4 +1,4 @@ -import { AminoConverters } from "../../aminoconverters"; +import { AminoConverters } from "../../aminotypes"; export function createAuthzAminoConverters(): AminoConverters { return { diff --git a/packages/stargate/src/modules/bank/aminomessages.spec.ts b/packages/stargate/src/modules/bank/aminomessages.spec.ts new file mode 100644 index 0000000000..54669cefbc --- /dev/null +++ b/packages/stargate/src/modules/bank/aminomessages.spec.ts @@ -0,0 +1,118 @@ +/* eslint-disable @typescript-eslint/naming-convention */ +import { coins } from "@cosmjs/proto-signing"; +import { MsgMultiSend, MsgSend } from "cosmjs-types/cosmos/bank/v1beta1/tx"; + +import { AminoTypes } from "../../aminotypes"; +import { AminoMsgMultiSend, AminoMsgSend, createBankAminoConverters } from "./aminomessages"; + +describe("AminoTypes", () => { + describe("toAmino", () => { + it("works for MsgSend", () => { + const msg: MsgSend = { + fromAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + toAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + amount: coins(1234, "ucosm"), + }; + const aminoTypes = new AminoTypes(createBankAminoConverters()); + const aminoMsg = aminoTypes.toAmino({ + typeUrl: "/cosmos.bank.v1beta1.MsgSend", + value: msg, + }); + const expected: AminoMsgSend = { + type: "cosmos-sdk/MsgSend", + value: { + from_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + to_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + amount: coins(1234, "ucosm"), + }, + }; + expect(aminoMsg).toEqual(expected); + }); + + it("works for MsgMultiSend", () => { + const msg: MsgMultiSend = { + inputs: [ + { address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", coins: coins(1234, "ucosm") }, + { address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", coins: coins(5678, "ucosm") }, + ], + outputs: [ + { address: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", coins: coins(6000, "ucosm") }, + { address: "cosmos142u9fgcjdlycfcez3lw8x6x5h7rfjlnfhpw2lx", coins: coins(912, "ucosm") }, + ], + }; + const aminoTypes = new AminoTypes(createBankAminoConverters()); + const aminoMsg = aminoTypes.toAmino({ + typeUrl: "/cosmos.bank.v1beta1.MsgMultiSend", + value: msg, + }); + const expected: AminoMsgMultiSend = { + type: "cosmos-sdk/MsgMultiSend", + value: { + inputs: [ + { address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", coins: coins(1234, "ucosm") }, + { address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", coins: coins(5678, "ucosm") }, + ], + outputs: [ + { address: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", coins: coins(6000, "ucosm") }, + { address: "cosmos142u9fgcjdlycfcez3lw8x6x5h7rfjlnfhpw2lx", coins: coins(912, "ucosm") }, + ], + }, + }; + expect(aminoMsg).toEqual(expected); + }); + }); + + describe("fromAmino", () => { + it("works for MsgSend", () => { + const aminoMsg: AminoMsgSend = { + type: "cosmos-sdk/MsgSend", + value: { + from_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + to_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + amount: coins(1234, "ucosm"), + }, + }; + const msg = new AminoTypes(createBankAminoConverters()).fromAmino(aminoMsg); + const expectedValue: MsgSend = { + fromAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + toAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + amount: coins(1234, "ucosm"), + }; + expect(msg).toEqual({ + typeUrl: "/cosmos.bank.v1beta1.MsgSend", + value: expectedValue, + }); + }); + + it("works for MsgMultiSend", () => { + const aminoMsg: AminoMsgMultiSend = { + type: "cosmos-sdk/MsgMultiSend", + value: { + inputs: [ + { address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", coins: coins(1234, "ucosm") }, + { address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", coins: coins(5678, "ucosm") }, + ], + outputs: [ + { address: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", coins: coins(6000, "ucosm") }, + { address: "cosmos142u9fgcjdlycfcez3lw8x6x5h7rfjlnfhpw2lx", coins: coins(912, "ucosm") }, + ], + }, + }; + const msg = new AminoTypes(createBankAminoConverters()).fromAmino(aminoMsg); + const expectedValue: MsgMultiSend = { + inputs: [ + { address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", coins: coins(1234, "ucosm") }, + { address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", coins: coins(5678, "ucosm") }, + ], + outputs: [ + { address: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", coins: coins(6000, "ucosm") }, + { address: "cosmos142u9fgcjdlycfcez3lw8x6x5h7rfjlnfhpw2lx", coins: coins(912, "ucosm") }, + ], + }; + expect(msg).toEqual({ + typeUrl: "/cosmos.bank.v1beta1.MsgMultiSend", + value: expectedValue, + }); + }); + }); +}); diff --git a/packages/stargate/src/modules/bank/aminomessages.ts b/packages/stargate/src/modules/bank/aminomessages.ts index f74530be49..e990001df7 100644 --- a/packages/stargate/src/modules/bank/aminomessages.ts +++ b/packages/stargate/src/modules/bank/aminomessages.ts @@ -3,7 +3,7 @@ import { AminoMsg, Coin } from "@cosmjs/amino"; import { MsgMultiSend, MsgSend } from "cosmjs-types/cosmos/bank/v1beta1/tx"; // eslint-disable-next-line import/no-cycle -import { AminoConverters } from "../../aminoconverters"; +import { AminoConverters } from "../../aminotypes"; /** A high level transaction of the coin module */ export interface AminoMsgSend extends AminoMsg { diff --git a/packages/stargate/src/modules/crisis/aminomessages.ts b/packages/stargate/src/modules/crisis/aminomessages.ts index 8e6a959d7a..a8f4721de8 100644 --- a/packages/stargate/src/modules/crisis/aminomessages.ts +++ b/packages/stargate/src/modules/crisis/aminomessages.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/naming-convention */ import { AminoMsg } from "@cosmjs/amino"; -import { AminoConverters } from "../../aminoconverters"; +import { AminoConverters } from "../../aminotypes"; // See https://github.com/cosmos/cosmos-sdk/blob/v0.45.1/proto/cosmos/crisis/v1beta1/tx.proto diff --git a/packages/stargate/src/modules/distribution/aminomessages.spec.ts b/packages/stargate/src/modules/distribution/aminomessages.spec.ts new file mode 100644 index 0000000000..0e6ae9dd95 --- /dev/null +++ b/packages/stargate/src/modules/distribution/aminomessages.spec.ts @@ -0,0 +1,106 @@ +/* eslint-disable @typescript-eslint/naming-convention */ +import { coins } from "@cosmjs/proto-signing"; +import { + MsgFundCommunityPool, + MsgSetWithdrawAddress, + MsgWithdrawDelegatorReward, + MsgWithdrawValidatorCommission, +} from "cosmjs-types/cosmos/distribution/v1beta1/tx"; + +import { AminoTypes } from "../../aminotypes"; +import { + AminoMsgFundCommunityPool, + AminoMsgSetWithdrawAddress, + AminoMsgWithdrawDelegatorReward, + AminoMsgWithdrawValidatorCommission, + createDistributionAminoConverters, +} from "./aminomessages"; + +describe("AminoTypes", () => { + describe("toAmino", () => { + it("works for MsgFundCommunityPool", async () => { + const msg: MsgFundCommunityPool = { + amount: coins(1234, "ucosm"), + depositor: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + }; + const aminoTypes = new AminoTypes(createDistributionAminoConverters()); + const aminoMsg = aminoTypes.toAmino({ + typeUrl: "/cosmos.distribution.v1beta1.MsgFundCommunityPool", + value: msg, + }); + const expected: AminoMsgFundCommunityPool = { + type: "cosmos-sdk/MsgFundCommunityPool", + value: { + amount: coins(1234, "ucosm"), + depositor: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + }, + }; + expect(aminoMsg).toEqual(expected); + }); + + it("works for MsgSetWithdrawAddress", async () => { + const msg: MsgSetWithdrawAddress = { + delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + withdrawAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + }; + const aminoTypes = new AminoTypes(createDistributionAminoConverters()); + const aminoMsg = aminoTypes.toAmino({ + typeUrl: "/cosmos.distribution.v1beta1.MsgSetWithdrawAddress", + value: msg, + }); + const expected: AminoMsgSetWithdrawAddress = { + type: "cosmos-sdk/MsgModifyWithdrawAddress", + value: { + delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + withdraw_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + }, + }; + expect(aminoMsg).toEqual(expected); + }); + + it("works for MsgWithdrawDelegatorReward", async () => { + const msg: MsgWithdrawDelegatorReward = { + delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + }; + const aminoTypes = new AminoTypes(createDistributionAminoConverters()); + const aminoMsg = aminoTypes.toAmino({ + typeUrl: "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward", + value: msg, + }); + const expected: AminoMsgWithdrawDelegatorReward = { + type: "cosmos-sdk/MsgWithdrawDelegationReward", + value: { + delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + }, + }; + expect(aminoMsg).toEqual(expected); + }); + + it("works for MsgWithdrawValidatorCommission", async () => { + const msg: MsgWithdrawValidatorCommission = { + validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + }; + const aminoTypes = new AminoTypes(createDistributionAminoConverters()); + const aminoMsg = aminoTypes.toAmino({ + typeUrl: "/cosmos.distribution.v1beta1.MsgWithdrawValidatorCommission", + value: msg, + }); + const expected: AminoMsgWithdrawValidatorCommission = { + type: "cosmos-sdk/MsgWithdrawValidatorCommission", + value: { + validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + }, + }; + expect(aminoMsg).toEqual(expected); + }); + }); + + describe("fromAmino", () => { + // TODO: MsgFundCommunityPool + // TODO: MsgSetWithdrawAddress + // TODO: MsgWithdrawDelegatorReward + // TODO: MsgWithdrawValidatorCommission + }); +}); diff --git a/packages/stargate/src/modules/evidence/aminomessages.ts b/packages/stargate/src/modules/evidence/aminomessages.ts index 6aafcafaf7..2c94fcf971 100644 --- a/packages/stargate/src/modules/evidence/aminomessages.ts +++ b/packages/stargate/src/modules/evidence/aminomessages.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/naming-convention */ import { AminoMsg } from "@cosmjs/amino"; -import { AminoConverters } from "../../aminoconverters"; +import { AminoConverters } from "../../aminotypes"; // See https://github.com/cosmos/cosmos-sdk/blob/v0.45.1/proto/cosmos/evidence/v1beta1/tx.proto diff --git a/packages/stargate/src/modules/feegrant/aminomessages.ts b/packages/stargate/src/modules/feegrant/aminomessages.ts index 7f6f8db18a..cad785bdec 100644 --- a/packages/stargate/src/modules/feegrant/aminomessages.ts +++ b/packages/stargate/src/modules/feegrant/aminomessages.ts @@ -1,4 +1,4 @@ -import { AminoConverters } from "../../aminoconverters"; +import { AminoConverters } from "../../aminotypes"; export function createFreegrantAminoConverters(): AminoConverters { return { diff --git a/packages/stargate/src/modules/gov/aminomessages.spec.ts b/packages/stargate/src/modules/gov/aminomessages.spec.ts new file mode 100644 index 0000000000..05861f952a --- /dev/null +++ b/packages/stargate/src/modules/gov/aminomessages.spec.ts @@ -0,0 +1,171 @@ +/* eslint-disable @typescript-eslint/naming-convention */ +import { TextProposal, VoteOption } from "cosmjs-types/cosmos/gov/v1beta1/gov"; +import { MsgDeposit, MsgSubmitProposal, MsgVote } from "cosmjs-types/cosmos/gov/v1beta1/tx"; +import Long from "long"; + +import { AminoTypes } from "../../aminotypes"; +import { + AminoMsgDeposit, + AminoMsgSubmitProposal, + AminoMsgVote, + createGovAminoConverters, +} from "./aminomessages"; + +describe("AminoTypes", () => { + describe("toAmino", () => { + it("works for MsgDeposit", () => { + const msg: MsgDeposit = { + amount: [{ amount: "12300000", denom: "ustake" }], + depositor: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + proposalId: Long.fromNumber(5), + }; + const aminoTypes = new AminoTypes(createGovAminoConverters()); + const aminoMsg = aminoTypes.toAmino({ + typeUrl: "/cosmos.gov.v1beta1.MsgDeposit", + value: msg, + }); + const expected: AminoMsgDeposit = { + type: "cosmos-sdk/MsgDeposit", + value: { + amount: [{ amount: "12300000", denom: "ustake" }], + depositor: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + proposal_id: "5", + }, + }; + expect(aminoMsg).toEqual(expected); + }); + + it("works for MsgSubmitProposal", () => { + const msg: MsgSubmitProposal = { + initialDeposit: [{ amount: "12300000", denom: "ustake" }], + proposer: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + content: { + typeUrl: "/cosmos.gov.v1beta1.TextProposal", + value: TextProposal.encode({ + description: "This proposal proposes to test whether this proposal passes", + title: "Test Proposal", + }).finish(), + }, + }; + const aminoTypes = new AminoTypes(createGovAminoConverters()); + const aminoMsg = aminoTypes.toAmino({ + typeUrl: "/cosmos.gov.v1beta1.MsgSubmitProposal", + value: msg, + }); + const expected: AminoMsgSubmitProposal = { + type: "cosmos-sdk/MsgSubmitProposal", + value: { + initial_deposit: [{ amount: "12300000", denom: "ustake" }], + proposer: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + content: { + type: "cosmos-sdk/TextProposal", + value: { + description: "This proposal proposes to test whether this proposal passes", + title: "Test Proposal", + }, + }, + }, + }; + expect(aminoMsg).toEqual(expected); + }); + + it("works for MsgVote", () => { + const msg: MsgVote = { + option: VoteOption.VOTE_OPTION_NO_WITH_VETO, + proposalId: Long.fromNumber(5), + voter: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", + }; + const aminoTypes = new AminoTypes(createGovAminoConverters()); + const aminoMsg = aminoTypes.toAmino({ + typeUrl: "/cosmos.gov.v1beta1.MsgVote", + value: msg, + }); + const expected: AminoMsgVote = { + type: "cosmos-sdk/MsgVote", + value: { + option: 4, + proposal_id: "5", + voter: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", + }, + }; + expect(aminoMsg).toEqual(expected); + }); + }); + + describe("fromAmino", () => { + it("works for MsgDeposit", () => { + const aminoMsg: AminoMsgDeposit = { + type: "cosmos-sdk/MsgDeposit", + value: { + amount: [{ amount: "12300000", denom: "ustake" }], + depositor: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + proposal_id: "5", + }, + }; + const msg = new AminoTypes(createGovAminoConverters()).fromAmino(aminoMsg); + const expectedValue: MsgDeposit = { + amount: [{ amount: "12300000", denom: "ustake" }], + depositor: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + proposalId: Long.fromNumber(5), + }; + expect(msg).toEqual({ + typeUrl: "/cosmos.gov.v1beta1.MsgDeposit", + value: expectedValue, + }); + }); + + it("works for MsgSubmitProposal", () => { + const aminoMsg: AminoMsgSubmitProposal = { + type: "cosmos-sdk/MsgSubmitProposal", + value: { + initial_deposit: [{ amount: "12300000", denom: "ustake" }], + proposer: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + content: { + type: "cosmos-sdk/TextProposal", + value: { + description: "This proposal proposes to test whether this proposal passes", + title: "Test Proposal", + }, + }, + }, + }; + const msg = new AminoTypes(createGovAminoConverters()).fromAmino(aminoMsg); + const expectedValue: MsgSubmitProposal = { + initialDeposit: [{ amount: "12300000", denom: "ustake" }], + proposer: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + content: { + typeUrl: "/cosmos.gov.v1beta1.TextProposal", + value: TextProposal.encode({ + description: "This proposal proposes to test whether this proposal passes", + title: "Test Proposal", + }).finish(), + }, + }; + expect(msg).toEqual({ + typeUrl: "/cosmos.gov.v1beta1.MsgSubmitProposal", + value: expectedValue, + }); + }); + + it("works for MsgVote", () => { + const aminoMsg: AminoMsgVote = { + type: "cosmos-sdk/MsgVote", + value: { + option: 4, + proposal_id: "5", + voter: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", + }, + }; + const msg = new AminoTypes(createGovAminoConverters()).fromAmino(aminoMsg); + const expectedValue: MsgVote = { + option: VoteOption.VOTE_OPTION_NO_WITH_VETO, + proposalId: Long.fromNumber(5), + voter: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", + }; + expect(msg).toEqual({ + typeUrl: "/cosmos.gov.v1beta1.MsgVote", + value: expectedValue, + }); + }); + }); +}); diff --git a/packages/stargate/src/modules/gov/aminomessages.ts b/packages/stargate/src/modules/gov/aminomessages.ts index cb48cfd0a5..67ba88d276 100644 --- a/packages/stargate/src/modules/gov/aminomessages.ts +++ b/packages/stargate/src/modules/gov/aminomessages.ts @@ -6,7 +6,7 @@ import { MsgDeposit, MsgSubmitProposal, MsgVote } from "cosmjs-types/cosmos/gov/ import { Any } from "cosmjs-types/google/protobuf/any"; import Long from "long"; -import { AminoConverters } from "../../aminoconverters"; +import { AminoConverters } from "../../aminotypes"; /** Supports submitting arbitrary proposal content. */ export interface AminoMsgSubmitProposal extends AminoMsg { diff --git a/packages/stargate/src/modules/ibc/aminomessages.spec.ts b/packages/stargate/src/modules/ibc/aminomessages.spec.ts new file mode 100644 index 0000000000..f4d7a63645 --- /dev/null +++ b/packages/stargate/src/modules/ibc/aminomessages.spec.ts @@ -0,0 +1,184 @@ +/* eslint-disable @typescript-eslint/naming-convention */ +import { coin } from "@cosmjs/proto-signing"; +import { MsgTransfer } from "cosmjs-types/ibc/applications/transfer/v1/tx"; +import Long from "long"; + +import { AminoTypes } from "../../aminotypes"; +import { AminoMsgTransfer, createIbcAminoConverters } from "./aminomessages"; + +describe("AminoTypes", () => { + describe("toAmino", () => { + it("works for MsgTransfer", () => { + const msg: MsgTransfer = { + sourcePort: "testport", + sourceChannel: "testchannel", + token: coin(1234, "utest"), + sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + receiver: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + timeoutHeight: { + revisionHeight: Long.fromString("123", true), + revisionNumber: Long.fromString("456", true), + }, + timeoutTimestamp: Long.fromString("789", true), + }; + const aminoTypes = new AminoTypes(createIbcAminoConverters()); + const aminoMsg = aminoTypes.toAmino({ + typeUrl: "/ibc.applications.transfer.v1.MsgTransfer", + value: msg, + }); + const expected: AminoMsgTransfer = { + type: "cosmos-sdk/MsgTransfer", + value: { + source_port: "testport", + source_channel: "testchannel", + token: coin(1234, "utest"), + sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + receiver: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + timeout_height: { + revision_height: "123", + revision_number: "456", + }, + timeout_timestamp: "789", + }, + }; + expect(aminoMsg).toEqual(expected); + }); + + it("works for MsgTransfer with empty values", () => { + const msg: MsgTransfer = { + sourcePort: "testport", + sourceChannel: "testchannel", + token: coin(1234, "utest"), + sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + receiver: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + timeoutHeight: { + revisionHeight: Long.UZERO, + revisionNumber: Long.UZERO, + }, + timeoutTimestamp: Long.UZERO, + }; + const aminoTypes = new AminoTypes(createIbcAminoConverters()); + const aminoMsg = aminoTypes.toAmino({ + typeUrl: "/ibc.applications.transfer.v1.MsgTransfer", + value: msg, + }); + const expected: AminoMsgTransfer = { + type: "cosmos-sdk/MsgTransfer", + value: { + source_port: "testport", + source_channel: "testchannel", + token: coin(1234, "utest"), + sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + receiver: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + timeout_height: { + revision_height: undefined, + revision_number: undefined, + }, + timeout_timestamp: undefined, + }, + }; + expect(aminoMsg).toEqual(expected); + }); + + it("works for MsgTransfer with no height timeout", () => { + const msg: MsgTransfer = { + sourcePort: "testport", + sourceChannel: "testchannel", + token: coin(1234, "utest"), + sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + receiver: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + timeoutHeight: undefined, + timeoutTimestamp: Long.UZERO, + }; + const aminoMsg = new AminoTypes(createIbcAminoConverters()).toAmino({ + typeUrl: "/ibc.applications.transfer.v1.MsgTransfer", + value: msg, + }); + const expected: AminoMsgTransfer = { + type: "cosmos-sdk/MsgTransfer", + value: { + source_port: "testport", + source_channel: "testchannel", + token: coin(1234, "utest"), + sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + receiver: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + timeout_height: {}, + timeout_timestamp: undefined, + }, + }; + expect(aminoMsg).toEqual(expected); + }); + }); + + describe("fromAmino", () => { + it("works for MsgTransfer", () => { + const aminoMsg: AminoMsgTransfer = { + type: "cosmos-sdk/MsgTransfer", + value: { + source_port: "testport", + source_channel: "testchannel", + token: coin(1234, "utest"), + sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + receiver: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + timeout_height: { + revision_height: "123", + revision_number: "456", + }, + timeout_timestamp: "789", + }, + }; + const msg = new AminoTypes(createIbcAminoConverters()).fromAmino(aminoMsg); + const expectedValue: MsgTransfer = { + sourcePort: "testport", + sourceChannel: "testchannel", + token: coin(1234, "utest"), + sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + receiver: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + timeoutHeight: { + revisionHeight: Long.fromString("123", true), + revisionNumber: Long.fromString("456", true), + }, + timeoutTimestamp: Long.fromString("789", true), + }; + expect(msg).toEqual({ + typeUrl: "/ibc.applications.transfer.v1.MsgTransfer", + value: expectedValue, + }); + }); + + it("works for MsgTransfer with default values", () => { + const aminoMsg: AminoMsgTransfer = { + type: "cosmos-sdk/MsgTransfer", + value: { + source_port: "testport", + source_channel: "testchannel", + token: coin(1234, "utest"), + sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + receiver: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + timeout_height: { + // revision_height omitted + // revision_number omitted + }, + // timeout_timestamp omitted + }, + }; + const msg = new AminoTypes(createIbcAminoConverters()).fromAmino(aminoMsg); + const expectedValue: MsgTransfer = { + sourcePort: "testport", + sourceChannel: "testchannel", + token: coin(1234, "utest"), + sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + receiver: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + timeoutHeight: { + revisionHeight: Long.UZERO, + revisionNumber: Long.UZERO, + }, + timeoutTimestamp: Long.UZERO, + }; + expect(msg).toEqual({ + typeUrl: "/ibc.applications.transfer.v1.MsgTransfer", + value: expectedValue, + }); + }); + }); +}); diff --git a/packages/stargate/src/modules/ibc/aminomessages.ts b/packages/stargate/src/modules/ibc/aminomessages.ts index 6fdb71b9ed..18f8d6b746 100644 --- a/packages/stargate/src/modules/ibc/aminomessages.ts +++ b/packages/stargate/src/modules/ibc/aminomessages.ts @@ -3,7 +3,7 @@ import { AminoMsg, Coin } from "@cosmjs/amino"; import { MsgTransfer } from "cosmjs-types/ibc/applications/transfer/v1/tx"; import Long from "long"; -import { AminoConverters } from "../../aminoconverters"; +import { AminoConverters } from "../../aminotypes"; // https://github.com/cosmos/ibc-go/blob/07b6a97b67d17fd214a83764cbdb2c2c3daef445/modules/core/02-client/types/client.pb.go#L297-L312 interface AminoHeight { diff --git a/packages/stargate/src/modules/slashing/aminomessages.ts b/packages/stargate/src/modules/slashing/aminomessages.ts index 5cbe7dd9a1..ccb535da17 100644 --- a/packages/stargate/src/modules/slashing/aminomessages.ts +++ b/packages/stargate/src/modules/slashing/aminomessages.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/naming-convention */ import { AminoMsg } from "@cosmjs/amino"; -import { AminoConverters } from "../../aminoconverters"; +import { AminoConverters } from "../../aminotypes"; // See https://github.com/cosmos/cosmos-sdk/blob/v0.45.1/proto/cosmos/slashing/v1beta1/tx.proto diff --git a/packages/stargate/src/modules/staking/aminomessages.spec.ts b/packages/stargate/src/modules/staking/aminomessages.spec.ts new file mode 100644 index 0000000000..ca8699d863 --- /dev/null +++ b/packages/stargate/src/modules/staking/aminomessages.spec.ts @@ -0,0 +1,342 @@ +/* eslint-disable @typescript-eslint/naming-convention */ +import { encodeBech32Pubkey } from "@cosmjs/amino"; +import { fromBase64 } from "@cosmjs/encoding"; +import { coin } from "@cosmjs/proto-signing"; +import { + MsgBeginRedelegate, + MsgCreateValidator, + MsgDelegate, + MsgEditValidator, + MsgUndelegate, +} from "cosmjs-types/cosmos/staking/v1beta1/tx"; + +import { AminoTypes } from "../../aminotypes"; +import { + AminoMsgBeginRedelegate, + AminoMsgCreateValidator, + AminoMsgDelegate, + AminoMsgEditValidator, + AminoMsgUndelegate, + createStakingAminoConverters, +} from "./aminomessages"; + +describe("AminoTypes", () => { + describe("toAmino", () => { + it("works for MsgBeginRedelegate", () => { + const msg: MsgBeginRedelegate = { + delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + validatorSrcAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + validatorDstAddress: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", + amount: coin(1234, "ucosm"), + }; + const aminoTypes = new AminoTypes(createStakingAminoConverters("cosmos")); + const aminoMsg = aminoTypes.toAmino({ + typeUrl: "/cosmos.staking.v1beta1.MsgBeginRedelegate", + value: msg, + }); + const expected: AminoMsgBeginRedelegate = { + type: "cosmos-sdk/MsgBeginRedelegate", + value: { + delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + validator_src_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + validator_dst_address: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", + amount: coin(1234, "ucosm"), + }, + }; + expect(aminoMsg).toEqual(expected); + }); + + it("works for MsgCreateValidator", () => { + const msg: MsgCreateValidator = { + description: { + moniker: "validator", + identity: "me", + website: "valid.com", + securityContact: "Hamburglar", + details: "...", + }, + commission: { + rate: "0.2", + maxRate: "0.3", + maxChangeRate: "0.1", + }, + minSelfDelegation: "123", + delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + pubkey: { + typeUrl: "/cosmos.crypto.secp256k1.PubKey", + value: fromBase64("A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ"), + }, + value: coin(1234, "ucosm"), + }; + const aminoTypes = new AminoTypes(createStakingAminoConverters("cosmos")); + const aminoMsg = aminoTypes.toAmino({ + typeUrl: "/cosmos.staking.v1beta1.MsgCreateValidator", + value: msg, + }); + const expected: AminoMsgCreateValidator = { + type: "cosmos-sdk/MsgCreateValidator", + value: { + description: { + moniker: "validator", + identity: "me", + website: "valid.com", + security_contact: "Hamburglar", + details: "...", + }, + commission: { + rate: "0.2", + max_rate: "0.3", + max_change_rate: "0.1", + }, + min_self_delegation: "123", + delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + pubkey: encodeBech32Pubkey( + { type: "tendermint/PubKeySecp256k1", value: "A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ" }, + "cosmos", + ), + value: coin(1234, "ucosm"), + }, + }; + expect(aminoMsg).toEqual(expected); + }); + + it("works for MsgDelegate", () => { + const msg: MsgDelegate = { + delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + amount: coin(1234, "ucosm"), + }; + const aminoTypes = new AminoTypes(createStakingAminoConverters("cosmos")); + const aminoMsg = aminoTypes.toAmino({ + typeUrl: "/cosmos.staking.v1beta1.MsgDelegate", + value: msg, + }); + const expected: AminoMsgDelegate = { + type: "cosmos-sdk/MsgDelegate", + value: { + delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + amount: coin(1234, "ucosm"), + }, + }; + expect(aminoMsg).toEqual(expected); + }); + + it("works for MsgEditValidator", () => { + const msg: MsgEditValidator = { + description: { + moniker: "validator", + identity: "me", + website: "valid.com", + securityContact: "Hamburglar", + details: "...", + }, + commissionRate: "0.2", + minSelfDelegation: "123", + validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + }; + const aminoTypes = new AminoTypes(createStakingAminoConverters("cosmos")); + const aminoMsg = aminoTypes.toAmino({ + typeUrl: "/cosmos.staking.v1beta1.MsgEditValidator", + value: msg, + }); + const expected: AminoMsgEditValidator = { + type: "cosmos-sdk/MsgEditValidator", + value: { + description: { + moniker: "validator", + identity: "me", + website: "valid.com", + security_contact: "Hamburglar", + details: "...", + }, + commission_rate: "0.2", + min_self_delegation: "123", + validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + }, + }; + expect(aminoMsg).toEqual(expected); + }); + + it("works for MsgUndelegate", () => { + const msg: MsgUndelegate = { + delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + amount: coin(1234, "ucosm"), + }; + const aminoTypes = new AminoTypes(createStakingAminoConverters("cosmos")); + const aminoMsg = aminoTypes.toAmino({ + typeUrl: "/cosmos.staking.v1beta1.MsgUndelegate", + value: msg, + }); + const expected: AminoMsgUndelegate = { + type: "cosmos-sdk/MsgUndelegate", + value: { + delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + amount: coin(1234, "ucosm"), + }, + }; + expect(aminoMsg).toEqual(expected); + }); + }); + + describe("fromAmino", () => { + it("works for MsgBeginRedelegate", () => { + const aminoMsg: AminoMsgBeginRedelegate = { + type: "cosmos-sdk/MsgBeginRedelegate", + value: { + delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + validator_src_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + validator_dst_address: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", + amount: coin(1234, "ucosm"), + }, + }; + const msg = new AminoTypes(createStakingAminoConverters("cosmos")).fromAmino(aminoMsg); + const expectedValue: MsgBeginRedelegate = { + delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + validatorSrcAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + validatorDstAddress: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", + amount: coin(1234, "ucosm"), + }; + expect(msg).toEqual({ + typeUrl: "/cosmos.staking.v1beta1.MsgBeginRedelegate", + value: expectedValue, + }); + }); + + it("works for MsgCreateValidator", () => { + const aminoMsg: AminoMsgCreateValidator = { + type: "cosmos-sdk/MsgCreateValidator", + value: { + description: { + moniker: "validator", + identity: "me", + website: "valid.com", + security_contact: "Hamburglar", + details: "...", + }, + commission: { + rate: "0.2", + max_rate: "0.3", + max_change_rate: "0.1", + }, + min_self_delegation: "123", + delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + pubkey: encodeBech32Pubkey( + { type: "tendermint/PubKeySecp256k1", value: "A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ" }, + "cosmos", + ), + value: coin(1234, "ucosm"), + }, + }; + const msg = new AminoTypes(createStakingAminoConverters("cosmos")).fromAmino(aminoMsg); + const expectedValue: MsgCreateValidator = { + description: { + moniker: "validator", + identity: "me", + website: "valid.com", + securityContact: "Hamburglar", + details: "...", + }, + commission: { + rate: "0.2", + maxRate: "0.3", + maxChangeRate: "0.1", + }, + minSelfDelegation: "123", + delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + pubkey: { + typeUrl: "/cosmos.crypto.secp256k1.PubKey", + value: fromBase64("A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ"), + }, + value: coin(1234, "ucosm"), + }; + expect(msg).toEqual({ + typeUrl: "/cosmos.staking.v1beta1.MsgCreateValidator", + value: expectedValue, + }); + }); + + it("works for MsgDelegate", () => { + const aminoMsg: AminoMsgDelegate = { + type: "cosmos-sdk/MsgDelegate", + value: { + delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + amount: coin(1234, "ucosm"), + }, + }; + const msg = new AminoTypes(createStakingAminoConverters("cosmos")).fromAmino(aminoMsg); + const expectedValue: MsgDelegate = { + delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + amount: coin(1234, "ucosm"), + }; + expect(msg).toEqual({ + typeUrl: "/cosmos.staking.v1beta1.MsgDelegate", + value: expectedValue, + }); + }); + + it("works for MsgEditValidator", () => { + const aminoMsg: AminoMsgEditValidator = { + type: "cosmos-sdk/MsgEditValidator", + value: { + description: { + moniker: "validator", + identity: "me", + website: "valid.com", + security_contact: "Hamburglar", + details: "...", + }, + commission_rate: "0.2", + min_self_delegation: "123", + validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + }, + }; + const msg = new AminoTypes(createStakingAminoConverters("cosmos")).fromAmino(aminoMsg); + const expectedValue: MsgEditValidator = { + description: { + moniker: "validator", + identity: "me", + website: "valid.com", + securityContact: "Hamburglar", + details: "...", + }, + commissionRate: "0.2", + minSelfDelegation: "123", + validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + }; + expect(msg).toEqual({ + typeUrl: "/cosmos.staking.v1beta1.MsgEditValidator", + value: expectedValue, + }); + }); + + it("works for MsgUndelegate", () => { + const aminoMsg: AminoMsgUndelegate = { + type: "cosmos-sdk/MsgUndelegate", + value: { + delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + amount: coin(1234, "ucosm"), + }, + }; + const msg = new AminoTypes(createStakingAminoConverters("cosmos")).fromAmino(aminoMsg); + const expectedValue: MsgUndelegate = { + delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", + validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", + amount: coin(1234, "ucosm"), + }; + expect(msg).toEqual({ + typeUrl: "/cosmos.staking.v1beta1.MsgUndelegate", + value: expectedValue, + }); + }); + }); +}); diff --git a/packages/stargate/src/signingstargateclient.spec.ts b/packages/stargate/src/signingstargateclient.spec.ts index 1b60074804..08b33095ff 100644 --- a/packages/stargate/src/signingstargateclient.spec.ts +++ b/packages/stargate/src/signingstargateclient.spec.ts @@ -483,37 +483,34 @@ describe("SigningStargateClient", () => { }; customRegistry.register(msgDelegateTypeUrl, CustomMsgDelegate); const customAminoTypes = new AminoTypes({ - prefix: "cosmos", - additions: { - "/cosmos.staking.v1beta1.MsgDelegate": { - aminoType: "cosmos-sdk/MsgDelegate", - toAmino: ({ - customDelegatorAddress, - customValidatorAddress, - customAmount, - }: CustomMsgDelegate): AminoMsgDelegate["value"] => { - assert(customDelegatorAddress, "missing customDelegatorAddress"); - assert(customValidatorAddress, "missing validatorAddress"); - assert(customAmount, "missing amount"); - return { - delegator_address: customDelegatorAddress, - validator_address: customValidatorAddress, - amount: { - amount: customAmount.amount, - denom: customAmount.denom, - }, - }; - }, - fromAmino: ({ - delegator_address, - validator_address, - amount, - }: AminoMsgDelegate["value"]): CustomMsgDelegate => ({ - customDelegatorAddress: delegator_address, - customValidatorAddress: validator_address, - customAmount: Coin.fromPartial(amount), - }), + "/cosmos.staking.v1beta1.MsgDelegate": { + aminoType: "cosmos-sdk/MsgDelegate", + toAmino: ({ + customDelegatorAddress, + customValidatorAddress, + customAmount, + }: CustomMsgDelegate): AminoMsgDelegate["value"] => { + assert(customDelegatorAddress, "missing customDelegatorAddress"); + assert(customValidatorAddress, "missing validatorAddress"); + assert(customAmount, "missing amount"); + return { + delegator_address: customDelegatorAddress, + validator_address: customValidatorAddress, + amount: { + amount: customAmount.amount, + denom: customAmount.denom, + }, + }; }, + fromAmino: ({ + delegator_address, + validator_address, + amount, + }: AminoMsgDelegate["value"]): CustomMsgDelegate => ({ + customDelegatorAddress: delegator_address, + customValidatorAddress: validator_address, + customAmount: Coin.fromPartial(amount), + }), }, }); const options = { @@ -774,37 +771,34 @@ describe("SigningStargateClient", () => { }; customRegistry.register(msgDelegateTypeUrl, CustomMsgDelegate); const customAminoTypes = new AminoTypes({ - prefix: "cosmos", - additions: { - "/cosmos.staking.v1beta1.MsgDelegate": { - aminoType: "cosmos-sdk/MsgDelegate", - toAmino: ({ - customDelegatorAddress, - customValidatorAddress, - customAmount, - }: CustomMsgDelegate): AminoMsgDelegate["value"] => { - assert(customDelegatorAddress, "missing customDelegatorAddress"); - assert(customValidatorAddress, "missing validatorAddress"); - assert(customAmount, "missing amount"); - return { - delegator_address: customDelegatorAddress, - validator_address: customValidatorAddress, - amount: { - amount: customAmount.amount, - denom: customAmount.denom, - }, - }; - }, - fromAmino: ({ - delegator_address, - validator_address, - amount, - }: AminoMsgDelegate["value"]): CustomMsgDelegate => ({ - customDelegatorAddress: delegator_address, - customValidatorAddress: validator_address, - customAmount: Coin.fromPartial(amount), - }), + "/cosmos.staking.v1beta1.MsgDelegate": { + aminoType: "cosmos-sdk/MsgDelegate", + toAmino: ({ + customDelegatorAddress, + customValidatorAddress, + customAmount, + }: CustomMsgDelegate): AminoMsgDelegate["value"] => { + assert(customDelegatorAddress, "missing customDelegatorAddress"); + assert(customValidatorAddress, "missing validatorAddress"); + assert(customAmount, "missing amount"); + return { + delegator_address: customDelegatorAddress, + validator_address: customValidatorAddress, + amount: { + amount: customAmount.amount, + denom: customAmount.denom, + }, + }; }, + fromAmino: ({ + delegator_address, + validator_address, + amount, + }: AminoMsgDelegate["value"]): CustomMsgDelegate => ({ + customDelegatorAddress: delegator_address, + customValidatorAddress: validator_address, + customAmount: Coin.fromPartial(amount), + }), }, }); const options = { diff --git a/packages/stargate/src/signingstargateclient.ts b/packages/stargate/src/signingstargateclient.ts index d4bdb1ee7d..cfba5f33a8 100644 --- a/packages/stargate/src/signingstargateclient.ts +++ b/packages/stargate/src/signingstargateclient.ts @@ -23,7 +23,7 @@ import { MsgTransfer } from "cosmjs-types/ibc/applications/transfer/v1/tx"; import { Height } from "cosmjs-types/ibc/core/client/v1/client"; import Long from "long"; -import { AminoTypes } from "./aminotypes"; +import { AminoConverters, AminoTypes } from "./aminotypes"; import { calculateFee, GasPrice } from "./fee"; import { authzTypes, @@ -39,6 +39,15 @@ import { MsgWithdrawDelegatorRewardEncodeObject, stakingTypes, } from "./modules"; +import { + createAuthzAminoConverters, + createBankAminoConverters, + createDistributionAminoConverters, + createFreegrantAminoConverters, + createGovAminoConverters, + createIbcAminoConverters, + createStakingAminoConverters, +} from "./modules"; import { DeliverTxResponse, StargateClient } from "./stargateclient"; export const defaultRegistryTypes: ReadonlyArray<[string, GeneratedType]> = [ @@ -81,6 +90,18 @@ export interface SigningStargateClientOptions { readonly gasPrice?: GasPrice; } +function createDefaultTypes(prefix: string): AminoConverters { + return { + ...createAuthzAminoConverters(), + ...createBankAminoConverters(), + ...createDistributionAminoConverters(), + ...createGovAminoConverters(), + ...createStakingAminoConverters(prefix), + ...createIbcAminoConverters(), + ...createFreegrantAminoConverters(), + }; +} + export class SigningStargateClient extends StargateClient { public readonly registry: Registry; public readonly broadcastTimeoutMs: number | undefined; @@ -123,7 +144,8 @@ export class SigningStargateClient extends StargateClient { super(tmClient); // TODO: do we really want to set a default here? Ideally we could get it from the signer such that users only have to set it once. const prefix = options.prefix ?? "cosmos"; - const { registry = createDefaultRegistry(), aminoTypes = new AminoTypes({ prefix }) } = options; + const { registry = createDefaultRegistry(), aminoTypes = new AminoTypes(createDefaultTypes(prefix)) } = + options; this.registry = registry; this.aminoTypes = aminoTypes; this.signer = signer;