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

Commit

Permalink
refactor: extract lnd version check to hasRouterSupport selector
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfelton committed Mar 29, 2020
1 parent 20174fc commit 2ae38c4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
12 changes: 12 additions & 0 deletions renderer/reducers/info.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createSelector } from 'reselect'
import semver from 'semver'
import get from 'lodash/get'
import createReducer from '@zap/utils/createReducer'
import { networks } from '@zap/utils/crypto'
Expand Down Expand Up @@ -231,6 +232,17 @@ infoSelectors.commitString = createSelector(infoSelectors.version, version => {
return commitString ? commitString.replace('commit=', '') : undefined
})

// Check wether node has support for Router service
infoSelectors.hasRouterSupport = createSelector(
infoSelectors.grpcProtoVersion,
version => {
if (!version) {
return false
}
return semver.gte(version, '0.7.1-beta', { includePrerelease: true })
}
)

// Get the node pubkey. If not set, try to extract it from the node uri.
infoSelectors.nodePubkey = createSelector(
infoSelectors.nodeUri,
Expand Down
6 changes: 2 additions & 4 deletions renderer/reducers/pay.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import get from 'lodash/get'
import { createSelector } from 'reselect'
import { send } from 'redux-electron-ipc'
import semver from 'semver'
import { grpc } from 'workers'
import { getIntl } from '@zap/i18n'
import { convert } from '@zap/utils/btc'
Expand Down Expand Up @@ -223,9 +222,8 @@ export const queryRoutes = (pubKey, amount) => async (dispatch, getState) => {
try {
let routes = []

// For lnd 0.7.1-beta and later, use payment probing.
const lndVersion = infoSelectors.grpcProtoVersion(getState())
if (semver.gte(lndVersion, '0.7.1-beta', { includePrerelease: true })) {
// Use payment probing if lnd version supports the Router service.
if (infoSelectors.hasRouterSupport(getState())) {
// First probe with a small amount for check for a valid route.
await grpc.services.Router.probePayment(pubKey, 1)
// Then probe with the full amount to check whether the channels contain enough balance for the payment.
Expand Down
6 changes: 2 additions & 4 deletions renderer/reducers/payment.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import config from 'config'
import { randomBytes, createHash } from 'crypto'
import { createSelector } from 'reselect'
import semver from 'semver'
import uniqBy from 'lodash/uniqBy'
import find from 'lodash/find'
import createReducer from '@zap/utils/createReducer'
Expand Down Expand Up @@ -270,9 +269,8 @@ export const payInvoice = ({ payReq, amt, feeLimit, retries = 0, originalPayment
try {
let data

// For lnd 0.7.1-beta and later, use the Router API.
const lndVersion = infoSelectors.grpcProtoVersion(getState())
if (semver.gte(lndVersion, '0.7.1-beta', { includePrerelease: true })) {
// Use Router service if lnd version supports it.
if (infoSelectors.hasRouterSupport(getState())()) {
data = await grpc.services.Router.sendPayment({
...payload,
timeoutSeconds: PAYMENT_TIMEOUT,
Expand Down

0 comments on commit 2ae38c4

Please sign in to comment.