Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add voteForDelegate()
  • Loading branch information
martiliones committed Feb 9, 2022
1 parent 6d40ec0 commit 4a8f497
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
70 changes: 70 additions & 0 deletions groups/voteForDelegate.js
@@ -0,0 +1,70 @@
const axios = require('axios');
const logger = require('../helpers/logger');
const keys = require('../helpers/keys');
const constants = require('../helpers/constants');
const transactionFormer = require('../helpers/transactionFormer');
const validator = require('../helpers/validator');

const DEFAULT_SEND_TOKENS_RETRIES = 4; // How much re-tries for send tokens requests by default. Total 4+1 tries

module.exports = (nodeManager) => {
/**
* Creates Token Transfer transaction, signs it, and broadcasts to ADAMANT network
* See https://github.com/Adamant-im/adamant/wiki/Transaction-Types#type-0-token-transfer-transaction
* @param {string} passPhrase Senders's passPhrase. Sender's address will be derived from it.
* @param {string} addressOrPublicKey Recipient's ADAMANT address or public key.
* Address is preferred, as if we get public key, we should derive address from it.
* @param {string, number} amount Amount to send
* @param {boolean} isAmountInADM If amount specified in ADM, or in sats (10^-8 ADM)
* @param {number} maxRetries How much times to retry request
* @returns {Promise} Request results
*/
return (passPhrase, votes, maxRetries = DEFAULT_SEND_TOKENS_RETRIES, retryNo = 0) => {

let transaction;

try {
if (!validator.validatePassPhrase(passPhrase))
return validator.badParameter('passPhrase')

const keyPair = keys.createKeypairFromPassPhrase(passPhrase);

if (!validator.validateAdmVotes(votes))
return validator.badParameter('votes')

const type = constants.transactionTypes.VOTE;

const data = {
type,
keyPair,
votes,
};

transaction = transactionFormer.createTransaction(type, data);

} catch (e) {

return validator.badParameter('#exception_catched#', e)

}

let url = nodeManager.node() + '/api/accounts/delegates';
return axios.post(url, transaction)
.then(function (response) {
return validator.formatRequestResults(response, true)
})
.catch(function (error) {
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}.`;
if (retryNo < maxRetries) {
logger.log(`${logMessage} Retrying…`);
return nodeManager.changeNodes()
.then(function () {
return module.exports(nodeManager)(passPhrase, addressOrPublicKey, amount, isAmountInADM, maxRetries, ++retryNo)
})
}
logger.warn(`${logMessage} No more attempts, returning error.`);
return validator.formatRequestResults(error, false)
})

}
};
3 changes: 2 additions & 1 deletion helpers/constants.js
Expand Up @@ -31,9 +31,10 @@ module.exports = {
RE_HEX: /^[a-fA-F0-9]+$/,
RE_BASE64: /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/,
RE_ADM_ADDRESS: /^U([0-9]{6,})$/,
RE_ADM_VOTE: /^(\+|-)[a-fA-F0-9]{64}$/,
RE_BTC_ADDRESS: /^(bc1|[13])[a-km-zA-HJ-NP-Z02-9]{25,39}$/,
RE_DASH_ADDRESS: /^[7X][1-9A-HJ-NP-Za-km-z]{33,}$/,
RE_DOGE_ADDRESS: /^[A|D|9][A-Z0-9]([0-9a-zA-Z]{9,})$/,
RE_LSK_ADDRESS: /^[0-9]{2,21}L$/

}
11 changes: 11 additions & 0 deletions helpers/validator.js
Expand Up @@ -52,6 +52,17 @@ module.exports = {
return true
},

validateAdmVotes(publicKeys) {
for (let i = publicKeys.length - 1; i >= 0; i--) {
const publicKey = publicKeys[i];

if (!publicKey || typeof(publicKey) !== 'string' || !constants.RE_ADM_VOTE.test(publicKey))
return false
}

return true
},

validateIntegerAmount(amount) {
if (!amount || typeof(amount) !== 'number' || isNaN(amount) || !Number.isSafeInteger(amount))
return false
Expand Down
2 changes: 2 additions & 0 deletions index.js
Expand Up @@ -3,6 +3,7 @@ const get = require('./groups/get');
const getPublicKey = require('./groups/getPublicKey');
const decodeMsg = require('./groups/decodeMsg');
const delegateNew = require('./groups/delegateNew');
const voteForDelegate = require('./groups/voteForDelegate');
const sendTokens = require('./groups/sendTokens');
const sendMessage = require('./groups/sendMessage');
const healthCheck = require('./helpers/healthCheck');
Expand All @@ -27,6 +28,7 @@ module.exports = (params, log) => {
sendTokens: sendTokens(nodeManager),
sendMessage: sendMessage(nodeManager),
delegateNew: delegateNew(nodeManager),
voteForDelegate: voteForDelegate(nodeManager),
decodeMsg,
eth,
dash,
Expand Down

0 comments on commit 4a8f497

Please sign in to comment.