|
| 1 | +const axios = require('axios'); |
| 2 | +const get = require('./get'); |
| 3 | +const logger = require('../helpers/logger'); |
| 4 | +const keys = require('../helpers/keys'); |
| 5 | +const constants = require('../helpers/constants'); |
| 6 | +const transactionFormer = require('../helpers/transactionFormer'); |
| 7 | +const validator = require('../helpers/validator'); |
| 8 | + |
| 9 | +const DEFAULT_VOTE_FOR_DELEGATE_RETRIES = 4; // How much re-tries for send tokens requests by default. Total 4+1 tries |
| 10 | + |
| 11 | +const publicKeysCache = { }; |
| 12 | + |
| 13 | +module.exports = (nodeManager) => { |
| 14 | + /** |
| 15 | + * Creates votes for delegate transaction, signs it, and broadcasts to ADAMANT network |
| 16 | + * See https://github.com/Adamant-im/adamant/wiki/Transaction-Types#type-3-vote-for-delegate-transaction |
| 17 | + * @param {string} passPhrase Senders's passPhrase. Sender's address will be derived from it. |
| 18 | + * @param {string[]} votes PublicKeys, ADM addresses and delegate names for upvote and downvote. |
| 19 | + * It would be more efficient to pass publicKey, otherwise the api will make additional queries |
| 20 | + * @param {number} maxRetries How much times to retry request |
| 21 | + * @returns {Promise} Request results |
| 22 | + */ |
| 23 | + return async (passPhrase, votes, maxRetries = DEFAULT_VOTE_FOR_DELEGATE_RETRIES, retryNo = 0) => { |
| 24 | + |
| 25 | + let transaction; |
| 26 | + |
| 27 | + try { |
| 28 | + if (!validator.validatePassPhrase(passPhrase)) { |
| 29 | + return validator.badParameter('passPhrase'); |
| 30 | + } |
| 31 | + |
| 32 | + const keyPair = keys.createKeypairFromPassPhrase(passPhrase); |
| 33 | + |
| 34 | + const uniqueVotes = []; |
| 35 | + |
| 36 | + for (let i = votes.length - 1; i >= 0; i--) { |
| 37 | + const vote = votes[i]; |
| 38 | + const voteName = vote.slice(1); |
| 39 | + const voteDirection = vote.charAt(0); |
| 40 | + |
| 41 | + const cachedPublicKey = publicKeysCache[voteName]; |
| 42 | + |
| 43 | + if (cachedPublicKey) { |
| 44 | + votes[i] = `${voteDirection}${cachedPublicKey}`; |
| 45 | + } else { |
| 46 | + if (validator.validateAdmVoteForAddress(vote)) { |
| 47 | + const res = await get(nodeManager)('/accounts', { address: voteName }); |
| 48 | + |
| 49 | + if (res.success) { |
| 50 | + const publicKey = res.data.account.publicKey; |
| 51 | + |
| 52 | + votes[i] = `${voteDirection}${publicKey}`; |
| 53 | + publicKeysCache[voteName] = publicKey; |
| 54 | + } else { |
| 55 | + logger.warn(`[ADAMANT js-api] Failed to get public key for ${vote}. ${res.errorMessage}.`); |
| 56 | + |
| 57 | + return validator.badParameter('votes'); |
| 58 | + } |
| 59 | + } else if (validator.validateAdmVoteForDelegateName(vote)) { |
| 60 | + const res = await get(nodeManager)('/delegates/get', { username: voteName }); |
| 61 | + |
| 62 | + if (res.success) { |
| 63 | + const publicKey = res.data.delegate.publicKey; |
| 64 | + |
| 65 | + votes[i] = `${voteDirection}${publicKey}`; |
| 66 | + publicKeysCache[voteName] = publicKey; |
| 67 | + } else { |
| 68 | + logger.warn(`[ADAMANT js-api] Failed to get public key for ${vote}. ${res.errorMessage}.`); |
| 69 | + |
| 70 | + return validator.badParameter('votes'); |
| 71 | + } |
| 72 | + } else if (!validator.validateAdmVoteForPublicKey(vote)) { |
| 73 | + return validator.badParameter('votes'); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // Exclude duplicates |
| 78 | + const foundCopy = uniqueVotes.find((v) => v.slice(1) === votes[i].slice(1)); |
| 79 | + |
| 80 | + if (!foundCopy) { |
| 81 | + uniqueVotes.push(votes[i]); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + const type = constants.transactionTypes.VOTE; |
| 86 | + |
| 87 | + const data = { |
| 88 | + type, |
| 89 | + keyPair, |
| 90 | + votes: uniqueVotes, |
| 91 | + }; |
| 92 | + |
| 93 | + transaction = transactionFormer.createTransaction(type, data); |
| 94 | + } catch (error) { |
| 95 | + return validator.badParameter('#exception_catched#', error) |
| 96 | + } |
| 97 | + |
| 98 | + const url = nodeManager.node() + '/api/accounts/delegates'; |
| 99 | + |
| 100 | + try { |
| 101 | + const response = await axios.post(url, transaction); |
| 102 | + |
| 103 | + return validator.formatRequestResults(response, true); |
| 104 | + } catch(error) { |
| 105 | + 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}.`; |
| 106 | + |
| 107 | + if (retryNo < maxRetries) { |
| 108 | + logger.log(`${logMessage} Retrying…`); |
| 109 | + |
| 110 | + return nodeManager.changeNodes() |
| 111 | + .then(() => ( |
| 112 | + module.exports(nodeManager)(passPhrase, addressOrPublicKey, amount, isAmountInADM, maxRetries, ++retryNo) |
| 113 | + )); |
| 114 | + } |
| 115 | + |
| 116 | + logger.warn(`${logMessage} No more attempts, returning error.`); |
| 117 | + |
| 118 | + return validator.formatRequestResults(error, false); |
| 119 | + } |
| 120 | + } |
| 121 | +}; |
0 commit comments