From 610f2c42af1ac88b2413ea0fd77c578616438b2f Mon Sep 17 00:00:00 2001 From: ali ebrahimi Date: Wed, 19 Jun 2024 01:59:39 +0330 Subject: [PATCH 1/5] Add notification center adapter --- .../notificationCenterAdapter.js | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 deployments/mailService/notificationCeneterAdapter/notificationCenterAdapter.js 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; From 16ba9713696864d36cf3ed84b5084ea9bd5781ad Mon Sep 17 00:00:00 2001 From: ali ebrahimi Date: Wed, 19 Jun 2024 02:00:12 +0330 Subject: [PATCH 2/5] use notification center service for sending email --- deployments/mailService/mailService.js | 91 +++++++------------------- 1 file changed, 25 insertions(+), 66 deletions(-) diff --git a/deployments/mailService/mailService.js b/deployments/mailService/mailService.js index e651d6f..661b837 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,32 @@ 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", + trackId: "", + metadata: null, + creationTime: Date.now(), + segment: { + payload, + }, + }; const promises = givethDevMailList.split(",").map((recipient) => { - return axios.post( - `${dappMailerUrl}/send`, - { - ...data, - recipient, - }, - { - headers: { - Authorization: dappMailerSecret, - }, - }, - ); + data.email = recipient; + return sendNotification(data); }); return (await Promise.all(promises)).map((response) => response.data); } catch (e) { From cff62807a11910cb842898952515c6227e57769d Mon Sep 17 00:00:00 2001 From: ali ebrahimi Date: Wed, 19 Jun 2024 02:00:48 +0330 Subject: [PATCH 3/5] remove global variables and pass them as argument --- .../givpower_distribute_extended_may_2024.js | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) 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", From 2317f0e8db7d386b514389d54eed939ba05d6134 Mon Sep 17 00:00:00 2001 From: ali ebrahimi Date: Sun, 14 Jul 2024 16:46:47 +0330 Subject: [PATCH 4/5] remove unnecessary trackId --- deployments/mailService/mailService.js | 1 - 1 file changed, 1 deletion(-) diff --git a/deployments/mailService/mailService.js b/deployments/mailService/mailService.js index 661b837..3279c6b 100644 --- a/deployments/mailService/mailService.js +++ b/deployments/mailService/mailService.js @@ -39,7 +39,6 @@ const sendReportEmail = async ({ sendEmail: true, sendSegment: true, eventName: "Notify reward amount", - trackId: "", metadata: null, creationTime: Date.now(), segment: { From b862fe6158f3a8cea041ade9bc86322e14e3f039 Mon Sep 17 00:00:00 2001 From: ali ebrahimi Date: Wed, 24 Jul 2024 03:41:32 +0330 Subject: [PATCH 5/5] change email field position based on notification center changes --- deployments/mailService/mailService.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deployments/mailService/mailService.js b/deployments/mailService/mailService.js index 3279c6b..720d63a 100644 --- a/deployments/mailService/mailService.js +++ b/deployments/mailService/mailService.js @@ -46,7 +46,7 @@ const sendReportEmail = async ({ }, }; const promises = givethDevMailList.split(",").map((recipient) => { - data.email = recipient; + data.segment.payload.email = recipient; return sendNotification(data); }); return (await Promise.all(promises)).map((response) => response.data);