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