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. 23
      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 { export default {
info, info,
peers, peers,
allChannels, 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)) .then(payments => event.sender.send('receivePayments', payments))
.catch(error => console.log('info error: ', error)) .catch(error => console.log('info error: ', error))
break 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: default:
return return
} }

15
app/reducers/balance.js

@ -1,3 +1,4 @@
import { ipcRenderer } from 'electron'
import { callApis } from '../api' import { callApis } from '../api'
// ------------------------------------ // ------------------------------------
// Constants // 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) => { export const fetchBalance = () => async (dispatch) => {
dispatch(getBalance()) dispatch(getBalance())
const balance = await callApis(['wallet_balance', 'channel_balance']) ipcRenderer.send('lnd', { msg: 'balance' })
dispatch(receiveBalance(balance))
} }
// Receive IPC event for peers
export const receiveBalance = (event, { walletBalance, channelBalance }) => dispatch => dispatch({ type: RECEIVE_BALANCE, walletBalance, channelBalance })
// ------------------------------------ // ------------------------------------
// Action Handlers // Action Handlers
// ------------------------------------ // ------------------------------------

23
app/reducers/invoice.js

@ -1,4 +1,5 @@
import { createSelector } from 'reselect' import { createSelector } from 'reselect'
import { ipcRenderer } from 'electron'
import { callApi } from '../api' import { callApi } from '../api'
import { btc, usd } from '../utils' 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() { export function sendInvoice() {
return { return {
type: SEND_INVOICE type: SEND_INVOICE
@ -101,18 +95,15 @@ export const fetchInvoice = payreq => async (dispatch) => {
return false return false
} }
// Send IPC event for invoices
export const fetchInvoices = () => async (dispatch) => { export const fetchInvoices = () => async (dispatch) => {
dispatch(getInvoice()) dispatch(getInvoices())
const invoices = await callApi('invoices') ipcRenderer.send('lnd', { msg: 'invoices' })
if (invoices) {
dispatch(receiveInvoices(invoices.data))
} else {
dispatch(invoiceFailed())
}
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) => { export const createInvoice = (amount, memo, currency, rate) => async (dispatch) => {
const value = currency === 'btc' ? btc.btcToSatoshis(amount) : btc.btcToSatoshis(usd.usdToBtc(amount, rate)) 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 { receivePeers } from './peers'
import { receiveChannels } from './channels' import { receiveChannels } from './channels'
import { receivePayments } from './payment' import { receivePayments } from './payment'
import { receiveInvoices } from './invoice'
import { receiveBalance } from './balance'
// Import all receiving IPC event handlers and pass them into createIpc // Import all receiving IPC event handlers and pass them into createIpc
const ipc = createIpc({ const ipc = createIpc({
'receiveInfo': receiveInfo, 'receiveInfo': receiveInfo,
'receivePeers': receivePeers, 'receivePeers': receivePeers,
'receiveChannels': receiveChannels, 'receiveChannels': receiveChannels,
'receivePayments': receivePayments 'receivePayments': receivePayments,
'receiveInvoices': receiveInvoices,
'receiveBalance': receiveBalance
}) })
export default ipc 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) => { export const fetchPayments = () => async (dispatch) => {
dispatch(getPayments()) dispatch(getPayments())
ipcRenderer.send('lnd', { msg: 'payments' }) 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 receivePayments = (event, { payments }) => dispatch => dispatch({ type: RECEIVE_PAYMENTS, payments })
export const payInvoice = payment_request => async (dispatch) => { export const payInvoice = payment_request => async (dispatch) => {

Loading…
Cancel
Save