Created
April 16, 2022 20:26
-
-
Save danmt/e856b48e000460b0a2d6b7d335963ee9 to your computer and use it in GitHub Desktop.
Get an instruction account from the remaining accounts list and decode it, bubble up in case of errors.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn get_remaining_account<'info, T: AccountSerialize + AccountDeserialize + Owner + Clone>( | |
remaining_accounts: &[AccountInfo<'info>], | |
index: usize, | |
) -> std::result::Result<Option<Account<'info, T>>, Error> { | |
let maybe_account: Option<&AccountInfo> = remaining_accounts.get(index); | |
let maybe_decoded_account: Option<std::result::Result<Account<'info, T>, Error>> = | |
maybe_account.map(Account::try_from); | |
match maybe_decoded_account { | |
Some(Ok(account)) => Ok(Some(account)), | |
Some(Err(_)) => return Err(error!(ErrorCode::InvalidAccount)), | |
None => Ok(None), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment