diff --git a/app.ts b/app.ts index 99e5609a..3db3da54 100644 --- a/app.ts +++ b/app.ts @@ -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 () => { diff --git a/bot/messages.ts b/bot/messages.ts index 131c9591..96aa23e9 100644 --- a/bot/messages.ts +++ b/bot/messages.ts @@ -1,4 +1,4 @@ -const { TelegramError } = require('telegraf'); +import { TelegramError } from 'telegraf' const QR = require('qrcode'); const { getCurrency, diff --git a/bot/start.ts b/bot/start.ts index 4597d259..9710fd6d 100644 --- a/bot/start.ts +++ b/bot/start.ts @@ -266,7 +266,7 @@ const initialize = (botToken: string, options: Partial el); + const [command, orderId] = params.filter((el: string) => el); if (!orderId) { const orders = await askForConfirmation(ctx.user, command); @@ -357,7 +357,7 @@ const initialize = (botToken: string, options: Partial el); + const [command, orderId] = params.filter((el: string) => el); if (!orderId) { const orders = await askForConfirmation(ctx.user, command); @@ -525,7 +525,7 @@ const initialize = (botToken: string, options: Partial el); + const [command, orderId] = params.filter((el: string) => el); if (!orderId) { const orders = await askForConfirmation(ctx.user, command); diff --git a/db_connect.ts b/db_connect.ts index 30e43ef7..1353b6f0 100644 --- a/db_connect.ts +++ b/db_connect.ts @@ -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 diff --git a/ln/pay_request.js b/ln/pay_request.ts similarity index 78% rename from ln/pay_request.js rename to ln/pay_request.ts index 59fe6d38..8f3afc5f 100644 --- a/ln/pay_request.js +++ b/ln/pay_request.ts @@ -1,16 +1,20 @@ -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; @@ -18,9 +22,17 @@ const payRequest = async ({ request, amount }) => { 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, @@ -44,7 +56,7 @@ const payRequest = async ({ request, amount }) => { } }; -const payToBuyer = async (bot, order) => { +const payToBuyer = async (bot: Telegraf, order: IOrder) => { try { // We check if the payment is on flight we don't do anything const isPending = await isPendingPayment(order.buyer_invoice); @@ -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; } diff --git a/tsconfig.json b/tsconfig.json index 8039f57e..f7ab49dd 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,7 @@ { "compilerOptions": { "strict": true, + "downlevelIteration": true, + "esModuleInterop": true } }