diff --git a/backend/app.js b/backend/app.js index ea5046d..dd4b096 100644 --- a/backend/app.js +++ b/backend/app.js @@ -8,10 +8,10 @@ const ownermodel = require('./models/owner'); const itemmodel = require('./models/item'); const ordermodel = require('./models/order'); const pricemodel = require('./models/price'); +const txmodel = require('./models/tx'); const mongoose = require('mongoose'); const stdrpc = require('stdrpc'); const CoinGecko = require('coingecko-api'); -//const RequestIP = require('@supercharge/request-ip'); var db = require('./config/db'); mongoose.connect('mongodb://'+db.user+':'+db.password+'@'+db.server+'/'+db.database).then(() => { @@ -84,7 +84,7 @@ var blockInterval = setInterval( function() { //console.log(docs); //} //}); - rpc.z_listreceivedbyaddress(fullnode.addr, 10).then(txs => { + rpc.z_listreceivedbyaddress(fullnode.addr, 1).then(txs => { var re = /.*ZGO::(.*)\sReply-To:\s(z\w+)/; async.each (txs, function(txData, callback) { var memo = hexToString(txData.memo); @@ -96,46 +96,58 @@ var blockInterval = setInterval( function() { var session = match[1]; var blocktime = txData.blocktime; //console.log(' ', session, blocktime); - usermodel.findOne({address: address, session: session, blocktime: blocktime}).then(function(doc){ - if (doc != null) { - console.log('Found user'); - } else { - console.log('User not found', session, blocktime); - var user = new usermodel({ - address: address, - session: session, - blocktime: blocktime - }); - user.save(function(error) { - if (error) { - console.log(error); - } + if (txData.confirmations >= 10 ) { + usermodel.findOne({address: address, session: session, blocktime: blocktime}).then(function(doc){ + if (doc != null) { + console.log('Found user'); + } else { + console.log('User not found', session, blocktime); + var user = new usermodel({ + address: address, + session: session, + blocktime: blocktime + }); + user.save(function(error) { + if (error) { + console.log(error); + } - console.log('User saved'); - }); - } - }); - ownermodel.findOne({address: address}).then(function (oDoc) { - if (oDoc != null) { - console.log('Found owner'); - } else { - console.log('Owner not found', session); - var owner = new ownermodel({ - address: address, - name: 'Z-Go-'.concat(address.substring(0,5)) - }); - owner.save().then(function(err) { - if (err) { - console.log(err); - } - console.log('Owner saved!'); - }).catch(() => { - console.log('Owner exists'); - }); - } - }).catch((err) => { - console.log(err); - }); + console.log('User saved'); + }); + } + }); + ownermodel.findOne({address: address}).then(function (oDoc) { + if (oDoc != null) { + console.log('Found owner'); + } else { + console.log('Owner not found', session); + var owner = new ownermodel({ + address: address, + name: 'Z-Go-'.concat(address.substring(0,5)) + }); + owner.save().then(function(err) { + if (err) { + console.log(err); + } + console.log('Owner saved!'); + }).catch(() => { + console.log('Owner exists'); + }); + } + }).catch((err) => { + console.log(err); + }); + txmodel.deleteMany({session: session}, function(err) { + if (err) console.log(err); + console.log('Deleted confirmed login'); + }); + } else { + txmodel.updateOne({address: address, session: session}, { confirmations: txData.confirmations, amount:txData.amount}, {new:true, upsert:true}, function(err,docs) { + if (err) { + console.log(err); + } + }); + } } } }, function (err) { @@ -188,6 +200,26 @@ app.get('/api/users', (req, res, next) => { }); }); +app.get('/api/pending', (req, res, next) => { + console.log('Get: /api/pending'); + txmodel.find({'session': req.query.session}). + then((documents) => { + if (documents.length > 0) { + //console.log('pending', documents); + res.status(200).json({ + message: 'Found pending txs', + txs: documents + }); + } else { + //console.log('pending not found', documents); + res.status(204).json({ + message: 'No txs found', + txs: null + }); + } + }); +}); + app.get('/api/getuser', (req, res, next) => { console.log('Get: /api/getuser/', req.query.session); var today = new Date().getTime() / 1000; diff --git a/backend/models/tx.js b/backend/models/tx.js new file mode 100644 index 0000000..d55ba1d --- /dev/null +++ b/backend/models/tx.js @@ -0,0 +1,10 @@ +const mongoose = require('mongoose'); + +const txSchema = mongoose.Schema({ + address: {type: String}, + session: {type: String, required:true}, + confirmations: {type: Number, required:true}, + amount: {type: Number, required:true} +}); + +module.exports = mongoose.model('Tx', txSchema); diff --git a/src/app/login/login.component.html b/src/app/login/login.component.html index 2a20d85..46425dd 100644 --- a/src/app/login/login.component.html +++ b/src/app/login/login.component.html @@ -31,7 +31,13 @@ -
diff --git a/src/app/login/login.component.ts b/src/app/login/login.component.ts
index 1d93dce..9ac2add 100644
--- a/src/app/login/login.component.ts
+++ b/src/app/login/login.component.ts
@@ -2,6 +2,7 @@ import { Component, OnInit, OnDestroy, Injectable, ChangeDetectorRef } from '@an
import { CanActivate, Router, RouterStateSnapshot, ActivatedRouteSnapshot, ActivatedRoute } from '@angular/router';
import { UserService } from '../user.service';
import { FullnodeService } from '../fullnode.service';
+import { Tx } from '../tx.model';
import { Subscription, Observable } from 'rxjs';
import { v4 as uuidv4 } from 'uuid';
var QRCode = require('easyqrcodejs');
@@ -16,12 +17,14 @@ var Buffer = require('buffer/').Buffer;
})
export class LoginComponent implements OnInit {
+ txs: Tx[] = [];
intervalHolder: any;
nodeAddress: string = '';
private FullnodeSub: Subscription = new Subscription();
private UserSub: Subscription = new Subscription();
public heightUpdate: Observable