diff --git a/deployments/mailService/mailService.js b/deployments/mailService/mailService.js index e651d6f..720d63a 100644 --- a/deployments/mailService/mailService.js +++ b/deployments/mailService/mailService.js @@ -1,6 +1,7 @@ const axios = require("axios"); const moment = require("moment"); -const { dappMailerUrl, givethDevMailList, dappMailerSecret } = process.env; +const sendNotification = require("./notificationCeneterAdapter/notificationCenterAdapter"); +const { givethDevMailList } = process.env; const sendReportEmail = async ({ pool, @@ -22,74 +23,31 @@ const sendReportEmail = async ({ * You can see the dapp-mail code here @see{@link https://github.com/Giveth/dapp-mailer/blob/master/src/services/send/send.hooks.js} */ const now = moment().format("YYYY-MM-DD HH:m:s"); - const data = { - template: "notification", - subject: `Notify reward ${farm} ${now}`, - image: "Giveth-review-banner-email.png", - text: ` - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Farm${farm}
Network${network}
Contract address${pool}
Amount${amount}
Transaction Hash${transactionHash}
Round${round}
Script${script}
Date${now}
message${message || ""}
- `, - // cta: `Manage Trace`, - // ctaRelativeUrl: `/campaigns/${data.campaignId}/milestones/${data.traceId}`, - unsubscribeType: "notifyReward-report", - unsubscribeReason: `You receive this email because you are in Giv power team`, - // message: data.message, + const payload = { + round, + date: now, + amount: amount.toString(), + contractAddress: pool, + farm, + message, + network, + script, + transactionHash, }; - const summaryMessage = `Notify reward report for ${farm}`; - data.title = summaryMessage; - data.secretIntro = summaryMessage; + const data = { + sendEmail: true, + sendSegment: true, + eventName: "Notify reward amount", + metadata: null, + creationTime: Date.now(), + segment: { + payload, + }, + }; const promises = givethDevMailList.split(",").map((recipient) => { - return axios.post( - `${dappMailerUrl}/send`, - { - ...data, - recipient, - }, - { - headers: { - Authorization: dappMailerSecret, - }, - }, - ); + data.segment.payload.email = recipient; + return sendNotification(data); }); return (await Promise.all(promises)).map((response) => response.data); } catch (e) { diff --git a/deployments/mailService/notificationCeneterAdapter/notificationCenterAdapter.js b/deployments/mailService/notificationCeneterAdapter/notificationCenterAdapter.js new file mode 100644 index 0000000..6eb5bfa --- /dev/null +++ b/deployments/mailService/notificationCeneterAdapter/notificationCenterAdapter.js @@ -0,0 +1,34 @@ +const axios = require("axios"); + +const notificationCenterUsername = process.env.NOTIFICATION_CENTER_USERNAME; +const notificationCenterPassword = process.env.NOTIFICATION_CENTER_PASSWORD; +const notificationCenterBaseUrl = process.env.NOTIFICATION_CENTER_BASE_URL; +const disableNotificationCenter = process.env.DISABLE_NOTIFICATION_CENTER; + +function createAuthenticationHeader() { + const str = `${notificationCenterUsername}:${notificationCenterPassword}`; + return `Basic ${Buffer.from(str).toString("base64")}`; +} + +async function sendNotification(data) { + try { + if (disableNotificationCenter !== "true") { + await axios.post( + `${notificationCenterBaseUrl}/thirdParty/notifications`, + data, + { + headers: { + Authorization: createAuthenticationHeader(), + }, + }, + ); + } + } catch (e) { + console.error("SendNotification error", { + errorResponse: e?.response?.data, + data, + }); + } +} + +export default sendNotification; diff --git a/deployments/notifyRewardAmount/optimism/givpower_distribute_extended_may_2024.js b/deployments/notifyRewardAmount/optimism/givpower_distribute_extended_may_2024.js index 0bd0819..d03bb2c 100644 --- a/deployments/notifyRewardAmount/optimism/givpower_distribute_extended_may_2024.js +++ b/deployments/notifyRewardAmount/optimism/givpower_distribute_extended_may_2024.js @@ -19,21 +19,30 @@ const distro = [ const initTime = 1715709600; // Timestamp of first round in seconds: Tuesday, MAY 14, 2024 18:00:00 GMT -let UnipoolTokenDistributor, currentTime, nonce; async function main() { console.log("Trying to call notifyRewardAmount...", { date: new Date().toString(), }); - currentTime = Math.floor(Date.now() / 1000); + const currentTime = Math.floor(Date.now() / 1000); const [signer, ...addrs] = await ethers.getSigners(); - nonce = await signer.getTransactionCount(); - UnipoolTokenDistributor = await ethers.getContractFactory( + const nonce = await signer.getTransactionCount(); + const UnipoolTokenDistributor = await ethers.getContractFactory( "UnipoolTokenDistributor", ); - await notifyRewardAmount(pools[0]); + await notifyRewardAmount( + pools[0], + UnipoolTokenDistributor, + nonce, + currentTime, + ); } -async function notifyRewardAmount(pool) { +async function notifyRewardAmount( + pool, + UnipoolTokenDistributor, + nonce, + currentTime, +) { const unipoolTokenDistributor = await UnipoolTokenDistributor.attach( pool.address, ); @@ -59,7 +68,6 @@ async function notifyRewardAmount(pool) { const tx = await ( await unipoolTokenDistributor.notifyRewardAmount(amount, { nonce }) ).wait(); - nonce += 1; console.log("tx:", tx); await sendReportEmail({ farm: "Giv power",