Skip to content
This repository has been archived by the owner on Nov 14, 2022. It is now read-only.

Spike: Referendum-Relayer: Display veto percentage #17

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/referendum-relayer/libs/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@ export interface ReferendumMessageBody {
proposal: ProposalInterface;
referendum?: ReferendumInterface;
vetoSum: number;
vetoThreshold?: number;
vetoPercentage?: string;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should vetoPercentage be number too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a string as I'm using toFixed(), I was tossing up between using 0 and 2 decimal places, what do you think?

}

export interface VetoSumFields {
vetoThreshold: number;
vetoPercentage: string;
}
25 changes: 25 additions & 0 deletions packages/referendum-relayer/libs/utils/fetchVetoPercentage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { Api } from "@cennznet/api";
import type { u128, Permill, EraIndex } from "@cennznet/types";

export async function fetchVetoThreshold(cennzApi: Api): Promise<number> {
const referendumThreshold = (
(await cennzApi.query.governance.referendumThreshold()) as Permill
).toNumber();

return referendumThreshold / 10000;
}

export async function fetchVetoPercentage(
cennzApi: Api,
vetoSum: number
): Promise<string> {
const stakingEra = (
(await cennzApi.query.staking.currentEra()) as EraIndex
).toJSON();

const totalStaked = (
(await cennzApi.query.staking.erasTotalStake(stakingEra)) as u128
).toNumber();

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Promise.all here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cennzApi.query.staking.erasTotalStake relies on the result from the first promise

return (vetoSum / totalStaked).toFixed();
}
5 changes: 3 additions & 2 deletions packages/referendum-relayer/libs/utils/getDiscordMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
ProposalStatus,
} from "@proposal-relayer/libs/types";
import type { DiscordMessage } from "@gov-libs/types";
import type { VetoSumFields } from "@referendum-relayer/libs/types";

import {
getReferendumFields,
Expand All @@ -19,7 +20,7 @@ export function getDiscordMessage(
proposalStatus: ProposalStatus,
proposalDetails: ProposalDetails,
proposalInfo: ProposalInfo,
vetoSum: number | undefined
vetoSumFields: VetoSumFields
): DiscordMessage {
const referendumFields = getReferendumFields(proposalDetails, proposalInfo);

Expand All @@ -35,7 +36,7 @@ export function getDiscordMessage(
proposalDetails.title,
referendumFields,
proposalStatus === "ReferendumDeliberation"
? getVetoSumField(vetoSum)
? getVetoSumField(vetoSumFields)
: undefined
),
],
Expand Down
9 changes: 7 additions & 2 deletions packages/referendum-relayer/libs/utils/getVetoFields.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { VetoSumFields } from "@referendum-relayer/libs/types";

import { MessageActionRow, MessageButton, EmbedFieldData } from "discord.js";
import { REFERENDUM_URL } from "@referendum-relayer/libs/constants";

Expand All @@ -14,11 +16,14 @@ function getVetoLink(proposalId: number): string {
return `${REFERENDUM_URL}/${proposalId}?stage=referendum`;
}

export function getVetoSumField(vetoSum: number): EmbedFieldData[] {
export function getVetoSumField({
vetoThreshold,
vetoPercentage,
}: VetoSumFields): EmbedFieldData[] {
return [
{
name: "Veto Sum",
value: `_**${vetoSum}**_`,
value: `_**${vetoPercentage} / ${vetoThreshold} %**_`,
inline: false,
},
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const logger = getLogger("ReferendumProcessor");

export async function handleReferendumNewMessage(
discordWebhook: InteractionWebhook,
{ proposalId, proposal, vetoSum }: ReferendumMessageBody,
{ proposalId, proposal, vetoSum, vetoThreshold }: ReferendumMessageBody,
queue: AMQPQueue,
message: AMQPMessage,
abortSignal: AbortSignal
Expand Down Expand Up @@ -53,7 +53,7 @@ export async function handleReferendumNewMessage(
status as ProposalStatus,
proposalDetails,
proposalInfo,
vetoSum
{ vetoPercentage: "0", vetoThreshold }
);

const { id: discordMessageId } = await discordWebhook.send(discordMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ const logger = getLogger("ReferendumProcessor");

export async function handleReferendumUpdateMessage(
discordWebhook: InteractionWebhook,
{ proposalId, proposal, referendum, vetoSum }: ReferendumMessageBody,
{
proposalId,
proposal,
referendum,
vetoSum,
vetoPercentage,
vetoThreshold,
}: ReferendumMessageBody,
queue: AMQPQueue,
message: AMQPMessage,
abortSignal: AbortSignal
Expand Down Expand Up @@ -53,7 +60,7 @@ export async function handleReferendumUpdateMessage(
status as ProposalStatus,
proposalDetails,
proposalInfo,
status === "ReferendumDeliberation" ? vetoSum : undefined
{ vetoPercentage, vetoThreshold }
);

await discordWebhook.editMessage(
Expand Down
22 changes: 18 additions & 4 deletions packages/referendum-relayer/libs/utils/monitorVetoSum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { ReferendumVoteCount, StorageKey } from "@cennznet/types";
import { getLogger } from "@gov-libs/utils/getLogger";
import { Referendum } from "@referendum-relayer/libs/models";
import { Proposal } from "@proposal-relayer/libs/models";
import { fetchVetoThreshold, fetchVetoPercentage } from "./fetchVetoPercentage";

const logger = getLogger("ReferendumListener");

Expand All @@ -29,23 +30,36 @@ export async function monitorVetoSum(
const proposal = await Proposal.findOne({ proposalId });
if (!proposal || proposal?.status === "Deliberation") return;

const vetoThreshold = await fetchVetoThreshold(cennzApi);

if (!referendum) {
logger.info(
"Referendum #%d: New referendum, sent to queue...",
proposalId
);
queue.publish(JSON.stringify({ proposalId, proposal, vetoSum }), {
type: "new",
});
queue.publish(
JSON.stringify({ proposalId, proposal, vetoSum, vetoThreshold }),
{
type: "new",
}
);
}

if (referendum) {
logger.info(
"Referendum #%d: Update found, sent to queue...",
proposalId
);

const vetoPercentage = await fetchVetoPercentage(cennzApi, vetoSum);
queue.publish(
JSON.stringify({ proposalId, proposal, referendum, vetoSum }),
JSON.stringify({
proposalId,
proposal,
referendum,
vetoThreshold,
vetoPercentage,
}),
{
type: "update",
}
Expand Down