As you can see below I am trying to place a requirement to make sure a Poll as started and not finished so we can initiate a candidate (had a candidate to a poll).
The problem is that the comparison is not working and I am getting the error triggered when it shouldn't.
I suspect it must be something with Anchor Serialization or some mismatching types in TS.
This is the code:
//Instructions
pub fn initialize_candidate(
context: Context<InitializeCandidate>,
candidate_name: String,
poll_id: u64,
) -> Result<()> {
let candidate = &mut context.accounts.candidate;
let poll = &mut context.accounts.poll;
let clock = Clock::get()?;
//------- Trying to debug ----------
msg!("poll_start: {}", poll.poll_start as i64);
msg!("clock.unix_timestamp: {}", clock.unix_timestamp);
msg!("Poll start < clock.unix_timestamp: {}", poll.poll_start as i64 > clock.unix_timestamp);
msg!("poll_end: {}", poll.poll_end as i64);
msg!("Poll end < clock.unix_timestamp: {}", poll.poll_end as i64 <= clock.unix_timestamp);
//----------------------------------
require!(poll.poll_owner == *context.accounts.signer.key, ErrorCode::Unauthorized);
require!(poll.poll_end as i64 <= clock.unix_timestamp, ErrorCode::PollEnded);
require!(poll.poll_start as i64 > clock.unix_timestamp, ErrorCode::PollNotStarted);
require!(poll.candidate_amount + 1 <= 10, ErrorCode::TooManyCandidates);
candidate.candidate_name = candidate_name;
candidate.candidate_votes = 0;
poll.candidate_amount += 1;
msg!("You have initialized a candidate called and added them to the poll");
Ok(())
}
Test from the test file:
it('Initialize and add Candidates to the pool', async () => {
//call the initialize candidate instruction to add candidates to the poll
try {
await program.methods
.initializeCandidate('Yes, easy!', new anchor.BN(1))
.rpc();
} catch (e) {
// Log the full error for debugging
console.log(`Error details: ${e.message}. Now the full log here:`, e);
assert.fail(
'Poll did not initialize successfully. Check the error message for more details'
);
}
try {
await program.methods
.initializeCandidate('No, never!', new anchor.BN(1))
.rpc();
} catch (e) {
// Log the full error for debugging
console.log(`Error details: ${e.message}. Now the full log here:`, e);
assert.fail(
'Poll did not initialize successfully. Check the error message for more details'
);
}
//Get the address of the poll
const [yesAddress] = PublicKey.findProgramAddressSync(
[
new anchor.BN(1).toArrayLike(Buffer, 'le', 8),
Buffer.from('Yes, easy!'),
],
programId
);
const [noAddress] = PublicKey.findProgramAddressSync(
[
new anchor.BN(1).toArrayLike(Buffer, 'le', 8),
Buffer.from('No, never!'),
],
programId
);
//Fetch the candidates
const yesCandidate = await program.account.candidate.fetch(yesAddress);
const noCandidate = await program.account.candidate.fetch(noAddress);
//lets confirm the candidates added to the pool have been initialized and have 0 votes
assert.equal(yesCandidate.candidateVotes.toNumber(), 0);
assert.equal(noCandidate.candidateVotes.toNumber(), 0);
});
These are the logs:
1) Initialize and add Candidates to the pool
Error details: AnchorError thrown in programs/voting-dapp/src/instructions/initialize_candidate.rs:48. Error Code: PollEnded. Error Number: 6003. Error Message: Unable to initialize a new candidat. Poll has already ended.. Now the full log here: AnchorError: AnchorError thrown in programs/voting-dapp/src/instructions/initialize_candidate.rs:48. Error Code: PollEnded. Error Number: 6003. Error Message: Unable to initialize a new candidat. Poll has already ended.
at Function.parse (/Users/josesantos/Desktop/Jose/School Of Solana/program-Jars1987-1/anchor_project/voting-dapp/node_modules/@coral-xyz/anchor/src/error.ts:152:14)
at translateError (/Users/josesantos/Desktop/Jose/School Of Solana/program-Jars1987-1/anchor_project/voting-dapp/node_modules/@coral-xyz/anchor/src/error.ts:277:35)
at MethodsBuilder.rpc [as _rpcFn] (/Users/josesantos/Desktop/Jose/School Of Solana/program-Jars1987-1/anchor_project/voting-dapp/node_modules/@coral-xyz/anchor/src/program/namespace/rpc.ts:35:29)
at processTicksAndRejections (node:internal/process/task_queues:95:5) {
errorLogs: [
'Program log: AnchorError thrown in programs/voting-dapp/src/instructions/initialize_candidate.rs:48. Error Code: PollEnded. Error Number: 6003. Error Message: Unable to initialize a new candidat. Poll has already ended.'
],
logs: [
'Program 5TkN5PsZXQaznE4FqkpgtDzj8D5PQvJHctnNc4hShTDV invoke [1]',
'Program log: Instruction: InitializeCandidate',
'Program 11111111111111111111111111111111 invoke [2]',
'Program 11111111111111111111111111111111 success',
'Program log: poll_start: 1731946140',
'Program log: clock.unix_timestamp: 1731966934',
'Program log: Poll start < clock.unix_timestamp: false',
'Program log: poll_end: 1735689599',
'Program log: Poll end < clock.unix_timestamp: false',
'Program log: AnchorError thrown in programs/voting-dapp/src/instructions/initialize_candidate.rs:48. Error Code: PollEnded. Error Number: 6003. Error Message: Unable to initialize a new candidat. Poll has already ended.',
'Program 5TkN5PsZXQaznE4FqkpgtDzj8D5PQvJHctnNc4hShTDV consumed 19328 of 200000 compute units',
'Program 5TkN5PsZXQaznE4FqkpgtDzj8D5PQvJHctnNc4hShTDV failed: custom program error: 0x1773'
],
error: {
errorCode: { code: 'PollEnded', number: 6003 },
errorMessage: 'Unable to initialize a new candidat. Poll has already ended',
comparedValues: undefined,
origin: {
file: 'programs/voting-dapp/src/instructions/initialize_candidate.rs',
line: 48
}
},
_programErrorStack: ProgramErrorStack {
stack: [
[PublicKey [PublicKey(5TkN5PsZXQaznE4FqkpgtDzj8D5PQvJHctnNc4hShTDV)]]
]
}
}
How do I fix this?