From ba202a956cc73e9eddda78318220b54b66666200 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sat, 17 Feb 2018 21:08:37 -0800 Subject: add channel balance column --- index.js | 24 +++++++++++++++++++++++- migrations/20180217065619-addChannelBalance.js | 11 +++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 migrations/20180217065619-addChannelBalance.js diff --git a/index.js b/index.js index d71327ca..44dc2dbf 100644 --- a/index.js +++ b/index.js @@ -37,7 +37,11 @@ const l = console.log io.origins('http://localhost:8085') const restClient = ba.restfulClient(process.env.BITCOINAVERAGE_PUBLIC, process.env.BITCOINAVERAGE_SECRET) - const lnrpc = await require('lnrpc')({ server: 'localhost:10001', tls: '/home/adam/.lnd.testa/tls.cert' }) + const lnrpc = await require('lnrpc')({ + server: 'localhost:10001', + tls: '/home/adam/.lnd.testa/tls.cert', + macaroon: '/home/adam/.lnd.testa/admin.macaroon', + }) const adminMacaroon = fs.readFileSync('/home/adam/.lnd.testa/admin.macaroon') const meta = new grpc.Metadata() meta.add('macaroon', adminMacaroon.toString('hex')) @@ -55,6 +59,8 @@ const l = console.log zmqSock.subscribe('rawblock') zmqSock.subscribe('rawtx') + const seen = [] + zmqSock.on('message', async (topic, message, sequence) => { const addresses = {} await db['User'].findAll({ @@ -66,6 +72,8 @@ const l = console.log switch (topic) { case 'rawtx': { + if (seen.includes(message)) return + let tx = bitcoin.Transaction.fromHex(message) let total = tx.outs.reduce((a, b) => a + b.value, 0) l(total) @@ -84,6 +92,7 @@ const l = console.log await user.save() l('HIT!') io.emit('tx', message) + seen.push(message) } } catch(e) { } }) @@ -103,6 +112,10 @@ const l = console.log res.json(await lnrpc.walletBalance({witness_only: true}, meta)) }) + app.get('/pendingchannels', async (req, res) => { + res.json(await lnrpc.pendingChannels({}, meta)) + }) + app.post('/openchannel', async (req, res) => { let user = await db['User'].findOne({ where: { @@ -121,7 +134,16 @@ const l = console.log user.balance = 0 user.channel = reverse(data.funding_txid).toString('hex') + + let list = await lnrpc.listChannels({}, meta) + let channels = list.channels.filter(c => c.channel_point.includes(user.channel)) + + if (channels.length) { + user.channelbalance = channels[0].local_balance + } + await user.save() + res.json(data) }) }) diff --git a/migrations/20180217065619-addChannelBalance.js b/migrations/20180217065619-addChannelBalance.js new file mode 100644 index 00000000..fc909f53 --- /dev/null +++ b/migrations/20180217065619-addChannelBalance.js @@ -0,0 +1,11 @@ +'use strict'; + +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.addColumn('users', 'channelbalance', { type: Sequelize.INTEGER }) + }, + + down: (queryInterface, Sequelize) => { + return queryInterface.removeColumn('users', 'channelbalance') + } +}; -- cgit v1.2.3