From 055f636360d390d299cbee4fdf616ce0268afe58 Mon Sep 17 00:00:00 2001 From: Steve Finkelstein Date: Tue, 9 Aug 2022 02:13:59 -0400 Subject: [PATCH] Add timeperiod validation for proposal entries (#674) * Add timeperiod validation for proposal entries Signed-off-by: Steve Finkelstein * Add timeperiod space validation to index export Signed-off-by: Steve Finkelstein Co-authored-by: mkt --- src/validations/index.ts | 4 +++- src/validations/timeperiod/examples.json | 14 ++++++++++++ src/validations/timeperiod/index.ts | 27 ++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/validations/timeperiod/examples.json create mode 100644 src/validations/timeperiod/index.ts diff --git a/src/validations/index.ts b/src/validations/index.ts index ba48e05da..440609eae 100644 --- a/src/validations/index.ts +++ b/src/validations/index.ts @@ -1,9 +1,11 @@ import basic from './basic'; import aaveSpaceValidation from './aave'; import nounsSpaceValidation from './nouns'; +import timeperiodSpaceValidation from './timeperiod'; export default { basic, aave: aaveSpaceValidation, - nouns: nounsSpaceValidation + nouns: nounsSpaceValidation, + timeperiod: timeperiodSpaceValidation }; diff --git a/src/validations/timeperiod/examples.json b/src/validations/timeperiod/examples.json new file mode 100644 index 000000000..02381e38e --- /dev/null +++ b/src/validations/timeperiod/examples.json @@ -0,0 +1,14 @@ +[ + { + "name": "Example of a prop entry time constrained validation", + "validation": { + "name": "timeperiod", + "params": { + "propEntryStart": 1659108444521, + "propEntryEnd": 1659194844521 + } + }, + "network": "1", + "userAddress": "" + } +] diff --git a/src/validations/timeperiod/index.ts b/src/validations/timeperiod/index.ts new file mode 100644 index 000000000..193ab54c7 --- /dev/null +++ b/src/validations/timeperiod/index.ts @@ -0,0 +1,27 @@ +export default async function validate( + author: string, + space, + proposal, + options +): Promise { + const onlyMembers = options.onlyMembers || space.filters?.onlyMembers; + const members = (space.members || []).map((address) => address.toLowerCase()); + const {propEntryStart = 0, propEntryEnd = 0} = options; + + if (!propEntryStart || !propEntryEnd || (propEntryStart >= propEntryEnd)) return false; + + if (members.includes(author.toLowerCase())) return true; + + if (onlyMembers) return false; + + const now = new Date().getTime(); + const startTime = new Date(propEntryStart).getTime(); + const endTime = new Date(propEntryEnd).getTime(); + + // Only allow proposals being submitted in this time window. + if (now >= startTime && now <= endTime) { + return true; + } + + return false; +}