Skip to content

Commit

Permalink
feat: rpc default provider and public nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Nov 6, 2023
1 parent 23c4393 commit 12402e3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
12 changes: 12 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/',
];
12 changes: 2 additions & 10 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
Nonce,
ProviderOptions,
RpcProviderOptions,
SequencerProviderOptions,
SimulateTransactionResponse,
StateUpdateResponse,
Storage,
Expand All @@ -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 {
Expand All @@ -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 = <ProviderInterface>providerOrOptions;
} else if (providerOrOptions && 'rpc' in providerOrOptions) {
// providerOrOptions is rpc option
this.provider = new RpcProvider(<RpcProviderOptions>providerOrOptions.rpc);
} else if (providerOrOptions && 'sequencer' in providerOrOptions) {
// providerOrOptions is sequencer option
this.provider = new SequencerProvider(<SequencerProviderOptions>providerOrOptions.sequencer);
} else {
// providerOrOptions is none, create SequencerProvider as default
this.provider = new SequencerProvider();
this.provider = new RpcProvider();
}
}

Expand Down
28 changes: 25 additions & 3 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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 ((<any>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;
Expand Down
2 changes: 1 addition & 1 deletion src/types/provider/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface ProviderOptions {
}

export type RpcProviderOptions = {
nodeUrl: string;
nodeUrl: string | NetworkName;
retries?: number;
headers?: object;
blockIdentifier?: BlockIdentifier;
Expand Down

0 comments on commit 12402e3

Please sign in to comment.