Fix protoDecimalToJson 0 fractional part

This commit is contained in:
keupsonite 2022-11-23 16:48:15 +01:00 committed by Simon Warta
parent 74346b2ce6
commit 63cfaad9e8
3 changed files with 36 additions and 2 deletions

View File

@ -82,6 +82,7 @@ export {
isAminoMsgDelegate,
isAminoMsgEditValidator,
isAminoMsgUndelegate,
protoDecimalToJson,
} from "./staking/aminomessages";
export {
isMsgBeginRedelegateEncodeObject,

View File

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

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