@@ -19,10 +19,16 @@ import capitalize from "lodash-es/capitalize.js";
19
19
20
20
import type { ChainENV } from "../../common.js" ;
21
21
import { 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" ;
23
27
import { dbg } from "../dbg.js" ;
24
28
import { ensureChainEnv } from "../ensureChainNetwork.js" ;
25
29
import { numToStr } from "../helpers/typesafeStringify.js" ;
30
+ import { urlValidator } from "../helpers/validations.js" ;
31
+ import { password } from "../prompt.js" ;
26
32
27
33
let chainIdPromise : Promise < number > | undefined ;
28
34
@@ -34,12 +40,14 @@ export async function getChainId() {
34
40
let chainId : number = CHAIN_IDS [ chainEnv ] ;
35
41
const envConfig = await initEnvConfig ( ) ;
36
42
37
- if ( envConfig ?. chainId !== undefined ) {
43
+ if ( envConfig ?. perEnvConfig ?. [ chainEnv ] ?. chainId !== undefined ) {
44
+ const customChainId = envConfig . perEnvConfig [ chainEnv ] . chainId ;
45
+
38
46
commandObj . logToStderr (
39
- `Using custom chain ID: ${ numToStr ( envConfig . chainId ) } from ${ envConfig . $getPath ( ) } ` ,
47
+ `Using custom chain ID: ${ numToStr ( customChainId ) } from ${ envConfig . $getPath ( ) } ` ,
40
48
) ;
41
49
42
- chainId = envConfig . chainId ;
50
+ chainId = customChainId ;
43
51
}
44
52
45
53
dbg ( `chainId: ${ numToStr ( chainId ) } ` ) ;
@@ -56,16 +64,28 @@ export async function getRpcUrl() {
56
64
if ( rpcUrlPromise === undefined ) {
57
65
rpcUrlPromise = ( async ( ) => {
58
66
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 ( ) ;
69
89
}
70
90
71
91
dbg ( `rpcUrl: ${ rpcUrl } ` ) ;
@@ -76,6 +96,44 @@ export async function getRpcUrl() {
76
96
return rpcUrlPromise ;
77
97
}
78
98
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
+
79
137
let ipfsGatewayPromise : Promise < string > | undefined ;
80
138
81
139
const IPFS_GATEWAYS : Record < ChainENV , string > = {
@@ -92,12 +150,14 @@ export async function getIpfsGateway() {
92
150
let ipfsGateway = IPFS_GATEWAYS [ chainEnv ] ;
93
151
const envConfig = await initEnvConfig ( ) ;
94
152
95
- if ( envConfig ?. ipfsGateway !== undefined ) {
153
+ if ( envConfig ?. perEnvConfig ?. [ chainEnv ] ?. ipfsGateway !== undefined ) {
154
+ const customIpfsGateway = envConfig . perEnvConfig [ chainEnv ] . ipfsGateway ;
155
+
96
156
commandObj . logToStderr (
97
- `Using custom IPFS Gateway: ${ envConfig . ipfsGateway } from ${ envConfig . $getPath ( ) } ` ,
157
+ `Using custom IPFS Gateway: ${ customIpfsGateway } from ${ envConfig . $getPath ( ) } ` ,
98
158
) ;
99
159
100
- ipfsGateway = envConfig . ipfsGateway ;
160
+ ipfsGateway = customIpfsGateway ;
101
161
}
102
162
103
163
dbg ( `ipfsGateway: ${ ipfsGateway } ` ) ;
@@ -136,12 +196,15 @@ export async function getBlockScoutUrl() {
136
196
let blockScoutUrl : string = BLOCK_SCOUT_URLS [ chainEnv ] ;
137
197
const envConfig = await initEnvConfig ( ) ;
138
198
139
- if ( envConfig ?. blockScoutUrl !== undefined ) {
199
+ if ( envConfig ?. perEnvConfig ?. [ chainEnv ] ?. blockScoutUrl !== undefined ) {
200
+ const customBlockScoutUrl =
201
+ envConfig . perEnvConfig [ chainEnv ] . blockScoutUrl ;
202
+
140
203
commandObj . logToStderr (
141
- `Using custom BlockScout URL: ${ envConfig . blockScoutUrl } from ${ envConfig . $getPath ( ) } ` ,
204
+ `Using custom BlockScout URL: ${ customBlockScoutUrl } from ${ envConfig . $getPath ( ) } ` ,
142
205
) ;
143
206
144
- blockScoutUrl = envConfig . blockScoutUrl ;
207
+ blockScoutUrl = customBlockScoutUrl ;
145
208
}
146
209
147
210
dbg ( `blockScoutUrl: ${ blockScoutUrl } ` ) ;
@@ -170,12 +233,14 @@ export async function getSubgraphUrl() {
170
233
let subgraphUrl : string = SUBGRAPH_URLS [ chainEnv ] ;
171
234
const envConfig = await initEnvConfig ( ) ;
172
235
173
- if ( envConfig ?. subgraphUrl !== undefined ) {
236
+ if ( envConfig ?. perEnvConfig ?. [ chainEnv ] ?. subgraphUrl !== undefined ) {
237
+ const customSubgraphUrl = envConfig . perEnvConfig [ chainEnv ] . subgraphUrl ;
238
+
174
239
commandObj . logToStderr (
175
- `Using custom Subgraph URL: ${ envConfig . subgraphUrl } from ${ envConfig . $getPath ( ) } ` ,
240
+ `Using custom Subgraph URL: ${ customSubgraphUrl } from ${ envConfig . $getPath ( ) } ` ,
176
241
) ;
177
242
178
- subgraphUrl = envConfig . subgraphUrl ;
243
+ subgraphUrl = customSubgraphUrl ;
179
244
}
180
245
181
246
dbg ( `subgraphUrl: ${ subgraphUrl } ` ) ;
0 commit comments