@leofcoin/launch-chain 0.2.6 → 0.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import Chain from '@leofcoin/chain/chain';
2
2
  import WSClient from '@leofcoin/endpoint-clients/ws';
3
3
  import HttpClient from '@leofcoin/endpoint-clients/http';
4
- type launchMode = 'direct' | 'remote';
4
+ type launchMode = 'direct' | 'remote' | 'server';
5
5
  type endpointReturns = {
6
6
  http?: string[];
7
7
  ws?: string[];
package/index.js CHANGED
@@ -72,72 +72,98 @@ const launch = async (options, password) => {
72
72
  options = defaultOptions;
73
73
  else
74
74
  options = { ...defaultOptions, ...options };
75
- const availableEndpoints = {
75
+ const clients = {
76
76
  http: [],
77
77
  ws: []
78
78
  };
79
- const availableClients = {
79
+ const endpoints = {
80
80
  http: [],
81
81
  ws: []
82
82
  };
83
- if (options.http) {
84
- for (const endpoint of options.http) {
85
- if (endpoint.port && !endpoint.url)
86
- endpoint.url = `http://localhost:${endpoint.port}`;
87
- const client = await getHttp(endpoint.url, options.networkVersion);
88
- if (client)
89
- availableEndpoints.http.push(endpoint.url) && availableClients.http.push({ url: endpoint.url, client });
83
+ let chain;
84
+ if (options.mode === 'remote') {
85
+ if (options.http) {
86
+ for (const endpoint of options.http) {
87
+ if (endpoint.port && !endpoint.url)
88
+ endpoint.url = `http://localhost:${endpoint.port}`;
89
+ const client = await getHttp(endpoint.url, options.networkVersion);
90
+ if (client)
91
+ endpoints.http.push(endpoint.url) && clients.http.push({ url: endpoint.url, client });
92
+ }
90
93
  }
91
- }
92
- if (options.ws) {
93
- for (const endpoint of options.ws) {
94
- if (endpoint.port && !endpoint.url)
95
- endpoint.url = `ws://localhost:${endpoint.port}`;
96
- const client = await getWS(endpoint.url, options.networkVersion);
97
- if (client)
98
- availableEndpoints.ws.push(endpoint.url) && availableClients.ws.push({ url: endpoint.url, client });
94
+ if (options.ws) {
95
+ for (const endpoint of options.ws) {
96
+ if (endpoint.port && !endpoint.url)
97
+ endpoint.url = `ws://localhost:${endpoint.port}`;
98
+ const client = await getWS(endpoint.url, options.networkVersion);
99
+ if (client)
100
+ endpoints.ws.push(endpoint.url) && clients.ws.push({ url: endpoint.url, client });
101
+ }
99
102
  }
103
+ if (endpoints.http.length === 0 && endpoints.ws.length === 0)
104
+ throw new Error(`no remotes connected`);
100
105
  }
101
- const endpoints = {
102
- http: [],
103
- ws: []
104
- };
105
- let chain;
106
- let mode;
107
- if (availableEndpoints.http.length > 0 || availableEndpoints.ws.length > 0) {
108
- availableEndpoints.http.forEach(endpoint => {
109
- endpoints.http.push(endpoint);
110
- });
111
- availableEndpoints.ws.forEach(endpoint => {
112
- endpoints.ws.push(endpoint);
113
- });
114
- mode = 'remote';
106
+ else if (options.mode === 'direct') {
107
+ await new Node({ network: options.network, stars: options.stars, networkVersion: options.networkVersion }, password);
108
+ await nodeConfig({ network: options.network, stars: options.stars, networkVersion: options.networkVersion });
109
+ chain = await new Chain();
110
+ if (options.ws) {
111
+ const importee = await import('@leofcoin/endpoints/ws');
112
+ const wsServer = importee.default;
113
+ for (const endpoint of options.ws) {
114
+ if (endpoint.port && !endpoint.url)
115
+ endpoint.url = `ws://localhost:${endpoint.port}`;
116
+ await wsServer(chain, endpoint.port, options.networkVersion);
117
+ endpoints.ws.push(endpoint.url);
118
+ const client = await getWS(endpoint.url, options.networkVersion);
119
+ if (client)
120
+ endpoints.ws.push(endpoint.url) && clients.ws.push({ url: endpoint.url, client });
121
+ }
122
+ }
123
+ if (options.http) {
124
+ const importee = await import('@leofcoin/endpoints/http');
125
+ const httpServer = importee.default;
126
+ for (const endpoint of options.http) {
127
+ if (endpoint.port && !endpoint.url)
128
+ endpoint.url = `http://localhost:${endpoint.port}`;
129
+ await httpServer(chain, endpoint.port, options.networkVersion);
130
+ endpoints.http.push(endpoint.url);
131
+ const client = await getHttp(endpoint.url, options.networkVersion);
132
+ if (client)
133
+ endpoints.http.push(endpoint.url) && clients.http.push({ url: endpoint.url, client });
134
+ }
135
+ }
115
136
  }
116
137
  else {
117
- if (options.forceRemote)
118
- throw new Error(`forceRemote was set but no remotes connected`);
119
138
  await new Node({ network: options.network, stars: options.stars, networkVersion: options.networkVersion }, password);
120
139
  await nodeConfig({ network: options.network, stars: options.stars, networkVersion: options.networkVersion });
121
140
  chain = await new Chain();
122
141
  if (options.ws) {
123
142
  const importee = await import('@leofcoin/endpoints/ws');
124
143
  const wsServer = importee.default;
125
- await wsServer(chain, options.ws[0].port, options.networkVersion);
126
- endpoints.ws.push(options.ws[0].url);
144
+ for (const endpoint of options.ws) {
145
+ if (endpoint.port && !endpoint.url)
146
+ endpoint.url = `ws://localhost:${endpoint.port}`;
147
+ await wsServer(chain, endpoint.port, options.networkVersion);
148
+ endpoints.ws.push(endpoint.url);
149
+ }
127
150
  }
128
151
  if (options.http) {
129
152
  const importee = await import('@leofcoin/endpoints/http');
130
153
  const httpServer = importee.default;
131
- await httpServer(chain, options.http[0].port, options.networkVersion);
132
- endpoints.http.push(options.http[0].url);
154
+ for (const endpoint of options.http) {
155
+ if (endpoint.port && !endpoint.url)
156
+ endpoint.url = `http://localhost:${endpoint.port}`;
157
+ await httpServer(chain, endpoint.port, options.networkVersion);
158
+ endpoints.http.push(endpoint.url);
159
+ }
133
160
  }
134
- mode = 'direct';
135
161
  }
136
162
  return {
137
163
  chain,
138
- mode,
164
+ mode: options.mode,
139
165
  endpoints,
140
- clients: availableClients
166
+ clients
141
167
  };
142
168
  };
143
169
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/launch-chain",
3
- "version": "0.2.6",
3
+ "version": "0.2.8",
4
4
  "description": "",
5
5
  "exports": {
6
6
  ".": "./index.js"
@@ -25,7 +25,7 @@
25
25
  "dependencies": {
26
26
  "@leofcoin/chain": "^1.4.8",
27
27
  "@leofcoin/endpoint-clients": "^0.2.3",
28
- "@leofcoin/endpoints": "^0.2.0"
28
+ "@leofcoin/endpoints": "^0.2.10"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@rollup/plugin-typescript": "^11.0.0",
package/src/index.ts CHANGED
@@ -4,7 +4,7 @@ import nodeConfig from '@leofcoin/lib/node-config'
4
4
  import WSClient from '@leofcoin/endpoint-clients/ws'
5
5
  import HttpClient from '@leofcoin/endpoint-clients/http'
6
6
 
7
- type launchMode = 'direct' | 'remote'
7
+ type launchMode = 'direct' | 'remote' | 'server'
8
8
 
9
9
  type endpointReturns = {
10
10
  http?: string[],
@@ -38,7 +38,7 @@ type launchOptions = {
38
38
  http?: endpointOptions[] | undefined,
39
39
  }
40
40
 
41
- const defaultOptions:launchOptions = {
41
+ const defaultOptions: launchOptions = {
42
42
  network: 'leofcoin:peach',
43
43
  networkVersion: 'peach',
44
44
  stars: ['wss://peach.leofcoin.org'],
@@ -119,53 +119,71 @@ const launch = async (options: launchOptions, password: string): Promise<launchR
119
119
  if (!options) options = defaultOptions
120
120
  else options = {...defaultOptions, ...options }
121
121
 
122
- const availableEndpoints: endpointReturns = {
122
+ const clients: clientReturns = {
123
123
  http: [],
124
124
  ws: []
125
125
  }
126
126
 
127
- const availableClients: clientReturns = {
127
+ const endpoints: endpointReturns = {
128
128
  http: [],
129
129
  ws: []
130
130
  }
131
131
 
132
- if (options.http) {
133
- for (const endpoint of options.http) {
134
- if (endpoint.port && !endpoint.url) endpoint.url = `http://localhost:${endpoint.port}`
135
- const client = await getHttp(endpoint.url, options.networkVersion)
136
- if (client) availableEndpoints.http.push(endpoint.url) && availableClients.http.push({url: endpoint.url, client})
132
+ let chain: Chain
133
+
134
+ if (options.mode === 'remote') {
135
+ if (options.http) {
136
+ for (const endpoint of options.http) {
137
+ if (endpoint.port && !endpoint.url) endpoint.url = `http://localhost:${endpoint.port}`
138
+ const client = await getHttp(endpoint.url, options.networkVersion)
139
+ if (client) endpoints.http.push(endpoint.url) && clients.http.push({url: endpoint.url, client})
140
+ }
137
141
  }
138
- }
142
+
143
+ if (options.ws) {
144
+ for (const endpoint of options.ws) {
145
+ if (endpoint.port && !endpoint.url) endpoint.url = `ws://localhost:${endpoint.port}`
146
+ const client = await getWS(endpoint.url, options.networkVersion)
147
+ if (client) endpoints.ws.push(endpoint.url) && clients.ws.push({url: endpoint.url, client})
148
+ }
149
+ }
150
+ if (endpoints.http.length === 0 && endpoints.ws.length === 0) throw new Error(`no remotes connected`)
151
+ } else if (options.mode === 'direct') {
152
+ await new Node({ network: options.network, stars: options.stars, networkVersion: options.networkVersion }, password)
153
+ await nodeConfig({ network: options.network, stars: options.stars, networkVersion: options.networkVersion })
139
154
 
140
- if (options.ws) {
141
- for (const endpoint of options.ws) {
142
- if (endpoint.port && !endpoint.url) endpoint.url = `ws://localhost:${endpoint.port}`
143
- const client = await getWS(endpoint.url, options.networkVersion)
144
- if (client) availableEndpoints.ws.push(endpoint.url) && availableClients.ws.push({url: endpoint.url, client})
155
+ chain = await new Chain()
156
+
157
+ if (options.ws) {
158
+ const importee = await import('@leofcoin/endpoints/ws')
159
+ const wsServer = importee.default
160
+
161
+ for (const endpoint of options.ws) {
162
+ if (endpoint.port && !endpoint.url) endpoint.url = `ws://localhost:${endpoint.port}`
163
+
164
+ await wsServer(chain, endpoint.port, options.networkVersion)
165
+ endpoints.ws.push(endpoint.url)
166
+
167
+ const client = await getWS(endpoint.url, options.networkVersion)
168
+ if (client) endpoints.ws.push(endpoint.url) && clients.ws.push({url: endpoint.url, client})
169
+ }
145
170
  }
146
- }
147
171
 
148
- const endpoints: endpointReturns = {
149
- http: [],
150
- ws: []
151
- }
172
+ if (options.http) {
173
+ const importee = await import('@leofcoin/endpoints/http')
174
+ const httpServer = importee.default
152
175
 
153
- let chain: Chain
154
- let mode: launchMode
155
-
156
- if (availableEndpoints.http.length > 0 || availableEndpoints.ws.length > 0) {
157
- availableEndpoints.http.forEach(endpoint => {
158
- endpoints.http.push(endpoint)
159
- })
160
-
161
- availableEndpoints.ws.forEach(endpoint => {
162
- endpoints.ws.push(endpoint)
163
- })
164
-
165
- mode = 'remote'
166
- } else {
167
- if (options.forceRemote) throw new Error(`forceRemote was set but no remotes connected`)
168
-
176
+ for (const endpoint of options.http) {
177
+ if (endpoint.port && !endpoint.url) endpoint.url = `http://localhost:${endpoint.port}`
178
+
179
+ await httpServer(chain, endpoint.port, options.networkVersion)
180
+ endpoints.http.push(endpoint.url)
181
+
182
+ const client = await getHttp(endpoint.url, options.networkVersion)
183
+ if (client) endpoints.http.push(endpoint.url) && clients.http.push({url: endpoint.url, client})
184
+ }
185
+ }
186
+ } else {
169
187
  await new Node({ network: options.network, stars: options.stars, networkVersion: options.networkVersion }, password)
170
188
  await nodeConfig({ network: options.network, stars: options.stars, networkVersion: options.networkVersion })
171
189
 
@@ -174,22 +192,31 @@ const launch = async (options: launchOptions, password: string): Promise<launchR
174
192
  if (options.ws) {
175
193
  const importee = await import('@leofcoin/endpoints/ws')
176
194
  const wsServer = importee.default
177
- await wsServer(chain, options.ws[0].port, options.networkVersion)
178
- endpoints.ws.push(options.ws[0].url)
195
+
196
+ for (const endpoint of options.ws) {
197
+ if (endpoint.port && !endpoint.url) endpoint.url = `ws://localhost:${endpoint.port}`
198
+ await wsServer(chain, endpoint.port, options.networkVersion)
199
+ endpoints.ws.push(endpoint.url)
200
+ }
179
201
  }
202
+
180
203
  if (options.http) {
181
204
  const importee = await import('@leofcoin/endpoints/http')
182
205
  const httpServer = importee.default
183
- await httpServer(chain, options.http[0].port, options.networkVersion)
184
- endpoints.http.push(options.http[0].url)
206
+
207
+ for (const endpoint of options.http) {
208
+ if (endpoint.port && !endpoint.url) endpoint.url = `http://localhost:${endpoint.port}`
209
+ await httpServer(chain, endpoint.port, options.networkVersion)
210
+ endpoints.http.push(endpoint.url)
211
+ }
185
212
  }
186
- mode = 'direct'
187
213
  }
214
+
188
215
  return {
189
216
  chain,
190
- mode,
217
+ mode: options.mode,
191
218
  endpoints,
192
- clients: availableClients
219
+ clients
193
220
  }
194
221
  }
195
222
 
package/test.js CHANGED
@@ -3,8 +3,6 @@ import launch from './index.js'
3
3
  const {chain, endpoints, clients, mode } = await launch({
4
4
  network: 'leofcoin:peach',
5
5
  networkVersion: 'peach',
6
- forceRemote: true,
7
- ws: [{ url:'wss://ws-remote.leofcoin.org'}],
8
- http: [{ url: 'https://remote.leofcoin.org' }]
6
+ mode: 'direct'
9
7
  })
10
8
  console.log(await clients.ws[0].client.networkStats());