From b6d6c6e0431a1fdfe650c4984a3b360502cd82a8 Mon Sep 17 00:00:00 2001 From: Milan Steiner Date: Wed, 9 Mar 2022 14:53:06 +0100 Subject: [PATCH] Remove default types from AminoTypes --- .../src/modules/wasm/aminomessages.spec.ts | 58 ++--- .../src/signingcosmwasmclient.spec.ts | 118 +++++---- .../src/signingcosmwasmclient.ts | 4 +- packages/stargate/src/aminotypes.spec.ts | 233 ++++++++++-------- packages/stargate/src/aminotypes.ts | 34 +-- packages/stargate/src/index.ts | 11 +- .../src/signingstargateclient.spec.ts | 114 ++++----- .../stargate/src/signingstargateclient.ts | 27 +- 8 files changed, 305 insertions(+), 294 deletions(-) diff --git a/packages/cosmwasm-stargate/src/modules/wasm/aminomessages.spec.ts b/packages/cosmwasm-stargate/src/modules/wasm/aminomessages.spec.ts index 476519b7..86e0c6a2 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 3b61b55a..7a98095b 100644 --- a/packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts +++ b/packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts @@ -851,39 +851,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 = { @@ -1156,37 +1153,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 = { diff --git a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts index 884e9f1a..de7aa56e 100644 --- a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts @@ -201,11 +201,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() }), } = options; this.registry = registry; this.aminoTypes = aminoTypes; diff --git a/packages/stargate/src/aminotypes.spec.ts b/packages/stargate/src/aminotypes.spec.ts index aef24686..7a53c5f3 100644 --- a/packages/stargate/src/aminotypes.spec.ts +++ b/packages/stargate/src/aminotypes.spec.ts @@ -38,6 +38,11 @@ import { AminoMsgVote, AminoMsgWithdrawDelegatorReward, AminoMsgWithdrawValidatorCommission, + createBankAminoConverters, + createDistributionAminoConverters, + createGovAminoConverters, + createIbcAminoConverters, + createStakingAminoConverters, } from "./modules"; describe("AminoTypes", () => { @@ -50,17 +55,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 +86,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, + }), }, }); @@ -123,7 +123,10 @@ describe("AminoTypes", () => { toAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", amount: coins(1234, "ucosm"), }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ + const aminoTypes = new AminoTypes({ + ...createBankAminoConverters(), + }); + const aminoMsg = aminoTypes.toAmino({ typeUrl: "/cosmos.bank.v1beta1.MsgSend", value: msg, }); @@ -149,7 +152,10 @@ describe("AminoTypes", () => { { address: "cosmos142u9fgcjdlycfcez3lw8x6x5h7rfjlnfhpw2lx", coins: coins(912, "ucosm") }, ], }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ + const aminoTypes = new AminoTypes({ + ...createBankAminoConverters(), + }); + const aminoMsg = aminoTypes.toAmino({ typeUrl: "/cosmos.bank.v1beta1.MsgMultiSend", value: msg, }); @@ -177,7 +183,10 @@ describe("AminoTypes", () => { depositor: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", proposalId: Long.fromNumber(5), }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ + const aminoTypes = new AminoTypes({ + ...createGovAminoConverters(), + }); + const aminoMsg = aminoTypes.toAmino({ typeUrl: "/cosmos.gov.v1beta1.MsgDeposit", value: msg, }); @@ -204,7 +213,10 @@ describe("AminoTypes", () => { }).finish(), }, }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ + const aminoTypes = new AminoTypes({ + ...createGovAminoConverters(), + }); + const aminoMsg = aminoTypes.toAmino({ typeUrl: "/cosmos.gov.v1beta1.MsgSubmitProposal", value: msg, }); @@ -231,7 +243,10 @@ describe("AminoTypes", () => { proposalId: Long.fromNumber(5), voter: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ + const aminoTypes = new AminoTypes({ + ...createGovAminoConverters(), + }); + const aminoMsg = aminoTypes.toAmino({ typeUrl: "/cosmos.gov.v1beta1.MsgVote", value: msg, }); @@ -253,7 +268,10 @@ describe("AminoTypes", () => { amount: coins(1234, "ucosm"), depositor: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ + const aminoTypes = new AminoTypes({ + ...createDistributionAminoConverters(), + }); + const aminoMsg = aminoTypes.toAmino({ typeUrl: "/cosmos.distribution.v1beta1.MsgFundCommunityPool", value: msg, }); @@ -272,7 +290,10 @@ describe("AminoTypes", () => { delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", withdrawAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ + const aminoTypes = new AminoTypes({ + ...createDistributionAminoConverters(), + }); + const aminoMsg = aminoTypes.toAmino({ typeUrl: "/cosmos.distribution.v1beta1.MsgSetWithdrawAddress", value: msg, }); @@ -291,7 +312,10 @@ describe("AminoTypes", () => { delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ + const aminoTypes = new AminoTypes({ + ...createDistributionAminoConverters(), + }); + const aminoMsg = aminoTypes.toAmino({ typeUrl: "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward", value: msg, }); @@ -309,7 +333,10 @@ describe("AminoTypes", () => { const msg: MsgWithdrawValidatorCommission = { validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ + const aminoTypes = new AminoTypes({ + ...createDistributionAminoConverters(), + }); + const aminoMsg = aminoTypes.toAmino({ typeUrl: "/cosmos.distribution.v1beta1.MsgWithdrawValidatorCommission", value: msg, }); @@ -331,7 +358,10 @@ describe("AminoTypes", () => { validatorDstAddress: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", amount: coin(1234, "ucosm"), }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ + const aminoTypes = new AminoTypes({ + ...createStakingAminoConverters("cosmos"), + }); + const aminoMsg = aminoTypes.toAmino({ typeUrl: "/cosmos.staking.v1beta1.MsgBeginRedelegate", value: msg, }); @@ -370,7 +400,10 @@ describe("AminoTypes", () => { }, value: coin(1234, "ucosm"), }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ + const aminoTypes = new AminoTypes({ + ...createStakingAminoConverters("cosmos"), + }); + const aminoMsg = aminoTypes.toAmino({ typeUrl: "/cosmos.staking.v1beta1.MsgCreateValidator", value: msg, }); @@ -408,7 +441,10 @@ describe("AminoTypes", () => { validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", amount: coin(1234, "ucosm"), }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ + const aminoTypes = new AminoTypes({ + ...createStakingAminoConverters("cosmos"), + }); + const aminoMsg = aminoTypes.toAmino({ typeUrl: "/cosmos.staking.v1beta1.MsgDelegate", value: msg, }); @@ -436,7 +472,10 @@ describe("AminoTypes", () => { minSelfDelegation: "123", validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ + const aminoTypes = new AminoTypes({ + ...createStakingAminoConverters("cosmos"), + }); + const aminoMsg = aminoTypes.toAmino({ typeUrl: "/cosmos.staking.v1beta1.MsgEditValidator", value: msg, }); @@ -464,7 +503,10 @@ describe("AminoTypes", () => { validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", amount: coin(1234, "ucosm"), }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ + const aminoTypes = new AminoTypes({ + ...createStakingAminoConverters("cosmos"), + }); + const aminoMsg = aminoTypes.toAmino({ typeUrl: "/cosmos.staking.v1beta1.MsgUndelegate", value: msg, }); @@ -494,7 +536,10 @@ describe("AminoTypes", () => { }, timeoutTimestamp: Long.fromString("789", true), }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ + const aminoTypes = new AminoTypes({ + ...createIbcAminoConverters(), + }); + const aminoMsg = aminoTypes.toAmino({ typeUrl: "/ibc.applications.transfer.v1.MsgTransfer", value: msg, }); @@ -529,7 +574,10 @@ describe("AminoTypes", () => { }, timeoutTimestamp: Long.UZERO, }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ + const aminoTypes = new AminoTypes({ + ...createIbcAminoConverters(), + }); + const aminoMsg = aminoTypes.toAmino({ typeUrl: "/ibc.applications.transfer.v1.MsgTransfer", value: msg, }); @@ -561,7 +609,7 @@ describe("AminoTypes", () => { timeoutHeight: undefined, timeoutTimestamp: Long.UZERO, }; - const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ + const aminoMsg = new AminoTypes({ ...createIbcAminoConverters() }).toAmino({ typeUrl: "/ibc.applications.transfer.v1.MsgTransfer", value: msg, }); @@ -587,20 +635,17 @@ describe("AminoTypes", () => { 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 +664,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,7 +686,10 @@ 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); }); }); @@ -661,7 +706,7 @@ describe("AminoTypes", () => { amount: coins(1234, "ucosm"), }, }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); + const msg = new AminoTypes({ ...createBankAminoConverters() }).fromAmino(aminoMsg); const expectedValue: MsgSend = { fromAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", toAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", @@ -687,7 +732,7 @@ describe("AminoTypes", () => { ], }, }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); + const msg = new AminoTypes({ ...createBankAminoConverters() }).fromAmino(aminoMsg); const expectedValue: MsgMultiSend = { inputs: [ { address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", coins: coins(1234, "ucosm") }, @@ -715,7 +760,7 @@ describe("AminoTypes", () => { proposal_id: "5", }, }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); + const msg = new AminoTypes({ ...createGovAminoConverters() }).fromAmino(aminoMsg); const expectedValue: MsgDeposit = { amount: [{ amount: "12300000", denom: "ustake" }], depositor: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", @@ -742,7 +787,7 @@ describe("AminoTypes", () => { }, }, }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); + const msg = new AminoTypes({ ...createGovAminoConverters() }).fromAmino(aminoMsg); const expectedValue: MsgSubmitProposal = { initialDeposit: [{ amount: "12300000", denom: "ustake" }], proposer: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", @@ -769,7 +814,7 @@ describe("AminoTypes", () => { voter: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", }, }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); + const msg = new AminoTypes({ ...createGovAminoConverters() }).fromAmino(aminoMsg); const expectedValue: MsgVote = { option: VoteOption.VOTE_OPTION_NO_WITH_VETO, proposalId: Long.fromNumber(5), @@ -800,7 +845,7 @@ describe("AminoTypes", () => { amount: coin(1234, "ucosm"), }, }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); + const msg = new AminoTypes({ ...createStakingAminoConverters("cosmos") }).fromAmino(aminoMsg); const expectedValue: MsgBeginRedelegate = { delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", validatorSrcAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", @@ -839,7 +884,7 @@ describe("AminoTypes", () => { value: coin(1234, "ucosm"), }, }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); + const msg = new AminoTypes({ ...createStakingAminoConverters("cosmos") }).fromAmino(aminoMsg); const expectedValue: MsgCreateValidator = { description: { moniker: "validator", @@ -877,7 +922,7 @@ describe("AminoTypes", () => { amount: coin(1234, "ucosm"), }, }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); + const msg = new AminoTypes({ ...createStakingAminoConverters("cosmos") }).fromAmino(aminoMsg); const expectedValue: MsgDelegate = { delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", @@ -905,7 +950,7 @@ describe("AminoTypes", () => { validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", }, }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); + const msg = new AminoTypes({ ...createStakingAminoConverters("cosmos") }).fromAmino(aminoMsg); const expectedValue: MsgEditValidator = { description: { moniker: "validator", @@ -933,7 +978,7 @@ describe("AminoTypes", () => { amount: coin(1234, "ucosm"), }, }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); + const msg = new AminoTypes({ ...createStakingAminoConverters("cosmos") }).fromAmino(aminoMsg); const expectedValue: MsgUndelegate = { delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", @@ -963,7 +1008,7 @@ describe("AminoTypes", () => { timeout_timestamp: "789", }, }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); + const msg = new AminoTypes({ ...createIbcAminoConverters() }).fromAmino(aminoMsg); const expectedValue: MsgTransfer = { sourcePort: "testport", sourceChannel: "testchannel", @@ -998,7 +1043,7 @@ describe("AminoTypes", () => { // timeout_timestamp omitted }, }; - const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); + const msg = new AminoTypes({ ...createIbcAminoConverters() }).fromAmino(aminoMsg); const expectedValue: MsgTransfer = { sourcePort: "testport", sourceChannel: "testchannel", @@ -1028,15 +1073,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 +1092,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 +1119,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 +1130,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 236705ba..23ecf848 100644 --- a/packages/stargate/src/aminotypes.ts +++ b/packages/stargate/src/aminotypes.ts @@ -3,35 +3,6 @@ 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 AminoTypesOptions { - /** - * The Bech32 address prefix of the chain you work with (also called Bech32 human-readable part). - */ - readonly prefix: string; - readonly additions?: AminoConverters; -} function isAminoConverter( converter: [string, AminoConverter | "not_supported_by_chain"], @@ -50,9 +21,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 5cf0c01d..a06494a8 100644 --- a/packages/stargate/src/index.ts +++ b/packages/stargate/src/index.ts @@ -1,6 +1,6 @@ export { Account, accountFromAny } from "./accounts"; export { AminoConverter, AminoConverters } from "./aminoconverters"; -export { AminoTypes, AminoTypesOptions } from "./aminotypes"; +export { AminoTypes } from "./aminotypes"; export { calculateFee, GasPrice } from "./fee"; export * as logs from "./logs"; export { @@ -74,6 +74,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/signingstargateclient.spec.ts b/packages/stargate/src/signingstargateclient.spec.ts index 1b600748..08b33095 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 d4bdb1ee..ff1992b3 100644 --- a/packages/stargate/src/signingstargateclient.ts +++ b/packages/stargate/src/signingstargateclient.ts @@ -23,6 +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 { AminoConverters } from "./aminoconverters"; import { AminoTypes } from "./aminotypes"; import { calculateFee, GasPrice } from "./fee"; import { @@ -39,6 +40,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]> = [ @@ -123,7 +133,10 @@ 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({ ...this.createDefaultTypes(prefix) }), + } = options; this.registry = registry; this.aminoTypes = aminoTypes; this.signer = signer; @@ -306,6 +319,18 @@ export class SigningStargateClient extends StargateClient { : this.signAmino(signerAddress, messages, fee, memo, signerData); } + public createDefaultTypes(prefix: string): AminoConverters { + return { + ...createAuthzAminoConverters(), + ...createBankAminoConverters(), + ...createDistributionAminoConverters(), + ...createGovAminoConverters(), + ...createStakingAminoConverters(prefix), + ...createIbcAminoConverters(), + ...createFreegrantAminoConverters(), + }; + } + private async signAmino( signerAddress: string, messages: readonly EncodeObject[],