Skip to content

Commit

Permalink
feat: allow custom strategies in basic validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Sekhmet committed Oct 2, 2024
1 parent de1436d commit b3de73a
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 7 deletions.
8 changes: 7 additions & 1 deletion apps/ui/src/components/FormSpaceProposal.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { VALIDATION_TYPES_INFO } from '@/helpers/constants';
import { getValidator } from '@/helpers/validation';
import { Validation } from '@/types';
import { NetworkID, Space, Validation } from '@/types';
const GUIDELINES_DEFINITION = {
type: 'string',
Expand Down Expand Up @@ -36,6 +36,9 @@ const template = defineModel<string>('template', {
});
defineProps<{
networkId: NetworkID;
space: Space;
snapshotChainId: string;
isProposalValidationValid: boolean;
}>();
Expand Down Expand Up @@ -130,6 +133,9 @@ watchEffect(() => {
<ModalSelectValidation
type="proposal"
:open="isSelectValidationModalOpen"
:network-id="networkId"
:default-chain-id="snapshotChainId"
:space="space"
:current="proposalValidation"
@close="isSelectValidationModalOpen = false"
@save="value => (proposalValidation = value)"
Expand Down
4 changes: 4 additions & 0 deletions apps/ui/src/components/FormSpaceVoting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const ignoreAbstainVotes = defineModel<boolean>('ignoreAbstainVotes', {
});
const props = defineProps<{
snapshotChainId: string;
space: Space;
}>();
Expand Down Expand Up @@ -308,6 +309,9 @@ watchEffect(() => {
<ModalSelectValidation
type="voting"
:open="isSelectValidationModalOpen"
:network-id="space.network"
:default-chain-id="snapshotChainId"
:space="space"
:current="voteValidation"
@close="isSelectValidationModalOpen = false"
@save="value => (voteValidation = value)"
Expand Down
62 changes: 57 additions & 5 deletions apps/ui/src/components/Modal/SelectValidation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ const validations = ref([] as ValidationDetails[]);
import { VALIDATION_TYPES_INFO } from '@/helpers/constants';
import { clone } from '@/helpers/utils';
import { getValidator } from '@/helpers/validation';
import { Validation } from '@/types';
import { StrategyConfig } from '@/networks/types';
import { NetworkID, Space, Validation } from '@/types';
const props = defineProps<{
open: boolean;
networkId: NetworkID;
defaultChainId: string;
space: Space;
type: 'voting' | 'proposal';
current?: Validation;
}>();
Expand All @@ -36,6 +40,8 @@ const hasError = ref(false);
const selectedValidation = ref(null as ValidationDetails | null);
const form = ref({} as Record<string, any>);
const rawParams = ref('{}');
const useCustomStrategies = ref(false);
const customStrategies = ref([] as StrategyConfig[]);
async function fetchValidations() {
if (isLoading.value || validations.value.length) return;
Expand All @@ -56,9 +62,6 @@ async function fetchValidations() {
const filteredValidations = computed(() => {
const apiValidations = validations.value.filter(validation => {
// TODO: add support for basic
if (validation.key === 'basic') return false;
if (props.type === 'proposal') return true;
return !validation.proposalValidationOnly;
Expand Down Expand Up @@ -155,7 +158,27 @@ function handleSelect(validationDetails: ValidationDetails) {
}
selectedValidation.value = validationDetails;
if (selectedValidation.value.key === 'passport-gated') {
if (selectedValidation.value.key !== props.current?.name) {
form.value = {};
rawParams.value = '{}';
}
if (selectedValidation.value.key === 'basic') {
if (form.value.strategies) {
useCustomStrategies.value = true;
customStrategies.value = form.value.strategies.map(strategy => ({
id: crypto.randomUUID(),
chainId: strategy.network,
address: strategy.name,
name: strategy.name,
paramsDefinition: null,
params: clone(strategy.params)
}));
}
form.value.strategies ??= [];
} else if (selectedValidation.value.key === 'passport-gated') {
form.value.scoreThreshold ??= 0;
form.value.operator ??= 'NONE';
form.value.stamps ??= [];
Expand All @@ -166,6 +189,19 @@ function handleApply() {
if (!selectedValidation.value) return;
const params = definition.value ? form.value : JSON.parse(rawParams.value);
if (selectedValidation.value.key === 'basic') {
if (useCustomStrategies.value) {
params.strategies = customStrategies.value.map(strategy => ({
name: strategy.name,
network: strategy.chainId,
params: strategy.params
}));
} else {
delete params.strategies;
}
}
emit('save', { name: selectedValidation.value.key, params });
emit('close');
}
Expand Down Expand Up @@ -220,6 +256,22 @@ watch(
}"
:error="formErrors.rawParams"
/>
<template v-if="selectedValidation.key === 'basic'">
<UiSwitch
v-model="useCustomStrategies"
title="Use custom strategies"
tooltip="Calculate the score with a different configuration of Voting Strategies"
/>
<UiStrategiesConfiguratorOffchain
v-if="useCustomStrategies"
v-model="customStrategies"
class="mt-3"
allow-duplicates
hide-empty-message
:network-id="networkId"
:default-chain-id="defaultChainId"
/>
</template>
</div>
<UiSelector
v-for="validation in filteredValidations"
Expand Down
6 changes: 5 additions & 1 deletion apps/ui/src/components/Ui/Switch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const enabled = defineModel<boolean>({ required: true });
defineProps<{
title: string;
tooltip?: string;
}>();
</script>

Expand All @@ -24,8 +25,11 @@ defineProps<{
<IC-switch-disabled v-else class="text-skin-text" />
</span>
</Switch>
<SwitchLabel class="text-skin-link">
<SwitchLabel class="text-skin-link truncate flex items-center gap-1">
{{ title }}
<UiTooltip v-if="tooltip" :title="tooltip">
<IH-question-mark-circle class="shrink-0" />
</UiTooltip>
</SwitchLabel>
</div>
</SwitchGroup>
Expand Down
4 changes: 4 additions & 0 deletions apps/ui/src/views/Space/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,9 @@ watchEffect(() => setTitle(`Edit settings - ${props.space.name}`));
v-model:only-members="onlyMembers"
v-model:guidelines="guidelines"
v-model:template="template"
:network-id="space.network"
:snapshot-chain-id="snapshotChainId"
:space="space"
:is-proposal-validation-valid="isProposalValidationValid"
@update-validity="v => (hasProposalErrors = !v)"
/>
Expand All @@ -414,6 +417,7 @@ watchEffect(() => setTitle(`Edit settings - ${props.space.name}`));
v-model:privacy="privacy"
v-model:vote-validation="voteValidation"
v-model:ignore-abstain-votes="ignoreAbstainVotes"
:snapshot-chain-id="snapshotChainId"
:space="space"
@update-validity="v => (hasVotingErrors = !v)"
/>
Expand Down

0 comments on commit b3de73a

Please sign in to comment.