Skip to content

Commit

Permalink
WIP5: reset files changed in other PRs
Browse files Browse the repository at this point in the history
  • Loading branch information
parhamsaremi committed Sep 6, 2023
1 parent 1b488fb commit 6bcc45e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 32 deletions.
13 changes: 5 additions & 8 deletions jobs/cancel_orders.ts → jobs/cancel_orders.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { Telegraf } from "telegraf";
import { MainContext } from "../bot/start";

const { User, Order } = require('../models');
const { cancelShowHoldInvoice, cancelAddInvoice } = require('../bot/commands');
const messages = require('../bot/messages');
const { getUserI18nContext, holdInvoiceExpirationInSecs } = require('../util');
import logger from "../logger";
const logger = require('../logger');

const cancelOrders = async (bot: Telegraf<MainContext>) => {
const cancelOrders = async bot => {
try {
const holdInvoiceTime = new Date();
holdInvoiceTime.setSeconds(
holdInvoiceTime.getSeconds() -
Number(process.env.HOLD_INVOICE_EXPIRATION_WINDOW)
parseInt(process.env.HOLD_INVOICE_EXPIRATION_WINDOW)
);
// We get the orders where the seller didn't pay the hold invoice before expired
// or where the buyer didn't add the invoice
Expand Down Expand Up @@ -73,7 +70,7 @@ const cancelOrders = async (bot: Telegraf<MainContext>) => {
// Now we cancel orders expired
// ==============================
orderTime = new Date();
let orderExpirationTime = Number(
let orderExpirationTime = parseInt(
process.env.ORDER_PUBLISHED_EXPIRATION_WINDOW
);
orderExpirationTime = orderExpirationTime + orderExpirationTime * 0.2;
Expand All @@ -99,4 +96,4 @@ const cancelOrders = async (bot: Telegraf<MainContext>) => {
}
};

module.exports = cancelOrders;
module.exports = cancelOrders;
36 changes: 12 additions & 24 deletions ln/pay_request.ts → ln/pay_request.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,26 @@
import { AuthenticatedLnd } from "lightning";
import { Telegraf } from "telegraf";
import { MainContext } from "../bot/start";
import { IOrder } from "../models/order";
import {
const {
payViaPaymentRequest,
getPayment,
deleteForwardingReputations
} from "lightning";
deleteForwardingReputations,
} = require('lightning');
const { parsePaymentRequest } = require('invoices');
const { User, PendingPayment } = require('../models');
import { lnd } from "./connect";
const lnd = require('./connect');
const { handleReputationItems, getUserI18nContext } = require('../util');
const messages = require('../bot/messages');
import logger from "../logger";
const logger = require('../logger');

const payRequest = async ({ request, amount }: { request: string; amount: number }) => {
const payRequest = async ({ request, amount }) => {
try {
const invoice = parsePaymentRequest({ request });
if (!invoice) return false;
// If the invoice is expired we return is_expired = true
if (invoice.is_expired) return invoice;

// We need to set a max fee amount
const maxFee = amount * Number(process.env.MAX_ROUTING_FEE);
const maxFee = amount * parseFloat(process.env.MAX_ROUTING_FEE);

type Params = {
lnd: AuthenticatedLnd;
request: string;
pathfinding_timeout: number;
max_fee?: number; // optional property
tokens?: number; // optional property
};

const params: Params = {
const params = {
lnd,
request,
pathfinding_timeout: 60000,
Expand All @@ -56,7 +44,7 @@ const payRequest = async ({ request, amount }: { request: string; amount: number
}
};

const payToBuyer = async (bot: Telegraf<MainContext>, order: IOrder) => {
const payToBuyer = async (bot, order) => {
try {
// We check if the payment is on flight we don't do anything
const isPending = await isPendingPayment(order.buyer_invoice);
Expand Down Expand Up @@ -111,14 +99,14 @@ const payToBuyer = async (bot: Telegraf<MainContext>, order: IOrder) => {
}
};

const isPendingPayment = async (request: string) => {
const isPendingPayment = async request => {
try {
const { id } = parsePaymentRequest({ request });
const { is_pending } = await getPayment({ lnd, id });

return !!is_pending;
} catch (error) {
const message = String(error);
const message = error.toString();
logger.error(`isPendingPayment catch error: ${message}`);
return false;
}
Expand All @@ -128,4 +116,4 @@ module.exports = {
payRequest,
payToBuyer,
isPendingPayment,
};
};

0 comments on commit 6bcc45e

Please sign in to comment.