Skip to content

Commit

Permalink
Feat/web3 1193 create values and ranges (#339)
Browse files Browse the repository at this point in the history
* WEB3 1193 replace percentage for BPS in create proposal flow

* Update preview proposer text color

* Create key-based validations for multiple items

* Fix emergency setkey protocol test

* Replace if for switch statement in getKeybasedValidation
  • Loading branch information
conradocanasm0 authored Oct 10, 2024
1 parent 618c4d8 commit 72a7de1
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 35 deletions.
6 changes: 3 additions & 3 deletions components/proposal/InputDynamic.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<div
v-if="props.modelValueErrors?.length"
class="text-red-500 text-xs my-2 h-4"
class="text-red-500 text-xs my-2 h-4 font-inter"
>
<p v-for="error of props.modelValueErrors" :key="error.$uid">
{{ error.$message }}
Expand Down Expand Up @@ -68,8 +68,8 @@ const value = useVModelWrapper<InputProps>(props, emit, "modelValue");
const hasErrors = computed(() => props.modelValueErrors?.length);
</script>
<style>
<style scoped>
.error {
@apply border border-red-500;
@apply bg-transparent border border-red-500;
}
</style>
10 changes: 4 additions & 6 deletions components/proposal/InputProtocolConfigOperation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const inputs = {
penalty_rate: {
component: InputDynamic,
props: {
decorator: "%",
decorator: "BPS",
maska: masks.percentage,
},
},
Expand All @@ -77,7 +77,7 @@ const inputs = {
mint_ratio: {
component: InputDynamic,
props: {
decorator: "%",
decorator: "BPS",
maska: masks.percentage,
},
},
Expand All @@ -94,30 +94,28 @@ const inputs = {
component: InputDynamic,
props: {
decorator: "contract",
maska: masks.ethereumAddress,
},
},
earner_rate_model: {
component: InputDynamic,
props: {
decorator: "contract",
maska: masks.ethereumAddress,
},
},
base_minter_rate: {
component: InputDynamic,
props: {
decorator: "%",
decorator: "BPS",
maska: masks.percentage,
},
},
max_earner_rate: {
component: InputDynamic,
props: {
decorator: "%",
decorator: "BPS",
maska: masks.percentage,
},
},
Expand Down
2 changes: 1 addition & 1 deletion components/proposal/Preview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</h2>
<div
class="text-grey-400 my-1 font-inter text-xs truncate w-52 lg:w-full"
class="text-grey-500 my-1 font-inter text-xs truncate w-52 lg:w-full"
>
Proposed by
<u><MAddressAvatar :address="proposal?.proposer" /></u>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe("Proposals", () => {
describe("type action: Emergency setKey", () => {
const value = "1";
const value = "60";
const key = "mint_delay";
const description = `Add config ${key} = ${value}`;
let proposalUrl = "";
Expand Down
80 changes: 56 additions & 24 deletions pages/proposal/create.vue
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,15 @@ import {
writeContract,
readContract,
} from "@wagmi/core";
import { encodeFunctionData, encodeAbiParameters, Hash, erc20Abi } from "viem";
import {
encodeFunctionData,
encodeAbiParameters,
Hash,
erc20Abi,
isAddress,
} from "viem";
import { useAccount } from "use-wagmi";
import { required, minLength, maxLength, url } from "@vuelidate/validators";
import { required, minLength, url, helpers } from "@vuelidate/validators";
import { useVuelidate } from "@vuelidate/core";
import { storeToRefs } from "pinia";
Expand Down Expand Up @@ -271,6 +277,21 @@ const formData = reactive({
discussionURL: null,
});
const validations = {
address: helpers.withMessage(
"Address is not valid",
(value: string) => isAddress(value) || !value,
),
range: (min: number, max: number) =>
helpers.withMessage(
`Invalid value, acceptable range is ${min}-${max}`,
(value: string) => {
const numberValue = Number(value);
return numberValue >= min && numberValue <= max;
},
),
};
const rules = computed(() => {
const constRules = {
description: { required, minLength: minLength(6) },
Expand All @@ -286,8 +307,7 @@ const rules = computed(() => {
proposalValue: { required },
proposalValue2: {
required,
minLength: minLength(42),
maxLength: maxLength(42),
addressValidation: validations.address,
},
proposalValue3: {},
...constRules,
Expand All @@ -299,22 +319,21 @@ const rules = computed(() => {
proposalValue: { required },
proposalValue2: {
required,
minLength: minLength(42),
maxLength: maxLength(42),
addressValidation: validations.address,
},
proposalValue3: {
required,
minLength: minLength(42),
maxLength: maxLength(42),
addressValidation: validations.address,
},
...constRules,
};
}
if (["setKey"].includes(type)) {
const selectedKey = formData.proposalValue || "";
return {
proposalValue: { required },
proposalValue2: { required },
proposalValue2: getKeyBasedValidation(selectedKey),
proposalValue3: {},
...constRules,
};
Expand All @@ -336,7 +355,7 @@ const rules = computed(() => {
].includes(type)
) {
return {
proposalValue: { required },
proposalValue: { required, range: validations.range(10, 100) },
proposalValue2: {},
proposalValue3: {},
...constRules,
Expand Down Expand Up @@ -854,20 +873,6 @@ function buildCalldatas(formData) {
return addressToHexWith32Bytes(inp);
}
if (
[
"penalty_rate",
"mint_ratio",
"base_minter_rate",
"max_earner_rate",
].includes(key)
) {
return encodeAbiParameters(
[{ type: "uint256" }],
[BigInt(percentageToBasispoints(inp))],
);
}
return encodeAbiParameters([{ type: "uint256" }], [BigInt(inp)]);
};
Expand Down Expand Up @@ -924,6 +929,33 @@ function buildCalldatasTtg(functionName: any, args: any) {
});
}
function getKeyBasedValidation(key: string) {
switch (key) {
case "minter_rate_model":
case "earner_rate_model":
return { required, address: validations.address };
case "base_minter_rate":
case "max_earner_rate":
return { required, range: validations.range(1, 5000) };
case "penalty_rate":
return { required, range: validations.range(1, 1000) };
case "update_collateral_interval":
return { required, range: validations.range(60, 31536000) }; // 1 minute to 1 year
case "update_collateral_threshold":
return { required, range: validations.range(1, 10) };
case "mint_delay":
return { required, range: validations.range(60, 86400) }; // 1 minute to 1 day
case "mint_ttl":
return { required, range: validations.range(3600, 864000) }; // 1 hour to 10 days
case "mint_ratio":
return { required, range: validations.range(50, 100) }; // Percent range
case "minter_freeze_time":
return { required, range: validations.range(3600, 2592000) }; // 1 hour to 1 month
default:
return { required };
}
}
function onBack() {
isPreview.value = false;
previewDescription.value = null;
Expand Down

0 comments on commit 72a7de1

Please sign in to comment.