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

handle all arc.js datetime types #2002

Merged
merged 5 commits into from
Jul 26, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { operationNotifierObserver } from "actions/arcActions";
import { IRootState } from "reducers";
import { Observable, of } from "rxjs";
import { map, mergeMap, toArray, first } from "rxjs/operators";
import { safeMoment } from "lib/util";

/**
* Defined in the order that Competition cards should be sorted in the List component.
Expand Down Expand Up @@ -72,10 +73,10 @@ export class CompetitionStatus {

export const competitionStatus = (competition: ICompetitionProposalState): CompetitionStatus => {
const now = moment();
const startTime = moment(competition.startTime);
const submissionsEndTime = moment(competition.suggestionsEndTime);
const votingStartTime = moment(competition.votingStartTime);
const endTime = moment(competition.endTime);
const startTime = safeMoment(competition.startTime);
const submissionsEndTime = safeMoment(competition.suggestionsEndTime);
const votingStartTime = safeMoment(competition.votingStartTime);
const endTime = safeMoment(competition.endTime);
const hasSubmissions = !!competition.totalSuggestions;
const hasWinners = !!competition.numberOfWinningSuggestions;
let status: CompetitionStatusEnum;
Expand Down
6 changes: 3 additions & 3 deletions src/components/Proposal/Voting/VoteButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import classNames from "classnames";
import Reputation from "components/Account/Reputation";
import { ActionTypes, default as PreTransactionModal } from "components/Shared/PreTransactionModal";
import Analytics from "lib/analytics";
import { fromWei, targetedNetwork } from "lib/util";
import { fromWei, targetedNetwork, safeMoment } from "lib/util";
import { Page } from "pages";
import * as React from "react";
import { connect } from "react-redux";
Expand Down Expand Up @@ -105,7 +105,7 @@ class VoteButtons extends React.Component<IProps, IState> {
(proposalState.stage === IProposalStage.Boosted && expired) ||
(proposalState.stage === IProposalStage.QuietEndingPeriod && expired) ||
(currentAccountState && currentAccountState.reputation.eq(new BN(0))) ||
(currentAccountState && (proposalState.createdAt < currentAccountState.createdAt) &&
(currentAccountState && (safeMoment(proposalState.createdAt) < safeMoment(currentAccountState.createdAt)) &&
//this is a workaround till https://github.com/daostack/subgraph/issues/548
(targetedNetwork() !== "ganache")) ||
currentVote === IProposalOutcome.Pass ||
Expand Down Expand Up @@ -133,7 +133,7 @@ class VoteButtons extends React.Component<IProps, IState> {
* 5) when `currentAccount` is not found in the subgraph, then a fake `currentAccountState` is created with
* rep == 0
*/
(currentAccountState && (proposalState.createdAt < currentAccountState.createdAt)) ?
(currentAccountState && (safeMoment(proposalState.createdAt) < safeMoment(currentAccountState.createdAt))) ?
"Must have had reputation in this DAO when the proposal was created" :
proposalState.stage === IProposalStage.ExpiredInQueue ||
(proposalState.stage === IProposalStage.Boosted && expired) ||
Expand Down
21 changes: 11 additions & 10 deletions src/lib/proposalHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as moment from "moment";

import { IProposalOutcome, IProposalStage, IProposalState } from "@daostack/arc.js";
import { safeMoment } from "lib/util";

export interface IRedemptionState {
accountAddress: string;
Expand All @@ -18,44 +19,44 @@ export interface IRedemptionState {
voterReputation: number;
}

export const closingTime = (proposal: IProposalState) => {
export const closingTime = (proposal: IProposalState): moment.Moment => {
switch (proposal.stage) {
case IProposalStage.ExpiredInQueue:
case IProposalStage.Queued:
return moment((Number(proposal.createdAt.toString()) + proposal.genesisProtocolParams.queuedVotePeriodLimit) * 1000);
return safeMoment(proposal.createdAt).add(proposal.genesisProtocolParams.queuedVotePeriodLimit, "seconds");
case IProposalStage.PreBoosted:
return moment((proposal.preBoostedAt + proposal.genesisProtocolParams.preBoostedVotePeriodLimit) * 1000);
return safeMoment(proposal.preBoostedAt).add(proposal.genesisProtocolParams.preBoostedVotePeriodLimit, "seconds");
case IProposalStage.Boosted:
return moment((proposal.boostedAt + proposal.genesisProtocolParams.boostedVotePeriodLimit) * 1000);
return safeMoment(proposal.boostedAt).add(proposal.genesisProtocolParams.boostedVotePeriodLimit, "seconds");
case IProposalStage.QuietEndingPeriod:
return moment((proposal.quietEndingPeriodBeganAt + proposal.genesisProtocolParams.quietEndingPeriod) * 1000);
return safeMoment(proposal.quietEndingPeriodBeganAt).add(proposal.genesisProtocolParams.quietEndingPeriod, "seconds");
case IProposalStage.Executed:
return moment(proposal.executedAt * 1000);
return safeMoment(proposal.executedAt);
}
};

export function proposalExpired(proposal: IProposalState) {
export function proposalExpired(proposal: IProposalState): boolean {
const res = (
(proposal.stage === IProposalStage.ExpiredInQueue) ||
(proposal.stage === IProposalStage.Queued && closingTime(proposal) <= moment())
);
return res;
}

export function proposalEnded(proposal: IProposalState) {
export function proposalEnded(proposal: IProposalState): boolean {
const res = (
(proposal.stage === IProposalStage.Executed) || proposalExpired(proposal));
return res;
}

export function proposalPassed(proposal: IProposalState) {
export function proposalPassed(proposal: IProposalState): boolean {
const res = (
(proposal.stage === IProposalStage.Executed && proposal.winningOutcome === IProposalOutcome.Pass)
);
return res;
}

export function proposalFailed(proposal: IProposalState) {
export function proposalFailed(proposal: IProposalState): boolean {
const res = (
(proposal.stage === IProposalStage.Executed && proposal.winningOutcome === IProposalOutcome.Fail) ||
proposalExpired(proposal)
Expand Down
8 changes: 4 additions & 4 deletions src/lib/proposalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import moment = require("moment-timezone");

const cloneDeep = require("clone-deep");

export function importUrlValues<Values>(defaultValues: Values) {
export function importUrlValues<Values>(defaultValues: Values): any {
const { search } = window.location;
const params = new URLSearchParams(search);
const initialFormValues: any = cloneDeep([defaultValues])[0];
Expand Down Expand Up @@ -41,14 +41,14 @@ export function importUrlValues<Values>(defaultValues: Values) {
return initialFormValues;
}

export const exportUrl = (values: any) => {
const setQueryString = (key: string) => {
export const exportUrl = (values: unknown): void => {
const setQueryString = (key: keyof typeof values): string => {
if (values[key] === undefined) {
return "";
}
if (typeof values[key] === "object") {
if (moment.isMoment(values[key])) {
return `${key}=${values[key].toString()}`;
return `${key}=${(values[key] as moment.Moment).toString()}`;
} else {
return `${key}=${JSON.stringify(values[key])}`;
}
Expand Down
26 changes: 25 additions & 1 deletion src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ export async function getNetworkId(web3Provider?: Web3Provider): Promise<string>
return network.chainId.toString();
} else {
const promise = new Promise<string>((resolve, reject) => {
web3Provider.send("net_version", (error, response) => {
web3Provider.send("net_version", (error: any, response: any) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it related?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it is just the usual typescript lint warning improvements, an ongoing improvement...does not affect execution of the code.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, yes I assume also the boolean in proposalHelpers.ts

if (error) {
reject(error);
} else {
Expand Down Expand Up @@ -634,3 +634,27 @@ export function ethBalance(address: Address): Observable<BN> {
const arc = getArc();
return arc.ethBalance(address);
}

/**
* arc.js is inconsistent in how it returns datetimes.
* Convert all possibilities safely to a `moment`.
* Is OK when the dateSpecifier is already a moment.
* If is a string, must be ISO-conformant.
* @param dateSpecifier
*/
export function safeMoment(dateSpecifier: moment.Moment | Date | number | string | undefined): moment.Moment {
switch (typeof dateSpecifier) {
case "object":
if (moment.isMoment(dateSpecifier)) {
return dateSpecifier;
}
// else assume is a Date, fallthrough
case "string":
return moment(dateSpecifier);
case "number":
// then should be a count of seconds in UNIX epoch
return moment.unix(dateSpecifier);
default:
throw new Error(`safeMoment: unknown type: ${typeof dateSpecifier}`);
}
}