diff --git a/backend/app.js b/backend/app.js index 1d812ec..163387f 100644 --- a/backend/app.js +++ b/backend/app.js @@ -6,6 +6,7 @@ const usermodel = require('./models/user'); const ownermodel = require('./models/owner'); const itemmodel = require('./models/item'); const ordermodel = require('./models/order'); +const pricemodel = require('./models/price'); const mongoose = require('mongoose'); const stdrpc = require('stdrpc'); const CoinGecko = require('coingecko-api'); @@ -27,6 +28,41 @@ const rpc = stdrpc({ const CoinGeckoClient = new CoinGecko(); +var intervalObject = setInterval( function() { + CoinGeckoClient.simple.price({ + ids: ['zcash'], + vs_currencies: ['usd', 'gbp', 'eur', 'cad', 'aud'] + }).then((data) => { + pricemodel.findOneAndUpdate({currency: 'usd'}, { price: data.data.zcash.usd, timestamp: Date.now()}, {new:true, upsert:true}, function(err,docs) { + if(err) { + console.log(err); + } + }); + pricemodel.findOneAndUpdate({currency: 'gbp'}, { price: data.data.zcash.gbp, timestamp: Date.now()}, {new:true, upsert:true}, function(err,docs) { + if(err) { + console.log(err); + } + }); + pricemodel.findOneAndUpdate({currency: 'eur'}, { price: data.data.zcash.eur, timestamp: Date.now()}, {new:true, upsert:true}, function(err,docs) { + if(err) { + console.log(err); + } + }); + pricemodel.findOneAndUpdate({currency: 'cad'}, { price: data.data.zcash.cad, timestamp: Date.now()}, {new:true, upsert:true}, function(err,docs) { + if(err) { + console.log(err); + } + }); + pricemodel.findOneAndUpdate({currency: 'aud'}, { price: data.data.zcash.aud, timestamp: Date.now()}, {new:true, upsert:true}, function(err,docs) { + if(err) { + console.log(err); + } + }); + }).catch((err) => { + console.log(err); + }); +}, 90000); + app.use(bodyparser.json()); app.use((req, res, next) => { @@ -248,17 +284,19 @@ app.delete('/api/item/:id', (req, res, next) => { app.get('/api/price', (req, res, next) => { console.log('Get /api/price'); - CoinGeckoClient.simple.price({ - ids: ['zcash'], - vs_currencies: ['usd'] - }). - then((data) => { - + const price = pricemodel.findOne({currency: 'usd'}).then((document) => { + if (document != null) { res.status(200).json({ message: 'price found!', - price: data.data.zcash.usd + price: document }); - }); + } else { + res.status(204).json({ + message: 'no price found!', + order: null + }); + } + }); }); app.get('/api/order', (req, res, next) => { diff --git a/backend/models/price.js b/backend/models/price.js new file mode 100644 index 0000000..7d3aaeb --- /dev/null +++ b/backend/models/price.js @@ -0,0 +1,10 @@ +const mongoose = require('mongoose'); + +const priceSchema = mongoose.Schema({ + currency: {type: String, required: true}, + price: {type: Number, required: true}, + timestamp: {type: Date, required: true, default: Date.now} +}); + + +module.exports = mongoose.model('Price', priceSchema); diff --git a/src/app/fullnode.service.ts b/src/app/fullnode.service.ts index 09a711b..d2e8389 100644 --- a/src/app/fullnode.service.ts +++ b/src/app/fullnode.service.ts @@ -36,11 +36,17 @@ export class FullnodeService{ } getPrice(){ - let obs = this.http.get<{message: string, price: number}>('http://localhost:3000/api/price'); + var currency = 'usd'; + const params = new HttpParams().append('currency', currency); + let obs = this.http.get<{message: string, price: any}>('http://localhost:3000/api/price', { headers:{}, params: params, observe: 'response'}); obs.subscribe((PriceData) => { - this.dataStore.price = PriceData.price; - console.log(this.dataStore.price); - this._priceUpdated.next(Object.assign({},this.dataStore).price); + if (PriceData.status == 200) { + this.dataStore.price = PriceData.body!.price.price; + console.log("price", this.dataStore.price); + this._priceUpdated.next(Object.assign({},this.dataStore).price); + } else { + console.log('No price found for currency', currency); + } }); return obs; @@ -59,7 +65,7 @@ export class FullnodeService{ subscribe((TxData) => { var memos: string[] = []; //this.addr = TxData.addr; - var re = /.*S4ZEC::(.*)\sReply-To:\s(z\w+)/; + var re = /.*ZGO::(.*)\sReply-To:\s(z\w+)/; for (var i=0; i < TxData.txs.length; i++) { var memo = this.hexToString(TxData.txs[i].memo); //console.log(TxData.txs[i].blocktime); diff --git a/src/app/login/login.component.html b/src/app/login/login.component.html index 3aa5a7c..2a20d85 100644 --- a/src/app/login/login.component.html +++ b/src/app/login/login.component.html @@ -12,6 +12,7 @@ || +
Last block seen: {{ heightUpdate | async }}