Remove uint64 range limit from parseCoins

This commit is contained in:
Simon Warta 2022-09-15 09:34:59 +02:00
parent fa0987c0ee
commit 186cdb1718
5 changed files with 53 additions and 4 deletions

View File

@ -55,11 +55,14 @@ and this project adheres to
- @cosmjs/tendermint-rpc: Fix `key` and `value` type in `RpcAbciQueryResponse`
to also include the `null` option.
- @cosmjs/tendermint-rpc: Fix decoding events without attributes ([#1198]).
- @cosmjs/amino, @cosmjs/proto-signing: Support amounts larger than the uint64
range in `parseCoins` ([#1268]).
[#1170]: https://github.com/cosmos/cosmjs/issues/1170
[#1177]: https://github.com/cosmos/cosmjs/issues/1177
[#1188]: https://github.com/cosmos/cosmjs/pull/1188
[#1198]: https://github.com/cosmos/cosmjs/pull/1198
[#1268]: https://github.com/cosmos/cosmjs/issues/1268
### Changed

View File

@ -121,6 +121,29 @@ describe("coins", () => {
]);
});
it("works for large numbers", () => {
expect(parseCoins(`${Number.MAX_SAFE_INTEGER}ureef`)).toEqual([
{
amount: "9007199254740991",
denom: "ureef",
},
]);
// 2**64-1
expect(parseCoins("18446744073709551615ureef")).toEqual([
{
amount: "18446744073709551615",
denom: "ureef",
},
]);
// 2**128-1
expect(parseCoins("340282366920938463463374607431768211455ureef")).toEqual([
{
amount: "340282366920938463463374607431768211455",
denom: "ureef",
},
]);
});
it("ignores empty elements", () => {
// start
expect(parseCoins(",819966000ucosm,700000000ustake")).toEqual([

View File

@ -1,4 +1,5 @@
import { Decimal, Uint53, Uint64 } from "@cosmjs/math";
import { Decimal, Uint53 } from "@cosmjs/math";
export interface Coin {
readonly denom: string;
readonly amount: string;
@ -62,7 +63,7 @@ export function parseCoins(input: string): Coin[] {
const match = part.match(/^([0-9]+)([a-zA-Z]+)/);
if (!match) throw new Error("Got an invalid coin string");
return {
amount: Uint64.fromString(match[1]).toString(),
amount: match[1].replace(/^0+/, "") || "0",
denom: match[2],
};
});

View File

@ -73,6 +73,29 @@ describe("coins", () => {
]);
});
it("works for large numbers", () => {
expect(parseCoins(`${Number.MAX_SAFE_INTEGER}ureef`)).toEqual([
{
amount: "9007199254740991",
denom: "ureef",
},
]);
// 2**64-1
expect(parseCoins("18446744073709551615ureef")).toEqual([
{
amount: "18446744073709551615",
denom: "ureef",
},
]);
// 2**128-1
expect(parseCoins("340282366920938463463374607431768211455ureef")).toEqual([
{
amount: "340282366920938463463374607431768211455",
denom: "ureef",
},
]);
});
it("works for two", () => {
expect(parseCoins("819966000ucosm,700000000ustake")).toEqual([
{

View File

@ -1,5 +1,4 @@
import { Coin } from "@cosmjs/amino";
import { Uint64 } from "@cosmjs/math";
/**
* Takes a coins list like "819966000ucosm,700000000ustake" and parses it.
@ -17,7 +16,7 @@ export function parseCoins(input: string): Coin[] {
const match = part.match(/^([0-9]+)([a-zA-Z][a-zA-Z0-9/]{2,127})$/);
if (!match) throw new Error("Got an invalid coin string");
return {
amount: Uint64.fromString(match[1]).toString(),
amount: match[1].replace(/^0+/, "") || "0",
denom: match[2],
};
});