Skip to content

Commit

Permalink
Merge pull request #402 from privacy-scaling-explorations/feat/initTi…
Browse files Browse the repository at this point in the history
…me-duration

feat: update voting start and end time
  • Loading branch information
crisgarner authored Oct 22, 2024
2 parents abe3cd9 + 41b4134 commit d1c6d30
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 29 deletions.
30 changes: 16 additions & 14 deletions packages/coordinator/scripts/uploadRoundMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ interface IUploadMetadataProps {
dotenv.config({ path: path.resolve(import.meta.dirname, "../.env") });

function isValidDate(formattedDateStr: string) {
const splitDate = formattedDateStr.split("-");
const parsed = parse(`${splitDate[2]}/${splitDate[1]}/${splitDate[0]}`, "P", new Date(), { locale: enGB });
const parsed = parse(`${formattedDateStr}Z`, "yyyy/M/d H:m:sX", new Date(), { locale: enGB });
return isValid(parsed);
}

Expand Down Expand Up @@ -66,23 +65,26 @@ export async function collectMetadata(): Promise<RoundMetadata> {

const askStartTime = () =>
new Promise<Date>((resolve, reject) => {
rl.question("When would you like to start this round? (Please respond in the format yyyy-mm-dd) ", (answer) => {
const valid = isValidDate(answer);
rl.question(
"When would you like to start this round? (Please respond in the format {Year}/{Month}/{Day} {Hour}:{Minute}:{Second} in UTC time) ",
(answer) => {
const valid = isValidDate(answer);

if (!valid) {
reject(new Error("Please answer in valid format."));
}
if (!valid) {
reject(new Error("Please answer in valid format."));
}

// eslint-disable-next-line no-console
console.log("You would like to start this round at:", answer);
resolve(new Date(answer));
});
// eslint-disable-next-line no-console
console.log("You would like to start this round at:", answer);
resolve(new Date(answer));
},
);
});

const askRegistrationEndTime = () =>
new Promise<Date>((resolve, reject) => {
rl.question(
"When would you like to end the registration for applications? (Please respond in the format yyyy-mm-dd) ",
"When would you like to end the registration for applications? (Please respond in the format {Year}/{Month}/{Day} {Hour}:{Minute}:{Second} in UTC time) ",
(answer) => {
const valid = isValidDate(answer);

Expand All @@ -100,7 +102,7 @@ export async function collectMetadata(): Promise<RoundMetadata> {
const askVotingStartTime = () =>
new Promise<Date>((resolve, reject) => {
rl.question(
"When would you like to start the voting for this round? (Please respond in the format yyyy-mm-dd) ",
"When would you like to start the voting for this round? (Please respond in the format {Year}/{Month}/{Day} {Hour}:{Minute}:{Second} in UTC time) ",
(answer) => {
const valid = isValidDate(answer);

Expand All @@ -118,7 +120,7 @@ export async function collectMetadata(): Promise<RoundMetadata> {
const askVotingEndTime = () =>
new Promise<Date>((resolve, reject) => {
rl.question(
"When would you like to end the voting for this round? (Please respond in the format yyyy-mm-dd) ",
"When would you like to end the voting for this round? (Please respond in the format {Year}/{Month}/{Day} {Hour}:{Minute}:{Second} in UTC time) ",
(answer) => {
const valid = isValidDate(answer);

Expand Down
18 changes: 12 additions & 6 deletions packages/interface/src/server/api/routers/maci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const PollSchema = z.object({
duration: z.union([z.string(), z.number(), z.bigint()]),
deployTime: z.union([z.string(), z.number(), z.bigint()]),
numSignups: z.union([z.string(), z.number(), z.bigint()]),
initTime: z.union([z.string(), z.number(), z.bigint()]).nullable(),
registryAddress: z.string(),
metadataUrl: z.string(),
}) satisfies ZodType<IPollData>;
Expand All @@ -31,21 +32,26 @@ export const maciRouter = createTRPCRouter({
fetchMetadata<IRoundMetadata>(poll.metadataUrl).then((metadata) => {
const data = metadata as unknown as IRoundMetadata;

const votingStartsAt =
poll.initTime === null ? new Date(data.votingStartsAt) : new Date(Number(poll.initTime) * 1000);
const votingEndsAt =
poll.initTime === null
? new Date(data.votingEndsAt)
: new Date((Number(poll.initTime) + Number(poll.duration)) * 1000);

return {
isMerged: poll.isMerged,
pollId: poll.id,
duration: poll.duration,
deployTime: poll.deployTime,
numSignups: poll.numSignups,
pollAddress: poll.address,
mode: poll.mode,
registryAddress: poll.registryAddress,
roundId: data.roundId,
description: data.description,
startsAt: data.startsAt,
registrationEndsAt: data.registrationEndsAt,
votingStartsAt: data.votingStartsAt,
votingEndsAt: data.votingEndsAt,
startsAt: new Date(data.startsAt),
registrationEndsAt: new Date(data.registrationEndsAt),
votingStartsAt,
votingEndsAt,
tallyFile: data.tallyFile,
} as IRoundData;
}),
Expand Down
6 changes: 3 additions & 3 deletions packages/interface/src/utils/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ export const useRoundState = (roundId: string): ERoundState => {
return ERoundState.DEFAULT;
}

if (round.registrationEndsAt && isAfter(round.registrationEndsAt, now)) {
if (isAfter(round.registrationEndsAt, now)) {
return ERoundState.APPLICATION;
}

if (round.votingEndsAt && isAfter(round.votingEndsAt, now)) {
if (isAfter(round.votingEndsAt, now)) {
return ERoundState.VOTING;
}

if (round.votingEndsAt && isAfter(now, round.votingEndsAt)) {
if (isAfter(now, round.votingEndsAt)) {
return ERoundState.TALLYING;
}

Expand Down
11 changes: 5 additions & 6 deletions packages/interface/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export const AttestationsQuery = `
export interface IPollData extends IGetPollData {
registryAddress: string;
metadataUrl: string;
initTime: bigint | number | string | null;
}

export interface IRoundMetadata {
Expand All @@ -109,17 +110,15 @@ export interface IRoundMetadata {
export interface IRoundData {
isMerged: boolean;
pollId: string;
duration: string;
deployTime: string;
numSignups: string;
pollAddress: string;
mode: string;
registryAddress: string;
roundId: string;
description: string;
startsAt: string;
registrationEndsAt: string;
votingStartsAt: string;
votingEndsAt: string;
startsAt: Date;
registrationEndsAt: Date;
votingStartsAt: Date;
votingEndsAt: Date;
tallyFile: string;
}

0 comments on commit d1c6d30

Please sign in to comment.