Skip to content

Commit

Permalink
V2 (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
carina-akaia authored Jun 3, 2024
1 parent fec6723 commit e895b76
Show file tree
Hide file tree
Showing 154 changed files with 4,420 additions and 2,206 deletions.
23 changes: 19 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
{
"editor.formatOnSave": true,
"eslint.validate": ["typescript"],
"eslint.validate": [
"typescript"
],
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit",
"source.fixAll": "explicit"
}
}

},
"cSpell.words": [
"fontsource",
"LIGHTGRAY",
"nadabot",
"naxios",
"Roboto",
"SHADOWGRAY",
"Snackbars",
"Subtractor",
"Unflag",
"uuidv4",
"wpdas",
"zustand"
]
}
55 changes: 54 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# nadabot-app

Nada.bot application https://nada.bot
Nada.bot application <https://nada.bot>

## Getting Started

Expand Down Expand Up @@ -33,3 +33,56 @@ NEXT_PUBLIC_SOCIAL_DB_CONTRACT_ID=v1.social08.testnet
# Pinata JWT (ask Shot for the JWT Key)
PINATA_JWT=<jwt-code>
```

## Project structure

```sh

[ / ]
├── [ common ] <--- # Abstract implementation details, reusable assets, and primitives, used in layouts
│ │ # and business logic across the codebase, and MUST NOT contain business logic themselves.
│ │ # AKA "shared" ( see link 1. )
│ │
│ ├── constants.ts
│ ├── assets
│ ├── contexts
│ ├── lib
│ ├── services
│ ├── store
│ └── ui
├── [ modules ] <--- # Business logic units broken down into categories.
│ │ # Simply put, this is a collection of directories that contain code implementing specific
│ │ # groups of app use cases and are named after functionalities they provide.
│ │
│ ├── auth
│ │
│ ├── [ core ] <--- # Follows the same structure as any other module, but contains business logic,
│ │ │ # that is shared between all or some of the other modules
│ │ │
│ │ └── [ hooks ] <--- # Feature-specific React hooks
│ │
│ ├── groups
│ └── stamps
└── [ pages ] <--- # App's entry point. Follows Nextjs App routing specification ( see link 2. ),
# with additional Private Folders following Nextjs App routing conventions ( see link 3. ),
# that facilitate layout code-splitting and further migration to App Router ( see link 4. )

```

### Links

1. [Shared layer from Feature-Sliced Design methodology](https://feature-sliced.design/docs/reference/layers#shared)
2. [Nextjs Pages routing conventions](https://nextjs.org/docs/getting-started/project-structure#pages-routing-conventions)
3. [Nextjs App Private Folders](https://nextjs.org/docs/app/building-your-application/routing/colocation#private-folders)
4. [Nextjs App Router](https://nextjs.org/docs/app)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Stack } from "@mui/material";
import Image from "next/image";

import useBreakPoints from "@nadabot/hooks/useBreakPoints";
import useBreakPoints from "@nadabot/common/ui/utils/useBreakPoints";

import nadabotIcon from "../images/nadabot-icon.png";

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
13 changes: 13 additions & 0 deletions constants.ts → common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export const ONE_TENTH_NEAR = utils.format.parseNearAmount("0.1")!;
export const ONE_HUNDREDTH_NEAR = utils.format.parseNearAmount("0.01")!;
// 0.02 NEAR
export const TWO_HUNDREDTHS_NEAR = utils.format.parseNearAmount("0.02")!;
// 0.03 NEAR
export const THREE_HUNDREDTHS_NEAR = utils.format.parseNearAmount("0.03")!;
// 300 Gas (full)
export const FULL_TGAS = "300000000000000";
// 30 Gas
Expand All @@ -44,3 +46,14 @@ export const MAX_PROVIDER_DESCRIPTION_LENGTH = 256;
export const MAX_PROVIDER_EXTERNAL_URL_LENGTH = 256;
export const MAX_PROVIDER_ICON_URL_LENGTH = 256;
export const MAX_GAS = 100; //100_000_000_000_000;
export const DEFAULT_ACCOUNT_ID_ARG_NAME = "account_id";

export const Routes = {
HOME: "/",
HOME_WITH_FILTERED_CHECKS: (
filterType: "newly-created" | "active" | "deactivated" | "flagged",
) => `/?filterType=${filterType}`,
ADD_STAMP: "/stamp/create",
ACCOUNT_INFO: (accountId: string) => `/account-info?accountId=${accountId}`,
ADMIN_HOME: "/admin",
};
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IPFS_NEAR_SOCIAL_THUMBNAIL_URL } from "@nadabot/constants";
import { IPFS_NEAR_SOCIAL_THUMBNAIL_URL } from "@nadabot/common/constants";

const getNearSocialProfileAvatarImage = (image_ipfs_cid?: string) =>
image_ipfs_cid
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
ProviderExternal,
ProviderExternalWithIsHuman,
} from "@nadabot/services/contracts/sybil.nadabot/interfaces/providers";
} from "@nadabot/common/services/contracts/sybil.nadabot/interfaces/providers";

/**
* Inserts { is_user_a_human: boolean } to regular provider (ProviderExternal)
Expand Down
2 changes: 1 addition & 1 deletion utils/providerSorts.ts → common/lib/providerSorts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProviderExternalWithIsHuman } from "@nadabot/services/contracts/sybil.nadabot/interfaces/providers";
import { ProviderExternalWithIsHuman } from "@nadabot/common/services/contracts/sybil.nadabot/interfaces/providers";

/**
* sort providers to show the most weight first
Expand Down
File renamed without changes.
52 changes: 52 additions & 0 deletions common/lib/time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Temporal } from "temporal-polyfill";

/**
* Converts a value in milliseconds to the equivalent number of days.
*
* @param {number | string | null} value - The value to convert,
* which can be a number of milliseconds, a string representing a number of milliseconds, or null.
*
* @return {number} The number of days equivalent to the input value.
* @throws {TypeError} If the input value cannot be converted to a number of milliseconds.
*/
export const millisecondsToDays = (value: number | string | null): number => {
try {
return Temporal.Duration.from({
milliseconds: typeof value === "string" ? parseInt(value) : value ?? 0,
}).total("days");
} catch {
const error = new TypeError(`Unable to convert \`${value}\` to days`);

console.error(error);
throw error;
}
};

/**
* Converts a value in days to the equivalent number of milliseconds.
*
* @param {number | string | null} value - The value to convert,
* which can be a number of days, a string representing a number of days, or null.
*
* @return {number} The number of milliseconds equivalent to the input value.
* @throws {TypeError} If the input value cannot be converted to a number of milliseconds.
*/
export const daysToMilliseconds = (value: number | string | null): number => {
try {
return Temporal.Duration.from({
days: typeof value === "string" ? parseInt(value) : value ?? 0,
}).total("milliseconds");
} catch {
const error = new TypeError(
`Unable to convert \`${value}\` to milliseconds`,
);

console.error(error);
throw error;
}
};

export const daysSinceTimestamp = (unixTimestampMs: number) =>
Temporal.Now.instant().since(
Temporal.Instant.fromEpochMilliseconds(unixTimestampMs),
).days;
File renamed without changes.
2 changes: 1 addition & 1 deletion hooks/useIsAdminPage.ts → common/lib/useIsAdminPage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useRouter } from "next/router";
import { useEffect, useState } from "react";

import { Routes } from "@nadabot/routes";
import { Routes } from "@nadabot/common/constants";

const useIsAdminPage = () => {
const router = useRouter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { setupWelldoneWallet } from "@near-wallet-selector/welldone-wallet";
import { setupXDEFI } from "@near-wallet-selector/xdefi";
import naxios from "@wpdas/naxios";

import { CONTRACT_ID, NETWORK } from "@nadabot/constants";
import { CONTRACT_ID, NETWORK } from "@nadabot/common/constants";

// Naxios (Contract/Wallet) Instance
export const naxiosInstance = new naxios({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StorageCache } from "@wpdas/naxios";

import { SOCIAL_DB_CONTRACT_ID } from "@nadabot/constants";
import { SOCIAL_DB_CONTRACT_ID } from "@nadabot/common/constants";

import { naxiosInstance } from "..";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@ import { Provider } from "near-api-js/lib/providers";
import {
FULL_TGAS,
ONE_HUNDREDTH_NEAR,
THREE_HUNDREDTHS_NEAR,
TWO_HUNDREDTHS_NEAR,
} from "@nadabot/constants";
} from "@nadabot/common/constants";

import {
CreateGroup,
GroupById,
GroupExternal,
UpdateGroup,
} from "./interfaces/groups";
import { GetHumanScoreInput, HumanScoreResponse } from "./interfaces/is-human";
import { Config } from "./interfaces/lib";
import {
ActivateProviderInput,
DeactivateProviderInput,
FlagProviderInput,
ProviderById,
ProviderExternal,
RegisterProviderInput,
UnflagProviderInput,
Expand Down Expand Up @@ -40,6 +48,16 @@ export const contractApi = naxiosInstance.contractApi({
*/
export const get_config = () => contractApi.view<{}, Config>("get_config");

/**
* Get Provider by its id
*/
export const get_provider = (args: ProviderById) =>
contractApi.view<object, ProviderExternal | undefined>(
"get_provider",
{ args },
{ useCache: true },
);

/**
* Get Providers
* @returns
Expand Down Expand Up @@ -172,6 +190,34 @@ export const admin_flag_provider = (args: FlagProviderInput) =>
* @returns
*/
export const admin_unflag_provider = (args: UnflagProviderInput) =>
contractApi.call<typeof args, Provider>("admin_unflag_provider", {
contractApi.call<typeof args, Provider>("admin_unflag_provider", { args });

export const create_group = (args: CreateGroup) =>
contractApi.call<typeof args, GroupExternal>("create_group", {
args,
deposit: THREE_HUNDREDTHS_NEAR,
});

export const update_group = (args: UpdateGroup) =>
contractApi.call<typeof args, GroupExternal>("update_group", {
args,
deposit: THREE_HUNDREDTHS_NEAR,
});

export const delete_group = (args: GroupById) =>
contractApi.call<typeof args, void>("delete_group", {
args,
deposit: ONE_HUNDREDTH_NEAR,
});

export const get_groups = () =>
contractApi.view<object, GroupExternal[]>("get_groups", undefined, {
useCache: true,
});

export const get_group = (args: GroupById) =>
contractApi.view<typeof args, GroupExternal | undefined>(
"get_group",
{ args },
{ useCache: true },
);
57 changes: 57 additions & 0 deletions common/services/contracts/sybil.nadabot/interfaces/groups.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { ProviderId } from "./providers";

export type GroupId = number;

export enum RulePrimitiveType {
Highest = "Highest",
Lowest = "Lowest",
}

export enum RuleGenericType {
Sum = "Sum",
DiminishingReturns = "DiminishingReturns",
IncreasingReturns = "IncreasingReturns",
}

export type RuleType =
| keyof typeof RulePrimitiveType
| keyof typeof RuleGenericType;

export type Rule =
| keyof typeof RulePrimitiveType
| {
/** Sum all scores with optional max value */
[RuleGenericType.Sum]: number | null | undefined;
}
| {
/** Sum with diminishing returns, factor in percentage (e.g., 10 for 10% reduction each) */
[RuleGenericType.DiminishingReturns]: number;
}
| {
/** Sum with increasing returns, factor in percentage (e.g., 10 for 10% increase each) */
[RuleGenericType.IncreasingReturns]: number;
};

export type Group = {
name: string;
rule: Rule;
};

export type GroupExternal = Group & {
id: GroupId;
providers: ProviderId[];
};

export interface GroupById {
group_id: GroupId;
}

export interface CreateGroup extends Pick<GroupExternal, "rule" | "providers"> {
group_name: GroupExternal["name"];
}

export interface UpdateGroup
extends GroupById,
Partial<Pick<GroupExternal, "rule" | "providers">> {
group_name?: GroupExternal["name"];
}
Loading

0 comments on commit e895b76

Please sign in to comment.