Skip to content

Commit

Permalink
removed some code
Browse files Browse the repository at this point in the history
  • Loading branch information
yashgo0018 committed Jun 18, 2024
1 parent 74067e3 commit 225da49
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/nextjs/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ NEXT_PUBLIC_ALCHEMY_API_KEY=
NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID=
NEXT_PUBLIC_PINATA_JWT=
NEXT_PUBLIC_PINATA_GATEWAY=
SERVER_PRIVATE_KEY=
52 changes: 52 additions & 0 deletions packages/nextjs/app/api/create-poll/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
// import { decodeEventLog } from "viem";
import { maciWrapperContract } from "~~/constants";

const CreatePollSchema = z.object({
name: z.string(),
description: z.string(),
type: z.enum(["removeAdmin", "addAdmin", "removeUser", "addUser"]),
duration: z.number().int().gt(0),
telegramChatId: z.string(),
votingModeQv: z.boolean().default(false),
});

export const POST = async (req: NextRequest) => {
// console.log(await req.json());
let body: typeof CreatePollSchema._type;
try {
const { success, error, data } = CreatePollSchema.safeParse(await req.json());
if (!success) {
return NextResponse.json({ error: "invalid body", issues: error.issues }, { status: 409 });
}
body = data;
} catch (e) {
return NextResponse.json({ error: "invalid body" }, { status: 409 });
}

const { name, description, duration, telegramChatId, votingModeQv } = body;

// create poll on the maci contract
// const tx =
await maciWrapperContract.write.createPoll([
name,
["I agree", "I dont agree"],
JSON.stringify({ description, telegramChatId }),
BigInt(duration),
votingModeQv ? 0 : 1,
]);
// const transaction = await publicClient.getTransactionReceipt({ hash: tx });

// const events: any[] = [];

// for (const log of transaction.logs) {
// try {
// events.push(decodeEventLog({ abi: maciWrapperContract.abi, data: log.data, topics: log.topics }));
// } catch (e) { }
// }

// const pollCreatedEvent = events.filter(event => event.eventName === "PollCreated")[0];

return NextResponse.json({ message: "The poll is created successfully" });
};
15 changes: 15 additions & 0 deletions packages/nextjs/app/api/fetch-polls/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { NextResponse } from "next/server";
import { maciWrapperContract } from "~~/constants";

// uint256 _page,
// uint256 _perPage,
// bool _ascending

(BigInt.prototype as any).toJSON = function () {
return this.toString();
};

export const GET = async () => {
const polls = await maciWrapperContract.read.fetchPolls([1n, 10n, true]);
return NextResponse.json({ polls });
};
27 changes: 27 additions & 0 deletions packages/nextjs/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import deployedContracts from "./contracts/deployedContracts";
import scaffoldConfig from "./scaffold.config";
import { ethers } from "ethers";
import { createPublicClient, createWalletClient, getContract, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";

export const chain = scaffoldConfig.targetNetworks[0];

export const publicClient = createPublicClient({ chain, transport: http() });

export const serverAccount = privateKeyToAccount(process.env.SERVER_PRIVATE_KEY as `0x${string}`);
export const serverWalletClient = createWalletClient({
chain: chain,
transport: http(),
key: process.env.SERVER_PRIVATE_KEY,
account: serverAccount,
});

export const maciWrapperContract = getContract({
abi: deployedContracts[chain.id].MACIWrapper.abi,
address: deployedContracts[chain.id].MACIWrapper.address,
publicClient: publicClient,
walletClient: serverWalletClient,
});

export const deploymentBlock = BigInt(deployedContracts[chain.id].MACIWrapper.deploymentBlockNumber);
export const ethersProvider = new ethers.JsonRpcProvider(chain.rpcUrls.default.http[0]);
1 change: 1 addition & 0 deletions packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"usehooks-ts": "^2.13.0",
"viem": "1.19.9",
"wagmi": "1.4.12",
"zod": "^3.23.8",
"zustand": "^4.1.2"
},
"devDependencies": {
Expand Down

0 comments on commit 225da49

Please sign in to comment.