Skip to content

Commit

Permalink
fix: module and subnet submit
Browse files Browse the repository at this point in the history
  • Loading branch information
EdSDR committed Oct 16, 2024
1 parent b48fa04 commit 11b8ccc
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 36 deletions.
80 changes: 44 additions & 36 deletions apps/commune-validator/src/app/components/delegated-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,17 @@ export function DelegatedList() {
}
};

const createUserModuleData = api.module.createUserModuleData.useMutation({
onSuccess: () => {
router.refresh();
setIsSubmitting(false);
},
onError: (error) => {
console.error("Error submitting data:", error);
setIsSubmitting(false);
},
});
const createManyUserModuleData =
api.module.createManyUserModuleData.useMutation({
onSuccess: () => {
router.refresh();
setIsSubmitting(false);
},
onError: (error) => {
console.error("Error submitting data:", error);
setIsSubmitting(false);
},
});

const deleteUserModuleData = api.module.deleteUserModuleData.useMutation({
onSuccess: () => {
Expand All @@ -184,16 +185,17 @@ export function DelegatedList() {
},
});

const createUserSubnetData = api.subnet.createUserSubnetData.useMutation({
onSuccess: () => {
router.refresh();
setIsSubmitting(false);
},
onError: (error) => {
console.error("Error submitting data:", error);
setIsSubmitting(false);
},
});
const createManyUserSubnetData =
api.subnet.createManyUserSubnetData.useMutation({
onSuccess: () => {
router.refresh();
setIsSubmitting(false);
},
onError: (error) => {
console.error("Error submitting data:", error);
setIsSubmitting(false);
},
});

const deleteUserSubnetData = api.subnet.deleteUserSubnetData.useMutation({
onSuccess: () => {
Expand All @@ -205,6 +207,7 @@ export function DelegatedList() {
});

const handleSubmit = async () => {
console.log(totalPercentage !== 100);
if (!selectedAccount?.address || totalPercentage !== 100) {
toast.error(
"Please connect your wallet and ensure total percentage is 100%",
Expand All @@ -224,28 +227,33 @@ export function DelegatedList() {
await deleteUserModuleData.mutateAsync({
userKey: selectedAccount.address,
});
// Submit new user module data
for (const delegatedModule of delegatedModules) {
await createUserModuleData.mutateAsync({
userKey: selectedAccount.address,
moduleId: delegatedModule.id,
weight: delegatedModule.percentage,
});
}

// Prepare data for createManyUserModuleData
const modulesData = delegatedModules.map((module) => ({
moduleId: module.id,
weight: module.percentage,
userKey: selectedAccount.address,
}));

// Submit new user module data in a single call
await createManyUserModuleData.mutateAsync(modulesData);

updateOriginalModules();
} else {
// Delete existing user subnet data
void deleteUserSubnetData.mutateAsync({
userKey: selectedAccount.address,
});
// Submit new user subnet data
for (const delegatedSubnet of delegatedSubnets) {
void createUserSubnetData.mutateAsync({
userKey: selectedAccount.address,
netuid: delegatedSubnet.id,
weight: delegatedSubnet.percentage,
});
}
// Prepare data for createUserSubnetData
const subnetsData = delegatedSubnets.map((subnet) => ({
netuid: subnet.id,
weight: subnet.percentage,
userKey: selectedAccount.address,
}));

// Submit new user subnet data in a single call
await createManyUserSubnetData.mutateAsync(subnetsData);

updateOriginalSubnets();
}
// Fetch updated data from the database
Expand Down
14 changes: 14 additions & 0 deletions packages/api/src/router/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ export const moduleRouter = {
userKey,
});
}),
createManyUserModuleData: authenticatedProcedure
.input(z.array(USER_MODULE_DATA_INSERT_SCHEMA))
.mutation(async ({ ctx, input }) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const userKey = ctx.sessionData!.userKey;

const dataToInsert = input.map((item) => ({
moduleId: item.moduleId,
weight: item.weight,
userKey,
}));

await ctx.db.insert(userModuleData).values(dataToInsert);
}),
createModuleReport: authenticatedProcedure
.input(MODULE_REPORT_INSERT_SCHEMA)
.mutation(async ({ ctx, input }) => {
Expand Down
14 changes: 14 additions & 0 deletions packages/api/src/router/subnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,18 @@ export const subnetRouter = {
set: { weight: input.weight },
});
}),
createManyUserSubnetData: authenticatedProcedure
.input(z.array(USER_SUBNET_DATA_INSERT_SCHEMA))
.mutation(async ({ ctx, input }) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const userKey = ctx.sessionData!.userKey;

const dataToInsert = input.map((item) => ({
netuid: item.netuid,
weight: item.weight,
userKey,
}));

await ctx.db.insert(userSubnetDataSchema).values(dataToInsert);
}),
} satisfies TRPCRouterRecord;

0 comments on commit 11b8ccc

Please sign in to comment.