Ver código fonte

heartbeat: add token value

Joystream Stats 3 anos atrás
pai
commit
9c7deb566f

+ 1 - 0
community-contributions/joystreamtelegrambot/package.json

@@ -24,6 +24,7 @@
   },
   "dependencies": {
     "@joystream/types": "^0.16.1",
+    "axios": "^0.21.1",
     "discord.js": "^12.5.3",
     "moment": "^2.29.1",
     "node-telegram-bot-api": "^0.51.0",

+ 3 - 2
community-contributions/joystreamtelegrambot/src/bot.ts

@@ -40,6 +40,7 @@ client.on("ready", async () => {
   discordChannels.council = await findDiscordChannel("council");
   discordChannels.proposals = await findDiscordChannel("proposals-bot");
   discordChannels.forum = await findDiscordChannel("forum-bot");
+  discordChannels.tokenomics = await findDiscordChannel("tokenomics");
 });
 
 const findDiscordChannel = (name: string) =>
@@ -190,13 +191,13 @@ const main = async () => {
       // heartbeat
       if (timestamp > lastHeartbeat + heartbeat) {
         const time = passedTime(lastHeartbeat, timestamp);
-        blocks = announce.heartbeat(
+        blocks = await announce.heartbeat(
           api,
           blocks,
           time,
           proposals,
           sendMessage,
-          discordChannels.proposals
+          discordChannels.tokenomics
         );
         lastHeartbeat = block.timestamp;
       }

+ 5 - 2
community-contributions/joystreamtelegrambot/src/lib/announcements.ts

@@ -17,6 +17,7 @@ import {
   memberHandle,
   memberHandleByAccount,
   proposalDetail,
+  fetchTokenValue,
 } from "./getters";
 import moment from "moment";
 
@@ -238,14 +239,15 @@ export const proposals = async (
 const getAverage = (array: number[]): number =>
   array.reduce((a: number, b: number) => a + b, 0) / array.length;
 
-export const heartbeat = (
+export const heartbeat = async (
   api: Api,
   blocks: Block[],
   timePassed: string,
   proposals: Proposals,
   sendMessage: Send,
   channel: any
-): [] => {
+): Promise<[]> => {
+  const price = await fetchTokenValue();
   const durations = blocks.map((b) => b.duration);
   const blocktime = getAverage(durations) / 1000;
 
@@ -273,6 +275,7 @@ export const heartbeat = (
 
   const msg = `  ${blocks.length} blocks produced in ${timePassed}
   Blocktime: ${blocktime.toFixed(3)}s
+  Price: ${price}
   Stake: ${avgStake.toFixed(1)} / ${avgIssued.toFixed()} M tJOY (${percent}%)
   Validators: ${avgVals.toFixed()} (${reward} tJOY/h)
   Nominators: ${getAverage(noms).toFixed()}

+ 6 - 0
community-contributions/joystreamtelegrambot/src/lib/getters.ts

@@ -1,4 +1,5 @@
 import { formatProposalMessage } from "./announcements";
+import axios from "axios";
 
 //types
 
@@ -108,3 +109,8 @@ export const proposalDetail = async (
   const createdAt: number = proposal.createdAt.toNumber();
   return { createdAt, finalizedAt, parameters, message, stage, result, exec };
 };
+
+export const fetchTokenValue = async () => {
+  const { data } = await axios.get("https://status.joystream.org/status");
+  return data ? `${Math.floor(+data.price * 100000000) / 100} $` : `?`;
+};