council.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const { registerJoystreamTypes } = require('@joystream/types');
  2. const { ApiPromise, WsProvider } = require('@polkadot/api');
  3. const TelegramBot = require('node-telegram-bot-api');
  4. // replace the value below with the Telegram token you receive from @BotFather
  5. const token = 'yourowntoken';
  6. // Create a bot that uses 'polling' to fetch new updates
  7. const bot = new TelegramBot(token);
  8. //get chat id here https://stackoverflow.com/questions/32423837/telegram-bot-how-to-get-a-group-chat-id
  9. const chatid = 'yourownchat';
  10. async function main () {
  11. registerJoystreamTypes()
  12. // Create the API and wait until ready
  13. const api = await ApiPromise.create({
  14. provider: new WsProvider()
  15. })
  16. var lastcouncilnotif = 0
  17. const unsubscribe = await api.rpc.chain.subscribeNewHeads(async (header) => {
  18. const block = header.number.toNumber()
  19. console.log(`Current block ${block}`)
  20. if (block>lastcouncilnotif) {
  21. const councilround = await api.query.councilElection.round()
  22. const councilendterm = (await api.query.council.termEndsAt()).toNumber()
  23. const annperiod = (await api.query.councilElection.announcingPeriod()).toNumber()
  24. const votingperiod = (await api.query.councilElection.votingPeriod()).toNumber()
  25. const revealingperiod = (await api.query.councilElection.revealingPeriod()).toNumber()
  26. const councilstage = await getcouncilStage(api)
  27. const councilperiod = (await api.query.councilElection.newTermDuration()).toNumber()
  28. switch (councilstage){
  29. case null:
  30. console.log('Council has been elected')
  31. if (block>lastcouncilnotif){
  32. bot.sendMessage(chatid, `<a href="https://testnet.joystream.org/#/council/members"> New council for round ${councilround}</a> has been elected at block ${councilendterm-councilperiod}.`, { parse_mode: 'html' })
  33. lastcouncilnotif=councilendterm
  34. }
  35. break;
  36. default:
  37. const annstage = councilstage.Announcing
  38. const votingstage = councilstage.Voting
  39. const revealingstage = councilstage.Revealing
  40. if (annstage) {
  41. console.log('Announcing Stage')
  42. if (block>lastcouncilnotif){
  43. bot.sendMessage(chatid, `New council election for round ${councilround} has been started at block ${annstage-annperiod}.<a href="https://testnet.joystream.org/#/council/applicants"> You can apply now!</a>`, { parse_mode: 'html' })
  44. lastcouncilnotif=annstage
  45. }
  46. }
  47. if (votingstage) {
  48. console.log('Voting Stage')
  49. if (block>lastcouncilnotif){
  50. bot.sendMessage(chatid, `Voting stage for council election has been started at block ${votingstage-votingperiod}. <a href="https://testnet.joystream.org/#/council/applicants">You can vote now!</a>`, { parse_mode: 'html' })
  51. lastcouncilnotif=votingstage
  52. }
  53. }
  54. if (revealingstage) {
  55. console.log('Revealing Stage')
  56. if (block>lastcouncilnotif){
  57. bot.sendMessage(chatid, `Revealing stage for council election has been started at block ${revealingstage-revealingperiod}. <a href="https://testnet.joystream.org/#/council/votes">Don't forget to reveal your vote!</a>`, { parse_mode: 'html' })
  58. lastcouncilnotif=revealingstage
  59. }
  60. }
  61. break;
  62. }
  63. }
  64. })
  65. }
  66. const getcouncilStage = async (api) => {
  67. const councilstage = await api.query.councilElection.stage()
  68. const councilstagejson = councilstage.toJSON()
  69. return councilstagejson
  70. }
  71. main()