Skip to content
Draft
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
5 changes: 5 additions & 0 deletions src/features/ballot/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export const AllocationSchema = z.object({
amount: z.number().min(0),
locked: z.boolean().default(true),
});
export const BallotV2Schema1 = z.object({
voterId: z.string(),
roundId: z.string(),
type: z.string(),
});
export const BallotV2Schema = z.object({
allocations: z.array(AllocationSchema),
});
Expand Down
14 changes: 13 additions & 1 deletion src/features/voters/hooks/useApproveVoters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export function useApproveVoters({
const signer = useEthersSigner();
const { data: round } = useCurrentRound();

const saveBallot = api.ballot.saveBallot.useMutation();

return useMutation({
mutationFn: async (voters: string[]) => {
if (!signer) throw new Error("Connect wallet first");
Expand All @@ -45,9 +47,19 @@ export function useApproveVoters({
),
),
);
return attest.mutateAsync(
await attest.mutateAsync(
attestations.map((att) => ({ ...att, data: [att.data] })),
);

await Promise.all(
voters.map((voter) =>
saveBallot.mutateAsync({
voterId: voter,
roundId: round.id,
type: round.type,
}),
),
);
},
onSuccess,
onError,
Expand Down
19 changes: 19 additions & 0 deletions src/server/api/routers/ballot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
AllocationSchema,
Allocation,
BallotPublishSchema,
BallotV2Schema1,
} from "~/features/ballot/types";
import { calculateBalancedAllocations } from "~/features/ballot/hooks/useBallotEditor";
import { TRPCError } from "@trpc/server";
Expand Down Expand Up @@ -43,6 +44,24 @@ export const ballotRouter = createTRPCRouter({
select: defaultBallotSelect,
});
}),
saveBallot: ballotProcedure
.input(BallotV2Schema1)
.mutation(async ({ input, ctx }) => {
const voterId = input.voterId;
const roundId = input.roundId;
const type = input.type as RoundTypes;

return ctx.db.ballotV2.upsert({
where: { voterId_roundId_type: { voterId, roundId, type } },
update: {},
create: {
voterId,
roundId,
type,
},
select: defaultBallotSelect,
});
}),
save: ballotProcedure
.input(AllocationSchema)
.mutation(async ({ input, ctx }) => {
Expand Down
16 changes: 8 additions & 8 deletions src/server/api/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,15 @@ export const ballotProcedure = protectedProcedure.use(roundMiddleware).use(
t.middleware(async ({ ctx, next }) => {
const voterId = ctx.session?.user.name!;
const roundId = ctx.round?.id!;
const type = ctx.round?.type as RoundTypes;

// Find or create ballot
const ballot = await ctx.db.ballotV2.upsert({
where: { voterId_roundId_type: { voterId, roundId, type } },
update: {},
create: { voterId, roundId, type },
include: { allocations: true },

// Find ballot
const ballot = await ctx.db.ballotV2.findFirst({
where: { voterId, roundId },
select: { id: true },
});
if (!ballot) {
throw new TRPCError({ code: "NOT_FOUND", message: "Ballot not found" });
}
return next({
ctx: { ...ctx, voterId, roundId, ballotId: ballot.id, ballot },
});
Expand Down