-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Explorer: replace usages of bn.js with BigInt
- Loading branch information
Showing
8 changed files
with
59 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
import { expect } from "chai"; | ||
import { lamportsToSol, LAMPORTS_PER_SOL } from "utils"; | ||
import BN from "bn.js"; | ||
|
||
describe("lamportsToSol", () => { | ||
it("0 lamports", () => { | ||
expect(lamportsToSol(new BN(0))).to.eq(0.0); | ||
expect(lamportsToSol(0)).to.eq(0.0); | ||
expect(lamportsToSol(BigInt(0))).to.eq(0.0); | ||
}); | ||
|
||
it("1 lamport", () => { | ||
expect(lamportsToSol(new BN(1))).to.eq(0.000000001); | ||
expect(lamportsToSol(new BN(-1))).to.eq(-0.000000001); | ||
expect(lamportsToSol(1)).to.eq(0.000000001); | ||
expect(lamportsToSol(BigInt(1))).to.eq(0.000000001); | ||
expect(lamportsToSol(-1)).to.eq(-0.000000001); | ||
expect(lamportsToSol(BigInt(-1))).to.eq(-0.000000001); | ||
}); | ||
|
||
it("1 SOL", () => { | ||
expect(lamportsToSol(new BN(LAMPORTS_PER_SOL))).to.eq(1.0); | ||
expect(lamportsToSol(new BN(-LAMPORTS_PER_SOL))).to.eq(-1.0); | ||
expect(lamportsToSol(LAMPORTS_PER_SOL)).to.eq(1.0); | ||
expect(lamportsToSol(BigInt(LAMPORTS_PER_SOL))).to.eq(1.0); | ||
expect(lamportsToSol(-LAMPORTS_PER_SOL)).to.eq(-1.0); | ||
expect(lamportsToSol(BigInt(-LAMPORTS_PER_SOL))).to.eq(-1.0); | ||
}); | ||
|
||
it("u64::MAX lamports", () => { | ||
expect(lamportsToSol(new BN(2).pow(new BN(64)))).to.eq( | ||
18446744073.709551615 | ||
); | ||
expect(lamportsToSol(new BN(2).pow(new BN(64)).neg())).to.eq( | ||
-18446744073.709551615 | ||
); | ||
expect(lamportsToSol(2n ** 64n)).to.eq(18446744073.709551615); | ||
expect(lamportsToSol(-(2n ** 64n))).to.eq(-18446744073.709551615); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { bigint, coerce, number, string } from "superstruct"; | ||
|
||
export const BigIntFromString = coerce(bigint(), string(), (value): bigint => { | ||
if (typeof value === "string") return BigInt(value); | ||
throw new Error("invalid bigint"); | ||
}); | ||
|
||
export const NumberFromString = coerce(number(), string(), (value): number => { | ||
if (typeof value === "string") return Number(value); | ||
throw new Error("invalid number"); | ||
}); |