diff --git a/web3.js/.flowconfig b/web3.js/.flowconfig index 6c17cdbd4d..2785fb1653 100644 --- a/web3.js/.flowconfig +++ b/web3.js/.flowconfig @@ -16,3 +16,4 @@ experimental.const_params=true include_warnings=true suppress_comment=\\(.\\|\n\\)*\\$FlowFixMe suppress_comment=\\(.\\|\n\\)*\\$FlowIssue +suppress_comment=\\(.\\|\n\\)*\\$FlowIgnore diff --git a/web3.js/module.d.ts b/web3.js/module.d.ts index ae543541a5..65b53f7e2f 100644 --- a/web3.js/module.d.ts +++ b/web3.js/module.d.ts @@ -662,11 +662,11 @@ declare module '@solana/web3.js' { commitment?: Commitment, ): Promise; - // === src/util/testnet.js === - export function testnetChannelEndpoint( - channel?: string, - tls?: boolean, - ): string; + // === src/util/cluster.js === + export type Cluster = 'devnet' | 'testnet' | 'mainnet-beta'; + export function clusterApiUrl(cluster?: Cluster, tls?: boolean): string; + + // === src/index.js === export const LAMPORTS_PER_SOL: number; } diff --git a/web3.js/module.flow.js b/web3.js/module.flow.js index 0baa795a22..27adc8a493 100644 --- a/web3.js/module.flow.js +++ b/web3.js/module.flow.js @@ -676,11 +676,14 @@ declare module '@solana/web3.js' { commitment: ?Commitment, ): Promise; - // === src/util/testnet.js === - declare export function testnetChannelEndpoint( - channel?: string, + // === src/util/cluster.js === + declare export type Cluster = 'devnet' | 'testnet' | 'mainnet-beta'; + + declare export function clusterApiUrl( + cluster?: Cluster, tls?: boolean, ): string; + // === src/index.js === declare export var LAMPORTS_PER_SOL: number; } diff --git a/web3.js/src/index.js b/web3.js/src/index.js index 4071bdf41a..ae0a5faf41 100644 --- a/web3.js/src/index.js +++ b/web3.js/src/index.js @@ -30,7 +30,7 @@ export { sendAndConfirmRecentTransaction, } from './util/send-and-confirm-transaction'; export {sendAndConfirmRawTransaction} from './util/send-and-confirm-raw-transaction'; -export {testnetChannelEndpoint} from './util/testnet'; +export {clusterApiUrl} from './util/cluster'; /** * There are 1-billion lamports in one SOL diff --git a/web3.js/src/util/cluster.js b/web3.js/src/util/cluster.js new file mode 100644 index 0000000000..2fe3a1157a --- /dev/null +++ b/web3.js/src/util/cluster.js @@ -0,0 +1,36 @@ +//@flow + +/** + * @private + */ +const endpoint = { + http: { + devnet: 'http://devnet.solana.com', + testnet: 'http://testnet.solana.com', + 'mainnet-beta': 'http://api.mainnet-beta.solana.com', + }, + https: { + devnet: 'https://devnet.solana.com', + testnet: 'https://testnet.solana.com', + 'mainnet-beta': 'https://api.mainnet-beta.solana.com', + }, +}; + +export type Cluster = 'devnet' | 'testnet' | 'mainnet-beta'; + +/** + * Retrieves the RPC API URL for the specified cluster + */ +export function clusterApiUrl(cluster?: Cluster, tls?: boolean): string { + const key = tls === false ? 'http' : 'https'; + + if (!cluster) { + return endpoint[key]['devnet']; + } + + const url = endpoint[key][cluster]; + if (!url) { + throw new Error(`Unknown ${key} cluster: ${cluster}`); + } + return url; +} diff --git a/web3.js/src/util/testnet.js b/web3.js/src/util/testnet.js deleted file mode 100644 index d20aac7d2d..0000000000 --- a/web3.js/src/util/testnet.js +++ /dev/null @@ -1,40 +0,0 @@ -//@flow - -import {testnetDefaultChannel} from '../../package.json'; - -/** - * @private - */ -const endpoint = { - http: { - edge: 'http://edge.devnet.solana.com:8899', - beta: 'http://beta.devnet.solana.com:8899', - stable: 'http://devnet.solana.com', - }, - https: { - edge: 'https://edge.devnet.solana.com:8443', - beta: 'https://beta.devnet.solana.com:8443', - stable: 'https://devnet.solana.com', - }, -}; - -/** - * Retrieves the RPC endpoint URL for the specified testnet release - * channel - */ -export function testnetChannelEndpoint( - channel?: string, - tls?: boolean, -): string { - const key = tls === false ? 'http' : 'https'; - - if (!channel) { - return endpoint[key][testnetDefaultChannel]; - } - - const url = endpoint[key][channel]; - if (!url) { - throw new Error(`Unknown ${key} channel: ${channel}`); - } - return url; -} diff --git a/web3.js/test/cluster.test.js b/web3.js/test/cluster.test.js new file mode 100644 index 0000000000..a705335ed9 --- /dev/null +++ b/web3.js/test/cluster.test.js @@ -0,0 +1,16 @@ +// @flow +import {clusterApiUrl} from '../src/util/cluster'; + +test('invalid', () => { + expect(() => { + // $FlowIgnore + clusterApiUrl('abc123'); + }).toThrow(); +}); + +test('devnet', () => { + expect(clusterApiUrl()).toEqual('https://devnet.solana.com'); + expect(clusterApiUrl('devnet')).toEqual('https://devnet.solana.com'); + expect(clusterApiUrl('devnet', true)).toEqual('https://devnet.solana.com'); + expect(clusterApiUrl('devnet', false)).toEqual('http://devnet.solana.com'); +}); diff --git a/web3.js/test/testnet.test.js b/web3.js/test/testnet.test.js deleted file mode 100644 index 2b40299909..0000000000 --- a/web3.js/test/testnet.test.js +++ /dev/null @@ -1,24 +0,0 @@ -// @flow -import {testnetChannelEndpoint} from '../src/util/testnet'; - -test('invalid', () => { - expect(() => { - testnetChannelEndpoint('abc123'); - }).toThrow(); -}); - -test('stable', () => { - expect(testnetChannelEndpoint('stable')).toEqual('https://devnet.solana.com'); - - expect(testnetChannelEndpoint('stable', true)).toEqual( - 'https://devnet.solana.com', - ); - - expect(testnetChannelEndpoint('stable', false)).toEqual( - 'http://devnet.solana.com', - ); -}); - -test('default', () => { - testnetChannelEndpoint(); // Should not throw -});