2

Please help me convert from ->

#[account(mut)]
pub user_token_source: Account<'info, TokenAccount>,

into ->

/// CHECK: Safe. user source token Account. user Account to swap from.
#[account(mut)]
pub user_token_source: UncheckedAccount<'info>,

Tried to convert w/ following w/o success

UncheckedAccount::try_from(
                &accounts.user_token_source.to_account_info(),
            ).clone(),

The error message is

error[E0716]: temporary value dropped while borrowed
  --> programs/amm-proxy/src/instructions/swap_base_in.rs:81:18
   |
60 |   impl<'a, 'b, 'c, 'info> From<&mut ProxySwapBaseIn<'info>>
   |                    ----- lifetime `'info` defined here
...
80 |               user_token_source: UncheckedAccount::try_from(
   |  ________________________________-
81 | |                 &accounts.user_token_source.to_account_info(),
   | |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
82 | |             ).clone(),
   | |_____________- argument requires that borrow lasts for `'info`
...
88 |           };
   |            - temporary value is freed at the end of this statement

1
  • And just like? ``` let account_info = accounts.user_token_source.to_account_info(); let smt = UncheckedAccount::try_from(&account_info); ```
    – chalda
    Commented Jun 5 at 15:13

2 Answers 2

3

The two types aren't inherently different under the hood. UncheckedAccount simply skips the validation checks a normal included account would go through in an Anchor instruction.

You can attempt to reconcile this using the current accounts you've passed in, along with the programId and ix data using the below:

let unchecked = UncheckedAccount::try_accounts(
      program_id, 
      accounts, 
      ix_data, 
      bumps, 
      reallocs
).unwrap();

However, this won't skip any of the checks, and at this point it'd be better to look at why the program is expecting an UncheckedAccount, and evaluate the current account types/validations in place currently.

1

Not sure if I fully understand your question, but you want to convert a typed account to an untyped account?

Since both AccountInfo and UncheckedAccount are basically just AccountInfo types, using .to_account_info works

Converting the token account Account<'info, TokenAccount> to AccountInfo would look like

let unchecked = ctx.accounts.user_token_source.to_account_info();
3
  • thx, when I changed as you suggested, the err is -> expected UncheckedAccount<'_>, found AccountInfo<'_>
    – att
    Commented Jun 5 at 12:14
  • Basically I'm trying to add swapInOut instruction to the https://github.com/raydium-io/raydium-contract-instructions/blob/master/example/programs/amm-proxy/src/lib.rs. After swapIn and get tokens, I need to know the amount of token I get, then I'll swapOut back for WSOL. Can you please comment out on that? Thank you
    – att
    Commented Jun 6 at 14:16
  • @att could you create a new question and provide more context? Is it still converting type TokenAccount to AccountInfo?
    – Jimii
    Commented Jun 8 at 10:43

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.