From 63cfaad9e8c7d31899c159134944af3ff567e85c Mon Sep 17 00:00:00 2001 From: keupsonite Date: Wed, 23 Nov 2022 16:48:15 +0100 Subject: [PATCH 1/5] Fix protoDecimalToJson 0 fractional part --- packages/stargate/src/modules/index.ts | 1 + .../src/modules/staking/aminomessages.spec.ts | 33 +++++++++++++++++++ .../src/modules/staking/aminomessages.ts | 4 +-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/packages/stargate/src/modules/index.ts b/packages/stargate/src/modules/index.ts index 3a63f0b0..ac3166b4 100644 --- a/packages/stargate/src/modules/index.ts +++ b/packages/stargate/src/modules/index.ts @@ -82,6 +82,7 @@ export { isAminoMsgDelegate, isAminoMsgEditValidator, isAminoMsgUndelegate, + protoDecimalToJson, } from "./staking/aminomessages"; export { isMsgBeginRedelegateEncodeObject, diff --git a/packages/stargate/src/modules/staking/aminomessages.spec.ts b/packages/stargate/src/modules/staking/aminomessages.spec.ts index 53927b24..0c4a7e54 100644 --- a/packages/stargate/src/modules/staking/aminomessages.spec.ts +++ b/packages/stargate/src/modules/staking/aminomessages.spec.ts @@ -18,10 +18,43 @@ import { AminoMsgEditValidator, AminoMsgUndelegate, createStakingAminoConverters, + protoDecimalToJson, } from "./aminomessages"; describe("AminoTypes", () => { describe("toAmino", () => { + it("works for protoDecimalToJson", () => { + const testSet = [ + { + decimal: "0", + expected: "0.000000000000000000", + }, + { + decimal: "1", + expected: "0.000000000000000001", + }, + { + decimal: "2497", + expected: "0.000000000000002497", + }, + { + decimal: "987000000000000000", + expected: "0.987000000000000000", + }, + { + decimal: "123987000000000000000", + expected: "123.987000000000000000", + }, + { + decimal: "4872000000000000000000", + expected: "4872.000000000000000000", + }, + ]; + for (let i = 0; i < testSet.length; i++) { + expect(protoDecimalToJson(testSet[i].decimal)).toEqual(testSet[i].expected); + } + }); + it("works for MsgBeginRedelegate", () => { const msg: MsgBeginRedelegate = { delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", diff --git a/packages/stargate/src/modules/staking/aminomessages.ts b/packages/stargate/src/modules/staking/aminomessages.ts index 5b46ec4b..c4e4adc5 100644 --- a/packages/stargate/src/modules/staking/aminomessages.ts +++ b/packages/stargate/src/modules/staking/aminomessages.ts @@ -29,10 +29,10 @@ interface Description { readonly details: string; } -function protoDecimalToJson(decimal: string): string { +export function protoDecimalToJson(decimal: string): string { const parsed = Decimal.fromAtomics(decimal, 18); const [whole, fractional] = parsed.toString().split("."); - return `${whole}.${fractional.padEnd(18, "0")}`; + return `${whole}.${(typeof fractional !== "undefined" ? fractional : "").padEnd(18, "0")}`; } function jsonDecimalToProto(decimal: string): string { From cc1e8531d81993d5485a8300d256c35b5d176c02 Mon Sep 17 00:00:00 2001 From: keupsonite Date: Fri, 25 Nov 2022 16:46:27 +0100 Subject: [PATCH 2/5] Update packages/stargate/src/modules/staking/aminomessages.ts Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com> --- packages/stargate/src/modules/staking/aminomessages.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/stargate/src/modules/staking/aminomessages.ts b/packages/stargate/src/modules/staking/aminomessages.ts index c4e4adc5..38f3131f 100644 --- a/packages/stargate/src/modules/staking/aminomessages.ts +++ b/packages/stargate/src/modules/staking/aminomessages.ts @@ -32,7 +32,7 @@ interface Description { export function protoDecimalToJson(decimal: string): string { const parsed = Decimal.fromAtomics(decimal, 18); const [whole, fractional] = parsed.toString().split("."); - return `${whole}.${(typeof fractional !== "undefined" ? fractional : "").padEnd(18, "0")}`; + return `${whole}.${(fractional ?? "").padEnd(18, "0")}`; } function jsonDecimalToProto(decimal: string): string { From e61dd42e822484e4e87d0cfa223a424d490f44d1 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 7 Dec 2022 10:52:38 +0100 Subject: [PATCH 3/5] Remove protoDecimalToJson export --- packages/stargate/src/modules/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/stargate/src/modules/index.ts b/packages/stargate/src/modules/index.ts index ac3166b4..3a63f0b0 100644 --- a/packages/stargate/src/modules/index.ts +++ b/packages/stargate/src/modules/index.ts @@ -82,7 +82,6 @@ export { isAminoMsgDelegate, isAminoMsgEditValidator, isAminoMsgUndelegate, - protoDecimalToJson, } from "./staking/aminomessages"; export { isMsgBeginRedelegateEncodeObject, From 911027da597e6c962f1ad2b804281e49a3125a27 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 7 Dec 2022 10:56:38 +0100 Subject: [PATCH 4/5] Organize protoDecimalToJson test code --- .../src/modules/staking/aminomessages.spec.ts | 41 +++++-------------- 1 file changed, 10 insertions(+), 31 deletions(-) diff --git a/packages/stargate/src/modules/staking/aminomessages.spec.ts b/packages/stargate/src/modules/staking/aminomessages.spec.ts index 0c4a7e54..25024d71 100644 --- a/packages/stargate/src/modules/staking/aminomessages.spec.ts +++ b/packages/stargate/src/modules/staking/aminomessages.spec.ts @@ -22,39 +22,18 @@ import { } from "./aminomessages"; describe("AminoTypes", () => { - describe("toAmino", () => { - it("works for protoDecimalToJson", () => { - const testSet = [ - { - decimal: "0", - expected: "0.000000000000000000", - }, - { - decimal: "1", - expected: "0.000000000000000001", - }, - { - decimal: "2497", - expected: "0.000000000000002497", - }, - { - decimal: "987000000000000000", - expected: "0.987000000000000000", - }, - { - decimal: "123987000000000000000", - expected: "123.987000000000000000", - }, - { - decimal: "4872000000000000000000", - expected: "4872.000000000000000000", - }, - ]; - for (let i = 0; i < testSet.length; i++) { - expect(protoDecimalToJson(testSet[i].decimal)).toEqual(testSet[i].expected); - } + describe("protoDecimalToJson", () => { + it("works", () => { + expect(protoDecimalToJson("0")).toEqual("0.000000000000000000"); + expect(protoDecimalToJson("1")).toEqual("0.000000000000000001"); + expect(protoDecimalToJson("2497")).toEqual("0.000000000000002497"); + expect(protoDecimalToJson("987000000000000000")).toEqual("0.987000000000000000"); + expect(protoDecimalToJson("123987000000000000000")).toEqual("123.987000000000000000"); + expect(protoDecimalToJson("4872000000000000000000")).toEqual("4872.000000000000000000"); }); + }); + describe("toAmino", () => { it("works for MsgBeginRedelegate", () => { const msg: MsgBeginRedelegate = { delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", From f8797e619c1a200016f9e9599fc9c1d2fbb9c8e8 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 7 Dec 2022 11:01:08 +0100 Subject: [PATCH 5/5] Add CHANGELOG entry for protoDecimalToJson fix --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8efd05b8..08279b5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,14 @@ and this project adheres to ## [Unreleased] +### Fixed + +- @cosmjs/stargate: Fix `protoDecimalToJson` for values with a 0 fractional + part, such as `0.000000000000000000`, `1.000000000000000000` or + `42.000000000000000000` ([#1326]). + +[#1326] : https://github.com/cosmos/cosmjs/pull/1326 + ### Deprecated - @cosmjs/stargate: Deprecate `QueryClient.queryUnverified` in favour of newly