Browse Source

feature(lnd-ipc): fetch payments with IPC

renovate/lint-staged-8.x
Jack Mallers 7 years ago
parent
commit
f9d94eb999
  1. 40
      app/lnd/index.js
  2. 13
      app/main.dev.js
  3. 16
      app/reducers/channels.js
  4. 6
      app/reducers/ipc.js
  5. 22
      app/reducers/payment.js

40
app/lnd/index.js

@ -3,6 +3,7 @@ import lightning from './lib/lightning'
const lnd = lightning(config.lightningRpc, config.lightningHost)
// LND Get Info
export function info() {
return new Promise((resolve, reject) => {
lnd.getInfo({}, (err, data) => {
@ -13,6 +14,7 @@ export function info() {
})
}
// LND List Peers
export function peers() {
return new Promise((resolve, reject) => {
lnd.listPeers({}, (err, data) => {
@ -23,7 +25,43 @@ export function peers() {
})
}
// LND List Channels
const channels = new Promise((resolve, reject) => {
lnd.listChannels({}, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
// LND List Pending Channels
const pendingChannels = new Promise((resolve, reject) => {
lnd.pendingChannels({}, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
// LND Get All Channels
export function allChannels() {
return Promise.all([channels, pendingChannels])
}
// LND Get Payments
export function payments() {
return new Promise((resolve, reject) => {
lnd.listPayments({}, (err, data) => {
if (err) { reject(err) }
resolve(data)
})
})
}
export default {
info,
peers
peers,
allChannels,
payments
}

13
app/main.dev.js

@ -100,10 +100,23 @@ ipcMain.on('lnd', (event, { msg, data }) => {
.catch(error => console.log('info error: ', error))
break
case 'peers':
// Data looks like { peers: [] }
lnd.peers()
.then(peers => event.sender.send('receivePeers', peers))
.catch(error => console.log('info error: ', error))
break
case 'channels':
// Data looks like [ { channels: [channel, channel, channel] }, { total_limbo_balance: 0, pending_open_channels: [], pending_closing_channels: [], pending_force_closing_channels: [] } ]
lnd.allChannels()
.then(data => event.sender.send('receiveChannels', { channels: data[0].channels, pendingChannels: data[1] }))
.catch(error => console.log('info error: ', error))
break
case 'payments':
// Data looks like { payments: [] }
lnd.payments()
.then(payments => event.sender.send('receivePayments', payments))
.catch(error => console.log('info error: ', error))
break
default:
return
}

16
app/reducers/channels.js

@ -1,4 +1,5 @@
import { createSelector } from 'reselect'
import { ipcRenderer } from 'electron'
import { callApi, callApis } from '../api'
// ------------------------------------
// Constants
@ -38,14 +39,6 @@ export function getChannels() {
}
}
export function receiveChannels(channels) {
return {
type: RECEIVE_CHANNELS,
channels: channels[0].data.channels,
pendingChannels: channels[1].data
}
}
export function openingChannel() {
return {
type: OPENING_CHANNEL
@ -64,12 +57,15 @@ export function openingFailure() {
}
}
// Send IPC event for peers
export const fetchChannels = () => async (dispatch) => {
dispatch(getChannels())
const channels = await callApis(['channels', 'pending_channels'])
dispatch(receiveChannels(channels))
ipcRenderer.send('lnd', { msg: 'channels' })
}
// Receive IPC event for channels
export const receiveChannels = (event, { channels, pendingChannels }) => dispatch => dispatch({ type: RECEIVE_CHANNELS, channels, pendingChannels })
export const openChannel = ({ pubkey, localamt, pushamt }) => async (dispatch) => {
const payload = { pubkey, localamt, pushamt }
dispatch(openingChannel())

6
app/reducers/ipc.js

@ -1,11 +1,15 @@
import createIpc from 'redux-electron-ipc'
import { receiveInfo } from './info'
import { receivePeers } from './peers'
import { receiveChannels } from './channels'
import { receivePayments } from './payment'
// Import all receiving IPC event handlers and pass them into createIpc
const ipc = createIpc({
'receiveInfo': receiveInfo,
'receivePeers': receivePeers
'receivePeers': receivePeers,
'receiveChannels': receiveChannels,
'receivePayments': receivePayments
})
export default ipc

22
app/reducers/payment.js

@ -1,4 +1,5 @@
import { createSelector } from 'reselect'
import { ipcRenderer } from 'electron'
import { callApi } from '../api'
// ------------------------------------
@ -29,13 +30,6 @@ export function getPayments() {
}
}
export function receivePayments(data) {
return {
type: RECEIVE_PAYMENTS,
payments: data.payments.reverse()
}
}
export function sendPayment() {
return {
type: SEND_PAYMENT
@ -55,19 +49,15 @@ export function paymentFailed() {
}
}
// Send IPC event for peers
export const fetchPayments = () => async (dispatch) => {
dispatch(getPayments())
const payments = await callApi('payments')
if (payments) {
dispatch(receivePayments(payments.data))
} else {
dispatch(paymentFailed())
}
return payments
ipcRenderer.send('lnd', { msg: 'payments' })
}
// Receive IPC event for peers
export const receivePayments = (event, { payments }) => dispatch => dispatch({ type: RECEIVE_PAYMENTS, payments })
export const payInvoice = payment_request => async (dispatch) => {
dispatch(sendPayment())
const payment = await callApi('sendpayment', 'post', { payment_request })

Loading…
Cancel
Save