Browse Source

feature(subscribe tx): setup subscribe folder and subscribe/update when new tx is detected

renovate/lint-staged-8.x
Jack Mallers 7 years ago
parent
commit
b1dfb55ec2
  1. 9
      app/lnd/index.js
  2. 5
      app/lnd/subscribe/index.js
  3. 8
      app/lnd/subscribe/transactions.js
  4. 15
      app/main.dev.js
  5. 10
      app/reducers/ipc.js
  6. 12
      app/reducers/transaction.js

9
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
}

5
app/lnd/subscribe/index.js

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

8
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))
}

15
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)
})

10
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

12
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] })
}
// ------------------------------------

Loading…
Cancel
Save