Skip to content

Commit e4963a0

Browse files
ln,bot: convert ln to TS (#646)
* ln,bot: convert ln to TS Convert ln module to TypeScript. * package.json: install type packages Add type packages needed for Typescript code to package.json. * use eslint on TS files Setup eslint so that it checks .ts files instead of .js. * fix eslint import ordering errors Fix "Import in body of module; reorder to top import/first" eslint errors. * bot: change signature of notAuthorized function From Promise<void> to Promise<undefined>, to get rid of the following error: ``` tests/bot/validation.spec.ts(376,19): error TS2339: Property 'tg_id' does not exist on type 'void | (UserDocument & { _id: ObjectId; })'. Property 'tg_id' does not exist on type 'void'. ``` * fix more eslint errors * eslint: silenced remaining errors Silenced remaining eslint errors as what they refer to is intended behavior.
1 parent e766e41 commit e4963a0

34 files changed

Lines changed: 479 additions & 162 deletions

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"node": true,
66
"jest": true
77
},
8+
"parser": "@typescript-eslint/parser",
89
"extends": ["standard", "eslint-config-prettier"],
910
"parserOptions": {
1011
"ecmaVersion": "latest"

.github/workflows/integrate.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ jobs:
5555
sleep 10
5656
5757
mongosh --eval 'db.getSiblingDB("lnp2pbot").createCollection("mycollection")'
58-
59-
- run: npm install @types/node @types/i18n @types/mocha
58+
59+
- run: npm install @types/node
6060
- run: npx tsc
6161
- run: npm ci
6262
- name: Run tests

app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import "dotenv/config";
22
import { SocksProxyAgent } from "socks-proxy-agent";
33
import { start } from "./bot/start";
44
import { connect as mongoConnect } from './db_connect'
5-
const { resubscribeInvoices } = require('./ln');
5+
import { resubscribeInvoices } from './ln';
66
import { logger } from "./logger";
77
import { Telegraf } from "telegraf";
88
import { delay } from './util';

bot/messages.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { TelegramError } from 'telegraf'
2-
import QR from 'qrcode';
1+
import { TelegramError, Telegraf } from 'telegraf'
32
import {
43
getCurrency,
54
numberFormat,
@@ -12,19 +11,18 @@ import {
1211
decimalRound,
1312
getUserAge,
1413
getStars,
14+
generateQRWithImage,
1515
} from '../util';
1616
import * as OrderEvents from './modules/events/orders';
1717
import { logger } from "../logger";
1818
import { HasTelegram, MainContext } from './start';
1919
import { UserDocument } from '../models/user'
2020
import { IOrder } from '../models/order'
21-
import { Telegraf } from 'telegraf';
2221
import { I18nContext } from '@grammyjs/i18n';
2322
import { IConfig } from '../models/config';
2423
import { IPendingPayment } from '../models/pending_payment';
2524
import { PayViaPaymentRequestResult } from 'lightning';
2625
import { IFiat } from '../util/fiatModel';
27-
import { generateQRWithImage } from '../util';
2826
import { CommunityContext } from './modules/community/communityContext';
2927

3028
const startMessage = async (ctx: MainContext) => {
@@ -132,7 +130,7 @@ const showQRCodeMessage = async (
132130
}
133131
};
134132

135-
const pendingSellMessage = async (ctx: MainContext, user: UserDocument, order: IOrder, channel: string, i18n: I18nContext) => {
133+
const pendingSellMessage = async (ctx: HasTelegram, user: UserDocument, order: IOrder, channel: string, i18n: I18nContext) => {
136134
try {
137135
const orderExpirationWindow =
138136
Number(process.env.ORDER_PUBLISHED_EXPIRATION_WINDOW) / 60 / 60;
@@ -168,7 +166,7 @@ const pendingSellMessage = async (ctx: MainContext, user: UserDocument, order: I
168166
}
169167
};
170168

171-
const pendingBuyMessage = async (bot: MainContext, user: UserDocument, order: IOrder, channel: string, i18n: I18nContext) => {
169+
const pendingBuyMessage = async (bot: HasTelegram, user: UserDocument, order: IOrder, channel: string, i18n: I18nContext) => {
172170
try {
173171
const orderExpirationWindow =
174172
Number(process.env.ORDER_PUBLISHED_EXPIRATION_WINDOW) / 60 / 60;
@@ -239,7 +237,7 @@ const expiredInvoiceMessage = async (ctx: MainContext) => {
239237
}
240238
};
241239

242-
const expiredInvoiceOnPendingMessage = async (bot: Telegraf<CommunityContext>, user: UserDocument, order: IOrder, i18n: I18nContext) => {
240+
const expiredInvoiceOnPendingMessage = async (bot: HasTelegram, user: UserDocument, order: IOrder, i18n: I18nContext) => {
243241
try {
244242
await bot.telegram.sendMessage(user.tg_id, i18n.t('invoice_expired_long'));
245243
await bot.telegram.sendMessage(
@@ -363,7 +361,7 @@ const beginTakeBuyMessage = async (ctx: MainContext, bot: HasTelegram, seller: U
363361
const expirationTime =
364362
Number(process.env.HOLD_INVOICE_EXPIRATION_WINDOW) / 60;
365363

366-
let caption = ctx.i18n.t('begin_take_buy', { expirationTime });
364+
const caption = ctx.i18n.t('begin_take_buy', { expirationTime });
367365

368366
await bot.telegram.sendMediaGroup(seller.tg_id, [{
369367
type: 'photo',
@@ -442,7 +440,7 @@ const showHoldInvoiceMessage = async (
442440
};
443441

444442
const onGoingTakeBuyMessage = async (
445-
bot: MainContext,
443+
bot: HasTelegram,
446444
seller: UserDocument,
447445
buyer: UserDocument,
448446
order: IOrder,
@@ -519,7 +517,7 @@ const beginTakeSellMessage = async (ctx: MainContext, bot: HasTelegram, buyer: U
519517
};
520518

521519
const onGoingTakeSellMessage = async (
522-
bot: MainContext,
520+
bot: HasTelegram,
523521
sellerUser: UserDocument,
524522
buyerUser: UserDocument,
525523
order: IOrder,
@@ -574,7 +572,7 @@ const takeSellWaitingSellerToPayMessage = async (
574572
};
575573

576574
const releasedSatsMessage = async (
577-
bot: MainContext,
575+
bot: HasTelegram,
578576
sellerUser: UserDocument,
579577
buyerUser: UserDocument,
580578
i18nBuyer: I18nContext,
@@ -594,7 +592,7 @@ const releasedSatsMessage = async (
594592
}
595593
};
596594

597-
const rateUserMessage = async (bot: Telegraf<CommunityContext>, caller: UserDocument, order: IOrder, i18n: I18nContext) => {
595+
const rateUserMessage = async (bot: HasTelegram, caller: UserDocument, order: IOrder, i18n: I18nContext) => {
598596
try {
599597
const starButtons = [];
600598
for (let num = 5; num > 0; num--) {
@@ -640,7 +638,7 @@ const notOrderMessage = async (ctx: MainContext) => {
640638
};
641639

642640
const publishBuyOrderMessage = async (
643-
bot: MainContext,
641+
bot: HasTelegram,
644642
user: UserDocument,
645643
order: IOrder,
646644
i18n: I18nContext,
@@ -678,7 +676,7 @@ const publishBuyOrderMessage = async (
678676
};
679677

680678
const publishSellOrderMessage = async (
681-
ctx: MainContext,
679+
ctx: HasTelegram,
682680
user: UserDocument,
683681
order: IOrder,
684682
i18n: I18nContext,
@@ -990,7 +988,7 @@ const notOrdersMessage = async (ctx: MainContext) => {
990988
}
991989
};
992990

993-
const notRateForCurrency = async (bot: MainContext, user: UserDocument, i18n: I18nContext) => {
991+
const notRateForCurrency = async (bot: HasTelegram, user: UserDocument, i18n: I18nContext) => {
994992
try {
995993
await bot.telegram.sendMessage(
996994
user.tg_id,
@@ -1182,7 +1180,7 @@ const counterPartyWantsCooperativeCancelMessage = async (
11821180
}
11831181
};
11841182

1185-
const invoicePaymentFailedMessage = async (bot: MainContext, user: UserDocument, i18n: I18nContext) => {
1183+
const invoicePaymentFailedMessage = async (bot: HasTelegram, user: UserDocument, i18n: I18nContext) => {
11861184
try {
11871185
await bot.telegram.sendMessage(
11881186
user.tg_id,
@@ -1247,7 +1245,7 @@ const showInfoMessage = async (ctx: MainContext, user: UserDocument, config: ICo
12471245
}
12481246
};
12491247

1250-
const buyerReceivedSatsMessage = async (bot: MainContext, buyerUser: UserDocument, sellerUser: UserDocument, i18n: I18nContext) => {
1248+
const buyerReceivedSatsMessage = async (bot: HasTelegram, buyerUser: UserDocument, sellerUser: UserDocument, i18n: I18nContext) => {
12511249
try {
12521250
await bot.telegram.sendMessage(
12531251
buyerUser.tg_id,
@@ -1651,7 +1649,7 @@ const currencyNotSupportedMessage = async (ctx: MainContext, currencies: Array<s
16511649
}
16521650
};
16531651

1654-
const notAuthorized = async (ctx: MainContext, tgId?: string) => {
1652+
const notAuthorized = async (ctx: MainContext, tgId?: string) : Promise<undefined> => {
16551653
try {
16561654
if (tgId) {
16571655
await ctx.telegram.sendMessage(tgId, ctx.i18n.t('not_authorized'));

bot/modules/community/communityContext.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import { MainContext } from '../../start';
22
import { SceneContextScene, WizardContextWizard, WizardSessionData } from 'telegraf/typings/scenes';
33
import { Update, Message } from 'telegraf/typings/core/types/typegram';
4-
import { Scenes, Telegraf } from 'telegraf';
4+
import { Telegraf } from 'telegraf';
55
import { ICommunity, IOrderChannel, IUsernameId } from '../../../models/community';
66
import { IOrder } from '../../../models/order';
77
import { UserDocument } from '../../../models/user';
88

9+
/* eslint-disable no-use-before-define */
910
export interface CommunityContext extends MainContext {
1011
scene: SceneContextScene<CommunityContext, WizardSessionData>;
1112
wizard: CommunityWizard;
1213
message: (Update.New & Update.NonChannel & Message.TextMessage) | undefined;
1314
}
15+
/* eslint-enable no-use-before-define */
1416

1517
// This type is catch-all for any wizard state and probably should be split into several
1618
// more specialized types.

bot/modules/community/scenes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export const communityWizard = new Scenes.WizardScene<CommunityContext>(
105105
}
106106
ctx.wizard.selectStep(0);
107107
// use ['steps'] syntax as steps is private property and TypeScript would complain
108+
// eslint-disable-next-line dot-notation
108109
return ctx.wizard['steps'][ctx.wizard.cursor](ctx);
109110
} catch (err) {
110111
logger.error(err);
@@ -230,7 +231,7 @@ const createCommunitySteps = {
230231
}
231232
const { bot, user } = ctx.wizard.state;
232233
const chan = itemsFromMessage(text);
233-
//ctx.wizard.state.channels = chan; // ???
234+
// ctx.wizard.state.channels = chan; // ???
234235
if (chan.length > 2) {
235236
await ctx.telegram.deleteMessage(
236237
ctx.message!.chat.id,

bot/modules/dispute/commands.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { validateParams, validateObjectId, validateDisputeOrder } from '../../va
55
import * as messages from './messages';
66
import * as globalMessages from '../../messages';
77
import { logger } from '../../../logger';
8-
import * as OrderEvents from '../../modules/events/orders';
98
import { removeAtSymbol } from '../../../util';
109

1110
const dispute = async (ctx: MainContext) => {

bot/modules/nostr/config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { generateSecretKey, getPublicKey as nostrGetPublicKey } from 'nostr-tools';
2-
import { SimplePool } from 'nostr-tools';
1+
import { generateSecretKey, getPublicKey as nostrGetPublicKey, SimplePool } from 'nostr-tools';
32

43
const nostrSkEnvVar = process.env.NOSTR_SK;
54
const sk = nostrSkEnvVar ? Buffer.from(nostrSkEnvVar, 'hex') : generateSecretKey();

bot/modules/nostr/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
require('websocket-polyfill');
21
import { logger } from '../../../logger';
32
import * as Config from './config';
43
import { createOrderEvent } from './events';
@@ -8,6 +7,8 @@ import { IOrder } from '../../../models/order';
87
import * as CommunityEvents from '../events/community';
98
import { CommunityContext } from '../community/communityContext';
109

10+
require('websocket-polyfill'); // is it needed?
11+
1112
export const configure = (bot: Telegraf<CommunityContext>) => {
1213
bot.command('/nostr', Commands.info);
1314

bot/modules/orders/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { takeOrderActionValidation, takeOrderValidation, takesell, takebuyValida
99
import { extractId } from '../../../util';
1010
import { Telegraf } from 'telegraf';
1111
import { CommunityContext } from '../community/communityContext';
12-
import { MainContext } from '../../start';
1312
export * as Scenes from './scenes';
1413

1514
export const configure = (bot: Telegraf<CommunityContext>) => {

0 commit comments

Comments
 (0)