ソースを参照

WorkerRewardAmountUpdated added, refactoring

singulart 2 年 前
コミット
dc6a7521fe

+ 9 - 49
scripts/wg-bot/src/joystream/discord.ts

@@ -1,13 +1,12 @@
 import { EventRecord } from '@polkadot/types/interfaces'
-import { getWorker, getMember, getMint, getEvents, getBlockHash } from '../lib/api';
+import { getWorker, getMember, getMint, getEvents, getBlockHash, getWorkerReward } from '../lib/api';
 import { getHiringOpening, getOpening } from './api_extension';
+import { getMintCapacityChangedEmbed, getOpeningAddedEmbed, getOpeningFilledEmbed, getWorkerRewardAmountUpdatedEmbed } from './embeds';
 
 import { wgEvents, workingGroups, joystreamBlue } from '../config'
 import Discord from 'discord.js';
 import { ApiPromise } from '@polkadot/api';
 import { u32 } from '@polkadot/types';
-import { formatBalance } from '@polkadot/util';
-import { ApplicationIdToWorkerIdMap } from '@joystream/types/working-group';
 
 export const processBlock = async (api: ApiPromise, client: Discord.Client, blockNumber: number) => {
     const hash = await getBlockHash(api, blockNumber);
@@ -26,36 +25,13 @@ export const processBlock = async (api: ApiPromise, client: Discord.Client, bloc
                     const mintId = (data[0] as u32).toNumber()
                     const minted = (data[1] as u32).toNumber()
                     const mint = (await getMint(api, hash, mintId)).capacity
-
-                    const exampleEmbed = new Discord.MessageEmbed()
-                        .setColor(joystreamBlue)
-                        .setTitle(`💰 💵 💸 💴 💶 ${formatBalance(minted, { withUnit: 'JOY' })} minted to the Treasury 💰 💵 💸 💴 💶 `)
-                        .addFields(
-                            { name: 'Balance', value: formatBalance(mint, { withUnit: 'JOY' }), inline: true },
-                            { name: 'Block', value: blockNumber + "", inline: true },
-                            { name: 'Tx', value: value.hash.toString(), inline: true },
-                        )
-                        .setTimestamp();
-                    channel.send({ embeds: [exampleEmbed] });
+                    channel.send({ embeds: [getMintCapacityChangedEmbed(minted, mint, blockNumber, value)] });
                 } else if (method === 'OpeningAdded') {
                     const openingId = (data[0] as u32).toNumber()
                     const openingObject = await getOpening(api, section, hash, openingId)
                     const hiringOpening = await getHiringOpening(api, hash, openingObject.hiring_opening_id)
                     const opening = JSON.parse(hiringOpening.human_readable_text.toString())
-                    const exampleEmbed = new Discord.MessageEmbed()
-                        .setColor(joystreamBlue)
-                        .setTitle(`⛩ ${opening.headline} ⛩`)
-                        .setDescription(opening.job.description)
-                        .addFields(
-                            { name: 'Reward', value: opening.reward, inline: true },
-                            { name: 'Application Stake', value: openingObject.policy_commitment.application_staking_policy.unwrap().amount.toString() || 'Not Set', inline: true },
-                            { name: 'Role Stake', value: openingObject.policy_commitment.role_staking_policy.unwrap().amount.toString() || 'Not Set', inline: true },
-                            { name: 'Created By', value: opening.creator.membership.handle, inline: true },
-                            { name: 'Block', value: blockNumber + "", inline: true },
-                            { name: 'Tx', value: value.hash.toString(), inline: true },
-                        )
-                        .setTimestamp();
-                    channel.send({ embeds: [exampleEmbed] });
+                    channel.send({ embeds: [getOpeningAddedEmbed(opening, openingObject, blockNumber, value)] });
                 } else if (method === 'OpeningFilled') {
                     const openingId = (data[0] as u32).toNumber()
                     const workerId = Object.values(JSON.parse(data[1].toString()))[0] as number
@@ -64,29 +40,13 @@ export const processBlock = async (api: ApiPromise, client: Discord.Client, bloc
                     const openingObject = await getOpening(api, section, hash, openingId)
                     const hiringOpening = await getHiringOpening(api, hash, openingObject.hiring_opening_id)
                     const opening = JSON.parse(hiringOpening.human_readable_text.toString())
-                    const exampleEmbed = new Discord.MessageEmbed()
-                        .setColor(joystreamBlue)
-                        .setTitle(`🎉 🥳 👏🏻 ${member.handle} was hired as ${opening.job.title} 🎉 🥳 👏🏻`)
-                        // .setTitle(`🎉 🥳 👏🏻 ${member.handle} was hired as !!!!!!!!!!!!!!!!!!!!!!!!!! 🎉 🥳 👏🏻`)
-                        .addFields(
-                            { name: 'Block', value: blockNumber + "", inline: true },
-                            { name: 'Tx', value: value.hash.toString(), inline: true },
-                        )
-                        .setTimestamp();
-                    channel.send({ embeds: [exampleEmbed] });
+                    channel.send({ embeds: [getOpeningFilledEmbed(opening, member, blockNumber, value)] });
                 } else if (method === 'WorkerRewardAmountUpdated') {
                     const workerId = (data[0] as u32).toNumber()
-                    const worker = (await getWorker(api, section, hash, workerId))
-                    const member = (await getMember(api, worker.member_id))
-                    const exampleEmbed = new Discord.MessageEmbed()
-                        .setColor(joystreamBlue)
-                        .setTitle(`💰💰💰 Salary of ${member.handle} updated`)
-                        .addFields(
-                            { name: 'Block', value: blockNumber + "", inline: true },
-                            { name: 'Tx', value: value.hash.toString(), inline: true },
-                        )
-                        .setTimestamp();
-                    channel.send({ embeds: [exampleEmbed] });
+                    const worker = await getWorker(api, section, hash, workerId)
+                    const member = await getMember(api, worker.member_id)
+                    const reward = await getWorkerReward(api, hash, worker.reward_relationship.unwrap())
+                    channel.send({ embeds: [getWorkerRewardAmountUpdatedEmbed(reward, member, blockNumber, value)] });
                 }
             } else {
                 console.log(`Channel not configured for ${section}`);

+ 65 - 0
scripts/wg-bot/src/joystream/embeds.ts

@@ -0,0 +1,65 @@
+import { joystreamBlue } from '../config'
+import { formatBalance } from '@polkadot/util';
+import { u128 } from '@polkadot/types';
+import { EventRecord } from '@polkadot/types/interfaces';
+import Discord from 'discord.js';
+import { Membership, OpeningOf, RewardRelationship } from '@joystream/types/augment-codec/all';
+
+
+export const getMintCapacityChangedEmbed = (minted: number, mint: u128, blockNumber: number, event: EventRecord): Discord.MessageEmbed => {
+
+    return new Discord.MessageEmbed()
+        .setColor(joystreamBlue)
+        .setTitle(`💰 💵 💸 💴 💶 ${formatBalance(minted, { withUnit: 'JOY' })} minted to the Treasury 💰 💵 💸 💴 💶 `)
+        .addFields(
+            { name: 'Balance', value: formatBalance(mint, { withUnit: 'JOY' }), inline: true },
+            { name: 'Block', value: blockNumber + "", inline: true },
+            { name: 'Tx', value: event.hash.toString(), inline: true },
+        )
+        .setTimestamp();
+}
+
+export const getOpeningAddedEmbed = (opening: any, openingObject: OpeningOf, blockNumber: number, event: EventRecord): Discord.MessageEmbed => {
+
+    return new Discord.MessageEmbed()
+        .setColor(joystreamBlue)
+        .setTitle(`⛩ ${opening.headline} ⛩`)
+        .setDescription(opening.job.description)
+        .addFields(
+            { name: 'Reward', value: opening.reward, inline: true },
+            { name: 'Application Stake', value: openingObject.policy_commitment.application_staking_policy.unwrap().amount.toString() || 'Not Set', inline: true },
+            { name: 'Role Stake', value: openingObject.policy_commitment.role_staking_policy.unwrap().amount.toString() || 'Not Set', inline: true },
+            { name: 'Created By', value: opening.creator.membership.handle, inline: true },
+            { name: 'Block', value: blockNumber + "", inline: true },
+            { name: 'Tx', value: event.hash.toString(), inline: true },
+        )
+        .setTimestamp();
+}
+
+export const getOpeningFilledEmbed = (opening: any, member: Membership, blockNumber: number, event: EventRecord): Discord.MessageEmbed => {
+
+    return new Discord.MessageEmbed()
+        .setColor(joystreamBlue)
+        .setTitle(`🎉 🥳 👏🏻 ${member.handle} was hired as ${opening.job.title} 🎉 🥳 👏🏻`)
+        .addFields(
+            { name: 'Block', value: blockNumber + "", inline: true },
+            { name: 'Tx', value: event.hash.toString(), inline: true },
+        )
+        .setTimestamp();
+}
+
+
+export const getWorkerRewardAmountUpdatedEmbed = (reward: RewardRelationship, member: Membership, 
+    blockNumber: number, event: EventRecord): Discord.MessageEmbed => {
+
+    return new Discord.MessageEmbed()
+        .setColor(joystreamBlue)
+        .setTitle(`💰💰💰 Salary of ${member.handle} updated`)
+        .addFields(
+            { name: 'Salary', value: formatBalance(reward.amount_per_payout, { withUnit: 'JOY' }), inline: true },
+            { name: 'Payout Frequency', value: reward.payout_interval + "", inline: true },
+            { name: 'Block', value: blockNumber + "", inline: true },
+            { name: 'Tx', value: event.hash.toString(), inline: true },
+        )
+        .setTimestamp();
+}