Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed status of orders after hold invoice expires #617

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions bot/ordersActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,32 @@ const getNewRangeOrderPayload = async order => {
}
};

const updateOrderStatus = async (orderId, newStatus) => {
try {
const order = await Order.findByIdAndUpdate(
orderId,
{ status: newStatus },
{ new: true }
);

if (order) {
OrderEvents.orderUpdated(order);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An event should not be published in Nostr with the new order status HOLD_INVOICE_EXPIRED

}

return order;
} catch (error) {
logger.error(error);
return null;
}
};




module.exports = {
createOrder,
getOrder,
getOrders,
getNewRangeOrderPayload,
updateOrderStatus,
};
40 changes: 40 additions & 0 deletions jobs/holdInvoiceMonitor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Order } from '../models';
import { checkHoldInvoiceExpiration } from '../ln/hold_invoice';
import { logger } from '../logger';
import { Document, ObjectId } from 'mongoose';

const CHECK_INTERVAL = 5 * 60 * 1000; // Check every 5 minutes

interface IOrder {
// Existing properties of Order, and adding hold_invoice_hash
hold_invoice_hash?: string;
_id: ObjectId;
// Other properties...
}

// Now, for the monitorHoldInvoices function:
const monitorHoldInvoices = async () => {
try {
// Find all orders with PAID_HOLD_INVOICE status
const orders: (IOrder & Document)[] = await Order.find({ status: 'PAID_HOLD_INVOICE' });

for (const order of orders) {
if (order.hold_invoice_hash) {
await checkHoldInvoiceExpiration({
hash: order.hold_invoice_hash,
orderId: order._id
});
}
}
} catch (error) {
logger.error('Error monitoring hold invoices:', error);
}
};

// Start monitoring process
const startMonitoring = () => {
setInterval(monitorHoldInvoices, CHECK_INTERVAL);
logger.info('Hold invoice monitoring started');
};

export { startMonitoring };
4 changes: 3 additions & 1 deletion jobs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import deleteOrders from "./delete_published_orders";
import calculateEarnings from './calculate_community_earnings'
import deleteCommunity from './communities'
import nodeInfo from './node_info'
import { startMonitoring } from "./holdInvoiceMonitor"; // Imported the startMonitoring function

export {
attemptPendingPayments,
Expand All @@ -16,4 +17,5 @@ export {
attemptCommunitiesPendingPayments,
deleteCommunity,
nodeInfo,
};
startMonitoring,
};
26 changes: 26 additions & 0 deletions ln/hold_invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { createHash, randomBytes } = require('crypto');
const lightning = require('lightning');
const lnd = require('./connect');
const { logger } = require('../logger');
const { updateOrderStatus } = require('../bot/ordersActions');

const createHoldInvoice = async ({ description, amount }) => {
try {
Expand Down Expand Up @@ -54,9 +55,34 @@ const getInvoice = async ({ hash }) => {
}
};


const checkHoldInvoiceExpiration = async ({ hash, orderId }) => {
try {
const invoice = await getInvoice({ hash });

if (invoice && invoice.is_expired) {
// Cancel the expired hold invoice
await cancelHoldInvoice({ hash });

// Update the order status
await updateOrderStatus(orderId, 'HOLD_INVOICE_EXPIRED');

logger.info(`Hold invoice ${hash} expired for order ${orderId}`);
return true;
}

return false;
} catch (error) {
logger.error(error);
return false;
}
};


module.exports = {
createHoldInvoice,
settleHoldInvoice,
cancelHoldInvoice,
getInvoice,
checkHoldInvoiceExpiration,
};