diff --git a/app/lnd/index.js b/app/lnd/index.js index 5660126a..d2ba4dd8 100644 --- a/app/lnd/index.js +++ b/app/lnd/index.js @@ -1,7 +1,14 @@ import config from './config' import lightning from './lib/lightning' +import subscribe from './subscribe' import methods from './methods' const lnd = lightning(config.lightningRpc, config.lightningHost) -export default (event, msg, data) => methods(lnd, event, msg, data) +const lndSubscribe = mainWindow => subscribe(mainWindow, lnd) +const lndMethods = (event, msg, data) => methods(lnd, event, msg, data) + +export default { + lndMethods, + lndSubscribe +} diff --git a/app/lnd/subscribe/index.js b/app/lnd/subscribe/index.js new file mode 100644 index 00000000..297a815c --- /dev/null +++ b/app/lnd/subscribe/index.js @@ -0,0 +1,5 @@ +import subscribeToTransactions from './transactions' + +export default (mainWindow, lnd) => { + subscribeToTransactions(mainWindow, lnd) +} \ No newline at end of file diff --git a/app/lnd/subscribe/transactions.js b/app/lnd/subscribe/transactions.js new file mode 100644 index 00000000..4b4b22f2 --- /dev/null +++ b/app/lnd/subscribe/transactions.js @@ -0,0 +1,8 @@ +export default function subscribeToTransactions(mainWindow, lnd) { + const call = lnd.subscribeTransactions({}) + + call.on('data', transaction => mainWindow.send('newTransaction', { transaction })) + call.on('end', () => console.log('end')) + call.on('error', error => console.log('error: ', error)) + call.on('status', status => console.log('status: ', status)) +} \ No newline at end of file diff --git a/app/main.dev.js b/app/main.dev.js index ee9f3a87..8d6b5416 100644 --- a/app/main.dev.js +++ b/app/main.dev.js @@ -13,7 +13,7 @@ */ import { app, BrowserWindow, ipcMain } from 'electron' import MenuBuilder from './menu' -import lnd from './lnd' +import { lndSubscribe, lndMethods } from './lnd' let mainWindow = null; @@ -90,9 +90,12 @@ app.on('ready', async () => { const menuBuilder = new MenuBuilder(mainWindow); menuBuilder.buildMenu(); + + // Subscribe to LND events + lndSubscribe(mainWindow) + + // LND CRUD methods + ipcMain.on('lnd', (event, { msg, data }) => { + lndMethods(event, msg, data) + }) }); - -ipcMain.on('lnd', (event, { msg, data }) => { - lnd(event, msg, data) -}) - diff --git a/app/reducers/ipc.js b/app/reducers/ipc.js index c61ccd20..21a822cc 100644 --- a/app/reducers/ipc.js +++ b/app/reducers/ipc.js @@ -22,7 +22,12 @@ import { import { receivePayments, paymentSuccessful } from './payment' import { receiveInvoices, createdInvoice, receiveFormInvoice } from './invoice' import { receiveBalance } from './balance' -import { receiveTransactions, transactionSuccessful, transactionError } from './transaction' +import { + receiveTransactions, + transactionSuccessful, + transactionError, + newTransaction +} from './transaction' // Import all receiving IPC event handlers and pass them into createIpc const ipc = createIpc({ @@ -61,7 +66,8 @@ const ipc = createIpc({ receiveTransactions, transactionSuccessful, - transactionError + transactionError, + newTransaction }) export default ipc diff --git a/app/reducers/transaction.js b/app/reducers/transaction.js index f3dc7ca5..1b5e590c 100644 --- a/app/reducers/transaction.js +++ b/app/reducers/transaction.js @@ -14,6 +14,8 @@ export const SEND_TRANSACTION = 'SEND_TRANSACTION' export const TRANSACTION_SUCCESSFULL = 'TRANSACTION_SUCCESSFULL' export const TRANSACTION_FAILED = 'TRANSACTION_FAILED' +export const ADD_TRANSACTION = 'ADD_TRANSACTION' + // ------------------------------------ // Actions // ------------------------------------ @@ -48,7 +50,7 @@ export const sendCoins = ({ value, addr, currency, rate }) => (dispatch) => { // TODO: Add payment to state, not a total re-fetch export const transactionSuccessful = (event, { amount, addr, txid }) => (dispatch) => { // Get the new list of transactions (TODO dont do an entire new fetch) - fetchTransactions() + dispatch(fetchTransactions()) // Close the form modal once the payment was succesful dispatch(setForm({ modalOpen: false })) // Show successful payment state @@ -64,6 +66,11 @@ export const transactionError = () => (dispatch) => { dispatch({ type: TRANSACTION_FAILED }) } +// Listener for when a new transaction is pushed from the subscriber +export const newTransaction = (event, { transaction }) => (dispatch) => { + dispatch({ type: ADD_TRANSACTION, transaction }) +} + // ------------------------------------ // Action Handlers @@ -73,7 +80,8 @@ const ACTION_HANDLERS = { [SEND_TRANSACTION]: state => ({ ...state, sendingTransaction: true }), [RECEIVE_TRANSACTIONS]: (state, { transactions }) => ({ ...state, transactionLoading: false, transactions }), [TRANSACTION_SUCCESSFULL]: state => ({ ...state, sendingTransaction: false }), - [TRANSACTION_FAILED]: state => ({ ...state, sendingTransaction: false }) + [TRANSACTION_FAILED]: state => ({ ...state, sendingTransaction: false }), + [ADD_TRANSACTION]: (state, { transaction }) => ({ ...state, transactions: [transaction, ...state.transactions] }) } // ------------------------------------