From 22ea8a52dad1bc28b7fedf76d80869bbf77fddde Mon Sep 17 00:00:00 2001 From: Piyush Dixit <79581397+PiyushDixit96@users.noreply.github.com> Date: Sat, 15 May 2021 23:37:06 +0530 Subject: add total amount --- index.js | 130 +++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 76 insertions(+), 54 deletions(-) diff --git a/index.js b/index.js index 54bc2f2..0ff0018 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,8 @@ #!/usr/bin/env node + const Request = require('request-promise'); const dotenv = require('dotenv'); dotenv.config(); - const token = process.env['TELEGRAM_TOKEN']; const chat_id = process.env['TELEGRAM_CHAT_ID']; const api_key = process.env['BINANCE_API_KEY']; @@ -10,28 +10,24 @@ const secret_key = process.env['BINANCE_SECRET_KEY']; const NODE_ENV = process.env.NODE_ENV || "development"; const port = process.env.PORT || 3000; const timeZone = process.env.TIME_ZONE_STRING || 'Asia/Kolkata'; - -if (process.env.NODE_ENV === "development") { +if(NODE_ENV === "development") { console.log("NODE_ENV development") -} else { +} else { console.log("NODE_ENV production") } //Indian time string -const event = new Date().toLocaleString('en-IN', { timeZone: timeZone, timeZoneName: 'short' }); - -//sending telegram message -const Slim_bot = require('slimbot'); -const bot = new Slim_bot(token); - +const event = new Date().toLocaleString('en-IN', { + timeZone: timeZone, + timeZoneName: 'short' +}); //only for heroku port error const express = require('express'); const app = express(); app.all('/', (req, res) => res.send('Bot is Running')); app.listen(port, () => console.log(`${event} - Server started on ${port} port`)); - //BinanceWS const binanceApi = require('binance'); -const binanceWS = new binanceApi.BinanceWS(true); +const binanceWS = new binanceApi.BinanceWS(false); try { binanceRest = new binanceApi.BinanceRest({ key: api_key, @@ -41,12 +37,11 @@ try { binanceWS.onUserData(binanceRest, data => { console.log(`${event} - Session Message: `, data); process_data(data); - - }, 60000).then(ws => { + }, 60000).then(() => { console.log(`${event} - Monitoring Spot User Order Data for binance.com`); sendMessage(`Binance Spot Order Monitor Started\nthis message shows that you or heroku(if your are using) restart the bot.`) }) -} catch (err) { +} catch(err) { console.error(`${event} - ${err}`) sendMessage(err.toString()) } @@ -54,39 +49,68 @@ try { function fixFloat(floatNum, Precision = 8) { const num = Number.parseFloat(floatNum).toFixed(Precision); const str = num.toString(); - return str.replace(/(\.\d+?)0+\b/g, "$1") //fix 20.000 to 20.0 or 0.0000000120 to 0.000000012 + return str.replace(/(\.\d+?)0+\b/g, "$1") //fix 20.000 to 20.0 or 0.0000000120 to 0.000000012 } - function process_data(data) { + let { + e: eventType, + x: executionType, + s: symbol, + p: price, + q: quantity, + S: side, + o: orderType, + i: orderId, + X: orderStatus, + l: lastTradeQuantity, + z: Cumulative_filled_quantity + } = data; + let str4 = symbol.slice(symbol.length - 4); + let str3 = symbol.slice(symbol.length - 3); + let sy; + if(["USDT", "BUSD", "TUSD", "USDC", "BIDR", "IDRT", "BVND"].includes(str4)) { + sy = str4 + } else if(["BNB", "BTC", "XRP", "TRX", "ETH", "AUD", "BRL", "EUR", "GBP", "RUB", "TRY", "PAX", "DAI", "UAH", "NGN", "VAI"].includes(str3)) { + sy = str3 + } + let total = `${fixFloat(Number(price) * Number(quantity))} ${sy}` + if(orderType !== "LIMIT") { + let { + L: Lprice + } = data; + price = Lprice + } let txt; - if (data.eventType === 'executionReport') { - if (data.executionType === 'NEW') { - if (data.orderStatus === 'NEW') { - txt = `💸 💸 💸\nSpot Order CREATED\nSide: ${data.side}\nSymbol: #${data.symbol}\nPrice: ${data.price}\nQuantity: ${fixFloat(data.quantity)}\nOrder ID: #ID${data.orderId}` - } - else if (data.orderStatus === 'REJECTED') { - txt = `🚫 🚫 🚫\nSpot Order REJECTED\nSide: ${data.side}\nSymbol: #${data.symbol}\nPrice: ${data.price}\nQuantity: ${fixFloat(data.quantity)}\nOrder ID: #ID${data.orderId}` + if(eventType === 'executionReport') { + if(executionType === 'NEW') { + if(orderStatus === 'NEW') { + txt = `💸 💸 💸\nSpot Order CREATED\nSide: ${side}\nSymbol: #${symbol}\nPrice: ${price}\nQuantity: ${fixFloat(quantity)}\nTotal: ${total}\nOrder ID: #ID${orderId}` + } else if(orderStatus === 'REJECTED') { + txt = `🚫 🚫 🚫\nSpot Order REJECTED\nSide: ${side}\nSymbol: #${symbol}\nPrice: ${price}\nQuantity: ${fixFloat(quantity)}\nTotal: ${total}\nOrder ID: #ID${orderId}` } - } else if (data.executionType === 'CANCELED') { - if (data.orderStatus === 'CANCELED') { - txt = `🛑 🛑 🛑\nSpot Order CANCELED\nSide: ${data.side}\nSymbol: #${data.symbol}\nPrice: ${data.price}\nQuantity: ${fixFloat(data.quantity)}\nOrder ID: #ID${data.orderId}` + } else if(executionType === 'CANCELED') { + if(orderStatus === 'CANCELED') { + txt = `🛑 🛑 🛑\nSpot Order CANCELED\nSide: ${side}\nSymbol: #${symbol}\nPrice: ${price}\nQuantity: ${fixFloat(quantity)}\nTotal: ${total}\nOrder ID: #ID${orderId}` } - } else if (data.executionType === 'TRADE') { - if (data.orderStatus === 'PARTIALLY_FILLED') { - txt = `💰 💰 💰\nSpot Order PARTIALLY FILLED\nSide: ${data.side}\nSymbol: #${data.symbol}\nPrice: ${data.price}\nLast Filled: ${fixFloat(data.lastTradeQuantity)}\nRemaining: ${fixFloat(Number(data.quantity) - Number(data.accumulatedQuantity))}\nOrder ID: #ID${data.orderId}` - } else if (data.orderStatus === 'FILLED') { - txt = `✅ ✅ ✅\nSpot Order FULLY FILLED\nSide: ${data.side}\nSymbol: #${data.symbol}\nPrice: ${data.price}\nFilled: ${fixFloat(data.accumulatedQuantity)}\nOrder ID: #ID${data.orderId}` + } else if(executionType === 'TRADE') { + if(orderStatus === 'PARTIALLY_FILLED') { + txt = `💰 💰 💰 Spot Order PARTIALLY FILLED Side: ${side} Symbol: #${symbol} Price: ${price} + Last Filled: ${fixFloat(lastTradeQuantity)} + Remaining: ${fixFloat(Number(quantity) - Number(Cumulative_filled_quantity))} + Total: ${total} Order ID: #ID${orderId}` + } else if(orderStatus === 'FILLED') { + txt = `✅ ✅ ✅\nSpot Order FULLY FILLED\nSide: ${side}\nSymbol: #${symbol}\nPrice: ${price}\nFilled: ${fixFloat(Cumulative_filled_quantity)}\nTotal: ${total}\nOrder ID: #ID${orderId}` } - } else if (['REPLACED', 'EXPIRED', 'PENDING_CANCEL'].includes(data.orderStatus)) { - txt = `🔴 🟡 🔵\nSpot Order ${data.orderStatus}\nSide: ${data.side}\nSymbol: #${data.symbol}\nPrice: ${data.price}\nQuantity: ${fixFloat(data.quantity)}\nOrder ID: #ID${data.orderId}` + } else if(['REPLACED', 'EXPIRED', 'PENDING_CANCEL'].includes(orderStatus)) { + txt = `🔴 🟡 🔵\nSpot Order ${orderStatus}\nSide: ${side}\nSymbol: #${symbol}\nPrice: ${price}\nQuantity: ${fixFloat(quantity)}\nTotal: ${total}\nOrder ID: #ID${orderId}` } else { - txt = `⚠️ ⚠️ ⚠️\nUndefined\nExecution Type: ${data.executionType}\nOrder Status ${data.orderStatus}\nFull Details:\n${msg}` + txt = `⚠️ ⚠️ ⚠️\nUndefined\nExecution Type: ${executionType}\nOrder Status ${orderStatus}\nFull Details:\n${data}` } sendMessage(txt) } } - +//sending telegram message function sendMessage(text) { let params = { chat_id: chat_id, @@ -100,22 +124,20 @@ function sendMessage(text) { resolveWithFullResponse: true, forever: true }; - return Request(options) - .then(resp => { - if (resp.statusCode !== 200) { - throw new Error(resp.statusCode + ':\n' + resp.body); - } - let updates = JSON.parse(resp.body); - if (updates.ok) { - console.log("Message send via Telegram") - return updates; - } else { - console.log(`something went wrong while sending message to telegram see detailed error below.`) - console.error(updates) - return null; - } - }) - .catch(error => { - throw error; - }); + return Request(options).then(resp => { + if(resp.statusCode !== 200) { + throw new Error(resp.statusCode + ':\n' + resp.body); + } + let updates = JSON.parse(resp.body); + if(updates.ok) { + console.log("Message send via Telegram") + return updates; + } else { + console.log(`something went wrong while sending message to telegram see detailed error below.`) + console.error(updates) + return null; + } + }).catch(error => { + throw error; + }); } -- cgit v1.2.3