Skip to content

Commit

Permalink
Merge pull request #1203 from cosmos/add-toBigInt
Browse files Browse the repository at this point in the history
Add {Uint32,Int53,Uint53,Uint64}.toBigInt
  • Loading branch information
webmaster128 authored Jun 29, 2022
2 parents 7810a33 + 1c2f434 commit 1e50f89
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to
`AbciQueryResponse`.
- @cosmjs/cosmwasm-stargate: Add `SigningCosmWasmClient.executeMultiple`
([#1072]).
- @cosmjs/math: Add `{Uint32,Int53,Uint53,Uint64}.toBigInt` converter methods.

[#1072]: https://github.com/cosmos/cosmjs/issues/1072
[#1176]: https://github.com/cosmos/cosmjs/pull/1176
Expand Down
51 changes: 51 additions & 0 deletions packages/math/src/integers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ describe("Integers", () => {
expect(new Uint32(4294967295).toNumber()).toEqual(4294967295);
});

it("can convert to BigInt", () => {
expect(new Uint32(0).toBigInt()).toEqual(BigInt(0));
expect(new Uint32(1).toBigInt()).toEqual(BigInt(1));
expect(new Uint32(42).toBigInt()).toEqual(BigInt(42));
expect(new Uint32(1000000000).toBigInt()).toEqual(BigInt(1000000000));
expect(new Uint32(2147483647).toBigInt()).toEqual(BigInt(2147483647));
expect(new Uint32(2147483648).toBigInt()).toEqual(BigInt(2147483648));
expect(new Uint32(4294967295).toBigInt()).toEqual(BigInt(4294967295));
});

it("can convert to string", () => {
expect(new Uint32(0).toString()).toEqual("0");
expect(new Uint32(1).toString()).toEqual("1");
Expand Down Expand Up @@ -209,6 +219,20 @@ describe("Integers", () => {
expect(new Int53(-9007199254740991).toNumber()).toEqual(-9007199254740991);
});

it("can convert to BigInt", () => {
expect(new Int53(0).toBigInt()).toEqual(BigInt(0));
expect(new Int53(1).toBigInt()).toEqual(BigInt(1));
expect(new Int53(42).toBigInt()).toEqual(BigInt(42));
expect(new Int53(1000000000).toBigInt()).toEqual(BigInt(1000000000));
expect(new Int53(2147483647).toBigInt()).toEqual(BigInt(2147483647));
expect(new Int53(2147483648).toBigInt()).toEqual(BigInt(2147483648));
expect(new Int53(4294967295).toBigInt()).toEqual(BigInt(4294967295));
expect(new Int53(9007199254740991).toBigInt()).toEqual(BigInt(9007199254740991));

expect(new Int53(-1).toBigInt()).toEqual(BigInt(-1));
expect(new Int53(-9007199254740991).toBigInt()).toEqual(BigInt(-9007199254740991));
});

it("can convert to string", () => {
expect(new Int53(0).toString()).toEqual("0");
expect(new Int53(1).toString()).toEqual("1");
Expand Down Expand Up @@ -287,6 +311,17 @@ describe("Integers", () => {
expect(new Uint53(9007199254740991).toNumber()).toEqual(9007199254740991);
});

it("can convert to BigInt", () => {
expect(new Uint53(0).toBigInt()).toEqual(BigInt(0));
expect(new Uint53(1).toBigInt()).toEqual(BigInt(1));
expect(new Uint53(42).toBigInt()).toEqual(BigInt(42));
expect(new Uint53(1000000000).toBigInt()).toEqual(BigInt(1000000000));
expect(new Uint53(2147483647).toBigInt()).toEqual(BigInt(2147483647));
expect(new Uint53(2147483648).toBigInt()).toEqual(BigInt(2147483648));
expect(new Uint53(4294967295).toBigInt()).toEqual(BigInt(4294967295));
expect(new Uint53(9007199254740991).toBigInt()).toEqual(BigInt(9007199254740991));
});

it("can convert to string", () => {
expect(new Uint53(0).toString()).toEqual("0");
expect(new Uint53(1).toString()).toEqual("1");
Expand Down Expand Up @@ -505,5 +540,21 @@ describe("Integers", () => {
expect(() => a.toNumber()).toThrowError(/number can only safely store up to 53 bits/i);
}
});

it("can export to BigInt", () => {
const a = Uint64.fromBytes([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
expect(a.toBigInt()).toEqual(BigInt(0));

const b = Uint64.fromBytes([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]);
expect(b.toBigInt()).toEqual(BigInt(1));

// value too large for 53 bit integer
const c = Uint64.fromBytes([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);
expect(c.toBigInt()).toEqual(BigInt("0xffffffffffffffff"));

// Number.MAX_SAFE_INTEGER + 1
const d = Uint64.fromBytes([0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
expect(d.toBigInt()).toEqual(BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1));
});
});
});
17 changes: 17 additions & 0 deletions packages/math/src/integers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const uint64MaxValue = new BN("18446744073709551615", 10, "be");
/** Internal interface to ensure all integer types can be used equally */
interface Integer {
readonly toNumber: () => number;
readonly toBigInt: () => bigint;
readonly toString: () => string;
}

Expand Down Expand Up @@ -103,6 +104,10 @@ export class Uint32 implements Integer, WithByteConverters {
return this.data;
}

public toBigInt(): bigint {
return BigInt(this.toNumber());
}

public toString(): string {
return this.data.toString();
}
Expand Down Expand Up @@ -139,6 +144,10 @@ export class Int53 implements Integer {
return this.data;
}

public toBigInt(): bigint {
return BigInt(this.toNumber());
}

public toString(): string {
return this.data.toString();
}
Expand All @@ -164,6 +173,10 @@ export class Uint53 implements Integer {
return this.data.toNumber();
}

public toBigInt(): bigint {
return BigInt(this.toNumber());
}

public toString(): string {
return this.data.toString();
}
Expand Down Expand Up @@ -245,6 +258,10 @@ export class Uint64 implements Integer, WithByteConverters {
return this.data.toString(10);
}

public toBigInt(): bigint {
return BigInt(this.toString());
}

public toNumber(): number {
return this.data.toNumber();
}
Expand Down

0 comments on commit 1e50f89

Please sign in to comment.