2

I'm required to use conditional logic in my ix where two mutable references to accounts could be be pointing to the same account. What happens when I don't sync them at the end?

1 Answer 1

2

You can use both accounts' values independently within your program but once the program is executed the final data that is saved on the blockchain is the data stored inside the last account passed to the instruction.

For example for this accounts structure, if accountFoo and accountBar point to the same account:

#[derive(Accounts)]
pub struct ChangeAccountsData<'info> {
    #[account(mut)]
    pub accountFoo: Account<'info, MyAccount>,
    #[account(mut)]
    pub accountBar: Account<'info, MyAccount>,
    ...
}

After the ix is executed, the value in accountBar is the one that is saved on the blockchain.

If you switch the accounts' order like this:

#[derive(Accounts)]
pub struct ChangeAccountsData<'info> {
    #[account(mut)]
    pub accountBar: Account<'info, MyAccount>,
    #[account(mut)]
    pub accountFoo: Account<'info, MyAccount>,
    ...
}

The value in accountFoo is the one that will be saved on the blockchain.

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.