Skip to content

Instantly share code, notes, and snippets.

@danmt
Created April 16, 2022 20:26
Show Gist options
  • Save danmt/e856b48e000460b0a2d6b7d335963ee9 to your computer and use it in GitHub Desktop.
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.
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