Skip to content

Commit bb65ba9

Browse files
authored
refactor(experimental): fix a few RPC methods
This change simply fixes a handful of incorrect RPC method signatures, as per the RPC docs. Stems from errors found in #2087.
1 parent db105a6 commit bb65ba9

File tree

5 files changed

+132
-25
lines changed

5 files changed

+132
-25
lines changed

packages/rpc-core/src/rpc-methods/__tests__/get-leader-schedule-test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('getLeaderSchedule', () => {
4646
describe('when called with no identity and no slot', () => {
4747
it('returns the leader schedule for all cluster nodes in the current epoch', async () => {
4848
expect.assertions(3);
49-
const res = await rpc.getLeaderSchedule({ commitment }).send();
49+
const res = await rpc.getLeaderSchedule(null, { commitment }).send();
5050
// Does not need null check (default slot)
5151
expect(res).toStrictEqual(expect.any(Object));
5252
for (const key of Object.keys(res)) {
@@ -79,7 +79,7 @@ describe('getLeaderSchedule', () => {
7979
expect.assertions(1);
8080
const identity = await getValidatorAddress();
8181
const res = await rpc
82-
.getLeaderSchedule({
82+
.getLeaderSchedule(null, {
8383
commitment,
8484
identity,
8585
})
@@ -114,7 +114,7 @@ describe('getLeaderSchedule', () => {
114114
it('returns an empty object', async () => {
115115
expect.assertions(1);
116116
const res = await rpc
117-
.getLeaderSchedule({
117+
.getLeaderSchedule(null, {
118118
commitment,
119119
// See scripts/fixtures/GQE2yjns7SKKuMc89tveBDpzYHwXfeuB2PGAbGaPWc6G.json
120120
identity: 'GQE2yjns7SKKuMc89tveBDpzYHwXfeuB2PGAbGaPWc6G' as Address,
@@ -128,7 +128,7 @@ describe('getLeaderSchedule', () => {
128128
it('returns an empty object', async () => {
129129
expect.assertions(1);
130130
const res = await rpc
131-
.getLeaderSchedule({
131+
.getLeaderSchedule(null, {
132132
commitment,
133133
// Randomly generated
134134
identity: 'BnWCFuxmi6uH3ceVx4R8qcbWBMPVVYVVFWtAiiTA1PAu' as Address,
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Address } from '@solana/addresses';
2+
import { Rpc, Slot } from '@solana/rpc-types';
3+
4+
import { GetLeaderScheduleApi } from '../getLeaderSchedule';
5+
6+
const rpc = null as unknown as Rpc<GetLeaderScheduleApi>;
7+
const slot = 0n as Slot;
8+
const identity = 'Joe11111111111111111111111111111' as Address<'Joe11111111111111111111111111111'>;
9+
10+
async () => {
11+
{
12+
const result = await rpc.getLeaderSchedule(slot).send();
13+
// Can be null if the slot corresponds to an epoch that does not exist
14+
result satisfies Record<Address, Slot[]> | null;
15+
}
16+
17+
{
18+
const result = await rpc.getLeaderSchedule(null).send();
19+
// Won't be null
20+
result satisfies Record<Address, Slot[]>;
21+
}
22+
23+
{
24+
const result = await rpc.getLeaderSchedule(slot, { identity }).send();
25+
// Can be null if the slot corresponds to an epoch that does not exist
26+
result satisfies Readonly<{ [key: Address]: Slot[] | undefined }> | null;
27+
}
28+
29+
{
30+
const result = await rpc.getLeaderSchedule(null, { identity }).send();
31+
// Won't be null
32+
result satisfies Readonly<{ [key: Address]: Slot[] | undefined }>;
33+
}
34+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Address } from '@solana/addresses';
2+
import { Rpc } from '@solana/rpc-types';
3+
4+
import { GetSupplyApi } from '../getSupply';
5+
6+
const rpc = null as unknown as Rpc<GetSupplyApi>;
7+
8+
async () => {
9+
{
10+
const result = await rpc.getSupply({ excludeNonCirculatingAccountsList: true }).send();
11+
result satisfies Readonly<{
12+
value: Readonly<{
13+
nonCirculatingAccounts: never[];
14+
}>;
15+
}>;
16+
}
17+
18+
{
19+
const result = await rpc.getSupply().send();
20+
result satisfies Readonly<{
21+
value: Readonly<{
22+
nonCirculatingAccounts: Address[];
23+
}>;
24+
}>;
25+
// @ts-expect-error should not be `never`
26+
result satisfies Readonly<{
27+
value: Readonly<{
28+
nonCirculatingAccounts: never[];
29+
}>;
30+
}>;
31+
}
32+
33+
{
34+
const result = await rpc.getSupply({ excludeNonCirculatingAccountsList: false }).send();
35+
result satisfies Readonly<{
36+
value: Readonly<{
37+
nonCirculatingAccounts: Address[];
38+
}>;
39+
}>;
40+
// @ts-expect-error should not be `never`
41+
result satisfies Readonly<{
42+
value: Readonly<{
43+
nonCirculatingAccounts: never[];
44+
}>;
45+
}>;
46+
}
47+
};
Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import type { Address } from '@solana/addresses';
22
import type { Commitment, IRpcApiMethods, Slot } from '@solana/rpc-types';
33

4+
type GetLeaderScheduleApiConfigBase = Readonly<{
5+
commitment?: Commitment;
6+
}>;
7+
48
/**
59
* This return type is a dictionary of validator identities, as base-58 encoded
610
* strings, and their corresponding leader slot indices as values
@@ -17,8 +21,10 @@ import type { Commitment, IRpcApiMethods, Slot } from '@solana/rpc-types';
1721
* }
1822
* ```
1923
*/
20-
type GetLeaderScheduleApiResponseBase = Readonly<{
21-
[key: Address]: Slot[];
24+
type GetLeaderScheduleApiResponseWithAllIdentities = Record<Address, Slot[]>;
25+
26+
type GetLeaderScheduleApiResponseWithSingleIdentity<TIdentity extends string> = Readonly<{
27+
[TAddress in TIdentity]?: Slot[];
2228
}>;
2329

2430
export interface GetLeaderScheduleApi extends IRpcApiMethods {
@@ -28,21 +34,33 @@ export interface GetLeaderScheduleApi extends IRpcApiMethods {
2834
*
2935
* When a slot is provided, the leader schedule for the epoch that corresponds
3036
* to the provided slot is returned, and this can be null if the slot corresponds
31-
* to an epoch that does not exist
37+
* to an epoch that does not exist.
38+
*
39+
* The RPC request payload provides a `null` value for `slot` when the slot is not
40+
* specified but the request wishes to include config.
3241
*/
42+
getLeaderSchedule<TIdentity extends Address>(
43+
slot: Slot,
44+
config: GetLeaderScheduleApiConfigBase &
45+
Readonly<{
46+
/** Only return results for this validator identity (base58 encoded address) */
47+
identity: Address;
48+
}>,
49+
): GetLeaderScheduleApiResponseWithSingleIdentity<TIdentity> | null;
3350
getLeaderSchedule(
3451
slot: Slot,
35-
config?: Readonly<{
36-
commitment?: Commitment;
37-
/** Only return results for this validator identity (base58 encoded address) */
38-
identity?: Address;
39-
}>,
40-
): GetLeaderScheduleApiResponseBase | null;
52+
config?: GetLeaderScheduleApiConfigBase,
53+
): GetLeaderScheduleApiResponseWithAllIdentities | null;
54+
getLeaderSchedule<TIdentity extends Address>(
55+
slot: null,
56+
config: GetLeaderScheduleApiConfigBase &
57+
Readonly<{
58+
/** Only return results for this validator identity (base58 encoded address) */
59+
identity: Address;
60+
}>,
61+
): GetLeaderScheduleApiResponseWithSingleIdentity<TIdentity>;
4162
getLeaderSchedule(
42-
config?: Readonly<{
43-
commitment?: Commitment;
44-
/** Only return results for this validator identity (base58 encoded address) */
45-
identity?: Address;
46-
}>,
47-
): GetLeaderScheduleApiResponseBase;
48-
}
63+
slot: null,
64+
config?: GetLeaderScheduleApiConfigBase,
65+
): GetLeaderScheduleApiResponseWithAllIdentities;
66+
}

packages/rpc-core/src/rpc-methods/getSupply.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type { Commitment, IRpcApiMethods, LamportsUnsafeBeyond2Pow53Minus1, RpcR
33

44
type GetSupplyConfig = Readonly<{
55
commitment?: Commitment;
6-
excludeNonCirculatingAccountsList?: boolean;
76
}>;
87

98
type GetSupplyApiResponseBase = RpcResponse<{
@@ -19,14 +18,18 @@ type GetSupplyApiResponseWithNonCirculatingAccounts = GetSupplyApiResponseBase &
1918
Readonly<{
2019
value: Readonly<{
2120
/** an array of account addresses of non-circulating accounts */
22-
nonCirculatingAccounts: [Address];
21+
nonCirculatingAccounts: Address[];
2322
}>;
2423
}>;
2524

2625
type GetSupplyApiResponseWithoutNonCirculatingAccounts = GetSupplyApiResponseBase &
2726
Readonly<{
2827
value: Readonly<{
29-
nonCirculatingAccounts: [];
28+
/** As per the docs:
29+
* "If `excludeNonCirculatingAccountsList` is enabled, the returned array will be empty."
30+
* See: https://solana.com/docs/rpc/http/getsupply
31+
*/
32+
nonCirculatingAccounts: never[];
3033
}>;
3134
}>;
3235

@@ -40,5 +43,10 @@ export interface GetSupplyApi extends IRpcApiMethods {
4043
excludeNonCirculatingAccountsList: true;
4144
}>,
4245
): GetSupplyApiResponseWithoutNonCirculatingAccounts;
43-
getSupply(config?: GetSupplyConfig): GetSupplyApiResponseWithNonCirculatingAccounts;
44-
}
46+
getSupply(
47+
config?: GetSupplyConfig &
48+
Readonly<{
49+
excludeNonCirculatingAccountsList?: false;
50+
}>,
51+
): GetSupplyApiResponseWithNonCirculatingAccounts;
52+
}

0 commit comments

Comments
 (0)