App.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import React from "react";
  2. import "bootstrap/dist/css/bootstrap.min.css";
  3. import "./index.css";
  4. import { Routes, Loading } from "./components";
  5. import moment from "moment";
  6. import * as get from "./lib/getters";
  7. import { domain, wsLocation } from "./config";
  8. // types
  9. import { Api, Block, NominatorsEntries, Proposals } from "./types";
  10. import { types } from "@joystream/types";
  11. import { ApiPromise, WsProvider } from "@polkadot/api";
  12. import { Header } from "@polkadot/types/interfaces";
  13. import { ValidatorId } from "@polkadot/types/interfaces";
  14. interface IProps {}
  15. interface IState {
  16. block: number;
  17. blocks: Block[];
  18. nominators: string[];
  19. validators: string[];
  20. loading: boolean;
  21. council: any;
  22. channels: number[];
  23. proposals: any;
  24. posts: number[];
  25. categories: number[];
  26. threads: number[];
  27. domain: string;
  28. proposalCount: number;
  29. handles: any;
  30. }
  31. const initialState = {
  32. blocks: [],
  33. block: 0,
  34. loading: true,
  35. nominators: [],
  36. validators: [],
  37. channels: [],
  38. posts: [],
  39. council: [],
  40. categories: [],
  41. threads: [],
  42. proposals: [],
  43. proposalCount: 0,
  44. domain,
  45. handles: {},
  46. };
  47. class App extends React.Component<IProps, IState> {
  48. async initializeSocket() {
  49. const provider = new WsProvider(wsLocation);
  50. const api = await ApiPromise.create({ provider, types });
  51. await api.isReady;
  52. //this.setState({loading:false})
  53. // const [chain, node, version] = await Promise.all([
  54. // api.rpc.system.chain(),
  55. // api.rpc.system.name(),
  56. // api.rpc.system.version()
  57. // ]);
  58. // this.setState({ chain, node, version, loading: false });
  59. let blocks: Block[] = [];
  60. let lastBlock: Block = { id: 0, timestamp: 0, duration: 6 };
  61. let openingId = await get.nextOpeningId(api);
  62. let nextWorkerId = await get.nextWorkerId(api);
  63. let channels = [];
  64. channels[0] = await get.currentChannelId(api);
  65. let posts = [];
  66. posts[0] = await get.currentPostId(api);
  67. let categories = [];
  68. categories[0] = await get.currentCategoryId(api);
  69. let threads = [];
  70. threads[0] = await get.currentThreadId(api);
  71. let { proposals } = this.state;
  72. const proposalCount = await get.proposalCount(api);
  73. for (let i = proposalCount; i > 0; i--) {
  74. this.fetchProposal(api, i);
  75. }
  76. //proposals.last = await get.proposalCount(api);
  77. //proposals.active = await get.activeProposalCount(api);
  78. //proposals.executing = await get.pendingProposals(api);
  79. this.setState({ channels, proposalCount, posts, categories, threads });
  80. const council: any = await api.query.council.activeCouncil();
  81. console.log(`council`, council);
  82. council.map((seat: any) => this.fetchHandle(api, seat.member));
  83. // count nominators and validators
  84. const validatorEntries = await api.query.session.validators();
  85. const nominatorEntries = await api.query.staking.nominators.entries();
  86. const validators = await validatorEntries.map((v) => {
  87. this.fetchHandle(api, v.toJSON());
  88. return String(v);
  89. });
  90. console.log(`validators`, validators);
  91. const nominators = nominatorEntries.map((n) => {
  92. const name = n[0].toHuman();
  93. this.fetchHandle(api, `${name}`);
  94. return `${name}`;
  95. });
  96. console.log(`nominators`, nominatorEntries);
  97. this.setState({ council, nominators, validators, loading: false });
  98. api.rpc.chain.subscribeNewHeads(
  99. async (header: Header): Promise<void> => {
  100. // current block
  101. const id = header.number.toNumber();
  102. if (blocks.find((b) => b.id === id)) return;
  103. const timestamp = (await api.query.timestamp.now()).toNumber();
  104. const duration = timestamp - lastBlock.timestamp;
  105. const block: Block = { id, timestamp, duration };
  106. blocks = blocks.concat(block);
  107. this.setState({ blocks, block: id });
  108. channels[1] = await get.currentChannelId(api);
  109. proposals.current = await get.proposalCount(api);
  110. categories[1] = await get.currentCategoryId(api);
  111. posts[1] = await get.currentPostId(api);
  112. threads[1] = await get.currentThreadId(api);
  113. lastBlock = block;
  114. // test storage providers
  115. // if (block.timestamp > lastCheck + checkPeriod) {
  116. // lastCheck = block.timestamp;
  117. // }
  118. // new storage provider (or lead) opportunity is opened
  119. // const nextOpeningId = await get.nextOpeningId(api);
  120. // if (nextOpeningId > openingId) {
  121. // openingId = nextOpeningId;
  122. // }
  123. // storage provider (or lead) opportunity is closed
  124. // const workerId = await get.nextWorkerId(api);
  125. // if (workerId > nextWorkerId) {
  126. // const worker = await api.query.storageWorkingGroup.workerById(
  127. // workerId - 1
  128. // );
  129. // const memberId = worker.member_id.toJSON();
  130. // const handle: string = await get.memberHandle(api, memberId);
  131. // nextWorkerId = workerId;
  132. // }
  133. lastBlock = block;
  134. }
  135. );
  136. }
  137. async fetchProposal(api: Api, id: number) {
  138. const proposal = await get.proposalDetail(api, id);
  139. let { proposals } = this.state;
  140. proposals[id] = proposal;
  141. this.setState({ proposals });
  142. }
  143. async fetchHandle(api: Api, id: string) {
  144. const handle = await get.memberHandleByAccount(api, id);
  145. //if (handle === "") return;
  146. let { handles } = this.state;
  147. handles[String(id)] = handle;
  148. this.setState({ handles });
  149. }
  150. // async fetchData() {
  151. // // inital axios requests go here
  152. // this.setState({ loading: false });
  153. // }
  154. render() {
  155. if (this.state.loading) return <Loading />;
  156. return <Routes {...this.state} />;
  157. }
  158. componentDidMount() {
  159. this.initializeSocket();
  160. //this.fetchData()
  161. }
  162. componentWillUnmount() {
  163. console.log("unmounting...");
  164. }
  165. constructor(props: any) {
  166. super(props);
  167. this.state = initialState;
  168. this.fetchProposal = this.fetchProposal.bind(this);
  169. this.fetchHandle = this.fetchHandle.bind(this);
  170. }
  171. }
  172. export default App;