From 12402e31e0f22de85351843baa2bae8be440918f Mon Sep 17 00:00:00 2001 From: Toni Tabak Date: Mon, 6 Nov 2023 15:04:05 +0100 Subject: [PATCH] feat: rpc default provider and public nodes --- src/constants.ts | 12 ++++++++++++ src/provider/default.ts | 12 ++---------- src/provider/rpc.ts | 28 +++++++++++++++++++++++++--- src/types/provider/configuration.ts | 2 +- 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index 1018d13cd..c065423a3 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -44,3 +44,15 @@ export const UDC = { ADDRESS: '0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf', ENTRYPOINT: 'deployContract', }; + +export const RPC_GOERLI_NODES = [ + 'https://starknet-testnet.public.blastapi.io', + 'https://rpc.starknet-testnet.lava.build', + 'http://limited-rpc.nethermind.io/goerli-juno', +]; + +export const RPC_MAINNET_NODES = [ + 'https://starknet-mainnet.public.blastapi.io', + 'https://rpc.starknet.lava.build', + 'http://limited-rpc.nethermind.io/mainnet-juno/', +]; diff --git a/src/provider/default.ts b/src/provider/default.ts index 89c69bac5..2a4feae79 100644 --- a/src/provider/default.ts +++ b/src/provider/default.ts @@ -23,7 +23,6 @@ import { Nonce, ProviderOptions, RpcProviderOptions, - SequencerProviderOptions, SimulateTransactionResponse, StateUpdateResponse, Storage, @@ -34,7 +33,6 @@ import { } from '../types'; import { ProviderInterface } from './interface'; import { RpcProvider } from './rpc'; -import { SequencerProvider } from './sequencer'; import { getAddressFromStarkName, getStarkName } from './starknetId'; export class Provider implements ProviderInterface { @@ -44,21 +42,15 @@ export class Provider implements ProviderInterface { if (providerOrOptions instanceof Provider) { // providerOrOptions is Provider this.provider = providerOrOptions.provider; - } else if ( - providerOrOptions instanceof RpcProvider || - providerOrOptions instanceof SequencerProvider - ) { + } else if (providerOrOptions instanceof RpcProvider) { // providerOrOptions is SequencerProvider or RpcProvider this.provider = providerOrOptions; } else if (providerOrOptions && 'rpc' in providerOrOptions) { // providerOrOptions is rpc option this.provider = new RpcProvider(providerOrOptions.rpc); - } else if (providerOrOptions && 'sequencer' in providerOrOptions) { - // providerOrOptions is sequencer option - this.provider = new SequencerProvider(providerOrOptions.sequencer); } else { // providerOrOptions is none, create SequencerProvider as default - this.provider = new SequencerProvider(); + this.provider = new RpcProvider(); } } diff --git a/src/provider/rpc.ts b/src/provider/rpc.ts index 27e1eb7d6..76f6e52df 100644 --- a/src/provider/rpc.ts +++ b/src/provider/rpc.ts @@ -1,6 +1,9 @@ import { HEX_STR_TRANSACTION_VERSION_1, HEX_STR_TRANSACTION_VERSION_2, + NetworkName, + RPC_GOERLI_NODES, + RPC_MAINNET_NODES, StarknetChainId, } from '../constants'; import { @@ -40,6 +43,16 @@ import { ProviderInterface } from './interface'; import { getAddressFromStarkName, getStarkName } from './starknetId'; import { Block } from './utils'; +const getDefaultNodeUrl = (networkName?: NetworkName): string => { + // eslint-disable-next-line no-console + console.warn('Using default public node url, please provide nodeUrl in provider options!'); + const randIdx = Math.floor(Math.random() * 3); + if (networkName && NetworkName.SN_MAIN === networkName) { + return RPC_MAINNET_NODES[randIdx]; + } + return RPC_GOERLI_NODES[randIdx]; +}; + const defaultOptions = { headers: { 'Content-Type': 'application/json' }, blockIdentifier: BlockTag.pending, @@ -59,9 +72,18 @@ export class RpcProvider implements ProviderInterface { private chainId?: StarknetChainId; - constructor(optionsOrProvider: RpcProviderOptions) { - const { nodeUrl, retries, headers, blockIdentifier, chainId } = optionsOrProvider; - this.nodeUrl = nodeUrl; + constructor(optionsOrProvider?: RpcProviderOptions) { + const { nodeUrl, retries, headers, blockIdentifier, chainId } = optionsOrProvider || {}; + if ((Object).values(NetworkName).includes(nodeUrl)) { + // Network name provided for nodeUrl + this.nodeUrl = getDefaultNodeUrl(nodeUrl as NetworkName); + } else if (nodeUrl) { + // NodeUrl provided + this.nodeUrl = nodeUrl; + } else { + // non provided fallback to default testnet + this.nodeUrl = getDefaultNodeUrl(); + } this.retries = retries || defaultOptions.retries; this.headers = { ...defaultOptions.headers, ...headers }; this.blockIdentifier = blockIdentifier || defaultOptions.blockIdentifier; diff --git a/src/types/provider/configuration.ts b/src/types/provider/configuration.ts index 743e66449..384a340a4 100644 --- a/src/types/provider/configuration.ts +++ b/src/types/provider/configuration.ts @@ -7,7 +7,7 @@ export interface ProviderOptions { } export type RpcProviderOptions = { - nodeUrl: string; + nodeUrl: string | NetworkName; retries?: number; headers?: object; blockIdentifier?: BlockIdentifier;