Anchor code
#[derive(Accounts)]
#[instruction(todo_index: u8)] // Added this to pass the index as an instruction argument
pub struct CreateTodo<'info> {
#[account(mut)]
pub authority: Signer<'info>,
#[account(
mut,
seeds = [USER_TAG, authority.key().as_ref()],
bump,
has_one = authority,
)]
pub user_profile: Box<Account<'info, UserProfile>>,
#[account(
init,
payer = authority,
seeds = [TODO_TAG, authority.key().as_ref(), &[todo_index as u8]], // Updated to use todo_index argument
bump,
space = 8 + std::mem::size_of::<TodoAccount>()
)]
pub todo_account: Box<Account<'info, TodoAccount>>,
pub system_program: Program<'info, System>,
}
Typescript client
it("Created Todo", async () => {
let user_profile_account = await program.account.userProfile.fetch(userPDA);
console.log(user_profile_account);
let [todoPDA, _bump] = anchor.web3.PublicKey.findProgramAddressSync(
[
utf8.encode("TODO_ACCOUNT"),
provider.wallet.publicKey.toBuffer(),
Uint8Array.from([user_profile_account.currentTodoIndex]),
],
program.programId
);
let content_todo = "My name is pranaya raj";
let trxHash = await program.methods
.createTodo(content_todo)
.accounts({
authority: provider.wallet.publicKey,
todoAccount: todoPDA,
userProfile: userPDA,
systemProgram: anchor.web3.SystemProgram.programId,
})
.rpc();
let { content, idx, markedBool } = await program.account.todoAccount.fetch(
todoPDA
);
expect(content).to.equal(content_todo);
expect(idx).to.equal(user_profile_account.currentTodoIndex);
expect(markedBool).to.equal(false);
});
yarn run v1.22.22
$ /Users/pranayraj/Desktop/SolanaDoc/todo_dapp/node_modules/.bin/ts-mocha -p ./tsconfig.json -t 1000000 'tests/**/*.ts'
The authority wallet addres is 12r4uFpQHVvVfX3qpAxHhddExdGMecFSZYPcVhxRPZNm
The PDA for creating User Profile is: ACcqx7Rk7zDLUb2rQsnQZaWKKvAPhyANxQJm7S3U9nGv
(node:4369) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
todo_dapp
Creating User Profile Account.......
✔ Initialized User Profile Account! (459ms)
The TodoPDA is: 2CQsCEpUXaDMGS9oZfgbzCY1cuXzNpNsBkFyYkj6ZQcz
1) Created Todo
✔ Marked Todo
✔ Removed Todo
3 passing (492ms)
1 failing
1) todo_dapp
Created Todo:
Error: AnchorError caused by account: todo_account. Error Code: ConstraintSeeds. Error Number: 2006. Error Message: A seeds constraint was violated.
Program log: Left:
Program log: 2CQsCEpUXaDMGS9oZfgbzCY1cuXzNpNsBkFyYkj6ZQcz
Program log: Right:
Program log: 7cHCMir3jWC3dcgqW1MV9tf2bhpHT7pTvTFNQ6Ka5Y2C
at Function.parse (node_modules/@coral-xyz/anchor/src/error.ts:168:14)
at translateError (node_modules/@coral-xyz/anchor/src/error.ts:277:35)
at MethodsBuilder.rpc [as _rpcFn] (node_modules/@coral-xyz/anchor/src/program/namespace/rpc.ts:35:29)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.