Skip to content

Commit

Permalink
Explorer: replace usages of bn.js with BigInt (#28040)
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry authored Sep 24, 2022
1 parent b9f4c8e commit 22b966b
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 58 deletions.
24 changes: 12 additions & 12 deletions explorer/src/__tests__/lamportsToSol.ts
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);
});
});
17 changes: 8 additions & 9 deletions explorer/src/components/account/StakeAccountSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import {
StakeMeta,
StakeAccountType,
} from "validators/accounts/stake";
import BN from "bn.js";
import { StakeActivationData } from "@solana/web3.js";
import { Epoch } from "components/common/Epoch";

const MAX_EPOCH = new BN(2).pow(new BN(64)).sub(new BN(1));
const U64_MAX = BigInt("0xffffffffffffffff");

export function StakeAccountSection({
account,
Expand Down Expand Up @@ -163,11 +162,11 @@ function DelegationCard({
const delegation = stakeAccount?.stake?.delegation;
if (delegation) {
voterPubkey = delegation.voter;
if (!delegation.activationEpoch.eq(MAX_EPOCH)) {
activationEpoch = delegation.activationEpoch.toNumber();
if (delegation.activationEpoch !== U64_MAX) {
activationEpoch = delegation.activationEpoch;
}
if (!delegation.deactivationEpoch.eq(MAX_EPOCH)) {
deactivationEpoch = delegation.deactivationEpoch.toNumber();
if (delegation.deactivationEpoch !== U64_MAX) {
deactivationEpoch = delegation.deactivationEpoch;
}
}
const { stake } = stakeAccount;
Expand Down Expand Up @@ -297,10 +296,10 @@ function isFullyInactivated(
}

const delegatedStake = stake.delegation.stake;
const inactiveStake = new BN(activation.inactive);
const inactiveStake = BigInt(activation.inactive);

return (
!stake.delegation.deactivationEpoch.eq(MAX_EPOCH) &&
delegatedStake.eq(inactiveStake)
stake.delegation.deactivationEpoch !== U64_MAX &&
delegatedStake === inactiveStake
);
}
2 changes: 1 addition & 1 deletion explorer/src/components/common/Epoch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { clusterPath } from "utils/url";
import { Copyable } from "./Copyable";

type Props = {
epoch: number;
epoch: number | bigint;
link?: boolean;
};
export function Epoch({ epoch, link }: Props) {
Expand Down
32 changes: 16 additions & 16 deletions explorer/src/components/instruction/serum/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
TransactionInstruction,
} from "@solana/web3.js";
import { enums, number, type, Infer, create } from "superstruct";
import { BigNumFromString } from "validators/bignum";
import { BigIntFromString } from "validators/number";

const SERUM_PROGRAM_IDS = [
"4ckmDgGdxQoPDLUkDT3vHgSAkzA3QRdNq5ywwY4sUSJn",
Expand Down Expand Up @@ -60,11 +60,11 @@ export type InitializeMarket = {
};

export const InitializeMarketInstruction = type({
baseLotSize: BigNumFromString,
quoteLotSize: BigNumFromString,
baseLotSize: BigIntFromString,
quoteLotSize: BigIntFromString,
feeRateBps: number(),
quoteDustThreshold: BigNumFromString,
vaultSignerNonce: BigNumFromString,
quoteDustThreshold: BigIntFromString,
vaultSignerNonce: BigIntFromString,
});

export function decodeInitializeMarket(
Expand Down Expand Up @@ -110,10 +110,10 @@ export type NewOrder = {

export const NewOrderInstruction = type({
side: Side,
limitPrice: BigNumFromString,
maxQuantity: BigNumFromString,
limitPrice: BigIntFromString,
maxQuantity: BigIntFromString,
orderType: OrderType,
clientId: BigNumFromString,
clientId: BigIntFromString,
});

export function decodeNewOrder(ix: TransactionInstruction): NewOrder {
Expand Down Expand Up @@ -208,7 +208,7 @@ export type CancelOrder = {

export const CancelOrderInstruction = type({
side: Side,
orderId: BigNumFromString,
orderId: BigIntFromString,
openOrdersSlot: number(),
});

Expand Down Expand Up @@ -272,7 +272,7 @@ export type CancelOrderByClientId = {
};

export const CancelOrderByClientIdInstruction = type({
clientId: BigNumFromString,
clientId: BigIntFromString,
});

export function decodeCancelOrderByClientId(
Expand Down Expand Up @@ -355,12 +355,12 @@ export type NewOrderV3 = {

export const NewOrderV3Instruction = type({
side: Side,
limitPrice: BigNumFromString,
maxBaseQuantity: BigNumFromString,
maxQuoteQuantity: BigNumFromString,
limitPrice: BigIntFromString,
maxBaseQuantity: BigIntFromString,
maxQuoteQuantity: BigIntFromString,
selfTradeBehavior: SelfTradeBehavior,
orderType: OrderType,
clientId: BigNumFromString,
clientId: BigIntFromString,
limit: number(),
});

Expand Down Expand Up @@ -399,7 +399,7 @@ export type CancelOrderV2 = {

export const CancelOrderV2Instruction = type({
side: Side,
orderId: BigNumFromString,
orderId: BigIntFromString,
});

export function decodeCancelOrderV2(ix: TransactionInstruction): CancelOrderV2 {
Expand Down Expand Up @@ -434,7 +434,7 @@ export type CancelOrderByClientIdV2 = {
};

export const CancelOrderByClientIdV2Instruction = type({
clientId: BigNumFromString,
clientId: BigIntFromString,
});

export function decodeCancelOrderByClientIdV2(
Expand Down
14 changes: 6 additions & 8 deletions explorer/src/utils/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import BN from "bn.js";
import {
HumanizeDuration,
HumanizeDurationLanguage,
Expand Down Expand Up @@ -36,7 +35,6 @@ export function microLamportsToLamports(
return microLamports / MICRO_LAMPORTS_PER_LAMPORT;
}

console.log(microLamports);
const microLamportsString = microLamports.toString().padStart(7, "0");
const splitIndex = microLamportsString.length - 6;
const lamportString =
Expand All @@ -56,17 +54,17 @@ export function microLamportsToLamportsString(
);
}

export function lamportsToSol(lamports: number | BN): number {
export function lamportsToSol(lamports: number | bigint): number {
if (typeof lamports === "number") {
return Math.abs(lamports) / LAMPORTS_PER_SOL;
return lamports / LAMPORTS_PER_SOL;
}

let signMultiplier = 1;
if (lamports.isNeg()) {
if (lamports < 0) {
signMultiplier = -1;
}

const absLamports = lamports.abs();
const absLamports = lamports < 0 ? -lamports : lamports;
const lamportsString = absLamports.toString(10).padStart(10, "0");
const splitIndex = lamportsString.length - 9;
const solString =
Expand All @@ -77,7 +75,7 @@ export function lamportsToSol(lamports: number | BN): number {
}

export function lamportsToSolString(
lamports: number | BN,
lamports: number | bigint,
maximumFractionDigits: number = 9
): string {
const sol = lamportsToSol(lamports);
Expand All @@ -92,7 +90,7 @@ export function SolBalance({
lamports,
maximumFractionDigits = 9,
}: {
lamports: number | BN;
lamports: number | bigint;
maximumFractionDigits?: number;
}) {
return (
Expand Down
10 changes: 5 additions & 5 deletions explorer/src/validators/accounts/stake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { Infer, number, nullable, enums, type } from "superstruct";
import { PublicKeyFromString } from "validators/pubkey";
import { BigNumFromString } from "validators/bignum";
import { BigIntFromString } from "validators/number";

export type StakeAccountType = Infer<typeof StakeAccountType>;
export const StakeAccountType = enums([
Expand All @@ -14,7 +14,7 @@ export const StakeAccountType = enums([

export type StakeMeta = Infer<typeof StakeMeta>;
export const StakeMeta = type({
rentExemptReserve: BigNumFromString,
rentExemptReserve: BigIntFromString,
authorized: type({
staker: PublicKeyFromString,
withdrawer: PublicKeyFromString,
Expand All @@ -33,9 +33,9 @@ export const StakeAccountInfo = type({
type({
delegation: type({
voter: PublicKeyFromString,
stake: BigNumFromString,
activationEpoch: BigNumFromString,
deactivationEpoch: BigNumFromString,
stake: BigIntFromString,
activationEpoch: BigIntFromString,
deactivationEpoch: BigIntFromString,
warmupCooldownRate: number(),
}),
creditsObserved: number(),
Expand Down
7 changes: 0 additions & 7 deletions explorer/src/validators/bignum.ts

This file was deleted.

11 changes: 11 additions & 0 deletions explorer/src/validators/number.ts
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");
});

1 comment on commit 22b966b

@denispalab
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for explorer ready!

✅ Preview
https://explorer-ghsfjcz4g-solana-labs.vercel.app

Built with commit 22b966b.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.