@@ -25,22 +25,23 @@ module.exports = (nodeManager) => {
2525 let transaction ;
2626
2727 try {
28- if ( ! validator . validatePassPhrase ( passPhrase ) )
28+ if ( ! validator . validatePassPhrase ( passPhrase ) ) {
2929 return validator . badParameter ( 'passPhrase' ) ;
30+ }
3031
3132 const keyPair = keys . createKeypairFromPassPhrase ( passPhrase ) ;
3233
3334 const uniqueVotes = [ ] ;
3435
35- for ( let i = votes . length - 1 ; i >= 0 ; i -- ) {
36- const vote = votes [ i ] ;
36+ votes . forEach ( ( vote , i ) => {
3737 const voteName = vote . slice ( 1 ) ;
3838 const voteDirection = vote . charAt ( 0 ) ;
3939
4040 const cachedPublicKey = publicKeysCache [ voteName ] ;
4141
4242 if ( cachedPublicKey ) {
4343 votes [ i ] = `${ voteDirection } ${ cachedPublicKey } ` ;
44+
4445 continue ;
4546 }
4647
@@ -49,34 +50,38 @@ module.exports = (nodeManager) => {
4950
5051 if ( res . success ) {
5152 const publicKey = res . data . account . publicKey ;
53+
5254 votes [ i ] = `${ voteDirection } ${ publicKey } ` ;
5355 publicKeysCache [ voteName ] = publicKey ;
5456 } else {
5557 logger . warn ( `[ADAMANT js-api] Failed to get public key for ${ vote } . ${ res . errorMessage } .` ) ;
56- return validator . badParameter ( 'votes' )
58+
59+ return validator . badParameter ( 'votes' ) ;
5760 }
5861 } else if ( validator . validateAdmVoteForDelegateName ( vote ) ) {
5962 const res = await get ( nodeManager ) ( '/delegates/get' , { username : voteName } ) ;
6063
6164 if ( res . success ) {
6265 const publicKey = res . data . delegate . publicKey ;
66+
6367 votes [ i ] = `${ voteDirection } ${ publicKey } ` ;
6468 publicKeysCache [ voteName ] = publicKey ;
6569 } else {
6670 logger . warn ( `[ADAMANT js-api] Failed to get public key for ${ vote } . ${ res . errorMessage } .` ) ;
67- return validator . badParameter ( 'votes' )
71+
72+ return validator . badParameter ( 'votes' ) ;
6873 }
6974 } else if ( ! validator . validateAdmVoteForPublicKey ( vote ) ) {
70- return validator . badParameter ( 'votes' )
75+ return validator . badParameter ( 'votes' ) ;
7176 }
7277
7378 // Exclude duplicates
74- const foundCopy = uniqueVotes . findIndex ( ( v ) => v . slice ( 1 ) === votes [ i ] . slice ( 1 ) ) ;
79+ const foundCopy = uniqueVotes . find ( ( v ) => v . slice ( 1 ) === votes [ i ] . slice ( 1 ) ) ;
7580
76- if ( foundCopy === - 1 ) {
81+ if ( ! foundCopy ) {
7782 uniqueVotes . push ( votes [ i ] ) ;
7883 }
79- }
84+ } ) ;
8085
8186 const type = constants . transactionTypes . VOTE ;
8287
@@ -87,30 +92,31 @@ module.exports = (nodeManager) => {
8792 } ;
8893
8994 transaction = transactionFormer . createTransaction ( type , data ) ;
95+ } catch ( error ) {
96+ return validator . badParameter ( '#exception_catched#' , error )
97+ }
9098
91- } catch ( e ) {
99+ const url = nodeManager . node ( ) + '/api/accounts/delegates' ;
92100
93- return validator . badParameter ( '#exception_catched#' , e )
101+ try {
102+ const response = await axios . post ( url , transaction ) ;
94103
95- }
104+ return validator . formatRequestResults ( response , true ) ;
105+ } catch ( error ) {
106+ const logMessage = `[ADAMANT js-api] Vote for delegate request: Request to ${ url } failed with ${ error . response ? error . response . status : undefined } status code, ${ error . toString ( ) } ${ error . response && error . response . data ? '. Message: ' + error . response . data . toString ( ) . trim ( ) : '' } . Try ${ retryNo + 1 } of ${ maxRetries + 1 } .` ;
96107
97- let url = nodeManager . node ( ) + '/api/accounts/delegates' ;
98- return axios . post ( url , transaction )
99- . then ( function ( response ) {
100- return validator . formatRequestResults ( response , true )
101- } )
102- . catch ( function ( error ) {
103- let logMessage = `[ADAMANT js-api] Vote for delegate request: Request to ${ url } failed with ${ error . response ? error . response . status : undefined } status code, ${ error . toString ( ) } ${ error . response && error . response . data ? '. Message: ' + error . response . data . toString ( ) . trim ( ) : '' } . Try ${ retryNo + 1 } of ${ maxRetries + 1 } .` ;
104- if ( retryNo < maxRetries ) {
105- logger . log ( `${ logMessage } Retrying…` ) ;
106- return nodeManager . changeNodes ( )
107- . then ( function ( ) {
108- return module . exports ( nodeManager ) ( passPhrase , addressOrPublicKey , amount , isAmountInADM , maxRetries , ++ retryNo )
109- } )
110- }
111- logger . warn ( `${ logMessage } No more attempts, returning error.` ) ;
112- return validator . formatRequestResults ( error , false )
113- } )
108+ if ( retryNo < maxRetries ) {
109+ logger . log ( `${ logMessage } Retrying…` ) ;
114110
111+ return nodeManager . changeNodes ( )
112+ . then ( ( ) => (
113+ module . exports ( nodeManager ) ( passPhrase , addressOrPublicKey , amount , isAmountInADM , maxRetries , ++ retryNo )
114+ ) ) ;
115+ }
116+
117+ logger . warn ( `${ logMessage } No more attempts, returning error.` ) ;
118+
119+ return validator . formatRequestResults ( error , false ) ;
120+ }
115121 }
116122} ;
0 commit comments