Bläddra i källkod

MintCapacityChanged

singulart 2 år sedan
förälder
incheckning
54abf2c275
3 ändrade filer med 58 tillägg och 12 borttagningar
  1. 3 1
      scripts/wg-bot/src/config.ts
  2. 21 11
      scripts/wg-bot/src/joystream/discord.ts
  3. 34 0
      scripts/wg-bot/src/simulate.ts

+ 3 - 1
scripts/wg-bot/src/config.ts

@@ -33,4 +33,6 @@ export const wgEvents = [
     'WorkerRewardAmountUpdated', 
     'WorkerRoleAccountUpdated', 
     'WorkerStorageUpdated'
-]
+]
+
+export const joystreamBlue = '#4038FF' // official joystream blue, see https://www.joystream.org/brand/guides/

+ 21 - 11
scripts/wg-bot/src/joystream/discord.ts

@@ -1,8 +1,10 @@
 import { EventRecord} from '@polkadot/types/interfaces'
-import { getWorker, getMember, getEvents, getBlockHash } from '../lib/api';
-import { wgEvents, workingGroups } from '../config'
+import { getWorker, getMember, getMint, getEvents, getBlockHash } from '../lib/api';
+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';
 
 export const processBlock = async (api: ApiPromise, client: Discord.Client, blockNumber: number) => {
     const hash = await getBlockHash(api, blockNumber);
@@ -16,15 +18,23 @@ export const processBlock = async (api: ApiPromise, client: Discord.Client, bloc
 
           const channel: Discord.TextChannel = client.channels.cache.get(workingGroups[section]) as Discord.TextChannel;
           if(channel) {
-              const exampleEmbed = new Discord.MessageEmbed()
-              .setColor('#4038FF') // official joystream blue, see https://www.joystream.org/brand/guides/
-              .setTitle('Working Group Update')
-              .setDescription('//TODO') 
-              .addFields(
-                { name: 'Data', value: data.toString(), inline: true },
-              )
-              .setTimestamp();
-              channel.send({ embeds: [exampleEmbed] });    
+
+            if(method === 'MintCapacityChanged') {
+                const mintId = (data[0] as u32).toNumber()
+                const minted = (data[1] as u32).toNumber()
+                const mint = await getMint(api, undefined, mintId)
+
+                const exampleEmbed = new Discord.MessageEmbed()
+                .setColor(joystreamBlue) 
+                .setTitle(`💰 💵 💸 💴 💶 ${formatBalance(minted, {withUnit: 'JOY'})} minted to the Treasury 💰 💵 💸 💴 💶 `)
+                .addFields(
+                  { name: 'Balance', value: mint.capacity + "", inline: true },
+                  { name: 'Block', value: blockNumber + "", inline: true },
+                  { name: 'Tx', value: value.hash.toString(), inline: true },
+                )
+                .setTimestamp();
+                channel.send({ embeds: [exampleEmbed] });    
+              }
           } else {
               console.log(`Channel not configured for ${section}`);
           }

+ 34 - 0
scripts/wg-bot/src/simulate.ts

@@ -0,0 +1,34 @@
+import { connectUpstream } from './joystream/ws'
+import { ApiPromise } from '@polkadot/api'
+import Discord, { Intents } from 'discord.js'
+import {processBlock} from './joystream/discord'
+import { workingGroups } from './config'
+
+const eventsMapping = {
+    'MintCapacityChanged': 4211575
+}
+
+
+
+const discordBotToken = process.env.TOKEN || undefined // environment variable TOKEN must be set
+
+;(async () => {
+  
+    const client = new Discord.Client({ intents: [Intents.FLAGS.GUILDS] });
+
+    client.once("ready", async () => {
+      console.log('Discord.js client ready');
+      Object.values(workingGroups).forEach( async (mappedChannel: string) => {
+        await client.channels.fetch(mappedChannel);
+      })
+    });
+    
+    await client.login(discordBotToken); 
+    console.log('Bot logged in successfully');    
+    
+    connectUpstream().then( async (api: ApiPromise) => {
+        Object.values(eventsMapping).forEach(async (block: number) => {
+            await processBlock(api, client, block)
+        })
+      })
+})()