Bladeren bron

update getFinalizedSpendingProposals

Joystream Stats 3 jaren geleden
bovenliggende
commit
d8dece7311
2 gewijzigde bestanden met toevoegingen van 55 en 4 verwijderingen
  1. 49 4
      rewards.ts
  2. 6 0
      types.ts

+ 49 - 4
rewards.ts

@@ -1,11 +1,17 @@
 import { ApiPromise } from "@polkadot/api";
 
 // types
-import { Bounty, CacheEvent, WorkerReward } from "./types";
+import { Bounty, CacheEvent, WorkerReward, SpendingProposal } 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 {
+  Proposal,
+  ProposalId,
+  SpendingParams,
+} from "@joystream/types/proposals";
+import { ProposalDetails, ProposalOf } from "@joystream/types/augment/types";
 import { Stake } from "@joystream/types/stake";
 import {
   RewardRelationship,
@@ -22,17 +28,22 @@ import {
   getMember,
   getWorker,
   getWorkerReward,
+  getProposalInfo,
+  getProposalDetails,
   getStake,
   getValidators,
   getValidatorCount,
 } from "./api";
-import {WorkerOf} from "@joystream/types/augment-codec/all";
+import { WorkerOf } from "@joystream/types/augment-codec/all";
+import { ProposalDetailsOf } from "@joystream/types/augment/types";
 
 export const filterMethods = {
   getBurnedTokens: ({ section, method }: CacheEvent) =>
     section === "balances" && method === "Transfer",
   newValidatorsRewards: ({ section, method }: CacheEvent) =>
     section === "staking" && method === "Reward",
+  finalizedSpendingProposals: ({ section, method }: CacheEvent) =>
+    section === "proposalsEngine" && method === "ProposalStatusUpdated",
 };
 
 export const getWorkerRewards = async (
@@ -60,7 +71,8 @@ export const getWorkerRewards = async (
 
     if (worker.reward_relationship.isSome) {
       // TODO changing salaries are not reflected
-      const rewardId: RewardRelationshipId = worker.reward_relationship.unwrap();
+      const rewardId: RewardRelationshipId =
+        worker.reward_relationship.unwrap();
       reward = await getWorkerReward(api, hash, rewardId);
     }
     workers.push({ id, stake, reward, status, handle, account, memberId });
@@ -95,12 +107,45 @@ export const getBurnedTokens = (
     transfers.forEach((transfer) => {
       let receiver = transfer.data[1] as AccountId;
       let amount = transfer.data[2] as Balance;
-      if (receiver.toString() === burnAddress) tokensBurned = Number(amount);
+      if (receiver.toString() === burnAddress) tokensBurned += Number(amount);
     })
   );
   return tokensBurned;
 };
 
+export const getFinalizedSpendingProposals = async (
+  api: ApiPromise,
+  blocks: [number, CacheEvent[]][]
+): Promise<SpendingProposal[]> => {
+  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;
+      let approvedData = finalizedData.proposalStatus.asApproved;
+      if (!approvedData.isExecuted) return;
+      if (!spendingProposals.some((proposal) => proposal.id === id)) {
+        const title = proposalInfo.title.toString();
+        const amount = +details.asSpending[0];
+        spendingProposals.push({ id, title, amount });
+      }
+    })
+  );
+  return spendingProposals;
+};
+
 export const getValidatorsRewards = (
   blocks: [number, CacheEvent[]][]
 ): number => {

+ 6 - 0
types.ts

@@ -41,6 +41,12 @@ export interface ProposalDetail {
   authorId: number;
 }
 
+export interface SpendingProposal {
+  id: number;
+  title: string;
+  amount: number;
+}
+
 export interface Round {
   round: number;
   start: number;