-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
74067e3
commit 225da49
Showing
5 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters