Skip to content

Commit 2cbf077

Browse files
committed
refactor: imporove get default nodes
1 parent b59952e commit 2cbf077

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

src/channel/rpc_0_7.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ export class RpcChannel {
8282
waitMode,
8383
} = optionsOrProvider || {};
8484
if (Object.values(NetworkName).includes(nodeUrl as NetworkName)) {
85-
this.nodeUrl = getDefaultNodeUrl(nodeUrl as NetworkName, optionsOrProvider?.default);
85+
this.nodeUrl = getDefaultNodeUrl(nodeUrl as NetworkName, optionsOrProvider?.default, '0.7');
8686
} else if (nodeUrl) {
8787
this.nodeUrl = nodeUrl;
8888
} else {
89-
this.nodeUrl = getDefaultNodeUrl(undefined, optionsOrProvider?.default);
89+
this.nodeUrl = getDefaultNodeUrl(undefined, optionsOrProvider?.default, '0.7');
9090
}
9191
this.baseFetch = baseFetch ?? fetch;
9292
this.blockIdentifier = blockIdentifier ?? defaultOptions.blockIdentifier;

src/channel/rpc_0_8.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ export class RpcChannel {
8989
waitMode,
9090
} = optionsOrProvider || {};
9191
if (Object.values(NetworkName).includes(nodeUrl as NetworkName)) {
92-
this.nodeUrl = getDefaultNodeUrl(nodeUrl as NetworkName, optionsOrProvider?.default);
92+
this.nodeUrl = getDefaultNodeUrl(nodeUrl as NetworkName, optionsOrProvider?.default, '0.8');
9393
} else if (nodeUrl) {
9494
this.nodeUrl = nodeUrl;
9595
} else {
96-
this.nodeUrl = getDefaultNodeUrl(undefined, optionsOrProvider?.default);
96+
this.nodeUrl = getDefaultNodeUrl(undefined, optionsOrProvider?.default, '0.8');
9797
}
9898
this.baseFetch = baseFetch ?? fetch;
9999
this.blockIdentifier = blockIdentifier ?? defaultOptions.blockIdentifier;

src/global/constants.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,14 @@ export const DEFAULT_GLOBAL_CONFIG: {
116116
},
117117
};
118118

119-
const vToUrl = (versionString: SupportedRpcVersion) =>
120-
`v${versionString.replace(/^v/, '').replace(/\./g, '_')}`;
121-
122-
export const RPC_NODES = {
119+
export const RPC_DEFAULT_NODES = {
123120
SN_MAIN: [
124-
`https://starknet-mainnet.public.blastapi.io/rpc/${vToUrl(DEFAULT_GLOBAL_CONFIG.rpcVersion)}`,
125-
`https://free-rpc.nethermind.io/mainnet-juno/${vToUrl(DEFAULT_GLOBAL_CONFIG.rpcVersion)}`,
121+
`https://starknet-mainnet.public.blastapi.io/rpc/`,
122+
`https://free-rpc.nethermind.io/mainnet-juno/`,
126123
],
127124
SN_SEPOLIA: [
128-
`https://starknet-sepolia.public.blastapi.io/rpc/${vToUrl(DEFAULT_GLOBAL_CONFIG.rpcVersion)}`,
129-
`https://free-rpc.nethermind.io/sepolia-juno/${vToUrl(DEFAULT_GLOBAL_CONFIG.rpcVersion)}`,
125+
`https://starknet-sepolia.public.blastapi.io/rpc/`,
126+
`https://free-rpc.nethermind.io/sepolia-juno/`,
130127
],
131128
} as const;
132129

src/utils/provider.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NetworkName, RPC_NODES, SupportedRpcVersion } from '../global/constants';
1+
import { NetworkName, RPC_DEFAULT_NODES, SupportedRpcVersion } from '../global/constants';
22
import {
33
BlockIdentifier,
44
BlockTag,
@@ -24,6 +24,7 @@ import { compressProgram } from './stark';
2424
import type { GetTransactionReceiptResponse } from './transactionReceipt/transactionReceipt.type';
2525
import { logger } from '../global/logger';
2626
import { ETransactionVersion } from '../provider/types/spec.type';
27+
import { config } from '../global/config';
2728

2829
/**
2930
* Helper - Async Sleep for 'delay' time
@@ -120,15 +121,39 @@ export function parseContract(contract: CompiledContract | string): ContractClas
120121
* // result = "https://starknet-mainnet.public.blastapi.io/rpc/v0_7"
121122
* ```
122123
*/
123-
export const getDefaultNodeUrl = (networkName?: NetworkName, mute: boolean = false): string => {
124+
export const getDefaultNodeUrl = (
125+
networkName?: NetworkName,
126+
mute: boolean = false,
127+
rpcVersion?: SupportedRpcVersion
128+
): string => {
124129
if (!mute) {
125130
logger.info('Using default public node url, please provide nodeUrl in provider options!');
126131
}
127-
const nodes = RPC_NODES[networkName ?? NetworkName.SN_SEPOLIA];
132+
const rpcNodes = getDefaultNodes(rpcVersion ?? config.get('rpcVersion'));
133+
134+
const nodes = rpcNodes[networkName ?? NetworkName.SN_SEPOLIA];
128135
const randIdx = Math.floor(Math.random() * nodes.length);
129136
return nodes[randIdx];
130137
};
131138

139+
/**
140+
* return Defaults RPC Nodes endpoints
141+
*/
142+
export function getDefaultNodes(rpcVersion: SupportedRpcVersion) {
143+
const vToUrl = (versionString: SupportedRpcVersion) =>
144+
`v${versionString.replace(/^v/, '').replace(/\./g, '_')}`;
145+
146+
const nodes: any = { ...RPC_DEFAULT_NODES };
147+
148+
Object.keys(nodes).forEach(function (key, _) {
149+
nodes[key] = nodes[key].map((it: any) => {
150+
return `${it}${vToUrl(rpcVersion)}`;
151+
});
152+
});
153+
154+
return nodes;
155+
}
156+
132157
export const validBlockTags = Object.values(BlockTag);
133158

134159
/**

0 commit comments

Comments
 (0)