From 50221e3fcec0d1af177b58c38997382557b64af0 Mon Sep 17 00:00:00 2001 From: nobita851 Date: Mon, 2 Dec 2024 02:58:34 +0530 Subject: [PATCH] Add price-fetch module --- operator/price.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 operator/price.js diff --git a/operator/price.js b/operator/price.js new file mode 100644 index 0000000..60195ac --- /dev/null +++ b/operator/price.js @@ -0,0 +1,34 @@ +const http = require('http'); + +const getPrice = (symbol) => { + console.log('Getting price for', symbol); + const req = http.request({ + hostname: 'data-api.binance.vision', + path: '/api/v3/ticker/price?symbol=' + symbol, + method: 'GET' + }, (res) => { + let data = ''; + + res.on('data', (chunk) => { + data += chunk; + }); + + res.on('end', () => { + try { + const parsedData = JSON.parse(data); + console.log('Price received:', parsedData.price); + resolve(parsedData.price); + } catch (e) { + reject(`Error parsing JSON: ${e.message}`); + } + }); + }); + + req.on('error', (e) => { + console.error(`problem with request: ${e.message}`); + }); + + req.end(); +} + +module.exports = { getPrice }; \ No newline at end of file