From 53f26c597fc8a396108a443d5f6969220d3a0664 Mon Sep 17 00:00:00 2001 From: Luke Larsen Date: Tue, 15 Oct 2024 10:56:59 -0400 Subject: [PATCH] Adding xt data source --- src/db.js | 11 +++++++++-- src/remoteData.js | 37 +++++++++++++++++++++++++++++++++++++ src/routes.js | 1 + 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/db.js b/src/db.js index 554be0a..87e71e9 100644 --- a/src/db.js +++ b/src/db.js @@ -29,14 +29,21 @@ async function jumpStart(){ dataSourceName: 'binance', data: [], enabled: true, - binance:10, + updateSnapshotTime:10, lastUpdated: 0, }) DataSourceDataSchema.create({ dataSourceName: 'coinMarketCap', data: [], enabled: true, - coinMarketCap: 10, + updateSnapshotTime: 10, + lastUpdated: 0, + }) + DataSourceDataSchema.create({ + dataSourceName: 'xt', + data: [], + enabled: true, + updateSnapshotTime: 120, lastUpdated: 0, }) } diff --git a/src/remoteData.js b/src/remoteData.js index c45cf52..7db84f3 100644 --- a/src/remoteData.js +++ b/src/remoteData.js @@ -71,6 +71,13 @@ async function getMarketData(marketData, dataSource, baseCurrency){ } } } + if (dataSource == 'xt'){ + let xtDataStored = await DataSourceDataSchema.findOne({ dataSourceName: 'xt' }).exec(); + if(xtDataStored.enabled){ + let xtData = await getDataXt(marketData, baseCurrency) + if(xtData){}else{console.log("Error retrieving xt.com data")} + } + } } /** @@ -341,6 +348,35 @@ async function getDataCoinGeckoDirect(marketData, baseCurrency){ } } +async function getDataXt(marketData, baseCurrency){ + // Format input data and output in a known format for the rest of the program + // Check what the base currency is + // https://sapi.xt.com/v4/public/ticker?symbol=pivx_usdt + // + let url = 'https://sapi.xt.com/v4/public/ticker/price?symbols='+ticker+'_usdt' + let data = await getData(url) + if(data.result){ + let XtReturnData ={} + XtReturnData['xt'] = {} + XtReturnData.xt['usdt'] = parseFloat(data.result[0].p) + XtReturnData.xt['usd'] = parseFloat(data.result[0].p) + let dataFromDisk = await getMarketDataSource(marketData,"xt") + if(dataFromDisk === undefined){ + const xtDataResult = new dataSource("xt",XtReturnData,Math.floor(new Date().getTime() / 1000)) + updateOrCreateDataSource(xtDataResult) + }else{ + // Update coingecko + updateDataSource(marketData, dataFromDisk, XtReturnData, Math.floor(new Date().getTime() / 1000)) + } + // For the old endpoint will be removed soon + return data.result + }else{ + console.log("coingecko Error") + console.log(data) + } +} + + //Auto check the data async function autoCheckData(){ // Fetch market data from disk @@ -351,6 +387,7 @@ async function autoCheckData(){ await getMarketData(arrMarketData, 'coinGeckoDirect', 'usd') await getMarketData(arrMarketData, 'binance','usd'); await getMarketData(arrMarketData, 'coinMarketCap','usd'); + await getMarketData(arrMarketData, 'xt','usd') arrMarketData = await readDataSource(); } // Aggregate the prices from our various sources diff --git a/src/routes.js b/src/routes.js index 9b56f75..7f6b2da 100644 --- a/src/routes.js +++ b/src/routes.js @@ -10,6 +10,7 @@ const dataSourceUpdateTime = { //listed in seconds coinGeckoDirect: 70, coinMarketCap: 10, binance: 10, + xt:120, } /** An optional prefix for the service router: useful if plugging in to an existing Express App */