@@ -19,10 +19,16 @@ import capitalize from "lodash-es/capitalize.js";
1919
2020import type { ChainENV } from "../../common.js" ;
2121import { commandObj } from "../commandObj.js" ;
22- import { initEnvConfig } from "../configs/project/env/env.js" ;
22+ import {
23+ CHAIN_RPC_CONTAINER_NAME ,
24+ CHAIN_RPC_PORT ,
25+ } from "../configs/project/chainContainers.js" ;
26+ import { initEnvConfig , initNewEnvConfig } from "../configs/project/env/env.js" ;
2327import { dbg } from "../dbg.js" ;
2428import { ensureChainEnv } from "../ensureChainNetwork.js" ;
2529import { numToStr } from "../helpers/typesafeStringify.js" ;
30+ import { urlValidator } from "../helpers/validations.js" ;
31+ import { password } from "../prompt.js" ;
2632
2733let chainIdPromise : Promise < number > | undefined ;
2834
@@ -34,12 +40,14 @@ export async function getChainId() {
3440 let chainId : number = CHAIN_IDS [ chainEnv ] ;
3541 const envConfig = await initEnvConfig ( ) ;
3642
37- if ( envConfig ?. chainId !== undefined ) {
43+ if ( envConfig ?. perEnvConfig ?. [ chainEnv ] ?. chainId !== undefined ) {
44+ const customChainId = envConfig . perEnvConfig [ chainEnv ] . chainId ;
45+
3846 commandObj . logToStderr (
39- `Using custom chain ID: ${ numToStr ( envConfig . chainId ) } from ${ envConfig . $getPath ( ) } ` ,
47+ `Using custom chain ID: ${ numToStr ( customChainId ) } from ${ envConfig . $getPath ( ) } ` ,
4048 ) ;
4149
42- chainId = envConfig . chainId ;
50+ chainId = customChainId ;
4351 }
4452
4553 dbg ( `chainId: ${ numToStr ( chainId ) } ` ) ;
@@ -56,16 +64,28 @@ export async function getRpcUrl() {
5664 if ( rpcUrlPromise === undefined ) {
5765 rpcUrlPromise = ( async ( ) => {
5866 const chainEnv = await ensureChainEnv ( ) ;
59- const { RPC_URLS } = await import ( "@fluencelabs/deal-ts-clients" ) ;
60- let rpcUrl : string = RPC_URLS [ chainEnv ] ;
61- const envConfig = await initEnvConfig ( ) ;
62-
63- if ( envConfig ?. rpcUrl !== undefined ) {
64- commandObj . logToStderr (
65- `Using custom RPC URL: ${ envConfig . rpcUrl } from ${ envConfig . $getPath ( ) } ` ,
66- ) ;
67-
68- rpcUrl = envConfig . rpcUrl ;
67+ let rpcUrl : string | undefined ;
68+ let envConfig = await initEnvConfig ( ) ;
69+
70+ if ( envConfig ?. perEnvConfig ?. [ chainEnv ] ?. rpcHttpUrl !== undefined ) {
71+ const customRpcUrl = envConfig . perEnvConfig [ chainEnv ] . rpcHttpUrl ;
72+ rpcUrl = customRpcUrl ;
73+ } else if ( chainEnv === "local" ) {
74+ rpcUrl = "http://localhost:8545" ;
75+ } else {
76+ rpcUrl = await password ( {
77+ message : `Enter private HTTP RPC URL to use with ${ chainEnv } env` ,
78+ validate ( rpcUrl : string ) {
79+ return urlValidator ( rpcUrl ) ;
80+ } ,
81+ } ) ;
82+
83+ envConfig = await initNewEnvConfig ( ) ;
84+ const perEnvConfig = envConfig . perEnvConfig ?? { } ;
85+ perEnvConfig [ chainEnv ] = perEnvConfig [ chainEnv ] ?? { } ;
86+ perEnvConfig [ chainEnv ] . rpcHttpUrl = rpcUrl ;
87+ envConfig . perEnvConfig = perEnvConfig ;
88+ await envConfig . $commit ( ) ;
6989 }
7090
7191 dbg ( `rpcUrl: ${ rpcUrl } ` ) ;
@@ -76,6 +96,44 @@ export async function getRpcUrl() {
7696 return rpcUrlPromise ;
7797}
7898
99+ let wsUrlPromise : Promise < string > | undefined ;
100+
101+ export async function getWsUrl ( ) {
102+ if ( wsUrlPromise === undefined ) {
103+ wsUrlPromise = ( async ( ) => {
104+ const chainEnv = await ensureChainEnv ( ) ;
105+ let rpcUrl : string | undefined ;
106+ let envConfig = await initEnvConfig ( ) ;
107+
108+ if ( envConfig ?. perEnvConfig ?. [ chainEnv ] ?. rpcWsUrl !== undefined ) {
109+ const customRpcUrl = envConfig . perEnvConfig [ chainEnv ] . rpcWsUrl ;
110+ rpcUrl = customRpcUrl ;
111+ } else if ( chainEnv === "local" ) {
112+ rpcUrl = `wss://${ CHAIN_RPC_CONTAINER_NAME } :${ CHAIN_RPC_PORT } ` ;
113+ } else {
114+ rpcUrl = await password ( {
115+ message : `Enter private Websocket RPC URL to use with ${ chainEnv } env` ,
116+ validate ( rpcUrl : string ) {
117+ return urlValidator ( rpcUrl ) ;
118+ } ,
119+ } ) ;
120+
121+ envConfig = await initNewEnvConfig ( ) ;
122+ const perEnvConfig = envConfig . perEnvConfig ?? { } ;
123+ perEnvConfig [ chainEnv ] = perEnvConfig [ chainEnv ] ?? { } ;
124+ perEnvConfig [ chainEnv ] . rpcWsUrl = rpcUrl ;
125+ envConfig . perEnvConfig = perEnvConfig ;
126+ await envConfig . $commit ( ) ;
127+ }
128+
129+ dbg ( `wsUrl: ${ rpcUrl } ` ) ;
130+ return rpcUrl ;
131+ } ) ( ) ;
132+ }
133+
134+ return wsUrlPromise ;
135+ }
136+
79137let ipfsGatewayPromise : Promise < string > | undefined ;
80138
81139const IPFS_GATEWAYS : Record < ChainENV , string > = {
@@ -92,12 +150,14 @@ export async function getIpfsGateway() {
92150 let ipfsGateway = IPFS_GATEWAYS [ chainEnv ] ;
93151 const envConfig = await initEnvConfig ( ) ;
94152
95- if ( envConfig ?. ipfsGateway !== undefined ) {
153+ if ( envConfig ?. perEnvConfig ?. [ chainEnv ] ?. ipfsGateway !== undefined ) {
154+ const customIpfsGateway = envConfig . perEnvConfig [ chainEnv ] . ipfsGateway ;
155+
96156 commandObj . logToStderr (
97- `Using custom IPFS Gateway: ${ envConfig . ipfsGateway } from ${ envConfig . $getPath ( ) } ` ,
157+ `Using custom IPFS Gateway: ${ customIpfsGateway } from ${ envConfig . $getPath ( ) } ` ,
98158 ) ;
99159
100- ipfsGateway = envConfig . ipfsGateway ;
160+ ipfsGateway = customIpfsGateway ;
101161 }
102162
103163 dbg ( `ipfsGateway: ${ ipfsGateway } ` ) ;
@@ -136,12 +196,15 @@ export async function getBlockScoutUrl() {
136196 let blockScoutUrl : string = BLOCK_SCOUT_URLS [ chainEnv ] ;
137197 const envConfig = await initEnvConfig ( ) ;
138198
139- if ( envConfig ?. blockScoutUrl !== undefined ) {
199+ if ( envConfig ?. perEnvConfig ?. [ chainEnv ] ?. blockScoutUrl !== undefined ) {
200+ const customBlockScoutUrl =
201+ envConfig . perEnvConfig [ chainEnv ] . blockScoutUrl ;
202+
140203 commandObj . logToStderr (
141- `Using custom BlockScout URL: ${ envConfig . blockScoutUrl } from ${ envConfig . $getPath ( ) } ` ,
204+ `Using custom BlockScout URL: ${ customBlockScoutUrl } from ${ envConfig . $getPath ( ) } ` ,
142205 ) ;
143206
144- blockScoutUrl = envConfig . blockScoutUrl ;
207+ blockScoutUrl = customBlockScoutUrl ;
145208 }
146209
147210 dbg ( `blockScoutUrl: ${ blockScoutUrl } ` ) ;
@@ -170,12 +233,14 @@ export async function getSubgraphUrl() {
170233 let subgraphUrl : string = SUBGRAPH_URLS [ chainEnv ] ;
171234 const envConfig = await initEnvConfig ( ) ;
172235
173- if ( envConfig ?. subgraphUrl !== undefined ) {
236+ if ( envConfig ?. perEnvConfig ?. [ chainEnv ] ?. subgraphUrl !== undefined ) {
237+ const customSubgraphUrl = envConfig . perEnvConfig [ chainEnv ] . subgraphUrl ;
238+
174239 commandObj . logToStderr (
175- `Using custom Subgraph URL: ${ envConfig . subgraphUrl } from ${ envConfig . $getPath ( ) } ` ,
240+ `Using custom Subgraph URL: ${ customSubgraphUrl } from ${ envConfig . $getPath ( ) } ` ,
176241 ) ;
177242
178- subgraphUrl = envConfig . subgraphUrl ;
243+ subgraphUrl = customSubgraphUrl ;
179244 }
180245
181246 dbg ( `subgraphUrl: ${ subgraphUrl } ` ) ;
0 commit comments