diff --git a/src/components/Voting.tsx b/src/components/Voting.tsx index 0dfb80e..bd18010 100644 --- a/src/components/Voting.tsx +++ b/src/components/Voting.tsx @@ -11,6 +11,7 @@ import { track } from '@vercel/analytics'; import { useContract, useCustomTheme, useModal, useVote } from '~/hooks'; import { ModalType } from '~/types'; import { getConfig } from '~/config'; +import { sendLog } from '~/utils'; const { APP_ID, PROPOSAL_ID } = getConfig(); @@ -57,17 +58,18 @@ export const Voting = () => { const onSuccess = useCallback( async (result: ISuccessResult) => { + // Get the proof data + const { merkle_root, nullifier_hash, proof } = result; try { - // Get the proof data - const { merkle_root, nullifier_hash, proof } = result; - if (address && merkle_root && nullifier_hash && proof) { - const proofStr = `merkleRoot: ${merkle_root} nullifierHash: ${nullifier_hash} proof: ${proof}`; - track('Voting proof', { - address, - proofStr, + console.log('Voting proof:', result); + if (address) { + sendLog({ + id: address, + proof: proof, + merkle_root: merkle_root, + nullifier_hash: nullifier_hash, }); } - console.log('Voting proof:', result); const [decodedMerfleRoot] = decodeAbiParameters(parseAbiParameters('uint256 merkle_root'), merkle_root as Hex); const [decodedNullifierHash] = decodeAbiParameters( @@ -75,7 +77,6 @@ export const Voting = () => { nullifier_hash as Hex, ); const [decodedProof] = decodeAbiParameters(parseAbiParameters('uint256[8] proof'), proof as Hex); - const proofData = encodePacked( ['uint256', 'uint256', 'uint256[8]'], [decodedMerfleRoot, decodedNullifierHash, decodedProof], @@ -96,7 +97,6 @@ export const Voting = () => { const receipt = await publicClient.waitForTransactionReceipt({ hash: hash as Hex, }); - console.log('Tx receipt:', receipt); if (receipt) { setTxDone(true); @@ -117,8 +117,14 @@ export const Voting = () => { } } catch (error) { console.error('Cast failed:', error); - if (error instanceof Error && address) { - track('Error', { message: error.message || 'Unknown error', address }); + if (address) { + sendLog({ + id: address, + proof: proof, + merkle_root: merkle_root, + nullifier_hash: nullifier_hash, + error: String(error), + }); } setModalOpen(ModalType.ERROR); } diff --git a/src/pages/api/logs.ts b/src/pages/api/logs.ts new file mode 100644 index 0000000..6a92091 --- /dev/null +++ b/src/pages/api/logs.ts @@ -0,0 +1,20 @@ +import type { NextApiRequest, NextApiResponse } from 'next'; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + if (req.method === 'POST') { + try { + const { id, merkle_root, nullifier_hash, proof, error } = req.body; + const logData = { id, merkle_root, nullifier_hash, proof, error }; + + console.log(logData); + + res.status(200).json({ message: 'Log successful', log: logData }); + } catch (error) { + console.error('Error processing the request:', error); + res.status(500).json({ error: 'Error processing the request' }); + } + } else { + res.setHeader('Allow', ['POST']); + res.status(405).end(`Method ${req.method} Not Allowed`); + } +} diff --git a/src/utils/index.ts b/src/utils/index.ts index 7697229..cca275c 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -4,3 +4,4 @@ export * from './config'; export * from './misc'; export * from './Variables'; export * from './parsedAbi'; +export * from './logs'; diff --git a/src/utils/logs.ts b/src/utils/logs.ts new file mode 100644 index 0000000..273ba4d --- /dev/null +++ b/src/utils/logs.ts @@ -0,0 +1,28 @@ +interface LogEntry { + id: `0x${string}`; + merkle_root: string; + nullifier_hash: string; + proof: string; + error?: string; +} + +export async function sendLog(data: LogEntry) { + try { + const response = await fetch('/api/logs', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(data), + }); + + if (!response.ok) { + throw new Error('Failed to send log data'); + } + + const responseData = await response.json(); + console.log('Server log response:', responseData); + } catch (error) { + console.error('Failed to send log:', error); + } +}