Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
carina-akaia committed Aug 15, 2024
1 parent 9e8a39a commit 7c42834
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 35 deletions.
27 changes: 5 additions & 22 deletions src/modules/access-control/components/AccessControlAdminsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
FormField,
} from "@/common/ui/components";
import { TextField } from "@/common/ui/form-fields";
import { AccountOption } from "@/modules/core";
import { AccountOption, validAccountId } from "@/modules/core";

export type AccessControlAdminsModalProps = {
admins: AccountId[];
Expand All @@ -40,27 +40,10 @@ export const AccessControlAdminsModal = create(
const form = useForm<ByAccountId>({
resolver: zodResolver(
object({
accountId: string()
.min(5, "Account ID is too short")
.refine(
(accountId) => !admins.includes(accountId),
"Account with this ID is already listed",
)
.refine(
async (account_id) =>
account_id.length > 4
? await nearRpc
.query<AccountView>({
request_type: "view_account",
finality: "final",
account_id,
})
.then((accountInfo) => accountInfo)
.catch(() => false)
: true,

{ message: "Account does not exist" },
),
accountId: validAccountId.refine(
(accountId) => !admins.includes(accountId),
"Account with this ID is already listed",
),
}),
),

Expand Down
2 changes: 1 addition & 1 deletion src/modules/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export * from "./components/ModalErrorBody";
export * from "./components/RuntimeErrorAlert";
export * from "./components/TokenIcon";
export * from "./components/TotalTokenValue";
export { core, useCoreState } from "./model";
export * from "./models";
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { ContractMetadata } from "@/common/types";
import { useTypedSelector } from "@/store";
import { RootModel } from "@/store/models";

export * from "./schemas";

interface CoreState {
contractMetadata: ContractMetadata;
oneNearUsdPrice: number;
Expand Down
22 changes: 22 additions & 0 deletions src/modules/core/models/schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { AccountView } from "near-api-js/lib/providers/provider";
import { string } from "zod";

import { nearRpc } from "@/common/api/near";

export const validAccountId = string()
.min(5, "Account ID is too short")
.refine(
async (account_id) =>
account_id.length > 4
? await nearRpc
.query<AccountView>({
request_type: "view_account",
finality: "final",
account_id,
})
.then((accountInfo) => accountInfo)
.catch(() => false)
: true,

{ message: "Account does not exist" },
);
16 changes: 10 additions & 6 deletions src/modules/donation/hooks/fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Pot, potlock } from "@/common/api/potlock";
import { TOTAL_FEE_BASIS_POINTS } from "@/modules/core/constants";

import { DonationInputs } from "../models";
import { donationFeeBasisPointsToPercents } from "../utils/converters";

export type DonationFeeInputs = Pick<
DonationInputs,
Expand All @@ -25,8 +26,6 @@ export type DonationFees = {
chefFeePercent: number;
};

export const basisPointsToPercent = (basisPoints: number) => basisPoints / 100;

export const useDonationFees = ({
pot,
amount,
Expand Down Expand Up @@ -55,7 +54,7 @@ export const useDonationFees = ({
protocolFeeFinalAmount ??
(amount * protocolFeeBasisPoints) / TOTAL_FEE_BASIS_POINTS;

const protocolFeePercent = basisPointsToPercent(
const protocolFeePercent = donationFeeBasisPointsToPercents(
protocolFeeInitialBasisPoints,
);

Expand All @@ -81,7 +80,9 @@ export const useDonationFees = ({
referralFeeFinalAmount ??
(amount * referralFeeBasisPoints) / TOTAL_FEE_BASIS_POINTS;

const referralFeePercent = basisPointsToPercent(referralFeeBasisPoints);
const referralFeePercent = donationFeeBasisPointsToPercents(
referralFeeBasisPoints,
);

/**
*? Chef fee:
Expand All @@ -90,7 +91,10 @@ export const useDonationFees = ({
const chefFeeInitialBasisPoints = pot?.chef_fee_basis_points ?? 0;
const chefFeeBasisPoints = bypassChefFee ? 0 : chefFeeInitialBasisPoints;
const chefFeeAmount = (amount * chefFeeBasisPoints) / TOTAL_FEE_BASIS_POINTS;
const chefFeePercent = basisPointsToPercent(chefFeeInitialBasisPoints);

const chefFeePercent = donationFeeBasisPointsToPercents(
chefFeeInitialBasisPoints,
);

/**
*? Project allocation:
Expand All @@ -105,7 +109,7 @@ export const useDonationFees = ({
const projectAllocationAmount =
(amount * projectAllocationBasisPoints) / TOTAL_FEE_BASIS_POINTS;

const projectAllocationPercent = basisPointsToPercent(
const projectAllocationPercent = donationFeeBasisPointsToPercents(
projectAllocationBasisPoints,
);

Expand Down
1 change: 1 addition & 0 deletions src/modules/donation/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from "./constants";
export * from "./hooks";
export * from "./models/schemas";
export * from "./utils/converters";
export * from "./utils/validation";

export { DonationRandomButton } from "./components/buttons";
Expand Down
8 changes: 6 additions & 2 deletions src/modules/donation/models/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
DonationAllocationStrategyEnum,
DonationPotDistributionStrategy,
} from "../types";
import { donationFeePercentsToBasisPoints } from "../utils/converters";
import {
isDonationAmountSufficient,
isDonationMatchingPotSelected,
Expand All @@ -33,8 +34,11 @@ export const donationTokenSchema = literal(NEAR_TOKEN_DENOM)

export const donationAmount = safePositiveNumber;

// TODO: Convert percents to basic points!
export const donationFeeBasicPoints = safePositiveNumber;
export const donationFeeBasicPoints = safePositiveNumber.transform((percents) =>
donationFeePercentsToBasisPoints(
safePositiveNumber.safeParse(percents).data ?? 0,
),
);

export const donationSchema = object({
tokenId: donationTokenSchema,
Expand Down
5 changes: 5 additions & 0 deletions src/modules/donation/utils/converters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const donationFeeBasisPointsToPercents = (basisPoints: number) =>
basisPoints / 100;

export const donationFeePercentsToBasisPoints = (percent: number) =>
percent * 100;
16 changes: 13 additions & 3 deletions src/modules/pot/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import {
import { conditional, evolve, isNonNullish, piped } from "remeda";

import { nearRpc, walletApi } from "@/common/api/near";
import {
NADABOT_CONTRACT_ID,
POTLOCK_LISTS_CONTRACT_ID,
} from "@/common/constants";
import { potFactory } from "@/common/contracts/potlock";
import { Pot } from "@/common/contracts/potlock/interfaces/pot-factory.interfaces";
import { floatToYoctoNear, timestamp } from "@/common/lib";
import { getTransactionStatus } from "@/common/services";
import { ProviderId } from "@/common/types";
import { donationAmount, donationFeeBasicPoints } from "@/modules/donation";
import { RootModel } from "@/store/models";

Expand Down Expand Up @@ -87,11 +92,14 @@ export const potModel = createModel<RootModel>()({
{
...potInputs,
source_metadata: { commit_hash, ...sourceMetadata },
registry_provider: isPgRegistrationRequired ? null : undefined, // TODO set the actual value

registry_provider: isPgRegistrationRequired
? POTLOCK_LISTS_CONTRACT_ID
: undefined,

sybil_wrapper_provider: isNadabotVerificationRequired
? null
: undefined, // TODO set the actual value
? `${NADABOT_CONTRACT_ID}:is_human`
: undefined,
},

{
Expand All @@ -110,6 +118,8 @@ export const potModel = createModel<RootModel>()({
isNonNullish,
piped(donationAmount.parse, floatToYoctoNear),
]),

chef_fee_basis_points: donationFeeBasicPoints.parse,
},
),

Expand Down
3 changes: 2 additions & 1 deletion src/modules/pot/models/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { infer as FromSchema, array, boolean, object, string } from "zod";

import { futureTimestamp, safePositiveNumber } from "@/common/lib";
import { validAccountId } from "@/modules/core";
import { donationAmount, donationFeeBasicPoints } from "@/modules/donation";

import {
Expand All @@ -21,7 +22,7 @@ export const potDeploymentSchema = object({
.optional()
.describe("List of pot admins' account ids."),

chef: string().optional().describe("Chef's account id."),
chef: validAccountId.optional().describe("Chef's account id."),

pot_name: string()
.min(3, "Pot name must be at least 3 characters long.")
Expand Down

0 comments on commit 7c42834

Please sign in to comment.