Merge pull request #1336 from cosmos/fix-protoDecimalToJson-simon

Fix protoDecimalToJson for 0 fractional parts
This commit is contained in:
Simon Warta 2022-12-07 11:53:39 +01:00 committed by GitHub
commit 2ded20c73b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 2 deletions

View File

@ -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

View File

@ -18,9 +18,21 @@ import {
AminoMsgEditValidator,
AminoMsgUndelegate,
createStakingAminoConverters,
protoDecimalToJson,
} from "./aminomessages";
describe("AminoTypes", () => {
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 = {

View File

@ -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}.${(fractional ?? "").padEnd(18, "0")}`;
}
function jsonDecimalToProto(decimal: string): string {