1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! Offchain helper for fetching required accounts to build instructions

use {
    crate::{get_extra_account_metas_address, instruction::ExecuteInstruction},
    solana_program::{instruction::AccountMeta, program_error::ProgramError, pubkey::Pubkey},
    spl_tlv_account_resolution::state::ExtraAccountMetas,
    std::future::Future,
};

/// Type representing the output of an account fetching function, for easy
/// chaining between APIs
pub type AccountDataResult = Result<Option<Vec<u8>>, AccountFetchError>;
/// Generic error type that can come out of any client while fetching account data
pub type AccountFetchError = Box<dyn std::error::Error + Send + Sync>;

/// Offchain helper to get all additional required account metas for a mint
///
/// To be client-agnostic and to avoid pulling in the full solana-sdk, this
/// simply takes a function that will return its data as `Future<Vec<u8>>` for
/// the given address. Can be called in the following way:
///
/// ```rust,ignore
/// use futures_util::TryFutureExt;
/// use solana_client::nonblocking::rpc_client::RpcClient;
/// use solana_program::pubkey::Pubkey;
///
/// let program_id = Pubkey::new_unique();
/// let mint = Pubkey::new_unique();
/// let client = RpcClient::new_mock("succeeds".to_string());
/// let mut account_metas = vec![];
///
/// get_extra_account_metas(
///     &mut account_metas,
///     |address| self.client.get_account(&address).map_ok(|opt| opt.map(|acc| acc.data)),
///     &mint,
///     &program_id,
/// ).await?;
/// ```
pub async fn get_extra_account_metas<F, Fut>(
    account_metas: &mut Vec<AccountMeta>,
    get_account_data_fn: F,
    mint: &Pubkey,
    permissioned_transfer_program_id: &Pubkey,
) -> Result<(), AccountFetchError>
where
    F: Fn(Pubkey) -> Fut,
    Fut: Future<Output = AccountDataResult>,
{
    let validation_address =
        get_extra_account_metas_address(mint, permissioned_transfer_program_id);
    let validation_account_data = get_account_data_fn(validation_address)
        .await?
        .ok_or(ProgramError::InvalidAccountData)?;
    ExtraAccountMetas::add_to_vec::<ExecuteInstruction>(account_metas, &validation_account_data)?;
    // The onchain helpers pull out the required accounts from an opaque
    // slice by pubkey, so the order doesn't matter here!
    account_metas.push(AccountMeta::new_readonly(
        *permissioned_transfer_program_id,
        false,
    ));
    account_metas.push(AccountMeta::new_readonly(validation_address, false));

    Ok(())
}