2021-12-15 00:37:32 -08:00
|
|
|
import * as nodemailer from "nodemailer"
|
|
|
|
import * as EmailValidator from "email-validator"
|
|
|
|
|
|
|
|
import { MangoClient } from "@blockworks-foundation/mango-client"
|
|
|
|
import { PublicKey } from "@solana/web3.js"
|
2022-03-23 16:09:01 -07:00
|
|
|
import {
|
|
|
|
NotifiClient,
|
|
|
|
NotifiEnvironment,
|
|
|
|
createAxiosInstance,
|
|
|
|
} from '@notifi-network/notifi-node';
|
|
|
|
import axios from 'axios';
|
2021-12-15 00:37:32 -08:00
|
|
|
|
|
|
|
import { UserError } from "./errors"
|
|
|
|
|
|
|
|
import config from "./environment"
|
2022-03-23 16:09:01 -07:00
|
|
|
import { randomUUID } from "crypto"
|
2021-12-15 00:37:32 -08:00
|
|
|
|
|
|
|
const mailjetTransport = require("nodemailer-mailjet-transport")
|
|
|
|
|
2022-03-23 16:09:01 -07:00
|
|
|
// Initialize Notifi client
|
|
|
|
const env: NotifiEnvironment = 'Development';
|
|
|
|
const axiosInstance = createAxiosInstance(axios, env);
|
|
|
|
const notifiClient = new NotifiClient(axiosInstance);
|
|
|
|
|
2021-12-15 00:37:32 -08:00
|
|
|
export const validateMangoAccount = (client: MangoClient, alert: any) => {
|
|
|
|
return new Promise<void>(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const mangoGroupPk = new PublicKey(alert.mangoGroupPk)
|
2021-12-16 01:24:14 -08:00
|
|
|
const mangoAccountPk = new PublicKey(alert.mangoAccountPk)
|
2021-12-15 00:37:32 -08:00
|
|
|
const mangoGroup = await client.getMangoGroup(mangoGroupPk)
|
|
|
|
const mangoAccount = await client.getMangoAccount(
|
|
|
|
mangoAccountPk,
|
|
|
|
mangoGroup.dexProgramId
|
|
|
|
)
|
|
|
|
if (!mangoGroup || !mangoAccount) {
|
2021-12-16 01:24:14 -08:00
|
|
|
reject(new UserError("Invalid mango account or mango group"))
|
2021-12-15 00:37:32 -08:00
|
|
|
} else {
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2021-12-16 01:24:14 -08:00
|
|
|
reject(new UserError("Invalid mango account or mango group"))
|
2021-12-15 00:37:32 -08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-20 18:37:10 -08:00
|
|
|
export const validateUpdatePassword = (password: string) => {
|
|
|
|
return new Promise<void>(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
if (password != config.updatePassword) {
|
|
|
|
reject(new UserError("Invalid password"))
|
|
|
|
} else {
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2021-12-21 00:47:34 -08:00
|
|
|
reject(new UserError("Invalid password"))
|
2021-12-20 18:37:10 -08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-15 00:37:32 -08:00
|
|
|
export const validateEmail = (email: string) => {
|
|
|
|
if (!EmailValidator.validate(email)) {
|
2021-12-16 01:24:14 -08:00
|
|
|
throw new UserError("Enter a valid email")
|
2021-12-15 00:37:32 -08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-23 16:09:01 -07:00
|
|
|
export const validateNotifiAlertId = (notifiAlertId: string) => {
|
|
|
|
if (!notifiAlertId) {
|
|
|
|
throw new UserError("Invalid notifiAlertId")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-15 00:37:32 -08:00
|
|
|
const sendEmail = async (email: string, message: string) => {
|
|
|
|
const transport = nodemailer.createTransport(
|
|
|
|
mailjetTransport({
|
|
|
|
auth: {
|
|
|
|
apiKey: config.mailJetKey,
|
|
|
|
apiSecret: config.mailJetSecret,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
const mailOptions = {
|
|
|
|
from: `${config.mailUser}@mango.markets`,
|
|
|
|
to: email,
|
|
|
|
subject: "Mango Alerts",
|
|
|
|
text: message,
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-12-16 01:24:14 -08:00
|
|
|
await transport.sendMail(mailOptions)
|
2022-03-23 16:09:01 -07:00
|
|
|
return true
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
const sendNotifiAlert = async (alertId: string, health: number, walletPublicKey: string ) => {
|
|
|
|
const sid = config.notifiSid
|
|
|
|
const secret = config.notifiSecret
|
|
|
|
if (!sid || !secret) {
|
|
|
|
throw new UserError("Missing sid/secret pair")
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// login with sid/secret to get jwt
|
|
|
|
const { token: jwt, expiry } = await notifiClient.logIn({ sid, secret })
|
|
|
|
// console.log(`login successfully, received jwt expire at ${expiry}`)
|
|
|
|
if (jwt) {
|
|
|
|
// trigger notifi to send notification
|
|
|
|
const key = randomUUID()
|
|
|
|
await notifiClient.sendSimpleHealthThreshold(jwt, {
|
|
|
|
key,
|
|
|
|
walletPublicKey,
|
|
|
|
walletBlockchain: "SOLANA",
|
|
|
|
healthValue: health,
|
|
|
|
})
|
|
|
|
// console.log(`sending alert with key: ${key}, walletPublicKey: ${walletPublicKey}, value: ${health}`);
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
throw new UserError("Invalid jwt, please login")
|
|
|
|
}
|
2021-12-15 00:37:32 -08:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
2022-03-23 16:09:01 -07:00
|
|
|
throw err
|
2021-12-15 00:37:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-23 16:09:01 -07:00
|
|
|
export const sendAlert = async (alert: any, message: string, health: number, walletPublicKey: string) => {
|
2021-12-15 00:37:32 -08:00
|
|
|
if (alert.alertProvider == "mail") {
|
|
|
|
const email = alert.email
|
2022-03-23 16:09:01 -07:00
|
|
|
const emailSent = await sendEmail(email, message)
|
|
|
|
return emailSent
|
|
|
|
} else if (alert.alertProvider == "notifi") {
|
|
|
|
try {
|
|
|
|
const alertSent = await sendNotifiAlert(alert.notifiAlertId, health, walletPublicKey)
|
|
|
|
return alertSent;
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
return false
|
|
|
|
}
|
2021-12-15 00:37:32 -08:00
|
|
|
}
|
2022-03-23 16:09:01 -07:00
|
|
|
|
|
|
|
return false
|
2021-12-15 00:37:32 -08:00
|
|
|
}
|