Skip to content

Commit

Permalink
WIP3
Browse files Browse the repository at this point in the history
  • Loading branch information
Mersho committed Sep 6, 2023
1 parent 98fe186 commit 6c19fda
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 18 deletions.
4 changes: 2 additions & 2 deletions app.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as dotenv from "dotenv";
dotenv.config()
const SocksProxyAgent = require('socks-proxy-agent')
import { SocksProxyAgent } from "socks-proxy-agent";
import { start } from "./bot/start";
import mongoConnect from './db_connect'
const { resubscribeInvoices } = require('./ln');
const logger = require('./logger');
import logger from "./logger";
const { delay } = require('./util');

(async () => {
Expand Down
2 changes: 1 addition & 1 deletion bot/messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { TelegramError } = require('telegraf');
import { TelegramError } from 'telegraf'
const QR = require('qrcode');
const {
getCurrency,
Expand Down
6 changes: 3 additions & 3 deletions bot/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
throw new Error(ctxUpdateAssertMsg);
}
const params = ctx.update.message.text.split(' ');
const [command, orderId] = params.filter((el) => el);
const [command, orderId] = params.filter((el: string) => el);

if (!orderId) {
const orders = await askForConfirmation(ctx.user, command);
Expand Down Expand Up @@ -357,7 +357,7 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
throw new Error(ctxUpdateAssertMsg);
}
const params = ctx.update.message.text.split(' ');
const [command, orderId] = params.filter((el) => el);
const [command, orderId] = params.filter((el: string) => el);

if (!orderId) {
const orders = await askForConfirmation(ctx.user, command);
Expand Down Expand Up @@ -525,7 +525,7 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
throw new Error(ctxUpdateAssertMsg);
}
const params = ctx.update.message.text.split(' ');
const [command, orderId] = params.filter((el) => el);
const [command, orderId] = params.filter((el: string) => el);

if (!orderId) {
const orders = await askForConfirmation(ctx.user, command);
Expand Down
2 changes: 1 addition & 1 deletion db_connect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mongoose from "mongoose";
const logger = require('./logger');
import logger from "./logger";

// connect to database
const credentials = process.env.DB_USER
Expand Down
34 changes: 23 additions & 11 deletions ln/pay_request.js → ln/pay_request.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
const {
import { AuthenticatedLnd } from "lightning";
import { Telegraf } from "telegraf";
import { MainContext } from "../bot/start";
import { IOrder } from "../models/order";
import {
payViaPaymentRequest,
getPayment,
deleteForwardingReputations,
} = require('lightning');
deleteForwardingReputations
} from "lightning";
const { parsePaymentRequest } = require('invoices');
const { User, PendingPayment } = require('../models');
const lnd = require('./connect');
import { lnd } from "./connect";
const { handleReputationItems, getUserI18nContext } = require('../util');
const messages = require('../bot/messages');
const logger = require('../logger');
import logger from "../logger";

const payRequest = async ({ request, amount }) => {
const payRequest = async ({ request, amount }: { request: string; amount: number }) => {
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 * parseFloat(process.env.MAX_ROUTING_FEE);
const maxFee = amount * Number(process.env.MAX_ROUTING_FEE);

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

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

const payToBuyer = async (bot, order) => {
const payToBuyer = async (bot: Telegraf<MainContext>, order: IOrder) => {
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 @@ -99,14 +111,14 @@ const payToBuyer = async (bot, order) => {
}
};

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

return !!is_pending;
} catch (error) {
const message = error.toString();
const message = String(error);
logger.error(`isPendingPayment catch error: ${message}`);
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"compilerOptions": {
"strict": true,
"downlevelIteration": true,
"esModuleInterop": true
}
}

0 comments on commit 6c19fda

Please sign in to comment.