-
Notifications
You must be signed in to change notification settings - Fork 911
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(experimental): add cluster level API for transports
This change introduces cluster-level transports as well as cluster-level JSON RPCs that infer the cluster from the provided transport.
- Loading branch information
1 parent
61f7ba0
commit e58bb22
Showing
10 changed files
with
174 additions
and
14 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
packages/rpc-transport/src/__typetests__/json-rpc-typetest.ts
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,48 @@ | ||
import { devnet, IRpcApiMethods, mainnet, Rpc, testnet } from '@solana/rpc-types'; | ||
|
||
import { createJsonRpcApi } from '../apis/methods/methods-api'; | ||
import { createJsonRpc } from '../json-rpc'; | ||
import { RpcDevnet, RpcMainnet, RpcTestnet } from '../json-rpc-types'; | ||
import { createHttpTransport } from '../transports/http/http-transport'; | ||
|
||
interface MyApiMethods extends IRpcApiMethods { | ||
foo(): number; | ||
bar(): string; | ||
} | ||
|
||
const api = createJsonRpcApi<MyApiMethods>(); | ||
|
||
const genericTransport = createHttpTransport({ url: 'http://localhost:8899' }); | ||
const devnetTransport = createHttpTransport({ url: devnet('https://api.devnet.solana.com') }); | ||
const testnetTransport = createHttpTransport({ url: testnet('https://api.testnet.solana.com') }); | ||
const mainnetTransport = createHttpTransport({ url: mainnet('https://api.mainnet-beta.solana.com') }); | ||
|
||
// When providing a generic transport, the RPC should be typed as an Rpc | ||
createJsonRpc({ api, transport: genericTransport }) satisfies Rpc<MyApiMethods>; | ||
//@ts-expect-error Should not be a devnet RPC | ||
createJsonRpc({ api, transport: genericTransport }) satisfies RpcDevnet<MyApiMethods>; | ||
//@ts-expect-error Should not be a testnet RPC | ||
createJsonRpc({ api, transport: genericTransport }) satisfies RpcTestnet<MyApiMethods>; | ||
//@ts-expect-error Should not be a mainnet RPC | ||
createJsonRpc({ api, transport: genericTransport }) satisfies RpcMainnet<MyApiMethods>; | ||
|
||
// When providing a devnet transport, the RPC should be typed as an RpcDevnet | ||
createJsonRpc({ api, transport: devnetTransport }) satisfies RpcDevnet<MyApiMethods>; | ||
//@ts-expect-error Should not be a testnet RPC | ||
createJsonRpc({ api, transport: devnetTransport }) satisfies RpcTestnet<MyApiMethods>; | ||
//@ts-expect-error Should not be a mainnet RPC | ||
createJsonRpc({ api, transport: devnetTransport }) satisfies RpcMainnet<MyApiMethods>; | ||
|
||
// When providing a testnet transport, the RPC should be typed as an RpcTestnet | ||
createJsonRpc({ api, transport: testnetTransport }) satisfies RpcTestnet<MyApiMethods>; | ||
//@ts-expect-error Should not be a devnet RPC | ||
createJsonRpc({ api, transport: testnetTransport }) satisfies RpcDevnet<MyApiMethods>; | ||
//@ts-expect-error Should not be a mainnet RPC | ||
createJsonRpc({ api, transport: testnetTransport }) satisfies RpcMainnet<MyApiMethods>; | ||
|
||
// When providing a mainnet transport, the RPC should be typed as an RpcMainnet | ||
createJsonRpc({ api, transport: mainnetTransport }) satisfies RpcMainnet<MyApiMethods>; | ||
//@ts-expect-error Should not be a devnet RPC | ||
createJsonRpc({ api, transport: mainnetTransport }) satisfies RpcDevnet<MyApiMethods>; | ||
//@ts-expect-error Should not be a testnet RPC | ||
createJsonRpc({ api, transport: mainnetTransport }) satisfies RpcTestnet<MyApiMethods>; |
2 changes: 1 addition & 1 deletion
2
...src/__typetests__/methods-api-typetest.ts → ...pis/__typetests__/methods-api-typetest.ts
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
2 changes: 1 addition & 1 deletion
2
...typetests__/subscriptions-api-typetest.ts → ...typetests__/subscriptions-api-typetest.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Rpc } from '@solana/rpc-types'; | ||
|
||
import { | ||
IRpcTransport, | ||
IRpcTransportDevnet, | ||
IRpcTransportMainnet, | ||
IRpcTransportTestnet, | ||
IRpcTransportWithCluster, | ||
} from './transports/transport-types'; | ||
|
||
export type RpcDevnet<TRpcMethods> = Rpc<TRpcMethods> & { '~cluster': 'devnet' }; | ||
export type RpcTestnet<TRpcMethods> = Rpc<TRpcMethods> & { '~cluster': 'testnet' }; | ||
export type RpcMainnet<TRpcMethods> = Rpc<TRpcMethods> & { '~cluster': 'mainnet' }; | ||
export type RpcFromTransport< | ||
TRpcMethods, | ||
TRpcTransport extends IRpcTransport | IRpcTransportWithCluster, | ||
> = TRpcTransport extends IRpcTransportDevnet | ||
? RpcDevnet<TRpcMethods> | ||
: TRpcTransport extends IRpcTransportTestnet | ||
? RpcTestnet<TRpcMethods> | ||
: TRpcTransport extends IRpcTransportMainnet | ||
? RpcMainnet<TRpcMethods> | ||
: Rpc<TRpcMethods>; |
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
39 changes: 39 additions & 0 deletions
39
packages/rpc-transport/src/transports/http/__typetests__/http-transport-typetest.ts
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,39 @@ | ||
import { devnet, mainnet, testnet } from '@solana/rpc-types'; | ||
|
||
import { IRpcTransport, IRpcTransportDevnet, IRpcTransportMainnet, IRpcTransportTestnet } from '../../transport-types'; | ||
import { createHttpTransport } from '../http-transport'; | ||
|
||
const genericUrl = 'http://localhost:8899'; | ||
const devnetUrl = devnet('https://api.devnet.solana.com'); | ||
const testnetUrl = testnet('https://api.testnet.solana.com'); | ||
const mainnetUrl = mainnet('https://api.mainnet-beta.solana.com'); | ||
|
||
// When providing a generic URL, the transport should be typed as an IRpcTransport | ||
createHttpTransport({ url: genericUrl }) satisfies IRpcTransport; | ||
//@ts-expect-error Should not be a devnet transport | ||
createHttpTransport({ url: genericUrl }) satisfies IRpcTransportDevnet; | ||
//@ts-expect-error Should not be a testnet transport | ||
createHttpTransport({ url: genericUrl }) satisfies IRpcTransportTestnet; | ||
//@ts-expect-error Should not be a mainnet transport | ||
createHttpTransport({ url: genericUrl }) satisfies IRpcTransportMainnet; | ||
|
||
// When providing a devnet URL, the transport should be typed as an IRpcTransportDevnet | ||
createHttpTransport({ url: devnetUrl }) satisfies IRpcTransportDevnet; | ||
//@ts-expect-error Should not be a testnet transport | ||
createHttpTransport({ url: devnetUrl }) satisfies IRpcTransportTestnet; | ||
//@ts-expect-error Should not be a mainnet transport | ||
createHttpTransport({ url: devnetUrl }) satisfies IRpcTransportMainnet; | ||
|
||
// When providing a testnet URL, the transport should be typed as an IRpcTransportTestnet | ||
createHttpTransport({ url: testnetUrl }) satisfies IRpcTransportTestnet; | ||
//@ts-expect-error Should not be a devnet transport | ||
createHttpTransport({ url: testnetUrl }) satisfies IRpcTransportDevnet; | ||
//@ts-expect-error Should not be a mainnet transport | ||
createHttpTransport({ url: testnetUrl }) satisfies IRpcTransportMainnet; | ||
|
||
// When providing a mainnet URL, the transport should be typed as an IRpcTransportMainnet | ||
createHttpTransport({ url: mainnetUrl }) satisfies IRpcTransportMainnet; | ||
//@ts-expect-error Should not be a devnet transport | ||
createHttpTransport({ url: mainnetUrl }) satisfies IRpcTransportDevnet; | ||
//@ts-expect-error Should not be a testnet transport | ||
createHttpTransport({ url: mainnetUrl }) satisfies IRpcTransportTestnet; |
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