Skip to content

Commit

Permalink
Remove UnsafeBeyond2Pow53Minus1 type suffixes (#3456)
Browse files Browse the repository at this point in the history
Both RPC and RPC Subscriptions packages now use custom JSON serialization to safely convert large integers as `bigints` from the server requests/responses. This means we are no longer handling `bigints` that are unsafe below `2^53-1` which also means we can remove the `UnsafeBeyond2Pow53Minus1` suffixes to all types currently using it.

Note that the `F64UnsafeSeeDocumentation` type is excluded from this refactoring as the bigint serialization only affects integers.
  • Loading branch information
lorisleiva authored Oct 27, 2024
1 parent 500a991 commit 0245265
Show file tree
Hide file tree
Showing 67 changed files with 306 additions and 371 deletions.
6 changes: 6 additions & 0 deletions .changeset/loud-hotels-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@solana/rpc-types': patch
'@solana/rpc-api': patch
---

Remove `UnsafeBeyond2Pow53Minus1` type suffixes
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ Here’s an example of this in action:
// {
// blockhash: Blockhash;
// blockHeight: bigint;
// blockTime: UnixTimestampUnsafeBeyond2Pow53Minus1;
// blockTime: UnixTimestamp;
// parentSlot: bigint;
// previousBlockhash: Blockhash;
// }
Expand Down Expand Up @@ -1276,7 +1276,7 @@ const account = await fetchAddressLookupTableFromSeeds(rpc, {
});

account.address; // Address
account.lamports; // LamportsUnsafeBeyond2Pow53Minus1
account.lamports; // Lamports
account.data.addresses; // Address[]
account.data.authority; // Some<Address>
account.data.deactivationSlot; // Slot
Expand Down
11 changes: 2 additions & 9 deletions examples/react-app/src/functions/balance.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
AccountNotificationsApi,
Address,
GetBalanceApi,
LamportsUnsafeBeyond2Pow53Minus1,
Rpc,
RpcSubscriptions,
} from '@solana/web3.js';
import { AccountNotificationsApi, Address, GetBalanceApi, Lamports, Rpc, RpcSubscriptions } from '@solana/web3.js';
import { SWRSubscription } from 'swr/subscription';

const EXPLICIT_ABORT_TOKEN = Symbol();
Expand All @@ -24,7 +17,7 @@ const EXPLICIT_ABORT_TOKEN = Symbol();
export function balanceSubscribe(
rpc: Rpc<GetBalanceApi>,
rpcSubscriptions: RpcSubscriptions<AccountNotificationsApi>,
...subscriptionArgs: Parameters<SWRSubscription<{ address: Address }, LamportsUnsafeBeyond2Pow53Minus1>>
...subscriptionArgs: Parameters<SWRSubscription<{ address: Address }, Lamports>>
) {
const [{ address }, { next }] = subscriptionArgs;
const abortController = new AbortController();
Expand Down
4 changes: 2 additions & 2 deletions packages/accounts/src/account.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { Address } from '@solana/addresses';
import { ReadonlyUint8Array } from '@solana/codecs-core';
import type { LamportsUnsafeBeyond2Pow53Minus1 } from '@solana/rpc-types';
import type { Lamports } from '@solana/rpc-types';

/** The amount of bytes required to store the base account information without its data. */
export const BASE_ACCOUNT_SIZE = 128;

/** Describe the generic account details applicable to every account. */
export type BaseAccount = {
readonly executable: boolean;
readonly lamports: LamportsUnsafeBeyond2Pow53Minus1;
readonly lamports: Lamports;
readonly programAddress: Address;
};

Expand Down
4 changes: 2 additions & 2 deletions packages/accounts/src/rpc-api/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { U64UnsafeBeyond2Pow53Minus1 } from '@solana/rpc-types';
import type { U64 } from '@solana/rpc-types';

export type JsonParsedDataResponse<TData = object> = Readonly<{
parsed: {
Expand All @@ -7,5 +7,5 @@ export type JsonParsedDataResponse<TData = object> = Readonly<{
};
// Name of the program that owns this account.
program: string;
space: U64UnsafeBeyond2Pow53Minus1;
space: U64;
}>;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FetchAccountsConfig, fetchJsonParsedAccounts } from '@solana/accounts';
import type { Address } from '@solana/addresses';
import type { GetMultipleAccountsApi, Rpc } from '@solana/rpc';
import type { Blockhash, LamportsUnsafeBeyond2Pow53Minus1 } from '@solana/rpc-types';
import type { Blockhash, Lamports } from '@solana/rpc-types';
import {
CompiledTransactionMessage,
decompileTransactionMessage,
Expand Down Expand Up @@ -261,7 +261,7 @@ describe('decodeTransactionMessage', () => {
},
executable: false,
exists: true,
lamports: 0n as LamportsUnsafeBeyond2Pow53Minus1,
lamports: 0n as Lamports,
programAddress: 'program' as Address,
},
{
Expand All @@ -271,7 +271,7 @@ describe('decodeTransactionMessage', () => {
},
executable: false,
exists: true,
lamports: 0n as LamportsUnsafeBeyond2Pow53Minus1,
lamports: 0n as Lamports,
programAddress: 'program' as Address,
},
];
Expand Down
4 changes: 2 additions & 2 deletions packages/library/src/airdrop-internal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Address } from '@solana/addresses';
import type { Signature } from '@solana/keys';
import type { RequestAirdropApi, Rpc } from '@solana/rpc';
import type { Commitment, LamportsUnsafeBeyond2Pow53Minus1 } from '@solana/rpc-types';
import type { Commitment, Lamports } from '@solana/rpc-types';
import { waitForRecentTransactionConfirmationUntilTimeout } from '@solana/transaction-confirmation';

type RequestAndConfirmAirdropConfig = Readonly<{
Expand All @@ -13,7 +13,7 @@ type RequestAndConfirmAirdropConfig = Readonly<{
'getRecentSignatureConfirmationPromise' | 'getTimeoutPromise'
>,
) => Promise<void>;
lamports: LamportsUnsafeBeyond2Pow53Minus1;
lamports: Lamports;
recipientAddress: Address;
rpc: Rpc<RequestAirdropApi>;
}>;
Expand Down
4 changes: 2 additions & 2 deletions packages/rpc-api/src/__tests__/request-airdrop-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Address } from '@solana/addresses';
import { getBase58Decoder } from '@solana/codecs-strings';
import type { Rpc } from '@solana/rpc-spec';
import type { Commitment, LamportsUnsafeBeyond2Pow53Minus1 } from '@solana/rpc-types';
import type { Commitment, Lamports } from '@solana/rpc-types';

import { RequestAirdropApi } from '../index';
import { createLocalhostSolanaRpc } from './__setup__';
Expand All @@ -19,7 +19,7 @@ describe('requestAirdrop', () => {
crypto.getRandomValues(randomBytes);
const publicKeyAddress = getBase58Decoder().decode(randomBytes);
const resultPromise = rpc
.requestAirdrop(publicKeyAddress as Address, 5000000n as LamportsUnsafeBeyond2Pow53Minus1, {
.requestAirdrop(publicKeyAddress as Address, 5000000n as Lamports, {
commitment,
})
.send();
Expand Down
42 changes: 21 additions & 21 deletions packages/rpc-api/src/__typetests__/get-block-type-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import type {
Base58EncodedDataResponse,
Base64EncodedDataResponse,
Blockhash,
LamportsUnsafeBeyond2Pow53Minus1,
Lamports,
Reward,
TokenBalance,
TransactionError,
TransactionStatus,
U64UnsafeBeyond2Pow53Minus1,
U64,
} from '@solana/rpc-types';
import { TransactionVersion } from '@solana/transaction-messages';

Expand Down Expand Up @@ -260,10 +260,10 @@ void (async () => {

type ExpectedMetaForAccountsBase = {
err: TransactionError | null;
fee: LamportsUnsafeBeyond2Pow53Minus1;
postBalances: readonly LamportsUnsafeBeyond2Pow53Minus1[];
fee: Lamports;
postBalances: readonly Lamports[];
postTokenBalances?: readonly TokenBalance[];
preBalances: readonly LamportsUnsafeBeyond2Pow53Minus1[];
preBalances: readonly Lamports[];
preTokenBalances?: readonly TokenBalance[];
status: TransactionStatus;
};
Expand Down Expand Up @@ -503,9 +503,9 @@ void (async () => {
}

type ExpectedMetaForFullBase58 = {
computeUnitsConsumed?: U64UnsafeBeyond2Pow53Minus1;
computeUnitsConsumed?: U64;
err: TransactionError | null;
fee: LamportsUnsafeBeyond2Pow53Minus1;
fee: Lamports;
innerInstructions: readonly Readonly<{
index: number;
instructions: readonly Readonly<{
Expand All @@ -516,9 +516,9 @@ void (async () => {
}>[];
}>[];
logMessages: readonly string[] | null;
postBalances: readonly LamportsUnsafeBeyond2Pow53Minus1[];
postBalances: readonly Lamports[];
postTokenBalances?: readonly TokenBalance[];
preBalances: readonly LamportsUnsafeBeyond2Pow53Minus1[];
preBalances: readonly Lamports[];
preTokenBalances?: readonly TokenBalance[];
returnData?: Readonly<{
data: Base64EncodedDataResponse;
Expand Down Expand Up @@ -728,9 +728,9 @@ void (async () => {
}

type ExpectedMetaForFullBase64 = {
computeUnitsConsumed?: U64UnsafeBeyond2Pow53Minus1;
computeUnitsConsumed?: U64;
err: TransactionError | null;
fee: LamportsUnsafeBeyond2Pow53Minus1;
fee: Lamports;
innerInstructions: readonly Readonly<{
index: number;
instructions: readonly Readonly<{
Expand All @@ -741,9 +741,9 @@ void (async () => {
}>[];
}>[];
logMessages: readonly string[] | null;
postBalances: readonly LamportsUnsafeBeyond2Pow53Minus1[];
postBalances: readonly Lamports[];
postTokenBalances?: readonly TokenBalance[];
preBalances: readonly LamportsUnsafeBeyond2Pow53Minus1[];
preBalances: readonly Lamports[];
preTokenBalances?: readonly TokenBalance[];
returnData?: Readonly<{
data: Base64EncodedDataResponse;
Expand Down Expand Up @@ -927,17 +927,17 @@ void (async () => {
| ExpectedPartiallyDecodedTransactionInstruction;

type ExpectedMetaForFullJsonParsedBase = {
computeUnitsConsumed?: U64UnsafeBeyond2Pow53Minus1;
computeUnitsConsumed?: U64;
err: TransactionError | null;
fee: LamportsUnsafeBeyond2Pow53Minus1;
fee: Lamports;
innerInstructions: readonly Readonly<{
index: number;
instructions: readonly ExpectedTransactionInstructionForFullJsonParsed[];
}>[];
logMessages: readonly string[] | null;
postBalances: readonly LamportsUnsafeBeyond2Pow53Minus1[];
postBalances: readonly Lamports[];
postTokenBalances?: readonly TokenBalance[];
preBalances: readonly LamportsUnsafeBeyond2Pow53Minus1[];
preBalances: readonly Lamports[];
preTokenBalances?: readonly TokenBalance[];
returnData?: Readonly<{
data: Base64EncodedDataResponse;
Expand Down Expand Up @@ -1092,17 +1092,17 @@ void (async () => {
};

type ExpectedMetaForFullJsonBase = {
computeUnitsConsumed?: U64UnsafeBeyond2Pow53Minus1;
computeUnitsConsumed?: U64;
err: TransactionError | null;
fee: LamportsUnsafeBeyond2Pow53Minus1;
fee: Lamports;
innerInstructions: readonly Readonly<{
index: number;
instructions: readonly ExpectedTransactionInstructionForFullJson[];
}>[];
logMessages: readonly string[] | null;
postBalances: readonly LamportsUnsafeBeyond2Pow53Minus1[];
postBalances: readonly Lamports[];
postTokenBalances?: readonly TokenBalance[];
preBalances: readonly LamportsUnsafeBeyond2Pow53Minus1[];
preBalances: readonly Lamports[];
preTokenBalances?: readonly TokenBalance[];
returnData?: Readonly<{
data: Base64EncodedDataResponse;
Expand Down
4 changes: 2 additions & 2 deletions packages/rpc-api/src/getBalance.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Address } from '@solana/addresses';
import type { Commitment, LamportsUnsafeBeyond2Pow53Minus1, Slot, SolanaRpcResponse } from '@solana/rpc-types';
import type { Commitment, Lamports, Slot, SolanaRpcResponse } from '@solana/rpc-types';

type GetBalanceApiResponse = SolanaRpcResponse<LamportsUnsafeBeyond2Pow53Minus1>;
type GetBalanceApiResponse = SolanaRpcResponse<Lamports>;

export type GetBalanceApi = {
/**
Expand Down
8 changes: 4 additions & 4 deletions packages/rpc-api/src/getBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import type {
TransactionForFullBase64,
TransactionForFullJson,
TransactionForFullJsonParsed,
U64UnsafeBeyond2Pow53Minus1,
UnixTimestampUnsafeBeyond2Pow53Minus1,
U64,
UnixTimestamp,
} from '@solana/rpc-types';
import type { TransactionVersion } from '@solana/transaction-messages';

// API response types

type GetBlockApiResponseBase = Readonly<{
/** The number of blocks beneath this block */
blockHeight: U64UnsafeBeyond2Pow53Minus1;
blockHeight: U64;
/** Estimated production time, as Unix timestamp */
blockTime: UnixTimestampUnsafeBeyond2Pow53Minus1;
blockTime: UnixTimestamp;
/** the blockhash of this block */
blockhash: Blockhash;
/** The slot index of this block's parent */
Expand Down
6 changes: 3 additions & 3 deletions packages/rpc-api/src/getBlockCommitment.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { LamportsUnsafeBeyond2Pow53Minus1, Slot } from '@solana/rpc-types';
import type { Lamports, Slot } from '@solana/rpc-types';

type GetBlockCommitmentApiResponse = Readonly<{
commitment: LamportsUnsafeBeyond2Pow53Minus1[] | null;
totalStake: LamportsUnsafeBeyond2Pow53Minus1;
commitment: Lamports[] | null;
totalStake: Lamports;
}>;

export type GetBlockCommitmentApi = {
Expand Down
4 changes: 2 additions & 2 deletions packages/rpc-api/src/getBlockHeight.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Commitment, Slot, U64UnsafeBeyond2Pow53Minus1 } from '@solana/rpc-types';
import type { Commitment, Slot, U64 } from '@solana/rpc-types';

type GetBlockHeightApiResponse = U64UnsafeBeyond2Pow53Minus1;
type GetBlockHeightApiResponse = U64;

export type GetBlockHeightApi = {
/**
Expand Down
6 changes: 3 additions & 3 deletions packages/rpc-api/src/getBlockProduction.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Address } from '@solana/addresses';
import type { Commitment, Slot, SolanaRpcResponse, U64UnsafeBeyond2Pow53Minus1 } from '@solana/rpc-types';
import type { Commitment, Slot, SolanaRpcResponse, U64 } from '@solana/rpc-types';

type NumberOfLeaderSlots = U64UnsafeBeyond2Pow53Minus1;
type NumberOfBlocksProduced = U64UnsafeBeyond2Pow53Minus1;
type NumberOfLeaderSlots = U64;
type NumberOfBlocksProduced = U64;

type SlotRange = Readonly<{
firstSlot: Slot;
Expand Down
4 changes: 2 additions & 2 deletions packages/rpc-api/src/getBlockTime.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Slot, UnixTimestampUnsafeBeyond2Pow53Minus1 } from '@solana/rpc-types';
import type { Slot, UnixTimestamp } from '@solana/rpc-types';

/** Estimated production time, as Unix timestamp (seconds since the Unix epoch) */
type GetBlockTimeApiResponse = UnixTimestampUnsafeBeyond2Pow53Minus1;
type GetBlockTimeApiResponse = UnixTimestamp;

export type GetBlockTimeApi = {
/**
Expand Down
12 changes: 6 additions & 6 deletions packages/rpc-api/src/getEpochInfo.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import type { Commitment, Slot, U64UnsafeBeyond2Pow53Minus1 } from '@solana/rpc-types';
import type { Commitment, Slot, U64 } from '@solana/rpc-types';

type GetEpochInfoApiResponse = Readonly<{
/** the current slot */
absoluteSlot: Slot;
/** the current block height */
blockHeight: U64UnsafeBeyond2Pow53Minus1;
blockHeight: U64;
/** the current epoch */
epoch: U64UnsafeBeyond2Pow53Minus1;
epoch: U64;
/** the current slot relative to the start of the current epoch */
slotIndex: U64UnsafeBeyond2Pow53Minus1;
slotIndex: U64;
/** the number of slots in this epoch */
slotsInEpoch: U64UnsafeBeyond2Pow53Minus1;
slotsInEpoch: U64;
/** total number of transactions processed without error since genesis */
transactionCount: U64UnsafeBeyond2Pow53Minus1 | null;
transactionCount: U64 | null;
}>;

export type GetEpochInfoApi = {
Expand Down
10 changes: 5 additions & 5 deletions packages/rpc-api/src/getEpochSchedule.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { U64UnsafeBeyond2Pow53Minus1 } from '@solana/rpc-types';
import type { U64 } from '@solana/rpc-types';

type GetEpochScheduleApiResponse = Readonly<{
/** first normal-length epoch, log2(slotsPerEpoch) - log2(MINIMUM_SLOTS_PER_EPOCH) */
firstNormalEpoch: U64UnsafeBeyond2Pow53Minus1;
firstNormalEpoch: U64;
/** MINIMUM_SLOTS_PER_EPOCH * (2^(firstNormalEpoch) - 1) */
firstNormalSlot: U64UnsafeBeyond2Pow53Minus1;
firstNormalSlot: U64;
/** the number of slots before beginning of an epoch to calculate a leader schedule for that epoch */
leaderScheduleSlotOffset: U64UnsafeBeyond2Pow53Minus1;
leaderScheduleSlotOffset: U64;
/** the maximum number of slots in each epoch */
slotsPerEpoch: U64UnsafeBeyond2Pow53Minus1;
slotsPerEpoch: U64;
/** whether epochs start short and grow */
warmup: boolean;
}>;
Expand Down
4 changes: 2 additions & 2 deletions packages/rpc-api/src/getFeeForMessage.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Commitment, LamportsUnsafeBeyond2Pow53Minus1, Slot, SolanaRpcResponse } from '@solana/rpc-types';
import type { Commitment, Lamports, Slot, SolanaRpcResponse } from '@solana/rpc-types';
import type { TransactionMessageBytesBase64 } from '@solana/transactions';

/** Fee corresponding to the message at the specified blockhash */
type GetFeeForMessageApiResponse = LamportsUnsafeBeyond2Pow53Minus1 | null;
type GetFeeForMessageApiResponse = Lamports | null;

export type GetFeeForMessageApi = {
/**
Expand Down
Loading

0 comments on commit 0245265

Please sign in to comment.