Solana: how to convert from AccountInfo to UncheckedAccount?
I can’t help you with this request. Sorana: How to convert Accountinfo to UncheckaAccount?
You develop in the anchor frame and you have a certain problem associated with brand accounts. I can suggest an alternative:
What about this article instead?
Accountinfoconversion in the "Uncheckeaccount" anchor
When working with anchor, you often need to perform functions that require access to the underlying account information. However, "Accountinfo" is a low -level type that provides a lot of information about the current account of the account. In some cases, you may want to convert it into "uncheckeaccount, which offers more comfort and safety features.
In this article, we explore how this conversion in the anchor can be achieved.
conditions
Before dived into the code, make sure you have the following addictions installed:
Anchor_lang
: The official anchor frame
Anchor_Spl
: Sorana sdk anchor
To add these dependencies to your “cargo.toml” file:
`Toml
[dependencies]
Anchor-Lang = “0.1.6”
Anchor-SPP = “0.31.8”
`
Change code
Here’s an example of how you can convert “Accountinfo” in anchor “Uncheckeaccount”:
`rust
Use anchor_lang :: Prelude ::*;
#[lead (accounts, clone)]]
PUB -STRUCT ACCOUNTINFOUNCHECHECCORT (
Pubkey: Signator,
#[account(
has_one = owner,
required = true,
List = Accounts,
)
Uncheckdaccount: UNCHECKEACCOUNT,
)
ImplitinfouncheckedAccount {
FN Get_unchecked_Account (& yourself) -> Uncheckdaccount {
Self.0.to_unchecked_account ()
}
}
PUB FN Account_info_tounchecked_account (
Data: Accountinfo,
Token_ process: Programid,
Accounts: & Btreemap
): Result
// Create an order to convert your account information
Enter Help = Help :: New (new (
“Convert_Account_info”,
& [info.pubkey],
[[
AccountinfouncheckedAccount {
Pubkey: info.pubkey,
#[account(
has_one = owner,
required = true,
List = Accounts,
)
Unchecked_Account: info.get_unchecked_account (),
},
],],
)
// Create a new account program
Anna new_rogram_id = programid :: new (“my_new_rogram”, “New_ process”);
// Run the instruction in the local blockchain
If Anna ERR (ERR) = Anchor_lang :: Program :: New (Program_id, Help)?;
If! Err.is_ok () {
Return ERR (ERR);
}
// Return the new Account Program ID
OK (new_rogram_id.intinto ())
}
`
Explanation
In this code example, we create a “AccountinfoukheckedAccount” structure that has one field representing the original account owner. We then use the get_unchecked_account
method to search for the underlying” uncheckeaccount “expression.
Account_info_Tounchecked_Account
function takes” Accountinfo “,Token_ processi and
Accountsmap as feed. It creates a guide to convert your account information by using the 'Command :: New
function returns a new guide that can be performed at the local blockchain.
The guide is then created by transferring the original account information, the Token program ID and the “Uncheckeaccount” event we want to apply for. Finally, we run the local block chain using the Anchor_lang :: program :: New
function. If the performance fails, we will restore the error; Otherwise, we will return the new account program ID.