Put cron in a function

This commit is contained in:
Ralfs 2021-04-10 01:26:00 +03:00
parent 91f66571bb
commit 07c080f249
1 changed files with 34 additions and 30 deletions

View File

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