diff --git a/app/lnd/index.js b/app/lnd/index.js index 944cfe3e..7cd730b4 100644 --- a/app/lnd/index.js +++ b/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 } diff --git a/app/main.dev.js b/app/main.dev.js index 70c20b43..98fa93ef 100644 --- a/app/main.dev.js +++ b/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 } diff --git a/app/reducers/balance.js b/app/reducers/balance.js index 5958f239..8609f2b3 100644 --- a/app/reducers/balance.js +++ b/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 // ------------------------------------ diff --git a/app/reducers/invoice.js b/app/reducers/invoice.js index bed8e818..60988dac 100644 --- a/app/reducers/invoice.js +++ b/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,18 +95,15 @@ 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()) - } - - return invoices + dispatch(getInvoices()) + ipcRenderer.send('lnd', { msg: '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)) diff --git a/app/reducers/ipc.js b/app/reducers/ipc.js index d750ce7f..98645466 100644 --- a/app/reducers/ipc.js +++ b/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 \ No newline at end of file diff --git a/app/reducers/payment.js b/app/reducers/payment.js index 3d8a3f1e..107ed19f 100644 --- a/app/reducers/payment.js +++ b/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) => {