1

I am calling an Anchor instruction (see below) from my tests to extend the lock date for this account.

Here is the Anchor code:

#[derive(Accounts)]
#[instruction(duration: u64)]
pub struct ExtendLock<'info> {
    #[account(mut)]
    pub owner: Signer<'info>,

    #[account(
        seeds = [
          lock.locker.as_ref(),
          owner.key().as_ref(),
          mint.key().as_ref()
        ],
        bump,
        has_one = mint,
        has_one = owner,
    )]
    pub lock: Account<'info, Lock>,

    pub mint: Account<'info, Mint>,

    pub system_program: Program<'info, System>,
}

pub fn extend_lock(ctx: Context<ExtendLock>, duration: u64) -> Result<()> {
    let lock = &mut ctx.accounts.lock;

    lock.unlock_date += duration;

    Ok(())
}

Notice the lock.unlock_date += duration...

Here is how I'm calling the instruction from my tests:

it("should extend a lock", async () => {
      const beforeDate = (
        await program.account.lock.fetch(env.user1Token1LockPDAKey)
      ).unlockDate.toNumber();
      
      console.log("-> ~ it ~ beforeDate:", beforeDate); // 1705041438
      
      const duration = new BN(60 * 60 * 24 * 30);
      const tx = await program.methods
        .extendLock(duration)
        .accounts({
          owner: env.user1.publicKey,
          lock: env.user1Token1LockPDAKey,
          mint: env.mint1Keypair.publicKey,
        })
        .signers([env.user1])
        .rpc();

      await provider.connection.confirmTransaction(tx);

      const afterDate = (
        await program.account.lock.fetch(env.user1Token1LockPDAKey)
      ).unlockDate.toNumber();

      console.log("-> ~ it ~ afterDate:", afterDate); // 1705041438
      
      expect(afterDate).to.equal(beforeDate + duration.toNumber());
    });

All of the accounts are correct and the test runs to completion, but the lock.unlock_date is the same. Code looks pretty straight forward to me, can someone point out what I'm doing wrong?

1 Answer 1

3

Add mut to pub lock: Account<'info, Lock>, to mark the account as writable:

    #[account(
        mut, //<--- add this
        seeds = [
          lock.locker.as_ref(),
          owner.key().as_ref(),
          mint.key().as_ref()
        ],
        bump,
        has_one = mint,
        has_one = owner,
    )]
    pub lock: Account<'info, Lock>,
1
  • 1
    That was it. Always something simple. Thank you John!
    – mmelvin0x
    Commented Dec 13, 2023 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.