Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
refactor: track payments sending by internal payment id
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfelton committed May 14, 2020
1 parent 49cea65 commit bef5f6e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 31 deletions.
2 changes: 1 addition & 1 deletion renderer/reducers/invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ const ACTION_HANDLERS = {
},
[RECEIVE_INVOICES]: (state, { invoices }) => {
state.isInvoicesLoading = false
state.invoices = uniqBy(state.invoices.concat(invoices), 'addIndex')
state.invoices = uniqBy(invoices.concat(state.invoices), 'addIndex')
},
[SEND_INVOICE]: state => {
state.isInvoicesLoading = true
Expand Down
1 change: 1 addition & 0 deletions renderer/reducers/payment/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const RECEIVE_PAYMENTS = 'RECEIVE_PAYMENTS'
export const SEND_PAYMENT = 'SEND_PAYMENT'
export const PAYMENT_SUCCESSFUL = 'PAYMENT_SUCCESSFUL'
export const PAYMENT_FAILED = 'PAYMENT_FAILED'
export const PAYMENT_COMPLETED = 'PAYMENT_COMPLETED'
export const DECREASE_PAYMENT_RETRIES = 'DECREASE_PAYMENT_RETRIES'

export const PAYMENT_STATUS_SENDING = 'sending'
Expand Down
1 change: 0 additions & 1 deletion renderer/reducers/payment/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ import { defineMessages } from 'react-intl'

/* eslint-disable max-len */
export default defineMessages({
payment_send_error: 'Unable to send payment: Invalid invoice (no payment hash)',
unknown: 'Unknown',
})
44 changes: 15 additions & 29 deletions renderer/reducers/payment/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import { grpc } from 'workers'
import { fetchBalance } from 'reducers/balance'
import { fetchChannels } from 'reducers/channels'
import { infoSelectors } from 'reducers/info'
import { showError } from 'reducers/notification'
import messages from './messages'
import { paymentsSending } from './selectors'
import { prepareKeysendPayload, prepareBolt11Payload } from './utils'
import * as constants from './constants'
Expand All @@ -24,6 +22,7 @@ const {
SEND_PAYMENT,
PAYMENT_SUCCESSFUL,
PAYMENT_FAILED,
PAYMENT_COMPLETED,
DECREASE_PAYMENT_RETRIES,
PAYMENT_STATUS_SENDING,
PAYMENT_STATUS_SUCCESSFUL,
Expand Down Expand Up @@ -75,11 +74,6 @@ const decPaymentRetry = paymentId => ({
* @returns {Function} Thunk
*/
export const sendPayment = data => dispatch => {
if (!data.paymentHash) {
dispatch(showError(getIntl().formatMessage(messages.payment_send_error)))
return
}

const payment = {
...data,
status: PAYMENT_STATUS_SENDING,
Expand All @@ -93,18 +87,14 @@ export const sendPayment = data => dispatch => {
})
}

/**
* updatePayment - Updates specified payment request.
*
* @param {string} paymentHash Payment hash
* @returns {Function} Thunk
*/
export const updatePayment = paymentHash => async dispatch => {
const { payments } = await grpc.services.Lightning.listPayments()
const payment = payments.find(p => p.paymentHash === paymentHash)
if (payment) {
dispatch(receivePayments([payment]))
}
export const paymentComplete = paymentId => async dispatch => {
const { payments } = await grpc.services.Lightning.listPayments({
maxPayments: 3,
indexOffset: 0,
reversed: true,
})
dispatch(receivePayments(payments))
dispatch({ type: PAYMENT_COMPLETED, paymentId })
}

/**
Expand All @@ -118,7 +108,7 @@ export const paymentSuccessful = ({ paymentId }) => async (dispatch, getState) =

// If we found a related entry in paymentsSending, gracefully remove it and handle as success case.
if (paymentSending) {
const { creationDate, paymentHash } = paymentSending
const { creationDate } = paymentSending

// Ensure payment stays in sending state for at least 1 second.
await delay(1000 - (Date.now() - creationDate * 1000))
Expand All @@ -129,7 +119,7 @@ export const paymentSuccessful = ({ paymentId }) => async (dispatch, getState) =
// Wait for another 3 seconds.
await delay(3000)

dispatch(updatePayment(paymentHash))
dispatch(paymentComplete(paymentId))
}

// Fetch new balance.
Expand Down Expand Up @@ -227,10 +217,6 @@ export const payInvoice = ({
} else {
dispatch(
sendPayment({
paymentHash:
route && route.isExact
? route.paymentHash.toString('hex')
: getTag(payReq, 'payment_hash'),
paymentId,
feeLimit,
value: amt,
Expand Down Expand Up @@ -305,10 +291,7 @@ const ACTION_HANDLERS = {
},
[RECEIVE_PAYMENTS]: (state, { payments }) => {
state.paymentLoading = false
state.payments = uniqBy(state.payments.concat(payments), 'paymentHash')
state.paymentsSending = state.paymentsSending.filter(
item => !payments.find(p => p.paymentHash === item.paymentHash)
)
state.payments = uniqBy(payments.concat(state.payments), 'paymentHash')
},
[SEND_PAYMENT]: (state, { payment }) => {
state.paymentsSending.push(payment)
Expand All @@ -331,6 +314,9 @@ const ACTION_HANDLERS = {
item.status = PAYMENT_STATUS_SUCCESSFUL
}
},
[PAYMENT_COMPLETED]: (state, { paymentId }) => {
state.paymentsSending = state.paymentsSending.filter(item => item.paymentId !== paymentId)
},
[PAYMENT_FAILED]: (state, { paymentId, error }) => {
const item = find(state.paymentsSending, { paymentId })
if (item) {
Expand Down

0 comments on commit bef5f6e

Please # to comment.