diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b42fdf579..4554257a1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to `sign`/`signAndBroadcast`/`signAndBroadcastSync` and related functions now have an additional parameter to specify the timeout height. After this height, a signed transaction will be considered invalid by the chain. ([#1489]) +- @cosmjs/amino: Export `omitDefault` to help build Amino JSON converters. [#1489]: https://github.com/cosmos/cosmjs/pull/1489 diff --git a/packages/amino/src/index.ts b/packages/amino/src/index.ts index 2edbb2db94..477dc1ba19 100644 --- a/packages/amino/src/index.ts +++ b/packages/amino/src/index.ts @@ -14,6 +14,7 @@ export { encodeSecp256k1Pubkey, } from "./encoding"; export { createMultisigThresholdPubkey } from "./multisig"; +export { omitDefault } from "./omitdefault"; export { makeCosmoshubPath } from "./paths"; export { Ed25519Pubkey, diff --git a/packages/amino/src/omitdefault.spec.ts b/packages/amino/src/omitdefault.spec.ts new file mode 100644 index 0000000000..de82d76f0d --- /dev/null +++ b/packages/amino/src/omitdefault.spec.ts @@ -0,0 +1,23 @@ +import { omitDefault } from "./omitdefault"; + +describe("omitDefault", () => { + it("works for numbers", () => { + expect(omitDefault(17)).toEqual(17); + expect(omitDefault(0)).toEqual(undefined); + }); + + it("works for bigint", () => { + expect(omitDefault(17n)).toEqual(17n); + expect(omitDefault(0n)).toEqual(undefined); + }); + + it("works for boolean", () => { + expect(omitDefault(true)).toEqual(true); + expect(omitDefault(false)).toEqual(undefined); + }); + + it("works for strings", () => { + expect(omitDefault("hi")).toEqual("hi"); + expect(omitDefault("")).toEqual(undefined); + }); +}); diff --git a/packages/amino/src/omitdefault.ts b/packages/amino/src/omitdefault.ts new file mode 100644 index 0000000000..146c81713e --- /dev/null +++ b/packages/amino/src/omitdefault.ts @@ -0,0 +1,18 @@ +/** + * Returns the given input. If the input is the default value + * of protobuf, undefined is retunred. Use this when creating Amino JSON converters. + */ +export function omitDefault(input: T): T | undefined { + switch (typeof input) { + case "string": + return input === "" ? undefined : input; + case "number": + return input === 0 ? undefined : input; + case "bigint": + return input === BigInt(0) ? undefined : input; + case "boolean": + return !input ? undefined : input; + default: + throw new Error(`Got unsupported type '${typeof input}'`); + } +} diff --git a/packages/stargate/src/modules/ibc/aminomessages.ts b/packages/stargate/src/modules/ibc/aminomessages.ts index 731e926887..ee14bf722e 100644 --- a/packages/stargate/src/modules/ibc/aminomessages.ts +++ b/packages/stargate/src/modules/ibc/aminomessages.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { AminoMsg, Coin } from "@cosmjs/amino"; +import { AminoMsg, Coin, omitDefault } from "@cosmjs/amino"; import { MsgTransfer } from "cosmjs-types/ibc/applications/transfer/v1/tx"; import { AminoConverters } from "../../aminotypes"; @@ -45,22 +45,6 @@ export function isAminoMsgTransfer(msg: AminoMsg): msg is AminoMsgTransfer { return msg.type === "cosmos-sdk/MsgTransfer"; } -function omitDefault(input: T): T | undefined { - if (typeof input === "string") { - return input === "" ? undefined : input; - } - - if (typeof input === "number") { - return input === 0 ? undefined : input; - } - - if (typeof input === "bigint") { - return input === BigInt(0) ? undefined : input; - } - - throw new Error(`Got unsupported type '${typeof input}'`); -} - export function createIbcAminoConverters(): AminoConverters { return { "/ibc.applications.transfer.v1.MsgTransfer": {