Skip to content

Commit

Permalink
Merge pull request #1270 from cosmos/parse-coins
Browse files Browse the repository at this point in the history
Remove uint64 range limit from parseCoins
  • Loading branch information
webmaster128 authored Sep 15, 2022
2 parents 0292982 + 186cdb1 commit e1ad942
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
50 changes: 50 additions & 0 deletions packages/amino/src/coins.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,56 @@ describe("coins", () => {
]);
});

it("trims leading zeros", () => {
expect(parseCoins("07643ureef")).toEqual([
{
amount: "7643",
denom: "ureef",
},
]);
expect(parseCoins("007643ureef")).toEqual([
{
amount: "7643",
denom: "ureef",
},
]);
expect(parseCoins("0ureef")).toEqual([
{
amount: "0",
denom: "ureef",
},
]);
expect(parseCoins("0000ureef")).toEqual([
{
amount: "0",
denom: "ureef",
},
]);
});

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([
Expand Down
5 changes: 3 additions & 2 deletions packages/amino/src/coins.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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],
};
});
Expand Down
50 changes: 50 additions & 0 deletions packages/proto-signing/src/coins.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,56 @@ describe("coins", () => {
]);
});

it("trims leading zeros", () => {
expect(parseCoins("07643ureef")).toEqual([
{
amount: "7643",
denom: "ureef",
},
]);
expect(parseCoins("007643ureef")).toEqual([
{
amount: "7643",
denom: "ureef",
},
]);
expect(parseCoins("0ureef")).toEqual([
{
amount: "0",
denom: "ureef",
},
]);
expect(parseCoins("0000ureef")).toEqual([
{
amount: "0",
denom: "ureef",
},
]);
});

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([
{
Expand Down
3 changes: 1 addition & 2 deletions packages/proto-signing/src/coins.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Coin } from "@cosmjs/amino";
import { Uint64 } from "@cosmjs/math";

/**
* Takes a coins list like "819966000ucosm,700000000ustake" and parses it.
Expand All @@ -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],
};
});
Expand Down

0 comments on commit e1ad942

Please sign in to comment.