diff --git a/Assistant.js b/Assistant.js index 461e560c2..416b02d1f 100644 --- a/Assistant.js +++ b/Assistant.js @@ -7,6 +7,8 @@ */ const MODULE_NAME = "Assistant"; + + var nodemailer = require('nodemailer'); let bot = BOT; @@ -26,7 +28,8 @@ getTicker: getTicker, sendMessage: sendMessage, rememberThis: rememberThis, - remindMeOf: remindMeOf + remindMeOf: remindMeOf, + sendEmail: sendEmail }; let utilities = UTILITIES.newCloudUtilities(bot, logger); @@ -1103,9 +1106,12 @@ if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] movePosition -> pPosition = " + JSON.stringify(pPosition)); } if (global.LOG_CONTROL[MODULE_NAME].logInfo === true) { logger.write(MODULE_NAME, "[INFO] movePosition -> pNewRate = " + pNewRate); } + /* Removing extra decimals. */ + pNewRate = Number(parseFloat(pNewRate).toFixed(8)); + let newAmountB; if (pPosition.type === "buy") { - newAmountB = parseFloat(pPosition.amountA / pNewRate).toFixed(7); // If it was fixed to 8 positions there could be some rounding up and there will be not enough AssetA for buying + newAmountB = Number(parseFloat(pPosition.amountA / pNewRate).toFixed(8)); } else { newAmountB = pPosition.amountB; } @@ -1332,6 +1338,10 @@ context.newHistoryRecord.messageRelevance = pRelevance; context.newHistoryRecord.messageTitle = pTitle; context.newHistoryRecord.messageBody = pBody; + + if(pRelevance > 6){ + sendEmail(pTitle, pBody, false); + } } @@ -1358,4 +1368,39 @@ return context.executionContext.remember[pKey]; } + + function sendEmail(pTitle, pBody, pSendToAdmins) { + try { + let emailList = global.EMAIL_CONFIG.distributionList; + if (pSendToAdmins) { + emailList = global.EMAIL_CONFIG.adminList; + } + + let transporter = nodemailer.createTransport({ + service: global.EMAIL_CONFIG.service, + auth: { + user: global.EMAIL_CONFIG.user, + pass: global.EMAIL_CONFIG.pass + } + }); + + let mailOptions = { + from: global.EMAIL_CONFIG.from, + bcc: emailList, + subject: bot.startMode + ' - ' + pTitle, + text: pBody + }; + + transporter.sendMail(mailOptions, function (error, info) { + if (error) { + logger.write(MODULE_NAME, "[ERROR] sendEmail -> err = " + error); + } else { + logger.write(MODULE_NAME, "[INFO] sendEmail -> Email sent = " + info.response); + } + }); + + } catch (err) { + logger.write(MODULE_NAME, "[ERROR] sendEmail -> err = " + err.message); + } + } }; \ No newline at end of file diff --git a/Run.js b/Run.js index b33b6cb63..fbfb498a1 100644 --- a/Run.js +++ b/Run.js @@ -30,6 +30,7 @@ process.on('exit', function (code) { }); global.USER_PROFILE = {}; +global.EMAIL_CONFIG = {}; readStoragePermissions(); @@ -57,7 +58,7 @@ function readStoragePermissions() { global.USER_PROFILE.storagePermissions = permissionsMap; - readExchangeAPIKey(); + readEmailConfiguration(); } catch (err) { console.log("[ERROR] readStoragePermissions -> err = " + err.message); @@ -65,6 +66,23 @@ function readStoragePermissions() { } } +function readEmailConfiguration() { + + let filePath; + + try { + let fs = require('fs'); + filePath = '../' + 'Email-Config' + '/' + 'Email.Config.json'; + global.EMAIL_CONFIG = JSON.parse(fs.readFileSync(filePath, 'utf8')); + + readExchangeAPIKey(); + } + catch (err) { + console.log("[ERROR] readEmailConfiguration -> err = " + err.message); + console.log("[HINT] You need to have a file at this path -> " + filePath); + } +} + function readExchangeAPIKey() { try { diff --git a/TradingBotProcessMainLoop.js b/TradingBotProcessMainLoop.js index 15da596f4..4dd3897be 100644 --- a/TradingBotProcessMainLoop.js +++ b/TradingBotProcessMainLoop.js @@ -855,6 +855,7 @@ clearTimeout(nextLoopTimeoutHandle); clearTimeout(checkLoopHealthHandle); bot.enableCheckLoopHealth = false; + assistant.sendEmail("FAIL", "Operation Failed. Aborting the process. err = " + err.message, true); callBackFunction(err); return; } @@ -867,6 +868,7 @@ clearTimeout(nextLoopTimeoutHandle); clearTimeout(checkLoopHealthHandle); bot.enableCheckLoopHealth = false; + assistant.sendEmail("FAIL", "Operation Failed. Aborting the process. err = " + err.message, true); callBackFunction(global.DEFAULT_FAIL_RESPONSE); return; } @@ -879,6 +881,7 @@ clearTimeout(nextLoopTimeoutHandle); clearTimeout(checkLoopHealthHandle); bot.enableCheckLoopHealth = false; + assistant.sendEmail("FAIL", "Operation Failed. Aborting the process. err = " + err.message, true); callBackFunction(err); } } @@ -890,6 +893,7 @@ clearTimeout(nextLoopTimeoutHandle); clearTimeout(checkLoopHealthHandle); bot.enableCheckLoopHealth = false; + assistant.sendEmail("FAIL", "Operation Failed. Aborting the process. err = " + err.message, true); callBackFunction(err); } }