Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create use thresholds hook and use correct thresholds for reject origin #4588

Closed
wants to merge 12 commits into from
43 changes: 24 additions & 19 deletions packages/apps-config/src/api/params/proposalThresholds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,34 @@

import { KULUPU_GENESIS, KUSAMA_GENESIS, POLKADOT_GENESIS } from '../constants';

export const PROPOSE_THRESHOLDS: Record<string, number> = {
[KULUPU_GENESIS]: 0.8,
[KUSAMA_GENESIS]: 0.5,
[POLKADOT_GENESIS]: 0.6,
default: 0.5
export interface Threshold {
value: number;
option: 'AtLeast' | 'MoreThan';

Choose a reason for hiding this comment

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

does it make sense to create an enum for these options? for simplifying further maintaining and exclude typos

}

export const PROPOSE_THRESHOLDS: Record<string, Threshold> = {
[KULUPU_GENESIS]: { option: 'MoreThan', value: 0.8 },
[KUSAMA_GENESIS]: { option: 'AtLeast', value: 0.5 },
[POLKADOT_GENESIS]: { option: 'AtLeast', value: 0.6 },
default: { option: 'AtLeast', value: 0.5 }
};

export const REJECT_THRESHOLDS: Record<string, number> = {
[KULUPU_GENESIS]: 0.5,
[KUSAMA_GENESIS]: 0.5,
[POLKADOT_GENESIS]: 0.5,
default: 0.5
export const REJECT_THRESHOLDS: Record<string, Threshold> = {
[KULUPU_GENESIS]: { option: 'MoreThan', value: 0.5 },
[KUSAMA_GENESIS]: { option: 'MoreThan', value: 0.5 },
[POLKADOT_GENESIS]: { option: 'MoreThan', value: 0.5 },
default: { option: 'MoreThan', value: 0.5 }
};

export const SLASH_THRESHOLDS: Record<string, number> = {
[KUSAMA_GENESIS]: 0.5,
[POLKADOT_GENESIS]: 0.75,
default: 0.5
export const SLASH_THRESHOLDS: Record<string, Threshold> = {
[KUSAMA_GENESIS]: { option: 'AtLeast', value: 0.5 },
[POLKADOT_GENESIS]: { option: 'AtLeast', value: 0.75 },
default: { option: 'AtLeast', value: 0.5 }
};

export const TREASURY_THRESHOLDS: Record<string, number> = {
[KULUPU_GENESIS]: 0.5,
[KUSAMA_GENESIS]: 0.6,
[POLKADOT_GENESIS]: 0.6,
default: 0.6
export const TREASURY_THRESHOLDS: Record<string, Threshold> = {
[KULUPU_GENESIS]: { option: 'MoreThan', value: 0.5 },
[KUSAMA_GENESIS]: { option: 'AtLeast', value: 0.6 },
[POLKADOT_GENESIS]: { option: 'AtLeast', value: 0.6 },
default: { option: 'AtLeast', value: 0.6 }
};
28 changes: 19 additions & 9 deletions packages/react-hooks/src/useThresholds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

import { useMemo } from 'react';

import { PROPOSE_THRESHOLDS, REJECT_THRESHOLDS, SLASH_THRESHOLDS, TREASURY_THRESHOLDS } from '@polkadot/apps-config';
import { PROPOSE_THRESHOLDS,
REJECT_THRESHOLDS,
SLASH_THRESHOLDS,
Threshold,
TREASURY_THRESHOLDS } from '@polkadot/apps-config';
jacogr marked this conversation as resolved.
Show resolved Hide resolved

import { useApi } from './useApi';
import { useMembers } from './useMembers';
Expand All @@ -25,6 +29,16 @@ export function getAtLeastThresholdMembersCount (membersCount: number, threshold
return Math.ceil(membersCount * thresholdRatio);

Choose a reason for hiding this comment

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

shall we add if (membersCount === 0) { return 0; } here, as it's done above?

}

function getThreshold (membersCount: number, threshold: Threshold): number {
return threshold.option === 'AtLeast'
? getAtLeastThresholdMembersCount(
membersCount,
threshold.value
)
: getMoreThanThresholdMembersCount(membersCount,
threshold.value);
jacogr marked this conversation as resolved.
Show resolved Hide resolved
}

export function useThresholds () : Thresholds {
const { api } = useApi();
const { members } = useMembers();
Expand All @@ -39,20 +53,16 @@ export function useThresholds () : Thresholds {
const genesisHash = api.genesisHash.toHex();

return {
proposalThreshold: getAtLeastThresholdMembersCount(
membersCount,
proposalThreshold: getThreshold(membersCount,
PROPOSE_THRESHOLDS[genesisHash] || PROPOSE_THRESHOLDS.default
),
slashProposalThreshold: getAtLeastThresholdMembersCount(
membersCount,
slashProposalThreshold: getThreshold(membersCount,
SLASH_THRESHOLDS[genesisHash] || SLASH_THRESHOLDS.default
),
treasuryProposalThreshold: getAtLeastThresholdMembersCount(
membersCount,
treasuryProposalThreshold: getThreshold(membersCount,
TREASURY_THRESHOLDS[genesisHash] || TREASURY_THRESHOLDS.default
),
treasuryRejectionThreshold: getMoreThanThresholdMembersCount(
membersCount,
treasuryRejectionThreshold: getThreshold(membersCount,
REJECT_THRESHOLDS[genesisHash] || REJECT_THRESHOLDS.default
)
};
Expand Down