From 62c4f68e501dafd45bfacb26e293bdf45c3cd3f6 Mon Sep 17 00:00:00 2001 From: aao056 Date: Thu, 9 Jan 2025 16:53:19 +0200 Subject: [PATCH] fix bug --- anchor/Anchor.toml | 2 +- anchor/programs/votingdapp/src/lib.rs | 11 ++++++++--- anchor/src/votingdapp-exports.ts | 2 +- anchor/target/idl/votingdapp.json | 2 +- anchor/target/types/votingdapp.ts | 2 +- src/components/results/results-data-access.tsx | 14 ++++++++++++++ src/components/results/results-ui.tsx | 18 +++++++++++++----- .../votingdapp/votingdapp-data-access.tsx | 2 +- src/components/votingdapp/votingdapp-ui.tsx | 5 ++++- 9 files changed, 44 insertions(+), 14 deletions(-) diff --git a/anchor/Anchor.toml b/anchor/Anchor.toml index 3c8c8ea..b5b587a 100644 --- a/anchor/Anchor.toml +++ b/anchor/Anchor.toml @@ -6,7 +6,7 @@ resolution = true skip-lint = false [programs.localnet] -votingdapp = "FPzzmxm38cjWpiCQzHwF1Gb6stPPxGXGqiA6RZ8WAGNn" +votingdapp = "259gW5LnYKg3PLeu4aBYb8bGRofBAFDnWsFjsTCG1cEq" [registry] url = "https://api.apr.dev" diff --git a/anchor/programs/votingdapp/src/lib.rs b/anchor/programs/votingdapp/src/lib.rs index 87c13aa..922d03b 100644 --- a/anchor/programs/votingdapp/src/lib.rs +++ b/anchor/programs/votingdapp/src/lib.rs @@ -2,7 +2,7 @@ use anchor_lang::prelude::*; -declare_id!("FPzzmxm38cjWpiCQzHwF1Gb6stPPxGXGqiA6RZ8WAGNn"); +declare_id!("259gW5LnYKg3PLeu4aBYb8bGRofBAFDnWsFjsTCG1cEq"); #[program] pub mod votingdapp { @@ -46,7 +46,6 @@ pub mod votingdapp { /// Casts a vote for a candidate by ID. pub fn vote(ctx: Context, candidate_id: u16) -> Result<()> { let election = &mut ctx.accounts.election; - // Get the current time from the Solana Clock let current_time = Clock::get()?.unix_timestamp; @@ -63,11 +62,13 @@ pub mod votingdapp { return Err(ErrorCode::AlreadyVoted.into()); } + let candidate_name: String; if let Some(candidate) = election .candidates .iter_mut() .find(|c: &&mut Candidate| c.id == candidate_id) { + candidate_name = candidate.name.clone(); candidate.voters.push(ctx.accounts.signer.key()) } else { return Err(ErrorCode::CandidateNotFound.into()); @@ -76,7 +77,11 @@ pub mod votingdapp { // Add the user to the voters list election.voters.push(ctx.accounts.signer.key()); - msg!("Vote cast for candidate ID: {}", candidate_id); + msg!( + "Vote cast for candidate: {} (ID: {})", + candidate_name, + candidate_id + ); Ok(()) } } diff --git a/anchor/src/votingdapp-exports.ts b/anchor/src/votingdapp-exports.ts index b3968d1..56128ad 100644 --- a/anchor/src/votingdapp-exports.ts +++ b/anchor/src/votingdapp-exports.ts @@ -21,7 +21,7 @@ export function getVotingdappProgramId(cluster: Cluster) { case 'devnet': case 'testnet': // This is the program ID for the Votingdapp program on devnet and testnet. - return new PublicKey('CounNZdmsQmWh7uVngV9FXW2dZ6zAgbJyYsvBpqbykg') + return new PublicKey('259gW5LnYKg3PLeu4aBYb8bGRofBAFDnWsFjsTCG1cEq') case 'mainnet-beta': default: return VOTINGDAPP_PROGRAM_ID diff --git a/anchor/target/idl/votingdapp.json b/anchor/target/idl/votingdapp.json index 09411f1..c9ff6b8 100644 --- a/anchor/target/idl/votingdapp.json +++ b/anchor/target/idl/votingdapp.json @@ -1,5 +1,5 @@ { - "address": "FPzzmxm38cjWpiCQzHwF1Gb6stPPxGXGqiA6RZ8WAGNn", + "address": "259gW5LnYKg3PLeu4aBYb8bGRofBAFDnWsFjsTCG1cEq", "metadata": { "name": "votingdapp", "version": "0.1.0", diff --git a/anchor/target/types/votingdapp.ts b/anchor/target/types/votingdapp.ts index f4d4ca7..ccfaf02 100644 --- a/anchor/target/types/votingdapp.ts +++ b/anchor/target/types/votingdapp.ts @@ -5,7 +5,7 @@ * IDL can be found at `target/idl/votingdapp.json`. */ export type Votingdapp = { - "address": "FPzzmxm38cjWpiCQzHwF1Gb6stPPxGXGqiA6RZ8WAGNn", + "address": "259gW5LnYKg3PLeu4aBYb8bGRofBAFDnWsFjsTCG1cEq", "metadata": { "name": "votingdapp", "version": "0.1.0", diff --git a/src/components/results/results-data-access.tsx b/src/components/results/results-data-access.tsx index da0b70b..bd666f6 100644 --- a/src/components/results/results-data-access.tsx +++ b/src/components/results/results-data-access.tsx @@ -2,17 +2,31 @@ import { useVotingdappProgram } from "../votingdapp/votingdapp-data-access"; export function useCandidateScores() { const { accounts } = useVotingdappProgram() + if (!accounts.data) return const candidates = accounts.data && accounts.data.length && accounts.data[0].account.candidates if (!candidates) return let total_votes: number = 0 candidates.forEach((cand) => { + if (!cand.voters.length) return + total_votes += cand.voters.length }) const scores: any[] = [] + console.log(candidates) + candidates.map((cand) => { + if (!cand.voters.length) { + scores.push({ + label: cand.name, + value: 0 + }) + + return + } + scores.push({ label: cand.name, value: calculatePercentage(cand.voters.length, total_votes) diff --git a/src/components/results/results-ui.tsx b/src/components/results/results-ui.tsx index 9a592b5..aa9aa30 100644 --- a/src/components/results/results-ui.tsx +++ b/src/components/results/results-ui.tsx @@ -1,17 +1,25 @@ -import * as React from 'react'; +import React, { useEffect } from 'react'; import { PieChart, pieArcLabelClasses } from '@mui/x-charts/PieChart'; import { valueFormatter, useCandidateScores } from './results-data-access'; +import { redirect } from 'next/navigation' export function ResultsChart() { + const size = { width: 500, - height: 300, + height: 200, }; - const candidates_scores: any = useCandidateScores() + const candidates_scores: any = useCandidateScores(); + + useEffect(() => { + if (!candidates_scores) { + redirect('/votingdapp'); + } + }, [candidates_scores]); const data = { - data: candidates_scores, + data: candidates_scores || [], // Provide a fallback to avoid errors during rendering valueFormatter, }; @@ -41,4 +49,4 @@ export function ResultsChart() { {...size} /> ); -} \ No newline at end of file +} diff --git a/src/components/votingdapp/votingdapp-data-access.tsx b/src/components/votingdapp/votingdapp-data-access.tsx index 9c5ad3e..c88ccea 100644 --- a/src/components/votingdapp/votingdapp-data-access.tsx +++ b/src/components/votingdapp/votingdapp-data-access.tsx @@ -1,7 +1,7 @@ import { useConnection } from '@solana/wallet-adapter-react' import { useMutation, useQuery } from '@tanstack/react-query' import { useMemo } from 'react' -import { Cluster, Keypair, PublicKey } from '@solana/web3.js' +import { Cluster, PublicKey } from '@solana/web3.js' import { useCluster } from '../cluster/cluster-data-access' import { useAnchorProvider } from '../solana/solana-provider' import { getVotingdappProgram, getVotingdappProgramId } from '@project/anchor' diff --git a/src/components/votingdapp/votingdapp-ui.tsx b/src/components/votingdapp/votingdapp-ui.tsx index cba1d90..96cb972 100644 --- a/src/components/votingdapp/votingdapp-ui.tsx +++ b/src/components/votingdapp/votingdapp-ui.tsx @@ -4,6 +4,7 @@ import { ellipsify } from '../ui/ui-layout' import { ExplorerLink } from '../cluster/cluster-ui' import { useVotingdappProgram, useVotingdapprogramAccount } from './votingdapp-data-access' import Image from 'next/image' +import { useWallet } from '@solana/wallet-adapter-react' export function VotingDappSystemCreate() { const { initialize } = useVotingdappProgram() @@ -69,6 +70,8 @@ function BlockchainVotingSystemCard({ account, candidates }: { account: PublicKe account }) + const wallet = useWallet() + const ListCandidates = candidates.map((candidate) =>
@@ -85,7 +88,7 @@ function BlockchainVotingSystemCard({ account, candidates }: { account: PublicKe