Implement database price feed
This commit is contained in:
parent
0f5a731d08
commit
01520adbc3
6 changed files with 93 additions and 26 deletions
|
@ -6,6 +6,7 @@ const usermodel = require('./models/user');
|
||||||
const ownermodel = require('./models/owner');
|
const ownermodel = require('./models/owner');
|
||||||
const itemmodel = require('./models/item');
|
const itemmodel = require('./models/item');
|
||||||
const ordermodel = require('./models/order');
|
const ordermodel = require('./models/order');
|
||||||
|
const pricemodel = require('./models/price');
|
||||||
const mongoose = require('mongoose');
|
const mongoose = require('mongoose');
|
||||||
const stdrpc = require('stdrpc');
|
const stdrpc = require('stdrpc');
|
||||||
const CoinGecko = require('coingecko-api');
|
const CoinGecko = require('coingecko-api');
|
||||||
|
@ -27,6 +28,41 @@ const rpc = stdrpc({
|
||||||
|
|
||||||
const CoinGeckoClient = new CoinGecko();
|
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(bodyparser.json());
|
||||||
|
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
|
@ -248,17 +284,19 @@ app.delete('/api/item/:id', (req, res, next) => {
|
||||||
|
|
||||||
app.get('/api/price', (req, res, next) => {
|
app.get('/api/price', (req, res, next) => {
|
||||||
console.log('Get /api/price');
|
console.log('Get /api/price');
|
||||||
CoinGeckoClient.simple.price({
|
const price = pricemodel.findOne({currency: 'usd'}).then((document) => {
|
||||||
ids: ['zcash'],
|
if (document != null) {
|
||||||
vs_currencies: ['usd']
|
|
||||||
}).
|
|
||||||
then((data) => {
|
|
||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
message: 'price found!',
|
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) => {
|
app.get('/api/order', (req, res, next) => {
|
||||||
|
|
10
backend/models/price.js
Normal file
10
backend/models/price.js
Normal file
|
@ -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);
|
|
@ -36,11 +36,17 @@ export class FullnodeService{
|
||||||
}
|
}
|
||||||
|
|
||||||
getPrice(){
|
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) => {
|
obs.subscribe((PriceData) => {
|
||||||
this.dataStore.price = PriceData.price;
|
if (PriceData.status == 200) {
|
||||||
console.log(this.dataStore.price);
|
this.dataStore.price = PriceData.body!.price.price;
|
||||||
this._priceUpdated.next(Object.assign({},this.dataStore).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;
|
return obs;
|
||||||
|
@ -59,7 +65,7 @@ export class FullnodeService{
|
||||||
subscribe((TxData) => {
|
subscribe((TxData) => {
|
||||||
var memos: string[] = [];
|
var memos: string[] = [];
|
||||||
//this.addr = TxData.addr;
|
//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++) {
|
for (var i=0; i < TxData.txs.length; i++) {
|
||||||
var memo = this.hexToString(TxData.txs[i].memo);
|
var memo = this.hexToString(TxData.txs[i].memo);
|
||||||
//console.log(TxData.txs[i].blocktime);
|
//console.log(TxData.txs[i].blocktime);
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
||
|
||
|
||||||
</pre>
|
</pre>
|
||||||
</h3>
|
</h3>
|
||||||
|
<p class="text">Last block seen: {{ heightUpdate | async }}</p>
|
||||||
</mat-card>
|
</mat-card>
|
||||||
</div>
|
</div>
|
||||||
<div align="center">
|
<div align="center">
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import { Component, OnInit, OnDestroy, Injectable } from '@angular/core';
|
import { Component, OnInit, OnDestroy, Injectable, ChangeDetectorRef } from '@angular/core';
|
||||||
import { CanActivate, Router, RouterStateSnapshot, ActivatedRouteSnapshot, ActivatedRoute } from '@angular/router';
|
import { CanActivate, Router, RouterStateSnapshot, ActivatedRouteSnapshot, ActivatedRoute } from '@angular/router';
|
||||||
import { UserService } from '../user.service';
|
import { UserService } from '../user.service';
|
||||||
import { FullnodeService } from '../fullnode.service';
|
import { FullnodeService } from '../fullnode.service';
|
||||||
import { Subscription } from 'rxjs';
|
import { Subscription, Observable } from 'rxjs';
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
var QRCode = require('easyqrcodejs');
|
var QRCode = require('easyqrcodejs');
|
||||||
var URLSafeBase64 = require('urlsafe-base64');
|
var URLSafeBase64 = require('urlsafe-base64');
|
||||||
|
@ -16,33 +16,39 @@ var Buffer = require('buffer/').Buffer;
|
||||||
})
|
})
|
||||||
|
|
||||||
export class LoginComponent implements OnInit {
|
export class LoginComponent implements OnInit {
|
||||||
|
intervalHolder: any;
|
||||||
memos: string[] = [];
|
memos: string[] = [];
|
||||||
nodeAddress: string = '';
|
nodeAddress: string = '';
|
||||||
private FullnodeSub: Subscription = new Subscription();
|
private FullnodeSub: Subscription = new Subscription();
|
||||||
private UserSub: Subscription = new Subscription();
|
private UserSub: Subscription = new Subscription();
|
||||||
|
public heightUpdate: Observable<number>;
|
||||||
|
public uZaddrUpdate: Observable<string>;
|
||||||
constructor(
|
constructor(
|
||||||
private activatedRoute: ActivatedRoute,
|
private activatedRoute: ActivatedRoute,
|
||||||
public fullnodeService: FullnodeService,
|
public fullnodeService: FullnodeService,
|
||||||
private router: Router,
|
private router: Router,
|
||||||
public userService: UserService
|
public userService: UserService,
|
||||||
|
private _changeDetectorRef: ChangeDetectorRef
|
||||||
){
|
){
|
||||||
//this.fullnodeService.getAddr();
|
//this.fullnodeService.getAddr();
|
||||||
|
this.heightUpdate = fullnodeService.heightUpdate;
|
||||||
|
this.uZaddrUpdate = userService.uZaddrUpdate;
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit(){
|
ngOnInit(){
|
||||||
console.log('Activated route data in Component:::', this.activatedRoute.data);
|
//console.log('Activated route data in Component:::', this.activatedRoute.data);
|
||||||
this.activatedRoute.data.subscribe((addrData) => {
|
this.activatedRoute.data.subscribe((addrData) => {
|
||||||
console.log('FETCH ADDRESS', addrData);
|
//console.log('FETCH ADDRESS', addrData);
|
||||||
this.nodeAddress = addrData.response.addr;
|
this.nodeAddress = addrData.response.addr;
|
||||||
console.log('Node addres ', this.nodeAddress);
|
//console.log('Node addres ', this.nodeAddress);
|
||||||
var localToken = localStorage.getItem('s4z_token');
|
var localToken = localStorage.getItem('s4z_token');
|
||||||
console.log(localToken);
|
//console.log(localToken);
|
||||||
if(localToken == null){
|
if(localToken == null){
|
||||||
var token = uuidv4();
|
var token = uuidv4();
|
||||||
localStorage.setItem('s4z_token', token);
|
localStorage.setItem('s4z_token', token);
|
||||||
console.log('Showing QR code for login');
|
console.log('Showing QR code for login');
|
||||||
console.log(URLSafeBase64.encode(Buffer.from('S4ZEC::'.concat(token))));
|
console.log(URLSafeBase64.encode(Buffer.from('ZGO::'.concat(token))));
|
||||||
var codeString = `zcash:${this.nodeAddress}?amount=0.001&memo=${URLSafeBase64.encode(Buffer.from('S4ZEC::'.concat(token)))}`;
|
var codeString = `zcash:${this.nodeAddress}?amount=0.001&memo=${URLSafeBase64.encode(Buffer.from('ZGO::'.concat(token)))}`;
|
||||||
console.log(codeString);
|
console.log(codeString);
|
||||||
var qrcode = new QRCode(document.getElementById("qrcode"), {
|
var qrcode = new QRCode(document.getElementById("qrcode"), {
|
||||||
text: codeString,
|
text: codeString,
|
||||||
|
@ -61,7 +67,7 @@ export class LoginComponent implements OnInit {
|
||||||
console.log('No login for existing token found');
|
console.log('No login for existing token found');
|
||||||
console.log('Showing QR code for login');
|
console.log('Showing QR code for login');
|
||||||
//console.log(URLSafeBase64.encode(Buffer.from('S4ZEC::'.concat(localToken))));
|
//console.log(URLSafeBase64.encode(Buffer.from('S4ZEC::'.concat(localToken))));
|
||||||
var codeString = `zcash:${this.nodeAddress}?amount=0.001&memo=${URLSafeBase64.encode(Buffer.from('S4ZEC::'.concat(localToken!)))}`;
|
var codeString = `zcash:${this.nodeAddress}?amount=0.001&memo=${URLSafeBase64.encode(Buffer.from('ZGO::'.concat(localToken!)))}`;
|
||||||
console.log(codeString);
|
console.log(codeString);
|
||||||
var qrcode = new QRCode(document.getElementById("qrcode"), {
|
var qrcode = new QRCode(document.getElementById("qrcode"), {
|
||||||
text: codeString,
|
text: codeString,
|
||||||
|
@ -73,10 +79,16 @@ export class LoginComponent implements OnInit {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
this.intervalHolder = setInterval(() => {
|
||||||
|
this.fullnodeService.getHeight();
|
||||||
|
this.fullnodeService.getMemos();
|
||||||
|
this._changeDetectorRef.markForCheck();
|
||||||
|
}, 1000 * 75);
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnDestroy(){
|
ngOnDestroy(){
|
||||||
this.FullnodeSub.unsubscribe();
|
this.FullnodeSub.unsubscribe();
|
||||||
this.UserSub.unsubscribe();
|
this.UserSub.unsubscribe();
|
||||||
|
clearInterval(this.intervalHolder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,12 +33,12 @@ export class UserService{
|
||||||
constructor(private http: HttpClient){
|
constructor(private http: HttpClient){
|
||||||
this.session = localStorage.getItem('s4z_token');
|
this.session = localStorage.getItem('s4z_token');
|
||||||
if (this.session != null) {
|
if (this.session != null) {
|
||||||
this.findUser(this.session);
|
this.findUser();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
findUser(session: string) {
|
findUser() {
|
||||||
const params = new HttpParams().append('session', session);
|
const params = new HttpParams().append('session', this.session!);
|
||||||
let obs = this.http.get<{message: string, user: any}>('http://localhost:3000/api/getuser', { headers:{}, params: params, observe: 'response'});
|
let obs = this.http.get<{message: string, user: any}>('http://localhost:3000/api/getuser', { headers:{}, params: params, observe: 'response'});
|
||||||
|
|
||||||
obs.subscribe((UserDataResponse) => {
|
obs.subscribe((UserDataResponse) => {
|
||||||
|
|
Loading…
Reference in a new issue