use new wrapper, rearrange exports
This commit is contained in:
parent
a72dd72280
commit
b9d81fc910
1 changed files with 33 additions and 64 deletions
|
|
@ -8,7 +8,11 @@ const { Constants, Schema } = Common
|
||||||
|
|
||||||
const { ErrorCode } = Constants
|
const { ErrorCode } = Constants
|
||||||
|
|
||||||
const LightningServices = require('../../../utils/lightningServices')
|
const { sendPaymentV2Invoice } = require('../../../utils/lightningServices/v2')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {import('../../../utils/lightningServices/types').PaymentV2} PaymentV2
|
||||||
|
*/
|
||||||
|
|
||||||
const Getters = require('./getters')
|
const Getters = require('./getters')
|
||||||
const Key = require('./key')
|
const Key = require('./key')
|
||||||
|
|
@ -903,20 +907,11 @@ const sendHRWithInitialMsg = async (
|
||||||
* @param {number} amount
|
* @param {number} amount
|
||||||
* @param {string} memo
|
* @param {string} memo
|
||||||
* @param {number} feeLimit
|
* @param {number} feeLimit
|
||||||
* @param {number=} maxParts
|
|
||||||
* @param {number=} timeoutSeconds
|
|
||||||
* @throws {Error} If no response in less than 20 seconds from the recipient, or
|
* @throws {Error} If no response in less than 20 seconds from the recipient, or
|
||||||
* lightning cannot find a route for the payment.
|
* lightning cannot find a route for the payment.
|
||||||
* @returns {Promise<string>} The payment's preimage.
|
* @returns {Promise<PaymentV2>} The payment's preimage.
|
||||||
*/
|
*/
|
||||||
const sendPayment = async (
|
const sendSpontaneousPayment = async (to, amount, memo, feeLimit) => {
|
||||||
to,
|
|
||||||
amount,
|
|
||||||
memo,
|
|
||||||
feeLimit,
|
|
||||||
maxParts = 3,
|
|
||||||
timeoutSeconds = 5
|
|
||||||
) => {
|
|
||||||
try {
|
try {
|
||||||
const SEA = require('../Mediator').mySEA
|
const SEA = require('../Mediator').mySEA
|
||||||
const getUser = () => require('../Mediator').getUser()
|
const getUser = () => require('../Mediator').getUser()
|
||||||
|
|
@ -1046,69 +1041,27 @@ const sendPayment = async (
|
||||||
throw new Error(orderResponse.response)
|
throw new Error(orderResponse.response)
|
||||||
}
|
}
|
||||||
|
|
||||||
const {
|
|
||||||
services: { router }
|
|
||||||
} = LightningServices
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {object} SendErr
|
|
||||||
* @prop {string} details
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Partial
|
|
||||||
* https://api.lightning.community/#sendpaymentv2
|
|
||||||
* @typedef {object} SendResponse
|
|
||||||
* @prop {string} failure_reason
|
|
||||||
* @prop {string} payment_preimage
|
|
||||||
*/
|
|
||||||
|
|
||||||
logger.info('Will now send payment through lightning')
|
logger.info('Will now send payment through lightning')
|
||||||
|
|
||||||
const sendPaymentV2Args = {
|
const payment = await sendPaymentV2Invoice({
|
||||||
/** @type {string} */
|
feeLimit: feeLimit.toString(),
|
||||||
payment_request: orderResponse.response,
|
payment_request: orderResponse.response
|
||||||
max_parts: maxParts,
|
|
||||||
timeout_seconds: timeoutSeconds,
|
|
||||||
no_inflight_updates: true,
|
|
||||||
fee_limit_sat: feeLimit
|
|
||||||
}
|
|
||||||
|
|
||||||
const preimage = await new Promise((res, rej) => {
|
|
||||||
const sentPaymentStream = router.sendPaymentV2(sendPaymentV2Args)
|
|
||||||
/**
|
|
||||||
* @param {SendResponse} response
|
|
||||||
*/
|
|
||||||
const dataCB = response => {
|
|
||||||
logger.info('SendPayment Data:', response)
|
|
||||||
if (response.failure_reason !== 'FAILURE_REASON_NONE') {
|
|
||||||
rej(new Error(response.failure_reason))
|
|
||||||
} else {
|
|
||||||
res(response.payment_preimage)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sentPaymentStream.on('data', dataCB)
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param {SendErr} err
|
|
||||||
*/
|
|
||||||
const errCB = err => {
|
|
||||||
logger.error('SendPayment Error:', err)
|
|
||||||
rej(err.details)
|
|
||||||
}
|
|
||||||
sentPaymentStream.on('error', errCB)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
if (Utils.successfulHandshakeAlreadyExists(to)) {
|
if (Utils.successfulHandshakeAlreadyExists(to)) {
|
||||||
await sendMessage(
|
await sendMessage(
|
||||||
to,
|
to,
|
||||||
Schema.encodeSpontaneousPayment(amount, memo || 'no memo', preimage),
|
Schema.encodeSpontaneousPayment(
|
||||||
|
amount,
|
||||||
|
memo || 'no memo',
|
||||||
|
payment.payment_preimage
|
||||||
|
),
|
||||||
require('../Mediator').getUser(),
|
require('../Mediator').getUser(),
|
||||||
require('../Mediator').mySEA
|
require('../Mediator').mySEA
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return preimage
|
return payment
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.error('Error inside sendPayment()')
|
logger.error('Error inside sendPayment()')
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
|
|
@ -1116,6 +1069,21 @@ const sendPayment = async (
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the preimage corresponding to the payment.
|
||||||
|
* @param {string} to
|
||||||
|
* @param {number} amount
|
||||||
|
* @param {string} memo
|
||||||
|
* @param {number} feeLimit
|
||||||
|
* @throws {Error} If no response in less than 20 seconds from the recipient, or
|
||||||
|
* lightning cannot find a route for the payment.
|
||||||
|
* @returns {Promise<string>} The payment's preimage.
|
||||||
|
*/
|
||||||
|
const sendPayment = async (to, amount, memo, feeLimit) => {
|
||||||
|
const payment = await sendSpontaneousPayment(to, amount, memo, feeLimit)
|
||||||
|
return payment.payment_preimage
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {UserGUNNode} user
|
* @param {UserGUNNode} user
|
||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
|
|
@ -1632,5 +1600,6 @@ module.exports = {
|
||||||
follow,
|
follow,
|
||||||
unfollow,
|
unfollow,
|
||||||
initWall,
|
initWall,
|
||||||
sendMessageNew
|
sendMessageNew,
|
||||||
|
sendSpontaneousPayment
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue