Advertisement
Guest User

Untitled

a guest
Oct 1st, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. diff --git a/tools/logger.js b/tools/logger.js
  2. index 356a91a..c788760 100644
  3. --- a/tools/logger.js
  4. +++ b/tools/logger.js
  5. @@ -1,21 +1,50 @@
  6. -var bittrex = require('node.bittrex.api');
  7. +var bittrex = require('node-bittrex-api');
  8. var argv = require('minimist')(process.argv.slice(2));
  9. var request = require('request');
  10. var mysql = require('mysql');
  11. +var fs = require("fs");
  12. var connection;
  13.  
  14. +var config = loadConfigFile("./loggerConfig.json");
  15. +if(typeof config == "undefined")
  16. +{
  17. + return;
  18. +}
  19. +
  20. var enableBackup = false;
  21. var iAmASlave = false;
  22. var isMasterUp = true;
  23. var listeningPort = 8800;
  24.  
  25. +var timeOfCandle = 3000;
  26. +
  27. +var updateDb = 0;
  28. +var updateSocket = 0;
  29. +
  30. +var updateDbOrderBook = 0;
  31. +var updateDbOrderBookOk = 0;
  32. +var updateSocketOrderBook = 0;
  33. +
  34. +var availablePairs = [];
  35. +var websocketsclient;
  36. +var websocketsclientSubscribe;
  37. +
  38. +var minimumVolumeToSubscribe = 150;
  39. +var maximumAllowedDrift = 4;
  40. +var timeBetweenOrderBookChecking = 20; //In seconds
  41. +
  42. +var orderBooks = {};
  43. +var nounce = {};
  44. +
  45. +var rescueDbs = [
  46. + {
  47. + host: "85.69.196.124",
  48. + port : 3306,
  49. + user : "root",
  50. + password : "",
  51. + database : 'trading'
  52. + }
  53. +];
  54.  
  55. if(argv.database)
  56. {
  57. @@ -142,24 +171,6 @@ function handleDisconnect() {
  58.  
  59. handleDisconnect();
  60.  
  61. -var timeOfCandle = 3000;
  62. -
  63. -var updateDb = 0;
  64. -var updateSocket = 0;
  65. -
  66. -var updateDbOrderBook = 0;
  67. -var updateDbOrderBookOk = 0;
  68. -var updateSocketOrderBook = 0;
  69. -
  70. -var availablePairs = [];
  71. -var websocketsclient;
  72. -var websocketsclientSubscribe;
  73. -
  74. -var minimumVolumeToSubscribe = 150;
  75. -var maximumAllowedDrift = 4;
  76. -var timeBetweenOrderBookChecking = 20; //In seconds
  77. -
  78. -var orderBooks = {};
  79.  
  80. function connectWebSocket(){
  81. console.log("Connection to websockets");
  82. @@ -329,11 +340,27 @@ function addToOrderBook(index, data)
  83. }
  84. if(typeof(orderBooks[index].pushedOnce) == "undefined")
  85. {
  86. - console.log("The orderBooks has not already been pused for " + index);
  87. + console.log("The orderBooks has not already been pushed for " + index);
  88. return;
  89. }
  90. updateSocketOrderBook += data.Buys.length + data.Sells.length;//+ data.Fills.length;
  91.  
  92. + //Control the nouce
  93. + if(typeof nounce[data.MarketName] == "undefined")
  94. + {
  95. + console.log("Creating nouce for "+data.MarketName+
  96. + ", beginning at "+data.Nounce);
  97. + nounce[data.MarketName] = data.Nounce - 1;
  98. + }
  99. + if((data.Nounce - 1 ) != nounce[data.MarketName])
  100. + {
  101. + console.log("There is missing data for " +data.Nounce + " : " +
  102. + data.Nounce + "-"+ nouce[data.MarketName]);
  103. + fs.appendFile( "missedNounce.log",data.Nounce + ":"+data.MarketName);
  104. + }
  105. + //Update the nouce
  106. + nounce[data.MarketName] = Number(data.Nounce);
  107. +
  108. for(var i in data.Buys)
  109. {
  110. let tmpObject = {};
  111. @@ -342,6 +369,7 @@ function addToOrderBook(index, data)
  112. tmpObject.type = data.Buys[i].Type;
  113. tmpObject.rate = data.Buys[i].Rate;
  114. tmpObject.quantity = data.Buys[i].Quantity;
  115. + tmpObject.nounce = Number(data.Nounce);
  116.  
  117. orderBooks[index].buy.push(tmpObject);
  118. }
  119. @@ -353,6 +381,7 @@ function addToOrderBook(index, data)
  120. tmpObject.type = data.Sells[i].Type;
  121. tmpObject.rate = data.Sells[i].Rate;
  122. tmpObject.quantity = data.Sells[i].Quantity;
  123. + tmpObject.nounce = Number(data.Nounce);
  124.  
  125. orderBooks[index].sell.push(tmpObject);
  126. }
  127. @@ -503,6 +532,54 @@ function createOrderBookSnapshot(index)
  128. });
  129. }
  130.  
  131. +function getMissingDataFromNounce(nounce, index, connectionsToTry)
  132. +{
  133. + console.log("Trying to get missing data for " + pair + " with nounce " + nounce);
  134. + var connectionToTry = connectionsToTry.shift();
  135. + if(typeof connectionToTry == "undefined")
  136. + {
  137. + console.log("We havent been able to retrieve data, sorry dude");
  138. + return;
  139. + }
  140. + var connectionHandler = mysql.createConnection(connectionsToTry);
  141. + connectionHandler.connect(function(err)
  142. + {
  143. + if(err)
  144. + {
  145. + console.log("Cannot connect to rescue db, trying with another one");
  146. + getMissingDataFromNonce(nounce, index, connectionToTry);
  147. + return;
  148. + }
  149. + console.log("Connected to rescue db, getting data");
  150. + connectionHandler.query("SELECT * FROM orderBooks WHERE nounce = ? AND name = ?", [nounce, index], function(error, results, fields)
  151. + {
  152. + console.log("Got rescue data for " + index + " !");
  153. +
  154. + results.forEach((row)=>{
  155. + connection.query("INSERT INTO orderBooks (name, buyOrSell, type, rate, quantity, date, nounce) VALUES (?,?,?,?,?,?,?)", [row.name, row.buyOrSell, row.type, row.rate, row.quantity, row.date, row.nounce], function(error, results, field)
  156. + {
  157. + if(err)
  158. + {
  159. + throw err;
  160. + }
  161. + });
  162. + });
  163. + });
  164. + });
  165. +}
  166. +
  167. +function loadConfigFile(pathToConfig)
  168. +{
  169. + console.log("Loading configuration file");
  170. + var tempConfig = JSON.parse(fs.readFileSync(pathToConfig, 'utf-8'));
  171. + if(typeof tempConfig == "undefined")
  172. + {
  173. + console.log("Cannot load configuration file");
  174. + return undefined;
  175. + }
  176. + return tempConfig;
  177. +}
  178. +
  179. websocketsclient.serviceHandlers.connected = function(connection)
  180. {
  181. console.log("Websocket connected");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement