tests.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //import TelegramBot from "node-telegram-bot-api";
  2. import { wsLocation } from "../config";
  3. // types
  4. import { Council, 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 council: Council = { round: 0, last: "" };
  24. let lastBlock: number = 0;
  25. let proposals: Proposals = {
  26. last: 1,
  27. current: 2,
  28. active: [],
  29. executing: [],
  30. };
  31. let categories = [0, 0];
  32. let posts = [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. council = await announce.council(api, council, currentBlock, sendMessage);
  42. lastBlock = currentBlock;
  43. log("first proposal");
  44. announce.proposals(api, proposals, lastBlock, sendMessage);
  45. log("last proposal");
  46. proposals.current = await get.proposalCount(api);
  47. proposals.last = proposals.current - 1;
  48. announce.proposals(api, proposals, lastBlock, sendMessage);
  49. log("first category");
  50. announce.categories(api, categories, sendMessage);
  51. log("last category");
  52. categories[1] = await get.currentCategoryId(api);
  53. categories[0] = categories[1] - 1;
  54. announce.categories(api, categories, sendMessage);
  55. log("first post");
  56. announce.posts(api, posts, sendMessage);
  57. log("last post");
  58. posts[1] = await get.currentPostId(api);
  59. posts[0] = posts[1] - 1;
  60. announce.posts(api, posts, sendMessage);
  61. log("first channel");
  62. announce.channels(api, channels, sendMessage);
  63. log("last channel");
  64. channels[1] = await get.currentChannelId(api);
  65. channels[0] = channels[1] - 1;
  66. announce.channels(api, channels, sendMessage);
  67. }
  68. );
  69. };
  70. main();