@@ -23,6 +23,7 @@ import {
2323 getChainDecimals ,
2424 getChainNativeCurrencyName ,
2525 getChainSymbol ,
26+ getRpcUrlForChain ,
2627} from "./utils.js" ;
2728
2829const legacyChain : LegacyChain = {
@@ -306,4 +307,75 @@ describe("defineChain", () => {
306307 testnet : undefined ,
307308 } ) ;
308309 } ) ;
310+
311+ describe ( "getRpcUrlForChain" , ( ) => {
312+ it ( "should construct RPC URL using chain ID and client ID when chain is a number" , ( ) => {
313+ const options = {
314+ client : { ...TEST_CLIENT , clientId : "test-client-id" } ,
315+ chain : 1 ,
316+ } ;
317+ const result = getRpcUrlForChain ( options ) ;
318+ expect ( result ) . toBe ( "https://1.rpc.thirdweb.com/test-client-id" ) ;
319+ } ) ;
320+
321+ it ( "should return the custom RPC URL if provided" , ( ) => {
322+ const options = {
323+ client : { ...TEST_CLIENT , clientId : "test-client-id" } ,
324+ chain : {
325+ id : 1 ,
326+ rpc : "https://custom-rpc.com" ,
327+ } ,
328+ } ;
329+ const result = getRpcUrlForChain ( options ) ;
330+ expect ( result ) . toBe ( "https://custom-rpc.com" ) ;
331+ } ) ;
332+
333+ it ( "should add client ID to thirdweb RPC URL" , ( ) => {
334+ const options = {
335+ client : { ...TEST_CLIENT , clientId : "test-client-id" } ,
336+ chain : {
337+ id : 1 ,
338+ rpc : "https://1.rpc.thirdweb.com" ,
339+ } ,
340+ } ;
341+ const result = getRpcUrlForChain ( options ) ;
342+ expect ( result ) . toBe ( "https://1.rpc.thirdweb.com/test-client-id" ) ;
343+ } ) ;
344+
345+ it ( "should honor client ID passed directly in rpc field" , ( ) => {
346+ const options = {
347+ client : { ...TEST_CLIENT , clientId : "test-client-id" } ,
348+ chain : {
349+ id : 1 ,
350+ rpc : "https://1.rpc.thirdweb.com/abc" ,
351+ } ,
352+ } ;
353+ const result = getRpcUrlForChain ( options ) ;
354+ expect ( result ) . toBe ( "https://1.rpc.thirdweb.com/abc" ) ;
355+ } ) ;
356+
357+ it ( "should replace template string in rpc url" , ( ) => {
358+ const options = {
359+ client : { ...TEST_CLIENT , clientId : "test-client-id" } ,
360+ chain : {
361+ id : 1 ,
362+ rpc : "https://1.rpc.thirdweb.com/${THIRDWEB_API_KEY}" ,
363+ } ,
364+ } ;
365+ const result = getRpcUrlForChain ( options ) ;
366+ expect ( result ) . toBe ( "https://1.rpc.thirdweb.com/test-client-id" ) ;
367+ } ) ;
368+
369+ it ( "should return the RPC URL without modification if it's not a thirdweb URL" , ( ) => {
370+ const options = {
371+ client : { ...TEST_CLIENT , clientId : "test-client-id" } ,
372+ chain : {
373+ id : 1 ,
374+ rpc : "https://custom-rpc.com" ,
375+ } ,
376+ } ;
377+ const result = getRpcUrlForChain ( options ) ;
378+ expect ( result ) . toBe ( "https://custom-rpc.com" ) ;
379+ } ) ;
380+ } ) ;
309381} ) ;
0 commit comments