Ver código fonte

20230223: Removed some chain endpoint api calls

mkbeefcake 1 ano atrás
pai
commit
8c46d4a1c0
3 arquivos alterados com 54 adições e 21 exclusões
  1. 41 14
      src/App.tsx
  2. 1 1
      src/components/Dashboard/index.tsx
  3. 12 6
      src/lib/election.ts

+ 41 - 14
src/App.tsx

@@ -214,26 +214,26 @@ const App = (props: {}) => {
     // getTokenomics().then((tokenomics) => save(`tokenomics`, tokenomics));
 
     // let { status, councils } = this.state;
-    // status.election = await updateElection(api);
-    // if (status.election?.stage) getElectionStatus(api);
+    // let _status = status;
+    // _status.election = await updateElection(api);
+    // if (_status.election?.stage) getElectionStatus(api);
     // councils.forEach((c) => {
     //   if (c?.round > status.council) status.council = c;
     // });
 
-    // let hash: string = await api.rpc.chain.getBlockHash(1);
-    // if (hash)
-    //   status.startTime = (await api.query.timestamp.now.at(hash)).toNumber();
+    let hash: string = await api.rpc.chain.getBlockHash(1);
+    if (hash)
+      _status.startTime = (await api.query.timestamp.now.at(hash)).toJSON();
 
-    const nextMemberId = await await api.query.members.nextMemberId();
-    // setMembers(nextMemberId - 1);
-    setProposals(await get.proposalCount(api));
-    setPosts(await get.currentPostId(api));
-    setThreads(await get.currentThreadId(api));
-    setCategories(await get.currentCategoryId(api));
-    // status.proposalPosts = await api.query.proposalsDiscussion.postCount();
+    console.log(`NextMemberID: `, (await api.query.members.nextMemberId()).toJSON());
+    console.log(`ProposalCount: `, await get.proposalCount(api));
+    console.log(`CurrentPostId: `, await get.currentPostId(api));
+    console.log(`CurrentThreadId: `, await get.currentThreadId(api));
+    console.log(`CurrentCategoryId: `, await get.currentCategoryId(api));
+    _status.proposalPosts = await api.query.proposalsDiscussion.postCount();
 
     await updateEra(api, status.era).then(async (era) => {
-      let _status = {era: 0, lastReward:0, validatorStake: 0}
+      // let _status = {era: 0, lastReward:0, validatorStake: 0}
       _status.era = era;
       _status.lastReward = await getLastReward(api, era);
       _status.validatorStake = await getTotalStake(api, era);
@@ -243,9 +243,36 @@ const App = (props: {}) => {
 
     });
 
-     // return status;
+    setStatus(_status)
+    save("status", _status);
+  // return status;
   }  
 
+  const getElectionStatus = async (api: ApiPromise): Promise<any> => {
+    getCouncilSize(api).then((councilSize) => {
+      let _election = election;
+      _election.councilSize = councilSize;
+      
+      setElection(_election);
+      save("election", _election);
+    });
+    getVotes(api).then((votes) => {
+      let _election = election;
+      _election.votes = votes;
+
+      setElection(_election);
+      save("election", election);
+    });
+    getCouncilApplicants(api).then((applicants) => {
+      let _election = election;
+      _election.applicants = applicants;
+
+      setElection(_election);
+      save("election", election);
+    });
+  }
+
+
   const updateEra = async (api: ApiPromise, old: number) => {
     const era = Number(await api.query.staking.currentEra());
     if (era === old) 

+ 1 - 1
src/components/Dashboard/index.tsx

@@ -31,7 +31,7 @@ const Dashboard = (props: IProps) => {
 			", From: " + new Date(council.electedAt.timestamp) + 
 			", Councilors: [ " + council.councilors.map(each => each.member.handle).join(", ") + " ]")
 
-	}, [])
+	}, [council])
  
 
   return (

+ 12 - 6
src/lib/election.ts

@@ -13,14 +13,20 @@ export const finalizedBlockHeight = async (api: ApiPromise) => {
 export const finalizedHash = (api: ApiPromise) =>
   api.rpc.chain.getFinalizedHead();
 
-export const getCouncilSize = async (api: ApiPromise): Promise<number> =>
-  Number((await api.query.councilElection.councilSize()).toJSON());
+export const getCouncilSize = async (api: ApiPromise): Promise<number> => {
+  const councilSize: any = await api.query.councilElection.councilSize();
+  return councilSize.toJSON() || 0;
+}
 
-export const getCouncilRound = async (api: ApiPromise): Promise<Number> =>
-  Number((await api.query.councilElection.round()).toJSON());
+export const getCouncilRound = async (api: ApiPromise): Promise<number> =>{
+  const councilRound: any = await api.query.councilElection.round();
+  return councilRound.toJSON() || 0;
+}
 
-export const getTermEndsAt = async (api: ApiPromise): Promise<Number> =>
-  Number((await api.query.council.termEndsAt()).toJSON());
+export const getTermEndsAt = async (api: ApiPromise): Promise<number> => {
+  const termsEndsAt: any = await api.query.council.termEndsAt();
+  return termsEndsAt.toJSON() || 0;
+}
 
 export const getElectionStage = async (
   api: ApiPromise