Skip to content

Commit

Permalink
fix: get subnet by netuid instead of table id on subnet expanded view
Browse files Browse the repository at this point in the history
  • Loading branch information
steinerkelvin committed Oct 18, 2024
1 parent ae1325b commit 3db86d7
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default async function ModulePage({ params }: Params) {
if (!moduleKey) {
notFound();
}
const mdl = await api.module.ByKeyLastBlock({ moduleKey: moduleKey });
const mdl = await api.module.byKeyLastBlock({ moduleKey: moduleKey });

if (!mdl) {
notFound();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { formatToken, smallAddress } from "@commune-ts/utils";
import type { Subnet } from "~/utils/types";
import { api } from "~/trpc/server";

const DIGITS = /^\d+$/;

interface Params {
params: {
slug: string[];
Expand All @@ -21,15 +23,15 @@ export default async function SubnetPage({ params }: Params) {
notFound();
}

const id = slug[0];

if (!/^\d+$/.test(String(id))) {
const netuid_ = slug[0];
if (!DIGITS.test(String(netuid_))) {
notFound();
}
const netuid = Number(netuid_);

const sbnt = await api.subnet.byId({ id: Number(id) + 1 });
const subnet = await api.subnet.byNetuidLastBlock({ netuid: netuid });

if (!sbnt) {
if (!subnet) {
notFound();
}

Expand All @@ -44,10 +46,10 @@ export default async function SubnetPage({ params }: Params) {
<ArrowLeftIcon className="h-5 w-5 text-cyan-500" />
Go back to Subnets list
</Link>
{sbnt.subnetMetadata && (
{subnet.subnetMetadata && (
<Link
target="_blank"
href={sbnt.subnetMetadata}
href={subnet.subnetMetadata}
className="flex animate-fade-left items-center gap-1 border border-white/20 bg-[#898989]/5 p-2 pr-3 text-white backdrop-blur-md transition duration-200 hover:border-cyan-500 hover:bg-cyan-500/10"
>
<GlobeAltIcon className="h-5 w-5 text-cyan-500" />
Expand All @@ -57,12 +59,12 @@ export default async function SubnetPage({ params }: Params) {
</div>
<h1 className="flex-grow animate-fade-right text-center font-semibold text-gray-300">
<span className="text-3xl font-semibold text-cyan-500">
{sbnt.name}
{subnet.name}
</span>{" "}
/ NETUID: {sbnt.netuid}
/ NETUID: {subnet.netuid}
</h1>
</div>
<SubnetDataGrid subnet={sbnt} />
<SubnetDataGrid subnet={subnet} />
</div>
);
}
Expand Down
17 changes: 16 additions & 1 deletion apps/commune-validator/src/app/(pages)/modules/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ import { ModuleCard } from "~/app/components/module-card";
import { PaginationControls } from "~/app/components/pagination-controls";
import { ViewControls } from "~/app/components/view-controls";
import { api } from "~/trpc/server";
import { z } from "zod";

const SORT_KEYS_SCHEMA = z.enum([
"id",
"emission",
"incentive",
"dividend",
"delegationFee",
"totalStakers",
"totalStaked",
"totalRewards",
"createdAt",
]);

export default async function ModulesPage({
searchParams,
Expand All @@ -15,10 +28,12 @@ export default async function ModulesPage({
const sortBy = searchParams.sortBy ?? "id";
const order = searchParams.order === "desc" ? "desc" : "asc";

const sortBy_ = SORT_KEYS_SCHEMA.parse(sortBy);

const { modules, metadata } = await api.module.paginatedAll({
page: currentPage,
limit: 24,
sortBy: sortBy,
sortBy: sortBy_,
order: order,
});

Expand Down
24 changes: 11 additions & 13 deletions packages/api/src/router/module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { TRPCRouterRecord } from "@trpc/server";
import { z } from "zod";

import { eq, sql, and } from "@commune-ts/db";
import { and, eq, sql } from "@commune-ts/db";
import {
computedModuleWeightsSchema,
moduleData,
Expand Down Expand Up @@ -29,22 +29,20 @@ export const moduleRouter = {
where: eq(moduleData.id, input.id),
});
}),
ByKeyLastBlock: publicProcedure
byKeyLastBlock: publicProcedure
.input(z.object({ moduleKey: z.string() }))
.query(async ({ ctx, input }) => {
const queryResult = await ctx.db
.select()
.from(moduleData)
.where(
and(
sql`${moduleData.atBlock} = (SELECT MAX(${moduleData.atBlock}) FROM ${moduleData})`,
eq(moduleData.moduleKey, input.moduleKey)
.select()
.from(moduleData)
.where(
and(
sql`${moduleData.atBlock} = (SELECT MAX(${moduleData.atBlock}) FROM ${moduleData})`,
eq(moduleData.moduleKey, input.moduleKey),
),
)

)
.limit(1)
.then((result) => result[0]);

.limit(1)
.then((result) => result[0]);
return queryResult;
}),
byUserModuleData: publicProcedure
Expand Down
18 changes: 17 additions & 1 deletion packages/api/src/router/subnet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { TRPCRouterRecord } from "@trpc/server";
import { z } from "zod";

import { eq, sql } from "@commune-ts/db";
import { and, eq, sql } from "@commune-ts/db";
import {
computedSubnetWeights,
subnetDataSchema,
Expand All @@ -23,6 +23,22 @@ export const subnetRouter = {
where: eq(subnetDataSchema.id, input.id),
});
}),
byNetuidLastBlock: publicProcedure
.input(z.object({ netuid: z.number() }))
.query(async ({ ctx, input }) => {
const queryResult = await ctx.db
.select()
.from(subnetDataSchema)
.where(
and(
sql`${subnetDataSchema.atBlock} = (SELECT MAX(${subnetDataSchema.atBlock}) FROM ${subnetDataSchema})`,
eq(subnetDataSchema.netuid, input.netuid),
),
)
.limit(1)
.then((result) => result[0]);
return queryResult;
}),
paginatedAll: publicProcedure
.input(
z.object({
Expand Down

0 comments on commit 3db86d7

Please sign in to comment.