Skip to content

Commit

Permalink
fix: match quorum formula based on contracts
Browse files Browse the repository at this point in the history
Onchain spaces only take for and abstain votes into quorum progress.
Against votes are not counted.

Offchain spaces take all votes into account (for oSnap/SafeSnap).
  • Loading branch information
Sekhmet committed Dec 19, 2024
1 parent 614aee1 commit 2867e47
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 16 deletions.
5 changes: 3 additions & 2 deletions apps/ui/src/components/ProposalExecutionsList.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script setup lang="ts">
import { getGenericExplorerUrl } from '@/helpers/explorer';
import { getProposalCurrentQuorum } from '@/helpers/quorum';
import { buildBatchFile } from '@/helpers/safe/ build';
import { getExecutionName } from '@/helpers/ui';
import { shorten, toBigIntOrNumber } from '@/helpers/utils';
Expand Down Expand Up @@ -101,8 +102,8 @@ function downloadExecution(execution: ProposalExecution) {
proposal.executions &&
proposal.executions.length > 0 &&
proposal.scores.length > 0 &&
toBigIntOrNumber(proposal.scores_total) >=
toBigIntOrNumber(proposal.quorum) &&
getProposalCurrentQuorum(proposal.network, proposal) >
proposal.quorum &&
toBigIntOrNumber(proposal.scores[0]) >
toBigIntOrNumber(proposal.scores[1]) &&
proposal.has_execution_window_opened
Expand Down
32 changes: 31 additions & 1 deletion apps/ui/src/helpers/quorum.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
import { describe, expect, it } from 'vitest';
import { formatQuorum } from './quorum';
import { formatQuorum, getProposalCurrentQuorum } from './quorum';

describe('getProposalCurrentQuorum', () => {
it('should only use for and abstain votes for onchain spaces', () => {
const currentQuorum = getProposalCurrentQuorum('eth', {
scores: [11, 20, 100],
scores_total: 131
});

expect(currentQuorum).toBe(111);
});

it('should use total score for offchain spaces', () => {
const currentQuorum = getProposalCurrentQuorum('s', {
scores: [11, 20, 100],
scores_total: 131
});

expect(currentQuorum).toBe(131);
});

it('should only use against votes for rejection quorum', () => {
const currentQuorum = getProposalCurrentQuorum('s', {
scores: [11, 20, 100],
scores_total: 131,
quorum_type: 'rejection'
});

expect(currentQuorum).toBe(20);
});
});

describe('formatQuorum', () => {
it('should format using 3 significant digits', () => {
Expand Down
28 changes: 21 additions & 7 deletions apps/ui/src/helpers/quorum.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import { Proposal } from '@/types';
import { offchainNetworks } from '@/networks';
import { NetworkID, Proposal } from '@/types';

const REJECTION_QUORUM_CHOICE_INDEX = 1;

export function quorumProgress(proposal: Proposal): number {
const totalScore =
proposal.quorum_type === 'rejection'
? proposal.scores[REJECTION_QUORUM_CHOICE_INDEX] ?? 0
: proposal.scores_total;
export function getProposalCurrentQuorum(
networkId: NetworkID,
proposal: {
scores: number[];
scores_total: number;
quorum_type?: string;
}
) {
if (offchainNetworks.includes(networkId)) {
return proposal.quorum_type === 'rejection'
? Number(proposal.scores[REJECTION_QUORUM_CHOICE_INDEX] ?? 0)
: Number(proposal.scores_total);
}

// Only For and Abstain votes are counted towards quorum progress in SX spaces.
return Number(proposal.scores[0]) + Number(proposal.scores[2]);
}

return totalScore / proposal.quorum;
export function quorumProgress(proposal: Proposal): number {
return getProposalCurrentQuorum(proposal.network, proposal) / proposal.quorum;
}

export function quorumChoiceProgress(
Expand Down
11 changes: 8 additions & 3 deletions apps/ui/src/networks/common/graphqlApi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
InMemoryCache
} from '@apollo/client/core';
import { CHAIN_IDS } from '@/helpers/constants';
import { getProposalCurrentQuorum } from '@/helpers/quorum';
import { getNames } from '@/helpers/stamp';
import { clone, compareAddresses } from '@/helpers/utils';
import { getNetwork } from '@/networks';
Expand Down Expand Up @@ -57,20 +58,24 @@ type ApiOptions = {
};

function getProposalState(
networkId: NetworkID,
proposal: ApiProposal,
current: number
): ProposalState {
// we have broken types, we should unify, this is quick fix for Nimbora PR
// those values are actually strings
// https://github.com/snapshot-labs/sx-monorepo/pull/529/files#r1691071502
const quorum = BigInt(proposal.quorum);
const scoresTotal = BigInt(proposal.scores_total);
const currentQuorum = getProposalCurrentQuorum(networkId, {
scores: [proposal.scores_1, proposal.scores_2, proposal.scores_3],
scores_total: proposal.scores_total
});
const scoresFor = BigInt(proposal.scores_1);
const scoresAgainst = BigInt(proposal.scores_2);

if (proposal.executed) return 'executed';
if (proposal.max_end <= current) {
if (scoresTotal < quorum) return 'rejected';
if (currentQuorum < quorum) return 'rejected';
return scoresFor > scoresAgainst ? 'passed' : 'rejected';
}
if (proposal.start > current) return 'pending';
Expand Down Expand Up @@ -294,7 +299,7 @@ function formatProposal(
)
? proposal.max_end <= current
: proposal.min_end <= current,
state: getProposalState(proposal, current),
state: getProposalState(networkId, proposal, current),
network: networkId,
privacy: null,
quorum: +proposal.quorum,
Expand Down
15 changes: 12 additions & 3 deletions apps/ui/src/networks/offchain/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from '@apollo/client/core';
import { CHAIN_IDS } from '@/helpers/constants';
import { parseOSnapTransaction } from '@/helpers/osnap';
import { getProposalCurrentQuorum } from '@/helpers/quorum';
import { getNames } from '@/helpers/stamp';
import { clone } from '@/helpers/utils';
import {
Expand Down Expand Up @@ -70,9 +71,17 @@ const DELEGATION_STRATEGIES = [

const DELEGATE_REGISTRY_URL = 'https://delegate-registry-api.snapshot.box';

function getProposalState(proposal: ApiProposal): ProposalState {
function getProposalState(
networkId: NetworkID,
proposal: ApiProposal
): ProposalState {
if (proposal.state === 'closed') {
if (proposal.scores_total < proposal.quorum) return 'rejected';
const currentQuorum = getProposalCurrentQuorum(networkId, {
scores: proposal.scores,
scores_total: proposal.scores_total
});

if (currentQuorum < proposal.quorum) return 'rejected';
return proposal.type !== 'basic' || proposal.scores[0] > proposal.scores[1]
? 'passed'
: 'rejected';
Expand Down Expand Up @@ -256,7 +265,7 @@ function formatProposal(proposal: ApiProposal, networkId: NetworkID): Proposal {
];
}

const state = getProposalState(proposal);
const state = getProposalState(networkId, proposal);

return {
id: proposal.id,
Expand Down

0 comments on commit 2867e47

Please sign in to comment.