tests.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //import TelegramBot from "node-telegram-bot-api";
  2. import { wsLocation } from "../config";
  3. // types
  4. import { Proposals } from "./types";
  5. import { types } from "@joystream/types";
  6. import { ApiPromise, WsProvider } from "@polkadot/api";
  7. import { Header } from "@polkadot/types/interfaces";
  8. // functions
  9. import * as announce from "./lib/announcements";
  10. import * as get from "./lib/getters";
  11. const log = (msg: string): void => console.log(msg);
  12. const sendMessage = log;
  13. const main = async () => {
  14. const provider = new WsProvider(wsLocation);
  15. const api = await ApiPromise.create({ provider, types });
  16. await api.isReady;
  17. const [chain, node, version] = await Promise.all([
  18. api.rpc.system.chain(),
  19. api.rpc.system.name(),
  20. api.rpc.system.version()
  21. ]);
  22. log(`Connected to ${chain} on ${node} v${version}`);
  23. let lastBlock = 0;
  24. let proposals: Proposals = {
  25. last: 1,
  26. current: 2,
  27. active: [],
  28. pending: []
  29. };
  30. let categories = [0, 0];
  31. let posts = [0, 0];
  32. let threads = [0, 0];
  33. let channels = [0, 0];
  34. const unsubscribe = await api.rpc.chain.subscribeNewHeads(
  35. async (block: Header): Promise<void> => {
  36. // council
  37. if (lastBlock > 0) process.exit;
  38. lastBlock = block.number.toNumber();
  39. const currentBlock = block.number.toNumber();
  40. log("current council");
  41. announce.councils(api, currentBlock, sendMessage);
  42. log("first proposal");
  43. announce.proposals(api, proposals, sendMessage);
  44. log("last proposal");
  45. proposals.current = await get.proposalCount(api);
  46. proposals.last = proposals.current - 1;
  47. announce.proposals(api, proposals, sendMessage);
  48. log("first category");
  49. announce.categories(api, categories, sendMessage);
  50. log("last category");
  51. categories[1] = await get.currentCategoryId(api);
  52. categories[0] = categories[1] - 1;
  53. announce.categories(api, categories, sendMessage);
  54. log("first post");
  55. announce.posts(api, posts, sendMessage);
  56. log("last post");
  57. posts[1] = await get.currentPostId(api);
  58. posts[0] = posts[1] - 1;
  59. announce.posts(api, posts, sendMessage);
  60. log("first thread");
  61. announce.threads(api, threads, sendMessage);
  62. log("last thread");
  63. threads[1] = await get.currentThreadId(api);
  64. threads[0] = threads[1] - 1;
  65. announce.threads(api, threads, sendMessage);
  66. log("first channel");
  67. announce.channels(api, channels, sendMessage);
  68. log("last channel");
  69. channels[1] = await get.currentChannelId(api);
  70. channels[0] = channels[1] - 1;
  71. announce.channels(api, channels, sendMessage);
  72. }
  73. );
  74. };
  75. main();