Joystream Stats 3 лет назад
Родитель
Сommit
8dbebba7df
3 измененных файлов с 61 добавлено и 79 удалено
  1. 14 10
      api.ts
  2. 10 23
      rewards.ts
  3. 37 46
      types.ts

+ 14 - 10
api.ts

@@ -33,7 +33,6 @@ import { Mint, MintId } from "@joystream/types/mint";
 import {
   Proposal,
   ProposalId,
-  ProposalDetails,
   DiscussionPost,
   SpendingParams,
   VoteKind,
@@ -44,6 +43,11 @@ import {
   RewardRelationshipId,
 } from "@joystream/types/recurring-rewards";
 import { WorkerId, Worker } from "@joystream/types/working-group";
+import {
+  ProposalOf,
+  ProposalDetailsOf,
+  WorkerOf,
+} from "@joystream/types/augment-codec/all";
 
 // blocks
 export const getBlock = (api: ApiPromise, hash: Hash): Promise<SignedBlock> =>
@@ -233,7 +237,7 @@ export const getWorker = (
   group: string,
   hash: Hash,
   id: number
-): Promise<Worker> => api.query[group].workerById.at(hash, id);
+): Promise<WorkerOf> => api.query[group].workerById.at(hash, id);
 
 export const getWorkers = (
   api: ApiPromise,
@@ -361,22 +365,22 @@ export const getProposalCount = async (
 export const getProposalInfo = async (
   api: ApiPromise,
   id: ProposalId
-): Promise<Proposal> =>
-  (await api.query.proposalsEngine.proposals(id)) as Proposal;
+): Promise<ProposalOf> =>
+  (await api.query.proposalsEngine.proposals(id)) as ProposalOf;
 
 export const getProposalDetails = async (
   api: ApiPromise,
   id: ProposalId
-): Promise<ProposalDetails> =>
+): Promise<ProposalDetailsOf> =>
   (await api.query.proposalsCodex.proposalDetailsByProposalId(
     id
-  )) as ProposalDetails;
+  )) as ProposalDetailsOf;
 
 export const getProposalType = async (
   api: ApiPromise,
   id: ProposalId
 ): Promise<string> => {
-  const details = (await getProposalDetails(api, id)) as ProposalDetails;
+  const details = (await getProposalDetails(api, id)) as ProposalDetailsOf;
   const [type]: string[] = Object.getOwnPropertyNames(details.toJSON());
   return type;
 };
@@ -385,7 +389,7 @@ export const getProposal = async (
   api: ApiPromise,
   id: ProposalId
 ): Promise<ProposalDetail> => {
-  const proposal = await getProposalInfo(api, id);
+  const proposal: ProposalOf = await getProposalInfo(api, id);
   const status: { [key: string]: any } = proposal.status;
   const stage: string = status.isActive ? "Active" : "Finalized";
   const { finalizedAt, proposalStatus } = status[`as${stage}`];
@@ -400,10 +404,10 @@ export const getProposal = async (
   const exec = proposalStatus ? proposalStatus["Approved"] : null;
 
   const { description, parameters, proposerId, votingResults } = proposal;
-  const member = await getMember(api, proposerId);
+  const member: Membership = await getMember(api, proposerId);
   const author = String(member ? member.handle : proposerId);
   const title = proposal.title.toString();
-  const type = await getProposalType(api, id);
+  const type: string = await getProposalType(api, id);
   const args: string[] = [String(id), title, type, stage, result, author];
   const message: string = ``; //formatProposalMessage(args)
   const created: number = Number(proposal.createdAt);

+ 10 - 23
rewards.ts

@@ -1,17 +1,20 @@
 import { ApiPromise } from "@polkadot/api";
-import { Option, Vec } from "@polkadot/types";
+
+// types
+import { Bounty, CacheEvent, WorkerReward } from "./types";
 import { AccountId, Balance } from "@polkadot/types/interfaces";
 import { Hash } from "@polkadot/types/interfaces";
+import { Membership } from "@joystream/types/members";
 import { Mint, MintId } from "@joystream/types/mint";
 import { Stake } from "@joystream/types/stake";
-import { WorkerOf } from "@joystream/types/augment-codec/all";
-import { Bounty, CacheEvent, MintStatistics, WorkerReward } from "./types";
+import { WorkerOf } from "@joystream/types/augment/all";
 import {
   RewardRelationship,
   RewardRelationshipId,
 } from "@joystream/types/recurring-rewards";
 
-import { getPercent, getTotalMinted, momentToString } from "./";
+// lib
+import { getPercent, getTotalMinted } from "./";
 import {
   getBlock,
   getBlockHash,
@@ -76,22 +79,6 @@ export const getBurnedTokens = (
   return tokensBurned;
 };
 
-export const getMintInfo = async (
-  api: ApiPromise,
-  mintId: MintId,
-  startHash: Hash,
-  endHash: Hash
-): Promise<MintStatistics> => {
-  const startMint: Mint = await getMint(api, startHash, mintId);
-  const endMint: Mint = await getMint(api, endHash, mintId);
-  let stats = new MintStatistics();
-  stats.startMinted = getTotalMinted(startMint);
-  stats.endMinted = getTotalMinted(endMint);
-  stats.diffMinted = stats.endMinted - stats.startMinted;
-  stats.percMinted = getPercent(stats.startMinted, stats.endMinted);
-  return stats;
-};
-
 export const getValidatorsRewards = (
   blocks: [number, CacheEvent[]][]
 ): number => {
@@ -114,10 +101,10 @@ export const getActiveValidators = async (
   let activeValidators: AccountId[];
   do {
     const hash: Hash = await getBlockHash(api, currentBlockNr);
-    const validators: Option<Vec<AccountId>> = await getValidators(api, hash);
-    if (!validators.isEmpty) {
+    const validators: AccountId[] = await getValidators(api, hash);
+    if (validators.length) {
       let max = await getValidatorCount(api, hash);
-      activeValidators = Array.from(validators.unwrap()).slice(0, max);
+      activeValidators = validators.slice(0, max);
     }
 
     if (searchPreviousBlocks) --currentBlockNr;

+ 37 - 46
types.ts

@@ -1,40 +1,50 @@
-import { Accountdata } from '@polkadot/types/interfaces'
+import { AccountId, AccountData } from "@polkadot/types/interfaces";
+import { GenericEventData } from "@polkadot/types/generic/Event";
+import { MemberId } from "@joystream/types/members";
+import { VotingResults } from "@joystream/types/proposals";
+import { Stake } from "@joystream/types/stake";
+import { RewardRelationship } from "@joystream/types/recurring-rewards";
 
 export interface AccountBalance {
-  accountId: string
-  balance: AccountData
+  accountId: string;
+  balance: AccountData;
 }
 
 export interface ElectionInfo {
-  durations: number[]
-  stage: any
-  round: number
-  stageEndsAt: number
-  termEndsAt: number
+  durations: number[];
+  stage: any;
+  round: number;
+  stageEndsAt: number;
+  termEndsAt: number;
+}
+
+export interface Vote {
+  vote: string;
+  handle: string;
 }
 
 export interface ProposalDetail {
-  created: number
-  finalizedAt: number
-  message: string
-  parameters: string
-  stage: any
-  result: string
-  exec: any
-  id: number
-  title: string
-  description: any
-  votes: VotingResults
-  type: string
-  votesByAccount?: Vote[]
-  author?: string
-  authorId: number
+  created: number;
+  finalizedAt: number;
+  message: string;
+  parameters: string;
+  stage: any;
+  result: string;
+  exec: any;
+  id: number;
+  title: string;
+  description: any;
+  votes: VotingResults;
+  type: string;
+  votesByAccount?: Vote[];
+  author?: string;
+  authorId: number;
 }
 
 export interface Round {
-  round: number
-  start: number
-  end: number
+  round: number;
+  start: number;
+  end: number;
 }
 
 export class Bounty {
@@ -56,25 +66,6 @@ export class CacheEvent {
   ) {}
 }
 
-export class MintStatistics {
-  startMinted: number;
-  endMinted: number;
-  diffMinted: number;
-  percMinted: number;
-
-  constructor(
-    startMinted: number = 0,
-    endMinted: number = 0,
-    diffMinted: number = 0,
-    percMinted: number = 0
-  ) {
-    this.startMinted = startMinted;
-    this.endMinted = endMinted;
-    this.diffMinted = diffMinted;
-    this.percMinted = percMinted;
-  }
-}
-
 export interface WorkerReward {
   id: number;
   memberId: MemberId;
@@ -83,4 +74,4 @@ export interface WorkerReward {
   stake: Stake;
   reward: RewardRelationship;
   status: string;
-}
+}