Joystream Stats 3 tahun lalu
induk
melakukan
7a0df3177d
4 mengubah file dengan 35 tambahan dan 35 penghapusan
  1. 5 5
      api.ts
  2. 1 1
      index.ts
  3. 1 1
      media.ts
  4. 28 28
      rewards.ts

+ 5 - 5
api.ts

@@ -84,7 +84,7 @@ export const getEvents = (
 ): Promise<Vec<EventRecord>> => api.query.system.events.at(hash);
 
 export const getEra = async (api: ApiPromise, hash: Hash): Promise<number> =>
-  Number(api.query.staking.currentEra.at(hash));
+  Number(await api.query.staking.currentEra.at(hash));
 
 export const getEraStake = async (
   api: ApiPromise,
@@ -250,11 +250,11 @@ export const getWorker = (
   id: number
 ): Promise<WorkerOf> => api.query[group].workerById.at(hash, id);
 
-export const getWorkers = (
+export const getWorkers = async (
   api: ApiPromise,
   group: string,
   hash: Hash
-): Promise<number> => api.query[group].activeWorkerCount.at(hash);
+): Promise<number> => Number(await api.query[group].activeWorkerCount.at(hash));
 
 export const getStake = async (
   api: ApiPromise,
@@ -510,12 +510,12 @@ export const getNextEntity = async (
 export const getNextChannel = async (
   api: ApiPromise,
   hash: Hash
-): Promise<number> => api.query.content.nextChannelId.at(hash);
+): Promise<number> => Number(await api.query.content.nextChannelId.at(hash));
 
 export const getNextVideo = async (
   api: ApiPromise,
   hash: Hash
-): Promise<number> => api.query.content.nextVideoId.at(hash);
+): Promise<number> => Number(await api.query.content.nextVideoId.at(hash));
 
 export const getEntity = (
   api: ApiPromise,

+ 1 - 1
index.ts

@@ -3,7 +3,7 @@ import { Mint, MintId } from "@joystream/types/mint";
 import { Moment } from "@polkadot/types/interfaces";
 
 export const getPercent = (value1: number, value2: number): number => {
-  if (value1 == 0) return value2 > 0 ? Infinity : 0;
+  if (value1 == 0) return value2 > 0 ? 100 : 0;
   return Number(((value2 * 100) / value1 - 100).toFixed(2));
 };
 

+ 1 - 1
media.ts

@@ -1,5 +1,5 @@
 import { ApiPromise } from "@polkadot/api";
-import { Vec, Option } from '@polkadot/types';
+import { Vec, Option } from "@polkadot/types";
 import { Hash } from "@joystream/types/common";
 import { Entity } from "@joystream/types/content-directory";
 import { ContentId, DataObject } from "@joystream/types/media";

+ 28 - 28
rewards.ts

@@ -66,8 +66,8 @@ export const getWorkerRewards = async (
     const member: Membership = await getMember(api, memberId, hash);
     const handle = member ? String(member.handle) : account.toString();
     const status = worker.is_active ? `active` : `inactive`;
-    let stake: Stake | undefined = undefined
-    let reward: RewardRelationship | undefined = undefined
+    let stake: Stake;
+    let reward: RewardRelationship;
 
     if (worker.role_stake_profile.isSome) {
       const roleStakeProfile = worker.role_stake_profile.unwrap();
@@ -80,9 +80,7 @@ export const getWorkerRewards = async (
         worker.reward_relationship.unwrap();
       reward = await getWorkerReward(api, hash, rewardId);
     }
-    if (stake && reward) {
-      workers.push({ id, stake, reward, status, handle, account, memberId });
-    }
+    workers.push({ id, stake, reward, status, handle, account, memberId });
   }
   return workers;
 };
@@ -124,32 +122,34 @@ export const getFinalizedSpendingProposals = async (
   api: ApiPromise,
   blocks: [number, CacheEvent[]][]
 ): Promise<SpendingProposal[]> => {
+  const selectedEvents: CacheEvent[] = [];
+  blocks.map(([key, proposalEvents]) =>
+    proposalEvents.map((proposalEvent) => selectedEvents.push(proposalEvent))
+  );
+
   let spendingProposals: SpendingProposal[] = [];
-  await blocks.forEach(([key, proposals]) =>
-    proposals.forEach(async (proposalEvent) => {
-      let statusUpdateData = proposalEvent.data[1] as any;
-      const finalizedAt = statusUpdateData.finalized.finalizedAt;
-      if (!(statusUpdateData.finalized && finalizedAt)) return;
-
-      const proposalId = proposalEvent.data[0] as ProposalId;
-      const id = +proposalId;
-      const proposalInfo: ProposalOf = await getProposalInfo(api, proposalId);
-      const finalizedData = proposalInfo.status.asFinalized;
-      const details: ProposalDetailsOf = await getProposalDetails(
-        api,
-        proposalId
-      );
-      if (!finalizedData.proposalStatus.isApproved || !details.isSpending)
-        return;
+  for (const proposalEvent of selectedEvents) {
+    let statusUpdateData = proposalEvent.data[1] as any;
+    const finalizedAt = statusUpdateData.finalized.finalizedAt;
+    if (!(statusUpdateData.finalized && finalizedAt)) continue;
+
+    const proposalId = proposalEvent.data[0] as ProposalId;
+    const proposalInfo: ProposalOf = await getProposalInfo(api, proposalId);
+    const finalizedData = proposalInfo.status.asFinalized;
+    const details: ProposalDetailsOf = await getProposalDetails(
+      api,
+      proposalId
+    );
+    if (finalizedData.proposalStatus.isApproved && details.isSpending) {
       let approvedData = finalizedData.proposalStatus.asApproved;
-      if (!approvedData.isExecuted) return;
-      if (!spendingProposals.some((proposal) => proposal.id === id)) {
+      if (!approvedData.isExecuted) continue;
+      if (!spendingProposals.some((proposal) => proposal.id === +proposalId)) {
         const title = proposalInfo.title.toString();
         const amount = +details.asSpending[0];
-        spendingProposals.push({ id, title, amount });
+        spendingProposals.push({ id: +proposalId, title, amount });
       }
-    })
-  );
+    }
+  }
   return spendingProposals;
 };
 
@@ -172,7 +172,7 @@ export const getActiveValidators = async (
 ): Promise<AccountId[]> => {
   const block = await getBlock(api, hash);
   let currentBlockNr = block.block.header.number.toNumber();
-  let activeValidators: AccountId[] | undefined = undefined;
+  let activeValidators: AccountId[];
   do {
     const hash: Hash = await getBlockHash(api, currentBlockNr);
     const validators: AccountId[] = await getValidators(api, hash);
@@ -183,6 +183,6 @@ export const getActiveValidators = async (
 
     if (searchPreviousBlocks) --currentBlockNr;
     else ++currentBlockNr;
-  } while (activeValidators == undefined);
+  } while (!activeValidators);
   return activeValidators;
 };