Skip to content

Commit d32f95d

Browse files
committed
ln: convert js to ts
1 parent 98709de commit d32f95d

7 files changed

+61
-54
lines changed

Diff for: ln/connect.js renamed to ln/connect.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const fs = require('fs');
2-
const path = require('path');
3-
const lightning = require('lightning');
4-
const { logger } = require('../logger');
1+
import * as fs from 'fs';
2+
import * as path from 'path';
3+
import * as lightning from 'lightning';
4+
import { logger } from '../logger';
55

66
const { authenticatedLndGrpc } = lightning;
77

@@ -47,4 +47,4 @@ const { lnd } = authenticatedLndGrpc({
4747
socket,
4848
});
4949

50-
module.exports = lnd;
50+
export { lnd };

Diff for: ln/hold_invoice.js renamed to ln/hold_invoice.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
const { createHash, randomBytes } = require('crypto');
2-
const lightning = require('lightning');
3-
const lnd = require('./connect');
4-
const { logger } = require('../logger');
1+
import { randomBytes, createHash } from 'crypto';
2+
import * as lightning from 'lightning';
3+
import { lnd } from './connect'
4+
import { logger } from '../logger';
55

6-
const createHoldInvoice = async ({ description, amount }) => {
6+
const createHoldInvoice = async ({ description, amount } : { description: string, amount: number }) => {
77
try {
88
const randomSecret = () => randomBytes(32);
9-
const sha256 = buffer => createHash('sha256').update(buffer).digest('hex');
9+
const sha256 = (buffer: Buffer): string => createHash('sha256').update(buffer).digest('hex');
1010
// We create a random secret
1111
const secret = randomSecret();
1212
const expiresAt = new Date();
1313
expiresAt.setSeconds(expiresAt.getSeconds() + 3600);
1414

1515
const hash = sha256(secret);
16-
const cltv_delta = parseInt(process.env.HOLD_INVOICE_CLTV_DELTA);
16+
const cltv_delta = Number(process.env.HOLD_INVOICE_CLTV_DELTA);
1717
const { request, id } = await lightning.createHodlInvoice({
1818
cltv_delta,
1919
lnd,
2020
description,
2121
id: hash,
2222
tokens: amount,
23-
expires_at: expiresAt,
23+
expires_at: expiresAt.toISOString(),
2424
});
2525

2626
// We sent back the response hash (id) to be used on testing
@@ -30,31 +30,31 @@ const createHoldInvoice = async ({ description, amount }) => {
3030
}
3131
};
3232

33-
const settleHoldInvoice = async ({ secret }) => {
33+
const settleHoldInvoice = async ( { secret }: { secret: string } ) => {
3434
try {
3535
await lightning.settleHodlInvoice({ lnd, secret });
3636
} catch (error) {
3737
logger.error(error);
3838
}
3939
};
4040

41-
const cancelHoldInvoice = async ({ hash }) => {
41+
const cancelHoldInvoice = async ( { hash }: { hash: string } ) => {
4242
try {
4343
await lightning.cancelHodlInvoice({ lnd, id: hash });
4444
} catch (error) {
4545
logger.error(error);
4646
}
4747
};
4848

49-
const getInvoice = async ({ hash }) => {
49+
const getInvoice = async ( { hash }: { hash: string } ) => {
5050
try {
5151
return await lightning.getInvoice({ lnd, id: hash });
5252
} catch (error) {
5353
logger.error(error);
5454
}
5555
};
5656

57-
module.exports = {
57+
export {
5858
createHoldInvoice,
5959
settleHoldInvoice,
6060
cancelHoldInvoice,

Diff for: ln/index.js renamed to ln/index.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
const {
1+
import {
22
createHoldInvoice,
33
settleHoldInvoice,
44
cancelHoldInvoice,
55
getInvoice,
6-
} = require('./hold_invoice');
7-
const { subscribeInvoice, payHoldInvoice } = require('./subscribe_invoice');
6+
} from './hold_invoice';
7+
import { subscribeInvoice, payHoldInvoice } from './subscribe_invoice';
88
const subscribeProbe = require('./subscribe_probe');
9-
const resubscribeInvoices = require('./resubscribe_invoices');
9+
import { resubscribeInvoices } from './resubscribe_invoices';
1010
const { payRequest, payToBuyer, isPendingPayment } = require('./pay_request');
11-
const { getInfo } = require('./info');
11+
import { getInfo } from './info';
1212

13-
module.exports = {
13+
export {
1414
createHoldInvoice,
1515
subscribeInvoice,
1616
resubscribeInvoices,

Diff for: ln/info.js

-13
This file was deleted.

Diff for: ln/info.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as lightning from "lightning";
2+
import { lnd } from './connect'
3+
import { logger } from "../logger";
4+
5+
export const getInfo = async () => {
6+
try {
7+
return await lightning.getWalletInfo({ lnd });
8+
} catch (error) {
9+
logger.error(error);
10+
}
11+
};

Diff for: ln/resubscribe_invoices.js renamed to ln/resubscribe_invoices.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
const { getInvoices } = require('lightning');
2-
const lnd = require('./connect');
3-
const { subscribeInvoice } = require('./subscribe_invoice');
4-
const { Order } = require('../models');
5-
const { logger } = require('../logger');
1+
import { Telegraf } from 'telegraf';
2+
import { MainContext } from "../bot/start";
3+
import { getInvoices, GetInvoicesResult } from 'lightning';
4+
import { lnd } from './connect';
5+
import { subscribeInvoice } from './subscribe_invoice';
6+
import { Order } from '../models';
7+
import { logger } from "../logger";
68

7-
const resubscribeInvoices = async bot => {
9+
const resubscribeInvoices = async (bot: Telegraf<MainContext>) => {
810
try {
911
let invoicesReSubscribed = 0;
10-
const isHeld = invoice => !!invoice.is_held;
12+
const isHeld = (invoice: any) => !!invoice.is_held;
1113
const unconfirmedInvoices = (
1214
await getInvoices({
1315
lnd,
@@ -29,9 +31,9 @@ const resubscribeInvoices = async bot => {
2931
}
3032
logger.info(`Invoices resubscribed: ${invoicesReSubscribed}`);
3133
} catch (error) {
32-
logger.error(`ResubcribeInvoice catch: ${error.toString()}`);
34+
logger.error(`ResuscribeInvoice catch: ${String(error)}`);
3335
return false;
3436
}
3537
};
3638

37-
module.exports = resubscribeInvoices;
39+
export { resubscribeInvoices };

Diff for: ln/subscribe_invoice.js renamed to ln/subscribe_invoice.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
1-
const { subscribeToInvoice } = require('lightning');
2-
const { Order, User } = require('../models');
1+
import { Telegraf } from "telegraf";
2+
import { MainContext } from "../bot/start";
3+
import { subscribeToInvoice } from 'lightning'
4+
import { Order, User } from '../models';
35
const { payToBuyer } = require('./pay_request');
4-
const lnd = require('./connect');
5-
const messages = require('../bot/messages');
6+
import { lnd } from "./connect";
7+
import * as messages from '../bot/messages';
68
const ordersActions = require('../bot/ordersActions');
79
const { getUserI18nContext, getEmojiRate, decimalRound } = require('../util');
8-
const { logger } = require('../logger');
10+
import { logger } from "../logger";
11+
import { IOrder } from "../models/order";
912
const OrderEvents = require('../bot/modules/events/orders');
1013

11-
const subscribeInvoice = async (bot, id, resub) => {
14+
const subscribeInvoice = async (bot: Telegraf<MainContext>, id: string, resub: boolean) => {
1215
try {
1316
const sub = subscribeToInvoice({ id, lnd });
1417
sub.on('invoice_updated', async invoice => {
1518
if (invoice.is_held && !resub) {
1619
const order = await Order.findOne({ hash: invoice.id });
20+
if (order === null) throw Error("Order was not found in DB");
1721
logger.info(
1822
`Order ${order._id} Invoice with hash: ${id} is being held!`
1923
);
2024
const buyerUser = await User.findOne({ _id: order.buyer_id });
2125
const sellerUser = await User.findOne({ _id: order.seller_id });
26+
if (buyerUser === null || sellerUser === null) throw Error("buyer or seller was not found in DB");
2227
order.status = 'ACTIVE';
2328
// This is the i18n context we need to pass to the message
2429
const i18nCtxBuyer = await getUserI18nContext(buyerUser);
@@ -48,12 +53,13 @@ const subscribeInvoice = async (bot, id, resub) => {
4853
rate
4954
);
5055
}
51-
order.invoice_held_at = Date.now();
56+
order.invoice_held_at = new Date();
5257
order.save();
5358
OrderEvents.orderUpdated(order);
5459
}
5560
if (invoice.is_confirmed) {
5661
const order = await Order.findOne({ hash: id });
62+
if (order === null) throw Error("Order was not found in DB");
5763
logger.info(
5864
`Order ${order._id} - Invoice with hash: ${id} was settled!`
5965
);
@@ -72,13 +78,14 @@ const subscribeInvoice = async (bot, id, resub) => {
7278
}
7379
};
7480

75-
const payHoldInvoice = async (bot, order) => {
81+
const payHoldInvoice = async (bot: Telegraf<MainContext>, order: IOrder) => {
7682
try {
7783
order.status = 'PAID_HOLD_INVOICE';
7884
await order.save();
7985
OrderEvents.orderUpdated(order);
8086
const buyerUser = await User.findOne({ _id: order.buyer_id });
8187
const sellerUser = await User.findOne({ _id: order.seller_id });
88+
if (buyerUser === null || sellerUser === null) throw Error("buyer or seller was not found in DB");
8289
// We need two i18n contexts to send messages to each user
8390
const i18nCtxBuyer = await getUserI18nContext(buyerUser);
8491
const i18nCtxSeller = await getUserI18nContext(sellerUser);
@@ -138,4 +145,4 @@ const payHoldInvoice = async (bot, order) => {
138145
}
139146
};
140147

141-
module.exports = { subscribeInvoice, payHoldInvoice };
148+
export { subscribeInvoice, payHoldInvoice };

0 commit comments

Comments
 (0)