FIX: redis balance is only a cashe; tx list is a source of truth for user's balance

This commit is contained in:
Overtorment 2019-01-21 13:17:04 +00:00
parent a08f02572f
commit 9c47746eba
2 changed files with 41 additions and 10 deletions

View File

@ -114,11 +114,42 @@ export class User {
}
async getBalance() {
return (await this._redis.get('balance_for_' + this._userid)) * 1;
let balance = (await this._redis.get('balance_for_' + this._userid)) * 1;
console.log('balance from db ', balance);
if (!balance) {
balance = await this.getCalculatedBalance();
console.log('calculated balance', balance);
await this.saveBalance(balance);
}
return balance;
}
async getCalculatedBalance() {
let calculatedBalance = 0;
let userinvoices = await this.getUserInvoices();
for (let invo of userinvoices) {
if (invo && invo.ispaid) {
calculatedBalance += +invo.amt;
}
}
let txs = await this.getTxs();
for (let tx of txs) {
if (tx.type === 'bitcoind_tx') {
// topup
calculatedBalance += tx.amount * 100000000;
} else {
calculatedBalance -= +tx.value;
}
}
return calculatedBalance;
}
async saveBalance(balance) {
return await this._redis.set('balance_for_' + this._userid, balance);
const key = 'balance_for_' + this._userid;
await this._redis.set(key, balance);
await this._redis.expire(key, 3600 * 24);
}
async savePaidLndInvoice(doc) {

View File

@ -57,7 +57,7 @@ router.post('/create', async function(req, res) {
logger.log('/create', [req.id]);
if (!(req.body.partnerid && req.body.partnerid === 'bluewallet' && req.body.accounttype)) return errorBadArguments(res);
let u = new User(redis);
let u = new User(redis, bitcoinclient, lightning);
await u.create();
await u.saveMetadata({ partnerid: req.body.partnerid, accounttype: req.body.accounttype, created_at: new Date().toISOString() });
res.send({ login: u.getLogin(), password: u.getPassword() });
@ -67,7 +67,7 @@ router.post('/auth', async function(req, res) {
logger.log('/auth', [req.id]);
if (!((req.body.login && req.body.password) || req.body.refresh_token)) return errorBadArguments(res);
let u = new User(redis);
let u = new User(redis, bitcoinclient, lightning);
if (req.body.refresh_token) {
// need to refresh token
@ -86,7 +86,7 @@ router.post('/auth', async function(req, res) {
router.post('/addinvoice', async function(req, res) {
logger.log('/addinvoice', [req.id]);
let u = new User(redis);
let u = new User(redis, bitcoinclient, lightning);
if (!(await u.loadByAuthorization(req.headers.authorization))) {
return errorBadAuth(res);
}
@ -105,7 +105,7 @@ router.post('/addinvoice', async function(req, res) {
router.post('/payinvoice', async function(req, res) {
logger.log('/payinvoice', [req.id]);
let u = new User(redis);
let u = new User(redis, bitcoinclient, lightning);
if (!(await u.loadByAuthorization(req.headers.authorization))) {
return errorBadAuth(res);
}
@ -151,7 +151,7 @@ router.post('/payinvoice', async function(req, res) {
return errorLnd(res);
}
let UserPayee = new User(redis);
let UserPayee = new User(redis, bitcoinclient, lightning);
UserPayee._userid = userid_payee; // hacky, fixme
let payee_balance = await UserPayee.getBalance();
payee_balance += info.num_satoshis * 1;
@ -247,7 +247,7 @@ router.get('/balance', async function(req, res) {
router.get('/getinfo', async function(req, res) {
logger.log('/getinfo', [req.id]);
let u = new User(redis);
let u = new User(redis, bitcoinclient, lightning);
if (!(await u.loadByAuthorization(req.headers.authorization))) {
return errorBadAuth(res);
}
@ -311,7 +311,7 @@ router.get('/getpending', async function(req, res) {
router.get('/decodeinvoice', async function(req, res) {
logger.log('/decodeinvoice', [req.id]);
let u = new User(redis, bitcoinclient);
let u = new User(redis, bitcoinclient, lightning);
if (!(await u.loadByAuthorization(req.headers.authorization))) {
return errorBadAuth(res);
}
@ -326,7 +326,7 @@ router.get('/decodeinvoice', async function(req, res) {
router.get('/checkrouteinvoice', async function(req, res) {
logger.log('/checkrouteinvoice', [req.id]);
let u = new User(redis, bitcoinclient);
let u = new User(redis, bitcoinclient, lightning);
if (!(await u.loadByAuthorization(req.headers.authorization))) {
return errorBadAuth(res);
}