mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 21:49:15 +00:00
Merge pull request #1336 from cosmos/fix-protoDecimalToJson-simon
Fix protoDecimalToJson for 0 fractional parts
This commit is contained in:
commit
2ded20c73b
@ -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
|
||||
|
@ -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 = {
|
||||
|
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user