@@ -18,16 +18,16 @@ import { ArgumentParser } from 'argparse';
1818
1919import { HOST , PORT } from '../helpers/consts' ;
2020import { IRequest , request } from '../helpers/request' ;
21- import { kusama , polkadot , statemint , westend } from './endpoints' ;
22- import { IConfig } from './types/endpoints' ;
21+ import * as endpoints from './endpoints' ;
2322
2423enum StatusCode {
2524 Success = 0 ,
2625 Failed = 1 ,
2726}
2827
2928interface ILatestE2eParser {
30- chain : string ;
29+ url : string ;
30+ chain : keyof typeof endpoints ;
3131}
3232
3333// This is a shallow mock of the actual response from `/blocks/head`. We only need the number field.
@@ -38,28 +38,15 @@ interface IBlockResponse {
3838const main = async ( args : ILatestE2eParser ) : Promise < StatusCode > => {
3939 const { Success, Failed } = StatusCode ;
4040
41- let config : IConfig ;
42- switch ( args . chain ) {
43- case 'polkadot' :
44- config = polkadot ;
45- break ;
46- case 'kusama' :
47- config = kusama ;
48- break ;
49- case 'westend' :
50- config = westend ;
51- break ;
52- case 'statemint' :
53- config = statemint ;
54- break ;
55- default :
56- config = polkadot ;
57- break ;
58- }
41+ const config = endpoints [ args . chain ] ?? endpoints . polkadot ;
42+
43+ const url = new URL ( args . url ) ;
44+ const host = url . hostname ;
45+ const port = Number ( url . port ) ;
5946
6047 let blockId : string ;
6148 try {
62- const res = await request ( '/blocks/head' , HOST , PORT ) ;
49+ const res = await request ( '/blocks/head' , host , port ) ;
6350 blockId = ( JSON . parse ( res . data ) as IBlockResponse ) . number ;
6451 } catch ( err ) {
6552 throw `Error fetching the latest block: ${ err as string } ` ;
@@ -86,41 +73,51 @@ const main = async (args: ILatestE2eParser): Promise<StatusCode> => {
8673 }
8774 }
8875
89- const responses = await Promise . all ( urls . map ( ( u ) => request ( u , HOST , PORT ) ) ) ;
76+ const responses = await Promise . all ( urls . map ( ( u ) => request ( u , host , port ) ) ) ;
9077 const errors : IRequest [ ] = [ ] ;
9178 responses . forEach ( ( res ) => {
9279 if ( res . statusCode && res . statusCode >= 400 ) {
9380 errors . push ( res ) ;
9481 }
9582 } ) ;
96- logErrors ( errors ) ;
83+ logResults ( errors ) ;
9784
9885 if ( errors . length > 0 ) {
9986 console . log ( `Finished with a status code of ${ Failed } ` ) ;
100- return Failed ;
87+ process . exit ( Failed ) ;
10188 } else {
10289 console . log ( `Finished with a status code of ${ Success } ` ) ;
103- return Success ;
90+ process . exit ( Success ) ;
10491 }
10592} ;
10693
107- const logErrors = ( errors : IRequest [ ] ) => {
108- console . log ( 'Received the following errors:' ) ;
109- errors . forEach ( ( err ) => {
110- console . log ( '----------------------------------------------' ) ;
111- console . log ( `Queried Endpoint: ${ err . path } ` ) ;
112- console . log ( `Status Code: ${ err . statusCode as number } ` ) ;
113- console . log ( `Received logging: ${ err . data } ` ) ;
114- } ) ;
94+ const logResults = ( errors : IRequest [ ] ) => {
95+ if ( errors . length > 0 ) {
96+ console . log ( 'Received the following errors:' ) ;
97+ errors . forEach ( ( err ) => {
98+ console . log ( '----------------------------------------------' ) ;
99+ console . log ( `Queried Endpoint: ${ err . path } ` ) ;
100+ console . log ( `Status Code: ${ err . statusCode as number } ` ) ;
101+ console . log ( `Received logging: ${ err . data } ` ) ;
102+ } ) ;
103+ } else {
104+ console . log ( 'No errors were received' ) ;
105+ }
115106} ;
116107
117108const parser = new ArgumentParser ( ) ;
118109
119110parser . add_argument ( '--chain' , {
120- choices : [ 'polkadot' , 'statemint' , 'westend' , 'kusama' ] ,
111+ choices : Object . keys ( endpoints ) ,
121112 default : 'polkadot' ,
122113} ) ;
114+ parser . add_argument ( '--url' , {
115+ default : `http://${ HOST } :${ PORT } ` ,
116+ } ) ;
123117
124118const args = parser . parse_args ( ) as ILatestE2eParser ;
125119
126- main ( args ) . finally ( ( ) => process . exit ( ) ) ;
120+ main ( args ) . catch ( ( e ) => {
121+ console . error ( 'Error' , e ) ;
122+ process . exit ( 1 ) ;
123+ } ) ;
0 commit comments