Minor performance fixes and refactoring
This commit is contained in:
parent
df7c29e0e6
commit
a1b55ff65c
1 changed files with 34 additions and 23 deletions
|
|
@ -30,8 +30,35 @@ const ordersProcessed = new Set()
|
||||||
* @typedef {import('../SimpleGUN').UserGUNNode} UserGUNNode
|
* @typedef {import('../SimpleGUN').UserGUNNode} UserGUNNode
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {object} InvoiceRequest
|
||||||
|
* @prop {number} expiry
|
||||||
|
* @prop {string} memo
|
||||||
|
* @prop {number} value
|
||||||
|
* @prop {boolean} private
|
||||||
|
*/
|
||||||
|
|
||||||
let currentOrderAddr = ''
|
let currentOrderAddr = ''
|
||||||
|
|
||||||
|
/** @param {InvoiceRequest} invoiceReq */
|
||||||
|
const _addInvoice = invoiceReq =>
|
||||||
|
new Promise((resolve, rej) => {
|
||||||
|
const {
|
||||||
|
services: { lightning }
|
||||||
|
} = LightningServices
|
||||||
|
|
||||||
|
lightning.addInvoice(invoiceReq, (
|
||||||
|
/** @type {any} */ error,
|
||||||
|
/** @type {{ payment_request: string }} */ response
|
||||||
|
) => {
|
||||||
|
if (error) {
|
||||||
|
rej(error)
|
||||||
|
} else {
|
||||||
|
resolve(response.payment_request)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} addr
|
* @param {string} addr
|
||||||
* @param {ISEA} SEA
|
* @param {ISEA} SEA
|
||||||
|
|
@ -66,12 +93,12 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
||||||
)} -- addr: ${addr}`
|
)} -- addr: ${addr}`
|
||||||
)
|
)
|
||||||
|
|
||||||
const alreadyAnswered = getUser()
|
const alreadyAnswered = await getUser()
|
||||||
.get(Key.ORDER_TO_RESPONSE)
|
.get(Key.ORDER_TO_RESPONSE)
|
||||||
.get(orderID)
|
.get(orderID)
|
||||||
.then()
|
.then()
|
||||||
|
|
||||||
if (await alreadyAnswered) {
|
if (alreadyAnswered) {
|
||||||
logger.info('this order is already answered, quitting')
|
logger.info('this order is already answered, quitting')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -79,7 +106,10 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
||||||
const senderEpub = await Utils.pubToEpub(order.from)
|
const senderEpub = await Utils.pubToEpub(order.from)
|
||||||
const secret = await SEA.secret(senderEpub, getUser()._.sea)
|
const secret = await SEA.secret(senderEpub, getUser()._.sea)
|
||||||
|
|
||||||
const decryptedAmount = await SEA.decrypt(order.amount, secret)
|
const [decryptedAmount, memo] = await Promise.all([
|
||||||
|
SEA.decrypt(order.amount, secret),
|
||||||
|
SEA.decrypt(order.memo, secret)
|
||||||
|
])
|
||||||
|
|
||||||
const amount = Number(decryptedAmount)
|
const amount = Number(decryptedAmount)
|
||||||
|
|
||||||
|
|
@ -101,8 +131,6 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const memo = await SEA.decrypt(order.memo, secret)
|
|
||||||
|
|
||||||
const invoiceReq = {
|
const invoiceReq = {
|
||||||
expiry: 36000,
|
expiry: 36000,
|
||||||
memo,
|
memo,
|
||||||
|
|
@ -117,22 +145,7 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
||||||
/**
|
/**
|
||||||
* @type {string}
|
* @type {string}
|
||||||
*/
|
*/
|
||||||
const invoice = await new Promise((resolve, rej) => {
|
const invoice = await _addInvoice(invoiceReq)
|
||||||
const {
|
|
||||||
services: { lightning }
|
|
||||||
} = LightningServices
|
|
||||||
|
|
||||||
lightning.addInvoice(invoiceReq, (
|
|
||||||
/** @type {any} */ error,
|
|
||||||
/** @type {{ payment_request: string }} */ response
|
|
||||||
) => {
|
|
||||||
if (error) {
|
|
||||||
rej(error)
|
|
||||||
} else {
|
|
||||||
resolve(response.payment_request)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
'onOrders() -> Successfully created the invoice, will now encrypt it'
|
'onOrders() -> Successfully created the invoice, will now encrypt it'
|
||||||
|
|
@ -154,7 +167,6 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
||||||
getUser()
|
getUser()
|
||||||
.get(Key.ORDER_TO_RESPONSE)
|
.get(Key.ORDER_TO_RESPONSE)
|
||||||
.get(orderID)
|
.get(orderID)
|
||||||
//@ts-ignore
|
|
||||||
.put(orderResponse, ack => {
|
.put(orderResponse, ack => {
|
||||||
if (ack.err && typeof ack.err !== 'number') {
|
if (ack.err && typeof ack.err !== 'number') {
|
||||||
rej(
|
rej(
|
||||||
|
|
@ -184,7 +196,6 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
||||||
getUser()
|
getUser()
|
||||||
.get(Key.ORDER_TO_RESPONSE)
|
.get(Key.ORDER_TO_RESPONSE)
|
||||||
.get(orderID)
|
.get(orderID)
|
||||||
//@ts-ignore
|
|
||||||
.put(orderResponse, ack => {
|
.put(orderResponse, ack => {
|
||||||
if (ack.err && typeof ack.err !== 'number') {
|
if (ack.err && typeof ack.err !== 'number') {
|
||||||
logger.error(
|
logger.error(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue