Browse Source

feature(lnd-ipc): ipc for invoice + balance

renovate/lint-staged-8.x
Jack Mallers 8 years ago
parent
commit
6f66a3856a
  1. 38
      app/lnd/index.js
  2. 12
      app/main.dev.js
  3. 15
      app/reducers/balance.js
  4. 21
      app/reducers/invoice.js
  5. 6
      app/reducers/ipc.js
  6. 4
      app/reducers/payment.js

38
app/lnd/index.js

@ -59,9 +59,45 @@ export function payments() {
})
}
// LND Get Invoices
export function invoices() {
return new Promise((resolve, reject) => {
lnd.listInvoices({}, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}
// LND Get Wallet Balance
const walletBalance = new Promise((resolve, reject) => {
lnd.walletBalance({}, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
// LND Get Channel Balance
const channelBalance = new Promise((resolve, reject) => {
lnd.channelBalance({}, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
// LND Get Wallet + Channel Balance
export function balance() {
return Promise.all([walletBalance, channelBalance])
}
export default {
info,
peers,
allChannels,
payments
payments,
invoices,
balance
}

12
app/main.dev.js

@ -117,6 +117,18 @@ ipcMain.on('lnd', (event, { msg, data }) => {
.then(payments => event.sender.send('receivePayments', payments))
.catch(error => console.log('info error: ', error))
break
case 'invoices':
// Data looks like { invoices: [] }
lnd.invoices()
.then(invoices => event.sender.send('receiveInvoices', invoices))
.catch(error => console.log('info error: ', error))
break
case 'balance':
// Balance looks like [ { balance: '129477456' }, { balance: '243914' } ]
lnd.balance()
.then(balance => event.sender.send('receiveBalance', { walletBalance: balance[0].balance, channelBalance: balance[1].balance }))
.catch(error => console.log('info error: ', error))
break
default:
return
}

15
app/reducers/balance.js

@ -1,3 +1,4 @@
import { ipcRenderer } from 'electron'
import { callApis } from '../api'
// ------------------------------------
// Constants
@ -14,20 +15,14 @@ export function getBalance() {
}
}
export function receiveBalance(data) {
return {
type: RECEIVE_BALANCE,
walletBalance: data[0].data.balance,
channelBalance: data[1].data.balance
}
}
export const fetchBalance = () => async (dispatch) => {
dispatch(getBalance())
const balance = await callApis(['wallet_balance', 'channel_balance'])
dispatch(receiveBalance(balance))
ipcRenderer.send('lnd', { msg: 'balance' })
}
// Receive IPC event for peers
export const receiveBalance = (event, { walletBalance, channelBalance }) => dispatch => dispatch({ type: RECEIVE_BALANCE, walletBalance, channelBalance })
// ------------------------------------
// Action Handlers
// ------------------------------------

21
app/reducers/invoice.js

@ -1,4 +1,5 @@
import { createSelector } from 'reselect'
import { ipcRenderer } from 'electron'
import { callApi } from '../api'
import { btc, usd } from '../utils'
// ------------------------------------
@ -63,13 +64,6 @@ export function getInvoices() {
}
}
export function receiveInvoices(data) {
return {
type: RECEIVE_INVOICES,
invoices: data.invoices.reverse()
}
}
export function sendInvoice() {
return {
type: SEND_INVOICE
@ -101,17 +95,14 @@ export const fetchInvoice = payreq => async (dispatch) => {
return false
}
// Send IPC event for invoices
export const fetchInvoices = () => async (dispatch) => {
dispatch(getInvoice())
const invoices = await callApi('invoices')
if (invoices) {
dispatch(receiveInvoices(invoices.data))
} else {
dispatch(invoiceFailed())
dispatch(getInvoices())
ipcRenderer.send('lnd', { msg: 'invoices' })
}
return invoices
}
// Receive IPC event for invoices
export const receiveInvoices = (event, { invoices }) => dispatch => dispatch({ type: RECEIVE_INVOICES, invoices })
export const createInvoice = (amount, memo, currency, rate) => async (dispatch) => {
const value = currency === 'btc' ? btc.btcToSatoshis(amount) : btc.btcToSatoshis(usd.usdToBtc(amount, rate))

6
app/reducers/ipc.js

@ -3,13 +3,17 @@ import { receiveInfo } from './info'
import { receivePeers } from './peers'
import { receiveChannels } from './channels'
import { receivePayments } from './payment'
import { receiveInvoices } from './invoice'
import { receiveBalance } from './balance'
// Import all receiving IPC event handlers and pass them into createIpc
const ipc = createIpc({
'receiveInfo': receiveInfo,
'receivePeers': receivePeers,
'receiveChannels': receiveChannels,
'receivePayments': receivePayments
'receivePayments': receivePayments,
'receiveInvoices': receiveInvoices,
'receiveBalance': receiveBalance
})
export default ipc

4
app/reducers/payment.js

@ -49,13 +49,13 @@ export function paymentFailed() {
}
}
// Send IPC event for peers
// Send IPC event for payments
export const fetchPayments = () => async (dispatch) => {
dispatch(getPayments())
ipcRenderer.send('lnd', { msg: 'payments' })
}
// Receive IPC event for peers
// Receive IPC event for payments
export const receivePayments = (event, { payments }) => dispatch => dispatch({ type: RECEIVE_PAYMENTS, payments })
export const payInvoice = payment_request => async (dispatch) => {

Loading…
Cancel
Save