Skip to content

Commit

Permalink
split status functions
Browse files Browse the repository at this point in the history
  • Loading branch information
josemarinas committed Jul 24, 2023
1 parent 5c3d30f commit c011b9b
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { isAddress } from "@ethersproject/address";
import { IAddresslistVotingClientMethods } from "../interfaces";
import {
CanVoteParams,
computeProposalStatusFilter,
CreateMajorityVotingProposalParams,
ExecuteProposalStep,
ExecuteProposalStepValue,
Expand All @@ -45,6 +44,7 @@ import {
import {
toAddresslistVotingProposal,
toAddresslistVotingProposalListItem,
computeProposalStatusFilter
} from "../utils";
import { AddresslistVoting__factory } from "@aragon/osx-ethers";
import { toUtf8Bytes } from "@ethersproject/strings";
Expand Down
62 changes: 60 additions & 2 deletions modules/client/src/addresslistVoting/internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {
decodeRatio,
getCompactProposalId,
hexToBytes,
InvalidProposalStatusError,
} from "@aragon/sdk-common";
import {
computeProposalStatus,
SubgraphAction,
SubgraphVoteValuesMap,
VoteValues,
Expand All @@ -21,7 +21,11 @@ import {
SubgraphAddresslistVotingProposalListItem,
SubgraphAddresslistVotingVoterListItem,
} from "./types";
import { DaoAction, ProposalMetadata } from "@aragon/sdk-client-common";
import {
DaoAction,
ProposalMetadata,
ProposalStatus,
} from "@aragon/sdk-client-common";

export function toAddresslistVotingProposal(
proposal: SubgraphAddresslistVotingProposal,
Expand Down Expand Up @@ -144,3 +148,57 @@ export function addresslistVotingInitParamsToContract(
params.addresses,
];
}

export function computeProposalStatus(
proposal:
| SubgraphAddresslistVotingProposal
| SubgraphAddresslistVotingProposalListItem,
): ProposalStatus {
const now = new Date();
const startDate = new Date(
parseInt(proposal.startDate) * 1000,
);
const endDate = new Date(parseInt(proposal.endDate) * 1000);
if (proposal.executed) {
return ProposalStatus.EXECUTED;
}
if (startDate >= now) {
return ProposalStatus.PENDING;
}
if (proposal.potentiallyExecutable || proposal.earlyExecutable) {
return ProposalStatus.SUCCEEDED;
}
if (endDate >= now) {
return ProposalStatus.ACTIVE;
}
return ProposalStatus.DEFEATED;
}

export function computeProposalStatusFilter(status: ProposalStatus) {
let where = {};
const now = Math.round(new Date().getTime() / 1000).toString();
switch (status) {
case ProposalStatus.PENDING:
where = { startDate_gte: now };
break;
case ProposalStatus.ACTIVE:
where = { startDate_lt: now, endDate_gte: now, executed: false };
break;
case ProposalStatus.EXECUTED:
where = { executed: true };
break;
case ProposalStatus.SUCCEEDED:
where = { potentiallyExecutable: true, endDate_lt: now };
break;
case ProposalStatus.DEFEATED:
where = {
potentiallyExecutable: false,
endDate_lt: now,
executed: false,
};
break;
default:
throw new InvalidProposalStatusError();
}
return where;
}
8 changes: 0 additions & 8 deletions modules/client/src/client-common/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,6 @@ export type SubgraphProposalBase = {
potentiallyExecutable: boolean;
};

export interface IComputeStatusProposal {
startDate: string;
endDate: string;
executed: boolean;
earlyExecutable?: boolean;
potentiallyExecutable: boolean;
}

export type ProposalQueryParams = Pagination & {
sortBy?: ProposalSortBy;
status?: ProposalStatus;
Expand Down
57 changes: 0 additions & 57 deletions modules/client/src/client-common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@ import { IDAO } from "@aragon/osx-ethers";
import { VoteValues, VotingMode } from "./types/plugin";
import {
CreateMajorityVotingProposalParams,
IComputeStatusProposal,
} from "./types/plugin";

import {
InvalidProposalStatusError,
InvalidVotingModeError,
} from "@aragon/sdk-common";
import { FAILING_PROPOSAL_AVAILABLE_FUNCTION_SIGNATURES } from "./internal";
import {
DaoAction,
DecodedApplyInstallationParams,
getFunctionFragment,
ProposalStatus,
} from "@aragon/sdk-client-common";

import { Result } from "@ethersproject/abi";
Expand All @@ -34,60 +31,6 @@ export function unwrapProposalParams(
];
}

export function computeProposalStatus(
proposal: IComputeStatusProposal,
): ProposalStatus {
const now = new Date();
const startDate = new Date(
parseInt(proposal.startDate) * 1000,
);
const endDate = new Date(parseInt(proposal.endDate) * 1000);
if (proposal.executed) {
return ProposalStatus.EXECUTED;
}
if (startDate >= now) {
return ProposalStatus.PENDING;
}
if (proposal.potentiallyExecutable || proposal.earlyExecutable) {
return ProposalStatus.SUCCEEDED;
}
if (endDate >= now) {
return ProposalStatus.ACTIVE;
}
return ProposalStatus.DEFEATED;
}

export function computeProposalStatusFilter(
status: ProposalStatus,
): Object {
let where = {};
const now = Math.round(new Date().getTime() / 1000).toString();
switch (status) {
case ProposalStatus.PENDING:
where = { startDate_gte: now };
break;
case ProposalStatus.ACTIVE:
where = { startDate_lt: now, endDate_gte: now, executed: false };
break;
case ProposalStatus.EXECUTED:
where = { executed: true };
break;
case ProposalStatus.SUCCEEDED:
where = { potentiallyExecutable: true, endDate_lt: now };
break;
case ProposalStatus.DEFEATED:
where = {
potentiallyExecutable: false,
endDate_lt: now,
executed: false,
};
break;
default:
throw new InvalidProposalStatusError();
}
return where;
}

export function votingModeToContracts(votingMode: VotingMode): number {
switch (votingMode) {
case VotingMode.STANDARD:
Expand Down
3 changes: 1 addition & 2 deletions modules/client/src/multisig/internal/client/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {
SubgraphMultisigVotingSettings,
} from "../types";
import {
computeProposalStatusFilter,
ExecuteProposalStep,
ExecuteProposalStepValue,
ProposalCreationSteps,
Expand All @@ -49,7 +48,7 @@ import {
QueryMultisigProposals,
QueryMultisigVotingSettings,
} from "../graphql-queries";
import { toMultisigProposal, toMultisigProposalListItem } from "../utils";
import { computeProposalStatusFilter, toMultisigProposal, toMultisigProposalListItem } from "../utils";
import { toUtf8Bytes } from "@ethersproject/strings";
import { IMultisigClientMethods } from "../interfaces";
import {
Expand Down
61 changes: 58 additions & 3 deletions modules/client/src/multisig/internal/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { getCompactProposalId, hexToBytes } from "@aragon/sdk-common";
import { computeProposalStatus, SubgraphAction } from "../../client-common";
import { InvalidProposalStatusError, getCompactProposalId, hexToBytes } from "@aragon/sdk-common";
import { SubgraphAction } from "../../client-common";
import { MultisigProposal, MultisigProposalListItem } from "../types";
import {
SubgraphMultisigProposal,
SubgraphMultisigProposalListItem,
} from "./types";
import { DaoAction, ProposalMetadata } from "@aragon/sdk-client-common";
import {
DaoAction,
ProposalMetadata,
ProposalStatus,
} from "@aragon/sdk-client-common";

export function toMultisigProposal(
proposal: SubgraphMultisigProposal,
Expand Down Expand Up @@ -98,3 +102,54 @@ export function toMultisigProposalListItem(
status: computeProposalStatus(proposal),
};
}

export function computeProposalStatus(
proposal: SubgraphMultisigProposal | SubgraphMultisigProposalListItem,
): ProposalStatus {
const now = new Date();
const startDate = new Date(
parseInt(proposal.startDate) * 1000,
);
const endDate = new Date(parseInt(proposal.endDate) * 1000);
if (proposal.executed) {
return ProposalStatus.EXECUTED;
}
if (now < startDate) {
return ProposalStatus.PENDING;
}
if (proposal.potentiallyExecutable && now < endDate) {
return ProposalStatus.SUCCEEDED;
}
if (now < endDate) {
return ProposalStatus.ACTIVE;
}
return ProposalStatus.DEFEATED;
}

export function computeProposalStatusFilter(status: ProposalStatus) {
let where = {};
const now = Math.round(new Date().getTime() / 1000).toString();
switch (status) {
case ProposalStatus.PENDING:
where = { startDate_gte: now };
break;
case ProposalStatus.ACTIVE:
where = { startDate_lt: now, endDate_gte: now, executed: false };
break;
case ProposalStatus.EXECUTED:
where = { executed: true };
break;
case ProposalStatus.SUCCEEDED:
where = { potentiallyExecutable: true, endDate_gte: now };
break;
case ProposalStatus.DEFEATED:
where = {
endDate_lt: now,
executed: false,
};
break;
default:
throw new InvalidProposalStatusError();
}
return where;
}
2 changes: 1 addition & 1 deletion modules/client/src/tokenVoting/internal/client/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
} from "@aragon/sdk-common";
import {
CanVoteParams,
computeProposalStatusFilter,
CreateMajorityVotingProposalParams,
ExecuteProposalStep,
ExecuteProposalStepValue,
Expand Down Expand Up @@ -70,6 +69,7 @@ import {
QueryTokenVotingSettings,
} from "../graphql-queries";
import {
computeProposalStatusFilter,
tokenVotingInitParamsToContract,
toTokenVotingMember,
toTokenVotingProposal,
Expand Down
57 changes: 56 additions & 1 deletion modules/client/src/tokenVoting/internal/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
computeProposalStatus,
SubgraphAction,
SubgraphVoteValuesMap,
VoteValues,
Expand Down Expand Up @@ -34,10 +33,12 @@ import {
decodeRatio,
getCompactProposalId,
hexToBytes,
InvalidProposalStatusError,
} from "@aragon/sdk-common";
import {
DaoAction,
ProposalMetadata,
ProposalStatus,
TokenType,
} from "@aragon/sdk-client-common";

Expand Down Expand Up @@ -276,3 +277,57 @@ export function toTokenVotingMember(
}),
};
}



export function computeProposalStatus(
proposal: SubgraphTokenVotingProposal | SubgraphTokenVotingProposalListItem,
): ProposalStatus {
const now = new Date();
const startDate = new Date(
parseInt(proposal.startDate) * 1000,
);
const endDate = new Date(parseInt(proposal.endDate) * 1000);
if (proposal.executed) {
return ProposalStatus.EXECUTED;
}
if (startDate >= now) {
return ProposalStatus.PENDING;
}
if (proposal.potentiallyExecutable || proposal.earlyExecutable) {
return ProposalStatus.SUCCEEDED;
}
if (endDate >= now) {
return ProposalStatus.ACTIVE;
}
return ProposalStatus.DEFEATED;
}

export function computeProposalStatusFilter(status: ProposalStatus) {
let where = {};
const now = Math.round(new Date().getTime() / 1000).toString();
switch (status) {
case ProposalStatus.PENDING:
where = { startDate_gte: now };
break;
case ProposalStatus.ACTIVE:
where = { startDate_lt: now, endDate_gte: now, executed: false };
break;
case ProposalStatus.EXECUTED:
where = { executed: true };
break;
case ProposalStatus.SUCCEEDED:
where = { potentiallyExecutable: true, endDate_lt: now };
break;
case ProposalStatus.DEFEATED:
where = {
potentiallyExecutable: false,
endDate_lt: now,
executed: false,
};
break;
default:
throw new InvalidProposalStatusError();
}
return where;
}
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,6 @@ describe("Client Multisig", () => {
QueryMultisigProposals,
{
where: {
potentiallyExecutable: false,
endDate_lt: nowFilter,
executed: false,
},
Expand Down

0 comments on commit c011b9b

Please sign in to comment.