Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: logs api #25

Merged
merged 4 commits into from
May 8, 2024
Merged
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
30 changes: 18 additions & 12 deletions src/components/Voting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -57,25 +58,25 @@ 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(
parseAbiParameters('uint256 nullifier_hash'),
nullifier_hash as Hex,
);
const [decodedProof] = decodeAbiParameters(parseAbiParameters('uint256[8] proof'), proof as Hex);

const proofData = encodePacked(
['uint256', 'uint256', 'uint256[8]'],
[decodedMerfleRoot, decodedNullifierHash, decodedProof],
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down
20 changes: 20 additions & 0 deletions src/pages/api/logs.ts
Original file line number Diff line number Diff line change
@@ -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`);
}
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './config';
export * from './misc';
export * from './Variables';
export * from './parsedAbi';
export * from './logs';
28 changes: 28 additions & 0 deletions src/utils/logs.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading