FIX: accounting for txs
This commit is contained in:
		
							parent
							
								
									967662be6b
								
							
						
					
					
						commit
						bd1128ddc8
					
				@ -6,9 +6,10 @@ export class User {
 | 
			
		||||
   *
 | 
			
		||||
   * @param {Redis} redis
 | 
			
		||||
   */
 | 
			
		||||
  constructor(redis, bitcoindrpc) {
 | 
			
		||||
  constructor(redis, bitcoindrpc, lightning) {
 | 
			
		||||
    this._redis = redis;
 | 
			
		||||
    this._bitcoindrpc = bitcoindrpc;
 | 
			
		||||
    this._lightning = lightning;
 | 
			
		||||
    this._userid = false;
 | 
			
		||||
    this._login = false;
 | 
			
		||||
    this._password = false;
 | 
			
		||||
@ -87,6 +88,23 @@ export class User {
 | 
			
		||||
    return await this._redis.get('bitcoin_address_for_' + this._userid);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Asks LND for new address, and imports it to bitcoind
 | 
			
		||||
   *
 | 
			
		||||
   * @returns {Promise<any>}
 | 
			
		||||
   */
 | 
			
		||||
  async generateAddress() {
 | 
			
		||||
    let self = this;
 | 
			
		||||
    return new Promise(function(resolve, reject) {
 | 
			
		||||
      self._lightning.newAddress({ type: 0 }, async function(err, response) {
 | 
			
		||||
        if (err) return reject('LND failure');
 | 
			
		||||
        await self.addAddress(response.address);
 | 
			
		||||
        self._bitcoindrpc.request('importaddress', [response.address, response.address, false]);
 | 
			
		||||
        resolve();
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  async getBalance() {
 | 
			
		||||
    return (await this._redis.get('balance_for_' + this._userid)) * 1;
 | 
			
		||||
  }
 | 
			
		||||
@ -110,6 +128,7 @@ export class User {
 | 
			
		||||
   */
 | 
			
		||||
  async getTxs() {
 | 
			
		||||
    let addr = await this.getAddress();
 | 
			
		||||
    if (!addr) throw new Error('cannot get transactions: no onchain address assigned to user');
 | 
			
		||||
    let txs = await this._bitcoindrpc.request('listtransactions', [addr, 100500, 0, true]);
 | 
			
		||||
    txs = txs.result;
 | 
			
		||||
    let result = [];
 | 
			
		||||
@ -141,6 +160,7 @@ export class User {
 | 
			
		||||
   */
 | 
			
		||||
  async getPendingTxs() {
 | 
			
		||||
    let addr = await this.getAddress();
 | 
			
		||||
    if (!addr) throw new Error('cannot get transactions: no onchain address assigned to user');
 | 
			
		||||
    let txs = await this._bitcoindrpc.request('listtransactions', [addr, 100500, 0, true]);
 | 
			
		||||
    txs = txs.result;
 | 
			
		||||
    let result = [];
 | 
			
		||||
@ -179,7 +199,6 @@ export class User {
 | 
			
		||||
   */
 | 
			
		||||
  async accountForPosibleTxids() {
 | 
			
		||||
    let imported_txids = await this._redis.lrange('imported_txids_for_' + this._userid, 0, -1);
 | 
			
		||||
    console.log(':::::::::::imported_txids', imported_txids);
 | 
			
		||||
    let onchain_txs = await this.getTxs();
 | 
			
		||||
    for (let tx of onchain_txs) {
 | 
			
		||||
      if (tx.type !== 'bitcoind_tx') continue;
 | 
			
		||||
 | 
			
		||||
@ -10,7 +10,7 @@ var Redis = require('ioredis');
 | 
			
		||||
var redis = new Redis(config.redis);
 | 
			
		||||
redis.monitor(function(err, monitor) {
 | 
			
		||||
  monitor.on('monitor', function(time, args, source, database) {
 | 
			
		||||
    console.log('REDIS', args);
 | 
			
		||||
    console.log('REDIS', JSON.stringify(args));
 | 
			
		||||
  });
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
@ -153,7 +153,7 @@ router.post('/payinvoice', async function(req, res) {
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
router.get('/getbtc', async function(req, res) {
 | 
			
		||||
  let u = new User(redis);
 | 
			
		||||
  let u = new User(redis, bitcoinclient, lightning);
 | 
			
		||||
  await u.loadByAuthorization(req.headers.authorization);
 | 
			
		||||
 | 
			
		||||
  if (!u.getUserId()) {
 | 
			
		||||
@ -162,23 +162,20 @@ router.get('/getbtc', async function(req, res) {
 | 
			
		||||
 | 
			
		||||
  let address = await u.getAddress();
 | 
			
		||||
  if (!address) {
 | 
			
		||||
    lightning.newAddress({ type: 0 }, async function(err, response) {
 | 
			
		||||
      if (err) return errorLnd(res);
 | 
			
		||||
      await u.addAddress(response.address);
 | 
			
		||||
      res.send([{ address: response.address }]);
 | 
			
		||||
      bitcoinclient.request('importaddress', [response.address, response.address, false]);
 | 
			
		||||
    });
 | 
			
		||||
  } else {
 | 
			
		||||
    res.send([{ address }]);
 | 
			
		||||
    bitcoinclient.request('importaddress', [address, address, false]);
 | 
			
		||||
    await u.generateAddress();
 | 
			
		||||
    address = await u.getAddress();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  res.send([{ address }]);
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
router.get('/balance', async function(req, res) {
 | 
			
		||||
  let u = new User(redis, bitcoinclient);
 | 
			
		||||
  let u = new User(redis, bitcoinclient, lightning);
 | 
			
		||||
  if (!(await u.loadByAuthorization(req.headers.authorization))) {
 | 
			
		||||
    return errorBadAuth(res);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (!(await u.getAddress())) await u.generateAddress(); // onchain address needed further
 | 
			
		||||
  await u.accountForPosibleTxids();
 | 
			
		||||
  let balance = await u.getBalance();
 | 
			
		||||
  res.send({ BTC: { AvailableBalance: balance } });
 | 
			
		||||
@ -197,22 +194,24 @@ router.get('/getinfo', async function(req, res) {
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
router.get('/gettxs', async function(req, res) {
 | 
			
		||||
  let u = new User(redis, bitcoinclient);
 | 
			
		||||
  let u = new User(redis, bitcoinclient, lightning);
 | 
			
		||||
  if (!(await u.loadByAuthorization(req.headers.authorization))) {
 | 
			
		||||
    return errorBadAuth(res);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (!(await u.getAddress())) await u.generateAddress(); // onchain addr needed further
 | 
			
		||||
  await u.accountForPosibleTxids();
 | 
			
		||||
  let txs = await u.getTxs();
 | 
			
		||||
  res.send(txs);
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
router.get('/getpending', async function(req, res) {
 | 
			
		||||
  let u = new User(redis, bitcoinclient);
 | 
			
		||||
  let u = new User(redis, bitcoinclient, lightning);
 | 
			
		||||
  if (!(await u.loadByAuthorization(req.headers.authorization))) {
 | 
			
		||||
    return errorBadAuth(res);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (!(await u.getAddress())) await u.generateAddress(); // onchain address needed further
 | 
			
		||||
  await u.accountForPosibleTxids();
 | 
			
		||||
  let txs = await u.getPendingTxs();
 | 
			
		||||
  res.send(txs);
 | 
			
		||||
 | 
			
		||||
@ -7,7 +7,7 @@
 | 
			
		||||
    "test": "echo \"Error: no test specified\" && exit 1",
 | 
			
		||||
    "dev": "nodemon node_modules/.bin/babel-node index.js",
 | 
			
		||||
    "start": "node_modules/.bin/babel-node index.js",
 | 
			
		||||
    "lint": "./node_modules/.bin/eslint ./ --fix"
 | 
			
		||||
    "lint": "./node_modules/.bin/eslint ./ controllers/ class/ --fix"
 | 
			
		||||
  },
 | 
			
		||||
  "author": "Igor Korsakov <overtorment@gmail.com>",
 | 
			
		||||
  "license": "MIT",
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user