Browse Source

Merge pull request #37 from LN-Zap/feature/subscribeinvoices

feature(subscribe invoices)
renovate/lint-staged-8.x
jackmallers 7 years ago
committed by GitHub
parent
commit
a2f68ccd22
  1. 3
      .eslintrc
  2. 4
      app/lnd/subscribe/index.js
  3. 10
      app/lnd/subscribe/invoices.js
  4. 4
      app/lnd/subscribe/transactions.js
  5. 2
      app/notifications/index.js
  6. 29
      app/reducers/invoice.js
  7. 3
      app/reducers/ipc.js
  8. 6
      app/reducers/transaction.js

3
.eslintrc

@ -22,7 +22,8 @@
"camelcase": 0,
"react/require-default-props": 0,
"max-len": ["error", 150],
"import/no-extraneous-dependencies": 0
"import/no-extraneous-dependencies": 0,
"no-new": 0
},
"plugins": [
"flowtype",

4
app/lnd/subscribe/index.js

@ -1,5 +1,7 @@
import subscribeToTransactions from './transactions'
import subscribeToInvoices from './invoices'
export default (mainWindow, lnd) => {
subscribeToTransactions(mainWindow, lnd)
}
subscribeToInvoices(mainWindow, lnd)
}

10
app/lnd/subscribe/invoices.js

@ -0,0 +1,10 @@
/* eslint-disable */
export default function subscribeToInvoices(mainWindow, lnd) {
const call = lnd.subscribeInvoices({})
call.on('data', invoice => mainWindow.send('invoiceUpdate', { invoice }))
call.on('end', () => console.log('end'))
call.on('error', error => console.log('error: ', error))
call.on('status', status => console.log('status: ', status))
}

4
app/lnd/subscribe/transactions.js

@ -1,3 +1,5 @@
/* eslint-disable */
export default function subscribeToTransactions(mainWindow, lnd) {
const call = lnd.subscribeTransactions({})
@ -5,4 +7,4 @@ export default function subscribeToTransactions(mainWindow, lnd) {
call.on('end', () => console.log('end'))
call.on('error', error => console.log('error: ', error))
call.on('status', status => console.log('status: ', status))
}
}

2
app/notifications/index.js

@ -5,4 +5,4 @@ export default {
onClick
})
}
}
}

29
app/reducers/invoice.js

@ -1,5 +1,6 @@
import { createSelector } from 'reselect'
import { ipcRenderer } from 'electron'
import { showNotification } from '../notifications'
import { btc, usd } from '../utils'
// ------------------------------------
// Constants
@ -20,6 +21,8 @@ export const INVOICE_SUCCESSFUL = 'INVOICE_SUCCESSFUL'
export const INVOICE_FAILED = 'INVOICE_FAILED'
export const UPDATE_INVOICE = 'UPDATE_INVOICE'
// ------------------------------------
// Actions
// ------------------------------------
@ -95,6 +98,17 @@ export const createInvoice = (amount, memo, currency, rate) => (dispatch) => {
// Receive IPC event for newly created invoice
export const createdInvoice = (event, invoice) => dispatch => dispatch({ type: INVOICE_SUCCESSFUL, invoice })
// Listen for invoice updates pushed from backend from subscribeToInvoices
export const invoiceUpdate = (event, { invoice }) => (dispatch) => {
dispatch({ type: UPDATE_INVOICE, invoice })
// HTML 5 desktop notification for the invoice update
const notifTitle = 'You\'ve been Zapped'
const notifBody = 'Congrats, someone just paid an invoice of yours'
showNotification(notifTitle, notifBody)
}
// ------------------------------------
// Action Handlers
// ------------------------------------
@ -116,7 +130,20 @@ const ACTION_HANDLERS = {
[INVOICE_SUCCESSFUL]: (state, { invoice }) => (
{ ...state, invoiceLoading: false, invoices: [invoice, ...state.invoices] }
),
[INVOICE_FAILED]: state => ({ ...state, invoiceLoading: false, data: null })
[INVOICE_FAILED]: state => ({ ...state, invoiceLoading: false, data: null }),
[UPDATE_INVOICE]: (state, action) => {
const updatedInvoices = state.invoices.map((invoice) => {
if (invoice.r_hash.toString('hex') !== action.invoice.r_hash.toString('hex')) { return invoice }
return {
...invoice,
...action.invoice
}
})
return { ...state, invoices: updatedInvoices }
}
}
const invoiceSelectors = {}

3
app/reducers/ipc.js

@ -20,7 +20,7 @@ import {
} from './channels'
import { receivePayments, paymentSuccessful } from './payment'
import { receiveInvoices, createdInvoice, receiveFormInvoice } from './invoice'
import { receiveInvoices, createdInvoice, receiveFormInvoice, invoiceUpdate } from './invoice'
import { receiveBalance } from './balance'
import {
receiveTransactions,
@ -42,6 +42,7 @@ const ipc = createIpc({
receiveInvoices,
receiveInvoice: receiveFormInvoice,
createdInvoice,
invoiceUpdate,
receiveBalance,

6
app/reducers/transaction.js

@ -72,9 +72,9 @@ export const newTransaction = (event, { transaction }) => (dispatch) => {
dispatch({ type: ADD_TRANSACTION, transaction })
// HTML 5 desktop notification for the new transaction
const notifTitle = transaction.amount > 0 ? `On-chain Transaction Received!` : `On-chain Transaction Sent!`
const notifBody = transaction.amount > 0 ? `Lucky you, you just received a new on-chain transaction. I'm jealous.` : `Hate to see 'em go but love to watch 'em leave. Your on-chain transaction successfully sent.`
const notifTitle = transaction.amount > 0 ? 'On-chain Transaction Received!' : 'On-chain Transaction Sent!'
const notifBody = transaction.amount > 0 ? 'Lucky you, you just received a new on-chain transaction. I\'m jealous.' : 'Hate to see \'em go but love to watch \'em leave. Your on-chain transaction successfully sent.' // eslint-disable-line
showNotification(notifTitle, notifBody)
}

Loading…
Cancel
Save