Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
aao056 committed Jan 9, 2025
1 parent 8839c6e commit 62c4f68
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 14 deletions.
2 changes: 1 addition & 1 deletion anchor/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ resolution = true
skip-lint = false

[programs.localnet]
votingdapp = "FPzzmxm38cjWpiCQzHwF1Gb6stPPxGXGqiA6RZ8WAGNn"
votingdapp = "259gW5LnYKg3PLeu4aBYb8bGRofBAFDnWsFjsTCG1cEq"

[registry]
url = "https://api.apr.dev"
Expand Down
11 changes: 8 additions & 3 deletions anchor/programs/votingdapp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use anchor_lang::prelude::*;

declare_id!("FPzzmxm38cjWpiCQzHwF1Gb6stPPxGXGqiA6RZ8WAGNn");
declare_id!("259gW5LnYKg3PLeu4aBYb8bGRofBAFDnWsFjsTCG1cEq");

#[program]
pub mod votingdapp {
Expand Down Expand Up @@ -46,7 +46,6 @@ pub mod votingdapp {
/// Casts a vote for a candidate by ID.
pub fn vote(ctx: Context<Vote>, 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;

Expand All @@ -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());
Expand All @@ -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(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion anchor/src/votingdapp-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion anchor/target/idl/votingdapp.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"address": "FPzzmxm38cjWpiCQzHwF1Gb6stPPxGXGqiA6RZ8WAGNn",
"address": "259gW5LnYKg3PLeu4aBYb8bGRofBAFDnWsFjsTCG1cEq",
"metadata": {
"name": "votingdapp",
"version": "0.1.0",
Expand Down
2 changes: 1 addition & 1 deletion anchor/target/types/votingdapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 14 additions & 0 deletions src/components/results/results-data-access.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 13 additions & 5 deletions src/components/results/results-ui.tsx
Original file line number Diff line number Diff line change
@@ -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,
};

Expand Down Expand Up @@ -41,4 +49,4 @@ export function ResultsChart() {
{...size}
/>
);
}
}
2 changes: 1 addition & 1 deletion src/components/votingdapp/votingdapp-data-access.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
5 changes: 4 additions & 1 deletion src/components/votingdapp/votingdapp-ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -69,6 +70,8 @@ function BlockchainVotingSystemCard({ account, candidates }: { account: PublicKe
account
})

const wallet = useWallet()

const ListCandidates = candidates.map((candidate) =>
<div key={candidate.id.toString()} className="card bg-base-300 w-96 shadow-2xl mb-6">
<figure className="px-10 pt-10">
Expand All @@ -85,7 +88,7 @@ function BlockchainVotingSystemCard({ account, candidates }: { account: PublicKe
<div className="card-actions w-full">
<button
className="btn btn-primary w-full"
onClick={() => vote.mutateAsync(candidate.id)}>
onClick={() => vote.mutateAsync({ candidateId: candidate.id, signerKey: wallet.publicKey })}>
Vote
</button>
</div>
Expand Down

0 comments on commit 62c4f68

Please # to comment.