3

I am getting this error when I try to pass an account info into a function and then want to use it in a spl transfer:

enter image description here

  pub fn move_in_direction(
    &mut self,
    direction: u8,
    player: AccountInfo,
    chest_vault: AccountInfo,
    vault_token_account: AccountInfo,
    player_token_account: AccountInfo,
    token_account_owner_pda: AccountInfo,
    token_program: AccountInfo,
    tokenOwnerBump: u8,
) -> Result<()> {
    let mut player_position: Option<(usize, usize)> = None;

    let bump = tokenOwnerBump;
    let transfer_instruction = Transfer {
        from: vault_token_account,
        to: player_token_account,
        authority: token_account_owner_pda,
    };

I do not quite understand why I am getting this error. Can someone explain my what it means that a generic argument becomes invariant? I was using AccountInfo in functions before but only if I afterwards use it in a transfer which has <'info> as generic type i get this error. So my guess is that <'info> is more specific than AccountInfo without the type but what does 'info actually do?

2 Answers 2

3

'info is a lifetime, a rust concept. tldr is that a struct A that holds a reference to some type B must specify a lifetime. This lets the compiler enforce that A isn't "alive" after B has "died".

Don't worry much, you don't actually have to immediately understand it to write programs on Solana. It's okay to just know what the definition of AccountInfo is generic over a lifetime, and sometimes you might just have to specify that just like you would for a generic type.

2

I found a solution, but I still don't understand completely what <'info> actually does. Would be nice if someone could explain this to me.

The solution is to add the generic type <'info> to the function and the parameters.

pub fn move_in_direction<'info>(
    &mut self,
    direction: u8,
    player: AccountInfo,
    chest_vault: AccountInfo,
    vault_token_account: AccountInfo<'info>,
    player_token_account: AccountInfo<'info>,
    token_account_owner_pda: AccountInfo<'info>,
    token_program: AccountInfo<'info>,
    tokenOwnerBump: u8,
) -> Result<()> {
    let mut player_position: Option<(usize, usize)> = None;

    let bump = tokenOwnerBump;
    let transfer_instruction = Transfer {
        from: vault_token_account,
        to: player_token_account,
        authority: token_account_owner_pda,
    };

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.