Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Payout Manager for Proportional Funding #326

Merged
merged 12 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
##? Deployment environment

# test | staging | production
NEXT_PUBLIC_ENV=test

NEXT_PUBLIC_DEBUG=
##? Debugging and testing only
##! WARNING: DO NOT USE THESE VARIABLES IN PRODUCTION DEPLOYMENTS, OTHERWISE IT WILL BREAK UX

# true | false
NEXT_PUBLIC_DEBUG=false

# Unlocks authorization, useful for validation and permission control testing
# true | false
NEXT_PUBLIC_IS_UNDER_INSPECTION=false
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"near-api-js": "^2.1.4",
"next": "^14.2.3",
"node-fetch": "^3.3.2",
"pinata-web3": "^0.5.4",
"query-string": "^9.1.0",
"react": "^18.3.1",
"react-copy-to-clipboard": "^5.1.0",
Expand Down
9 changes: 8 additions & 1 deletion src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import { Metadata } from "next";
import { NETWORK, PLATFORM_NAME } from "./_config";
import { ChronologicalSortOrderVariant, type TokenId } from "./types";

export const DEBUG = Boolean(process.env.NEXT_PUBLIC_DEBUG);
export const DEBUG = process.env.NEXT_PUBLIC_DEBUG === "true" ? true : false;

/**
* Unlocks authorization, useful for validation and permission control testing
*/
export const IS_UNDER_INSPECTION =
process.env.NEXT_PUBLIC_IS_UNDER_INSPECTION === "true" ? true : false;

export const ICONS_ASSET_ENDPOINT_URL = "/assets/icons";
export const IMAGES_ASSET_ENDPOINT_URL = "/assets/images";
export const PLATFORM_TWITTER_ACCOUNT_ID = "PotLock_";
Expand Down
2 changes: 1 addition & 1 deletion src/common/contracts/core/donation/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const getDonationsForRecipient = (args: { recipient_id: string }) =>
/**
* Get donations for donor id
*/
export const getDonationsForDonor = (args: { donor_id: string }) =>
export const get_donations_for_donor = (args: { donor_id: string }) =>
contractApi.view<typeof args, DirectDonation[]>("get_donations_for_donor", {
args,
});
Expand Down
36 changes: 17 additions & 19 deletions src/common/contracts/core/pot/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ApprovedApplication,
Challenge,
Payout,
type PayoutInput,
PotBatchDonationItem,
PotConfig,
PotDonation,
Expand All @@ -18,30 +19,27 @@ import {
} from "./interfaces";

const contractApi = (potId: string) =>
naxiosInstance.contractApi({
contractId: potId,
cache: new MemoryCache({ expirationTime: 10 }), // 10 seg
});
naxiosInstance.contractApi({ contractId: potId, cache: new MemoryCache({ expirationTime: 10 }) });

// READ METHODS
/**
* Get pot detail(config)
*/
export const getConfig = (args: { potId: string }) =>
export const get_config = (args: { potId: string }) =>
contractApi(args.potId).view<{}, PotConfig>("get_config", { args });

/**
* Check if round is active
*/
export const getList = (args: { potId: string }) =>
export const is_round_active = (args: { potId: string }) =>
contractApi(args.potId).view<typeof args, boolean>("is_round_active", {
args,
});

/**
* Get round funding donations
*/
export const getMatchingPoolDonations = async (args: {
export const get_matching_pool_donations = async (args: {
potId: string;
from_index?: number;
limit?: number;
Expand All @@ -53,53 +51,53 @@ export const getMatchingPoolDonations = async (args: {
/**
* Get round funding donations
*/
export const getDonationsForDonor = async (args: { potId: string; donor_id: string }) =>
export const get_donations_for_donor = async (args: { potId: string; donor_id: string }) =>
contractApi(args.potId).view<typeof args, PotDonation[]>("get_donations_for_donor", {
args,
});

/**
* Get round donations for a project id
*/
export const getDonationsForProject = async (args: { potId: string; project_id: string }) =>
export const get_donations_for_project = async (args: { potId: string; project_id: string }) =>
contractApi(args.potId).view<typeof args, PotDonation[]>("get_donations_for_project", {
args,
});

/**
* Get application by project id
*/
export const getApplicationByProjectId = async (args: { potId: string; project_id: string }) =>
export const get_application_by_project_id = async (args: { potId: string; project_id: string }) =>
contractApi(args.potId).view<typeof args, Application>("get_application_by_project_id", {
args,
});

/**
* Get round approved applications
*/
export const getApprovedApplications = async (args: { potId: string }) =>
export const get_approved_applications = async (args: { potId: string }) =>
contractApi(args.potId).view<typeof args, ApprovedApplication[]>("get_approved_applications", {
args,
});

/**
* Get round applications
*/
export const getApplications = async (args: { potId: string }) =>
export const get_applications = async (args: { potId: string }) =>
contractApi(args.potId).view<typeof args, Application[]>("get_applications", {
args,
});

/**
* Get round payout challanges
* Get round payout challenges
*/
export const getPayoutsChallenges = async (args: { potId: string }) =>
export const get_payouts_challenges = async (args: { potId: string }) =>
contractApi(args.potId).view<typeof args, Challenge[]>("get_payouts_challenges");

/**
* Get round payouts
*/
export const getPayouts = async (args: { potId: string }) =>
export const get_payouts = async (args: { potId: string }) =>
contractApi(args.potId).view<typeof args, Payout[]>("get_payouts", {
args,
});
Expand All @@ -108,7 +106,7 @@ export const getPayouts = async (args: { potId: string }) =>
/**
* Challenge round payout
*/
export const challengePayouts = ({ potId, reason }: { potId: string; reason: string }) => {
export const challenge_payouts = ({ potId, reason }: { potId: string; reason: string }) => {
const args = { reason };
const deposit = parseNearAmount(calculateDepositByDataSize(args))!;

Expand All @@ -122,7 +120,7 @@ export const challengePayouts = ({ potId, reason }: { potId: string; reason: str
/**
* Admin update round payout Challenge
*/
export const adminUpdatePayoutsChallenge = (args: {
export const admin_update_payouts_challenge = (args: {
potId: string;
challengerId: string;
notes: string;
Expand All @@ -140,7 +138,7 @@ export const adminUpdatePayoutsChallenge = (args: {
/**
* Admin update round payout Challenge
*/
export const chefSetPayouts = (args: { potId: string; payouts: Payout[] }) =>
export const chef_set_payouts = (args: { potId: string; payouts: PayoutInput[] }) =>
contractApi(args.potId).call<typeof args, Payout[]>("chef_set_payouts", {
args,
deposit: "1",
Expand All @@ -150,7 +148,7 @@ export const chefSetPayouts = (args: { potId: string; payouts: Payout[] }) =>
/**
* Admin process payout
*/
export const adminProcessPayouts = (args: { potId: string }) =>
export const admin_process_payouts = (args: { potId: string }) =>
contractApi(args.potId).call<typeof args, Payout[]>("admin_process_payouts", {
args,
deposit: "1",
Expand Down
26 changes: 26 additions & 0 deletions src/common/contracts/core/pot/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import useSWR from "swr";

import type { ByPotId } from "@/common/api/indexer";
import type { ConditionalActivation } from "@/common/types";

import * as contractClient from "./client";

export const useConfig = ({ enabled = true, potId }: ByPotId & ConditionalActivation) =>
useSWR(["useConfig", potId], ([_queryKey, potIdKey]) =>
!enabled ? undefined : contractClient.get_config({ potId: potIdKey }),
);

export const useApplications = ({ enabled = true, potId }: ByPotId & ConditionalActivation) =>
useSWR(["useApplications", potId], ([_queryKey, potIdKey]) =>
!enabled ? undefined : contractClient.get_applications({ potId: potIdKey }),
);

export const usePayouts = ({ enabled = true, potId }: ByPotId & ConditionalActivation) =>
useSWR(["usePayouts", potId], ([_queryKey, potIdKey]) =>
!enabled ? undefined : contractClient.get_payouts({ potId: potIdKey }),
);

export const usePayoutChallenges = ({ enabled = true, potId }: ByPotId & ConditionalActivation) =>
useSWR(["usePayoutChallenges", potId], ([_queryKey, potIdKey]) =>
!enabled ? undefined : contractClient.get_payouts_challenges({ potId: potIdKey }),
);
4 changes: 3 additions & 1 deletion src/common/contracts/core/pot/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as potContractClient from "./client";
import * as potContractHooks from "./hooks";

export type * from "./hooks";
export * from "./interfaces";

export { potContractClient };
export { potContractClient, potContractHooks };
11 changes: 8 additions & 3 deletions src/common/contracts/core/pot/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AccountId, ProviderId, TokenId } from "@/common/types";
import { AccountId, type IndivisibleUnits, ProviderId, TokenId } from "@/common/types";

export enum ApplicationStatus {
Pending = "Pending",
Expand All @@ -25,12 +25,17 @@ export type ApprovedApplication = Omit<Application, "status"> & {
status: ApplicationStatus.Approved;
};

export interface Payout {
export type PayoutInput = {
amount: IndivisibleUnits;
project_id: AccountId;
};

export type Payout = {
id: string;
project_id: string;
amount: string;
paid_at: number;
}
};

export interface PayoutDetailed extends Payout {
totalAmount: string;
Expand Down
4 changes: 2 additions & 2 deletions src/common/contracts/metapool/liquid-staking/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { MemoryCache } from "@wpdas/naxios";

import { METAPOOL_LIQUID_STAKING_CONTRACT_ACCOUNT_ID } from "@/common/_config";
import { naxiosInstance } from "@/common/api/near/client";
import { U128String } from "@/common/types";
import { IndivisibleUnits } from "@/common/types";

export const contractApi = naxiosInstance.contractApi({
contractId: METAPOOL_LIQUID_STAKING_CONTRACT_ACCOUNT_ID,
cache: new MemoryCache({ expirationTime: 600 }),
});

export const get_stnear_price = () => contractApi.view<{}, U128String>("get_stnear_price");
export const get_stnear_price = () => contractApi.view<{}, IndivisibleUnits>("get_stnear_price");
6 changes: 3 additions & 3 deletions src/common/contracts/metapool/vote/interface.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { AccountId, U128String } from "@/common/types";
import { AccountId, IndivisibleUnits } from "@/common/types";

/**
* u128 with 24 decimals (NEAR standard)
*/
export type VotingPower = U128String;
export type VotingPower = IndivisibleUnits;

export type Voter = {
voter_id: AccountId;
balance_in_contract: U128String;
balance_in_contract: IndivisibleUnits;

locking_positions: {
index: number;
Expand Down
25 changes: 16 additions & 9 deletions src/common/lib/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@ import { Big } from "big.js";
import { formatNearAmount, parseNearAmount } from "near-api-js/lib/utils/format";

import { NATIVE_TOKEN_DECIMALS } from "../constants";
import { U128String } from "../types";
import { IndivisibleUnits } from "../types";

export { formatNearAmount, parseNearAmount };

export const stringifiedU128ToBigNum = (amount: U128String, decimals: number) =>
export const indivisibleUnitsToBigNum = (amount: IndivisibleUnits, decimals: number) =>
Big(amount).div(Big(10).pow(decimals));

export const stringifiedU128ToFloat = (amount: U128String, decimals: number) =>
parseFloat(stringifiedU128ToBigNum(amount, decimals).toFixed(2));
// TODO: Adjust all consuming code with custom precision
export const indivisibleUnitsToFloat = (
amount: IndivisibleUnits,
decimals: number,
fracDigits?: number,
) => parseFloat(indivisibleUnitsToBigNum(amount, decimals).toFixed(fracDigits ?? 2));

export const floatToBigNum = (amount: number, decimals: number) =>
Big(amount).mul(Big(10).pow(decimals));
export const floatToIndivisible = (amountFloat: number, decimals: number) =>
Big(amountFloat).mul(Big(10).pow(decimals)).toFixed().toString();

export const yoctoNearToFloat = (amountYoctoNear: U128String) =>
stringifiedU128ToFloat(amountYoctoNear, NATIVE_TOKEN_DECIMALS);
export const yoctoNearToFloat = (amountYoctoNear: IndivisibleUnits) =>
indivisibleUnitsToFloat(amountYoctoNear, NATIVE_TOKEN_DECIMALS);

export const floatToYoctoNear = (amountFloat: number): U128String =>
const floatToBigNum = (amountFloat: number, decimals: number) =>
Big(amountFloat).mul(Big(10).pow(decimals));

export const floatToYoctoNear = (amountFloat: number): IndivisibleUnits =>
floatToBigNum(amountFloat, NATIVE_TOKEN_DECIMALS).toFixed().toString();
2 changes: 1 addition & 1 deletion src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export type { infer as FromSchema } from "zod";

export type UnionFromStringList<ListOfMembers extends string[]> = ListOfMembers[number];

export type U128String = string;
export type IndivisibleUnits = string;

export type ClientConfig = { swr?: SWRConfiguration };

Expand Down
4 changes: 2 additions & 2 deletions src/entities/_shared/token/components/TokenTotalValue.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NATIVE_TOKEN_DECIMALS } from "@/common/constants";
import { stringifiedU128ToFloat } from "@/common/lib";
import { indivisibleUnitsToFloat } from "@/common/lib";
import { ByTokenId } from "@/common/types";
import { Skeleton } from "@/common/ui/components";
import { cn } from "@/common/ui/utils";
Expand All @@ -24,7 +24,7 @@ export const TokenTotalValue: React.FC<TokenTotalValueProps> = ({
const amount =
"amountFloat" in props
? props.amountFloat
: stringifiedU128ToFloat(
: indivisibleUnitsToFloat(
props.amountBigString,
token?.metadata.decimals ?? NATIVE_TOKEN_DECIMALS,
);
Expand Down
10 changes: 5 additions & 5 deletions src/entities/_shared/token/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { nearHooks } from "@/common/api/near";
import { NATIVE_TOKEN_ID, PLATFORM_LISTED_TOKEN_IDS } from "@/common/constants";
import { refExchangeHooks } from "@/common/contracts/ref-finance";
import { ftHooks } from "@/common/contracts/tokens";
import { isAccountId, stringifiedU128ToBigNum, stringifiedU128ToFloat } from "@/common/lib";
import { indivisibleUnitsToBigNum, indivisibleUnitsToFloat, isAccountId } from "@/common/lib";
import { formatWithCommas } from "@/common/lib/formatWithCommas";
import type { AccountId, ByTokenId, ConditionalActivation } from "@/common/types";

Expand Down Expand Up @@ -141,11 +141,11 @@ export const useToken = ({
usdPrice: oneTokenUsdPrice ? Big(oneTokenUsdPrice) : undefined,

balance: accountSummary?.amount
? stringifiedU128ToBigNum(accountSummary.amount, ntMetadata.decimals)
? indivisibleUnitsToBigNum(accountSummary.amount, ntMetadata.decimals)
: undefined,

balanceFloat: accountSummary?.amount
? stringifiedU128ToFloat(accountSummary.amount, ntMetadata.decimals)
? indivisibleUnitsToFloat(accountSummary.amount, ntMetadata.decimals)
: undefined,

balanceUsd:
Expand All @@ -168,11 +168,11 @@ export const useToken = ({
usdPrice: oneFtUsdPrice ? Big(oneFtUsdPrice) : undefined,

balance: ftBalance
? stringifiedU128ToBigNum(ftBalance, ftMetadata.decimals)
? indivisibleUnitsToBigNum(ftBalance, ftMetadata.decimals)
: undefined,

balanceFloat: ftBalance
? stringifiedU128ToFloat(ftBalance, ftMetadata.decimals)
? indivisibleUnitsToFloat(ftBalance, ftMetadata.decimals)
: undefined,

balanceUsd:
Expand Down
Loading
Loading