Refactor cron code and sendSms code

This commit is contained in:
Ralfs 2021-04-14 00:02:14 +03:00
parent 69e1cb3763
commit 6a60ddb1c2
2 changed files with 37 additions and 25 deletions

View File

@ -74,6 +74,28 @@ app.listen(config.port, () => {
sendLogsToDiscord(readyMessage, null);
});
const handleAlert = async (alert: any, mangoGroups: any[], db: any) => {
try {
const marginAccountPk = new PublicKey(alert.marginAccountPk);
const marginAccount = await client.getMarginAccount(connection, marginAccountPk, dexProgramId);
const collateralRatio = marginAccount.getCollateralRatio(mangoGroups[alert.mangoGroupPk]['mangoGroup'], mangoGroups[alert.mangoGroupPk]['prices']);
if ((100 * collateralRatio) <= alert.collateralRatioThresh) {
let message = MESSAGE.replace('@ratio@', alert.collateralRatioThresh);
message += marginAccount.toPrettyString(
mangoGroups[alert.mangoGroupPk]['mangoGroup'],
mangoGroups[alert.mangoGroupPk]['prices']
);
message += '\nVisit https://trade.mango.markets/'
const alertSent = await sendAlert(alert, message);
if (alertSent) {
db.collection('alerts').updateOne({ _id: new ObjectId(alert._id) }, { '$set': { open: false } });
}
}
} catch (e) {
sendLogsToDiscord(null, e);
}
}
const runCron = async () => {
const mongoConnection = await MongoClient.connect(config.dbConnectionString, { useUnifiedTopology: true });
const db = mongoConnection.db(config.db);
@ -83,21 +105,7 @@ const runCron = async () => {
const uniqueMangoGroupPks: string[] = [...new Set(alerts.map(alert => alert.mangoGroupPk))];
const mangoGroups:any = await reduceMangoGroups(client, connection, uniqueMangoGroupPks);
alerts.forEach(async (alert) => {
const marginAccountPk = new PublicKey(alert.marginAccountPk);
const marginAccount = await client.getMarginAccount(connection, marginAccountPk, dexProgramId);
const collateralRatio = marginAccount.getCollateralRatio(mangoGroups[alert.mangoGroupPk]['mangoGroup'], mangoGroups[alert.mangoGroupPk]['prices']);
if ((100 * collateralRatio) <= alert.collateralRatioThresh) {
let message = MESSAGE.replace('@ratio@', alert.collateralRatioThresh);
message += marginAccount.toPrettyString(
mangoGroups[alert.mangoGroupPk]['mangoGroup'],
mangoGroups[alert.mangoGroupPk]['prices']
);
message += '\nVisit https://trade.mango.markets/'
const alertSent = sendAlert(alert, message);
if (alertSent) {
db.collection('alerts').updateOne({ _id: new ObjectId(alert._id) }, { '$set': { open: false } });
}
}
handleAlert(alert, mangoGroups, db);
});
const expiryTime = Date.now() - (1000 * 60 * 15); // 15 Minutes
db.collection('alerts').deleteMany({ alertProvider: 'tg', tgChatId: { '$exists': false }, timestamp: { '$lt': expiryTime } });

View File

@ -35,8 +35,8 @@ export const validateMarginAccount = (client: MangoClient, connection: Connectio
export const validatePhoneNumber = (phoneNumber: string) => {
return new Promise<void>((resolve, reject) => {
twilioClient.lookups.phoneNumbers(phoneNumber).fetch((error, _) => {
if (error) {
twilioClient.lookups.phoneNumbers(phoneNumber).fetch((e, _) => {
if (e) {
reject(new UserError('The entered phone number is incorrect'));
} else {
resolve();
@ -53,12 +53,16 @@ export const validateEmail = (email: string) => {
}
const sendSms = (phoneNumber: string, message: string) => {
twilioClient.messages
.create({
from: config.twilioNumber,
to: phoneNumber,
body: message,
}).catch(error => { throw error })
return new Promise<void>((resolve, reject) => {
twilioClient.messages
.create({
from: config.twilioNumber,
to: phoneNumber,
body: message,
})
.then(_ => resolve())
.catch(e => reject(e))
});
}
const sendEmail = (email: string, message: string) => {
@ -74,10 +78,10 @@ const sendEmail = (email: string, message: string) => {
transporter.sendMail( mailOptions );
}
export const sendAlert = (alert: any, message: string) => {
export const sendAlert = async (alert: any, message: string) => {
if (alert.alertProvider == 'sms') {
const phoneNumber = `+${alert.phoneNumber.code}${alert.phoneNumber.phone}`;
sendSms(phoneNumber, message);
await sendSms(phoneNumber, message);
} else if (alert.alertProvider == 'mail') {
const email = alert.email;
sendEmail(email, message);