Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bot, jobs,tests/bot: convert requires to imports #638

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions app.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import "dotenv/config";
import { SocksProxyAgent } from "socks-proxy-agent";
import { MainContext, start } from "./bot/start";
import { start } from "./bot/start";
import { connect as mongoConnect } from './db_connect'
const { resubscribeInvoices } = require('./ln');
import { logger } from "./logger";
import { Telegraf } from "telegraf";
const { delay } = require('./util');
import { delay } from './util';
import { CommunityContext } from "./bot/modules/community/communityContext";

(async () => {
process.on('unhandledRejection', e => {
Expand All @@ -24,7 +25,7 @@ const { delay } = require('./util');
mongoose.connection
.once('open', async () => {
logger.info('Connected to Mongo instance.');
let options: Partial<Telegraf.Options<MainContext>> = { handlerTimeout: 60000 };
let options: Partial<Telegraf.Options<CommunityContext>> = { handlerTimeout: 60000 };
if (process.env.SOCKS_PROXY_HOST) {
const agent = new SocksProxyAgent(process.env.SOCKS_PROXY_HOST);
options = {
Expand Down
31 changes: 15 additions & 16 deletions bot/commands.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import { validateFiatSentOrder, validateReleaseOrder } from './validations';
const {
import {
createHoldInvoice,
subscribeInvoice,
cancelHoldInvoice,
settleHoldInvoice,
} = require('../ln');
} from '../ln';
import { Order, User, Dispute } from '../models';
import * as messages from './messages';
import { getBtcFiatPrice, deleteOrderFromChannel, getUserI18nContext, getFee } from '../util';
import { getBtcFiatPrice, deleteOrderFromChannel, getUserI18nContext, getFee, removeLightningPrefix } from '../util';
import * as ordersActions from './ordersActions';
const OrderEvents = require('./modules/events/orders');
const { removeLightningPrefix } = require('../util');
import * as OrderEvents from './modules/events/orders';

import { resolvLightningAddress } from '../lnurl/lnurl-pay';
import { logger } from '../logger';
import { Telegraf } from 'telegraf';
import { IOrder } from '../models/order';
import { UserDocument } from '../models/user';
import { MainContext } from './start';
import { HasTelegram, MainContext } from './start';
import { CommunityContext } from './modules/community/communityContext';
import { Types } from 'mongoose';

const waitPayment = async (ctx: MainContext, bot: MainContext, buyer: UserDocument, seller: UserDocument, order: IOrder, buyerInvoice: any) => {
const waitPayment = async (ctx: MainContext, bot: HasTelegram, buyer: UserDocument, seller: UserDocument, order: IOrder, buyerInvoice: any) => {
try {
// If there is not fiat amount the function don't do anything
if (order.fiat_amount === undefined) {
Expand Down Expand Up @@ -88,7 +87,7 @@ const waitPayment = async (ctx: MainContext, bot: MainContext, buyer: UserDocume
}
};

const addInvoice = async (ctx: CommunityContext, bot: MainContext, order: IOrder | null) => {
const addInvoice = async (ctx: CommunityContext, bot: HasTelegram, order: IOrder | null = null) => {
try {
ctx.deleteMessage();
ctx.scene.leave();
Expand Down Expand Up @@ -177,7 +176,7 @@ const addInvoice = async (ctx: CommunityContext, bot: MainContext, order: IOrder
}
};

const rateUser = async (ctx: CommunityContext, bot: MainContext, rating: number, orderId: string) => {
const rateUser = async (ctx: CommunityContext, bot: HasTelegram, rating: number, orderId: string) => {
try {
ctx.deleteMessage();
ctx.scene.leave();
Expand Down Expand Up @@ -240,7 +239,7 @@ const saveUserReview = async (targetUser: UserDocument, rating: number) => {
}
};

const cancelAddInvoice = async (ctx: CommunityContext, order: IOrder | null, job?: any) => {
const cancelAddInvoice = async (ctx: CommunityContext, order: IOrder | null = null, job?: any) => {
try {
let userAction = false;
let userTgId = null;
Expand Down Expand Up @@ -357,7 +356,7 @@ const cancelAddInvoice = async (ctx: CommunityContext, order: IOrder | null, job
}
};

const showHoldInvoice = async (ctx: CommunityContext, bot: MainContext, order: IOrder | null) => {
const showHoldInvoice = async (ctx: CommunityContext, bot: HasTelegram, order: IOrder | null = null) => {
try {
ctx.deleteMessage();
if (!order) {
Expand Down Expand Up @@ -427,7 +426,7 @@ const showHoldInvoice = async (ctx: CommunityContext, bot: MainContext, order: I
}
};

const cancelShowHoldInvoice = async (ctx: CommunityContext, order: IOrder | null, job?: any) => {
const cancelShowHoldInvoice = async (ctx: CommunityContext, order: IOrder | null = null, job?: any) => {
try {
let userAction = false;
let userTgId = null;
Expand Down Expand Up @@ -550,7 +549,7 @@ const cancelShowHoldInvoice = async (ctx: CommunityContext, order: IOrder | null
* @param {*} order
* @returns
*/
const addInvoicePHI = async (ctx: CommunityContext, bot: MainContext, orderId: string) => {
const addInvoicePHI = async (ctx: CommunityContext, bot: HasTelegram, orderId: string) => {
try {
ctx.deleteMessage();
const order = await Order.findOne({ _id: orderId });
Expand All @@ -574,7 +573,7 @@ const addInvoicePHI = async (ctx: CommunityContext, bot: MainContext, orderId: s
}
};

const cancelOrder = async (ctx: CommunityContext, orderId: string, user: UserDocument | null) => {
const cancelOrder = async (ctx: CommunityContext, orderId: string, user: UserDocument | null = null) => {
try {
if (user === null) {
const tgUser = (ctx.update as any).callback_query.from;
Expand Down Expand Up @@ -700,7 +699,7 @@ const cancelOrder = async (ctx: CommunityContext, orderId: string, user: UserDoc
}
};

const fiatSent = async (ctx: MainContext, orderId: string, user: UserDocument | null) => {
const fiatSent = async (ctx: MainContext, orderId: string, user: UserDocument | null = null) => {
try {
if (!user) {
const tgUser = (ctx.update as any).callback_query.from;
Expand Down Expand Up @@ -736,7 +735,7 @@ const fiatSent = async (ctx: MainContext, orderId: string, user: UserDocument |
}
};

const release = async (ctx: MainContext, orderId: string, user: UserDocument | null) => {
const release = async (ctx: MainContext, orderId: string, user: UserDocument | null = null) => {
try {
if (!user) {
const tgUser = (ctx.update as any).callback_query.from;
Expand Down
20 changes: 7 additions & 13 deletions bot/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
const { initialize, start } = require('./start');
const {
createOrder,
getOrder,
getNewRangeOrderPayload,
} = require('./ordersActions');
const {
import { initialize, start } from './start';
import { createOrder, getOrder, getNewRangeOrderPayload } from './ordersActions';
import {
validateSellOrder,
validateBuyOrder,
validateUser,
Expand All @@ -13,8 +9,8 @@ const {
validateTakeBuyOrder,
validateReleaseOrder,
validateDisputeOrder,
} = require('./validations');
const {
} from './validations';
import {
startMessage,
initBotErrorMessage,
invoicePaymentRequestMessage,
Expand All @@ -36,12 +32,11 @@ const {
onGoingTakeBuyMessage,
pendingSellMessage,
pendingBuyMessage,
beginDisputeMessage,
notOrderMessage,
customMessage,
} = require('./messages');
} from './messages';

module.exports = {
export {
initialize,
start,
createOrder,
Expand Down Expand Up @@ -75,7 +70,6 @@ module.exports = {
onGoingTakeBuyMessage,
pendingSellMessage,
pendingBuyMessage,
beginDisputeMessage,
notOrderMessage,
customMessage,
getNewRangeOrderPayload,
Expand Down
Loading
Loading