Skip to content

Commit

Permalink
Fix fee normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
carina-akaia committed Oct 3, 2024
1 parent a805377 commit cc1a8fa
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type CustomSybilCheck = {
weight: SybilProviderWeight;
};

export interface Config {
export interface PotFactoryConfig {
owner: string;
admins: string[];
protocol_fee_basis_points: number;
Expand Down
20 changes: 8 additions & 12 deletions src/common/contracts/potlock/pot-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,18 @@ import { ByAccountId } from "@/common/types";
import {
PotArgs,
PotDeploymentResult,
PotFactoryConfig,
} from "./interfaces/pot-factory.interfaces";

export type { PotDeploymentResult };

/**
* Contract API
*/
export const contractApi = naxiosInstance.contractApi({
contractId: POT_FACTORY_CONTRACT_ID,
cache: new MemoryCache({ expirationTime: 5 }), // 10 seg
});

// READ METHODS

type Config = {
require_whitelist: boolean;
whitelisted_deployers: string[];
};

export const get_config = () => contractApi.view<{}, Config>("get_config");
export const get_config = () =>
contractApi.view<{}, PotFactoryConfig>("get_config");

export const calculate_min_deployment_deposit = (args: {
args: PotArgs;
Expand All @@ -36,7 +28,11 @@ export const calculate_min_deployment_deposit = (args: {
.view<typeof args, string>("calculate_min_deployment_deposit", { args })
.then((amount) =>
Big(amount).plus(Big("20000000000000000000000")).toFixed(),
);
)
.catch((error) => {
console.error(error);
return undefined;
});

export const deploy_pot = async (args: {
pot_args: PotArgs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const AccessControlList: React.FC<AccessControlListProps> = ({
<>
{isEditingEnabled && <AccessControlListModal id={modalId} {...props} />}

<div un-flex="~" un-justify="between" un-items="center">
<div className="flex items-center justify-between">
{accountList}

{isEditingEnabled && (
Expand Down
27 changes: 8 additions & 19 deletions src/modules/access-control/components/AccessControlListModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,7 @@ export const AccessControlListModal = create(

<DialogDescription>
<Form {...form}>
<form
onSubmit={onAccountSubmit}
un-flex="~"
un-gap="3"
un-items="start"
>
<form className="flex items-start gap-3">
<FormField
name="accountId"
control={form.control}
Expand All @@ -130,27 +125,21 @@ export const AccessControlListModal = create(
/>

<Button
type="submit"
onClick={onAccountSubmit}
variant="standard-filled"
disabled={isAccountFormDisabled}
>
Add
{"Add"}
</Button>
</form>
</Form>
</DialogDescription>

<div
un-flex="~ col"
className="flex flex-col"
style={{ display: accountIds.length > 0 ? undefined : "none" }}
>
<div
un-flex="~"
un-justify="between"
un-gap="4"
un-p="x-5 y-2"
un-bg="neutral-50"
>
<div className="p-x-5 p-y-2 flex justify-between gap-4 bg-neutral-50">
<div className="flex items-center gap-4">
<Checkbox
checked={selectedAccounts.length === accountIds.length}
Expand All @@ -159,7 +148,7 @@ export const AccessControlListModal = create(
/>

<span className="prose font-500 text-neutral-600">
{`${accountIds.length} Admins` +
{`${accountIds.length} Account(s)` +
(selectedAccounts.length > 0
? `, ${selectedAccounts.length} selected`
: "")}
Expand All @@ -176,7 +165,7 @@ export const AccessControlListModal = create(
<MdDeleteOutline width={18} height={18} />

<span className="prose line-height-none">
Remove all selected
{"Remove all selected"}
</span>
</Button>
</div>
Expand All @@ -202,7 +191,7 @@ export const AccessControlListModal = create(
<MdDeleteOutline width={18} height={18} />

<span className="prose font-500 line-height-none">
Remove
{"Remove"}
</span>
</Button>
}
Expand Down
23 changes: 12 additions & 11 deletions src/modules/donation/models/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {

import { NEAR_TOKEN_DENOM } from "@/common/constants";
import { safePositiveNumber } from "@/common/lib";
import { TOTAL_FEE_BASIS_POINTS } from "@/modules/core/constants";
import { TokenAvailableBalance } from "@/modules/token";

import {
Expand All @@ -23,10 +22,6 @@ import {
DonationAllocationStrategyEnum,
DonationGroupAllocationStrategyEnum,
} from "../types";
import {
donationFeeBasisPointsToPercents,
donationFeePercentsToBasisPoints,
} from "../utils/converters";
import {
isDonationAmountSufficient,
isDonationMatchingPotSelected,
Expand All @@ -39,15 +34,21 @@ export const donationTokenSchema = literal(NEAR_TOKEN_DENOM)

export const donationAmount = safePositiveNumber;

export const donationFeeBasisPoints = preprocess(
/**
* # Heads up!
*
* The donation fee is stored in basis points, but the schema expects it to be a percentage.
*
* Thus make sure to convert it to percents before passing to the form
* and convert it back to basis points before passing to the contract.
*/
export const donationFee = preprocess(
(value) =>
typeof value === "string"
? donationFeePercentsToBasisPoints(safePositiveNumber.parse(value))
: value,
typeof value === "string" ? safePositiveNumber.parse(value) : value,

safePositiveNumber,
).refine((basisPoints) => basisPoints <= TOTAL_FEE_BASIS_POINTS, {
message: `Fee cannot exceed ${donationFeeBasisPointsToPercents(TOTAL_FEE_BASIS_POINTS)}%.`,
).refine((percents) => percents < 100, {
message: `Fee must be less than 100%.`,
});

export const donationSchema = object({
Expand Down
4 changes: 4 additions & 0 deletions src/modules/pot-editor/utils/normalization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
import {
donationAmount,
donationFeeBasisPointsToPercents,
donationFeePercentsToBasisPoints,
} from "@/modules/donation";
import { PotInputs } from "@/modules/pot";

Expand Down Expand Up @@ -133,6 +134,9 @@ export const potInputsToPotArgs = ({
},

{
referral_fee_matching_pool_basis_points: donationFeePercentsToBasisPoints,
referral_fee_public_round_basis_points: donationFeePercentsToBasisPoints,
chef_fee_basis_points: donationFeePercentsToBasisPoints,
application_start_ms: timestamp.parse,
application_end_ms: timestamp.parse,
public_round_start_ms: timestamp.parse,
Expand Down
8 changes: 4 additions & 4 deletions src/modules/pot/models/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { futureTimestamp, safePositiveNumber } from "@/common/lib";
import { validAccountIdOrNothing } from "@/modules/core";
import {
donationAmount,
donationFeeBasisPoints,
donationFee,
donationFeeBasisPointsToPercents,
} from "@/modules/donation";

Expand Down Expand Up @@ -149,23 +149,23 @@ export const potSchema = object({
.optional()
.describe("Whether the projects must have Nadabot verification."),

referral_fee_matching_pool_basis_points: donationFeeBasisPoints
referral_fee_matching_pool_basis_points: donationFee
.refine(isPotMatchingPoolReferralFeeValid, {
message: `Cannot exceed ${donationFeeBasisPointsToPercents(
POT_MAX_REFERRAL_FEE_MATCHING_POOL_BASIS_POINTS,
)}%.`,
})
.describe("Matching pool referral fee in basis points."),

referral_fee_public_round_basis_points: donationFeeBasisPoints
referral_fee_public_round_basis_points: donationFee
.refine(isPotPublicRoundReferralFeeValid, {
message: `Cannot exceed ${donationFeeBasisPointsToPercents(
POT_MAX_REFERRAL_FEE_PUBLIC_ROUND_BASIS_POINTS,
)}%.`,
})
.describe("Public round referral fee in basis points."),

chef_fee_basis_points: donationFeeBasisPoints
chef_fee_basis_points: donationFee
.refine(isPotChefFeeValid, {
message: `Cannot exceed ${donationFeeBasisPointsToPercents(
POT_MAX_CHEF_FEE_BASIS_POINTS,
Expand Down
20 changes: 14 additions & 6 deletions src/modules/pot/utils/validation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { donationFeeBasisPointsToPercents } from "@/modules/donation";

import {
POT_MAX_APPROVED_PROJECTS,
POT_MAX_CHEF_FEE_BASIS_POINTS,
Expand Down Expand Up @@ -36,11 +38,17 @@ export const isPotCooldownPeriodValid = (cooldown_period_ms: number) =>
export const isPotMaxProjectsValid = (max_projects: number) =>
max_projects <= POT_MAX_APPROVED_PROJECTS;

export const isPotMatchingPoolReferralFeeValid = (basisPoints: number) =>
basisPoints <= POT_MAX_REFERRAL_FEE_MATCHING_POOL_BASIS_POINTS;
export const isPotMatchingPoolReferralFeeValid = (percents: number) =>
percents <=
donationFeeBasisPointsToPercents(
POT_MAX_REFERRAL_FEE_MATCHING_POOL_BASIS_POINTS,
);

export const isPotPublicRoundReferralFeeValid = (basisPoints: number) =>
basisPoints <= POT_MAX_REFERRAL_FEE_PUBLIC_ROUND_BASIS_POINTS;
export const isPotPublicRoundReferralFeeValid = (percents: number) =>
percents <=
donationFeeBasisPointsToPercents(
POT_MAX_REFERRAL_FEE_PUBLIC_ROUND_BASIS_POINTS,
);

export const isPotChefFeeValid = (basisPoints: number) =>
basisPoints <= POT_MAX_CHEF_FEE_BASIS_POINTS;
export const isPotChefFeeValid = (percents: number) =>
percents <= donationFeeBasisPointsToPercents(POT_MAX_CHEF_FEE_BASIS_POINTS);

0 comments on commit cc1a8fa

Please sign in to comment.