Skip to main content

dlp_api/instruction_builder/
call_handler.rs

1use borsh::to_vec;
2use dlp::{
3    args::CallHandlerArgs,
4    discriminator::DlpDiscriminator,
5    pda::{
6        ephemeral_balance_pda_from_payer,
7        validator_fees_vault_pda_from_validator,
8    },
9    total_size_budget, AccountSizeClass, DLP_PROGRAM_DATA_SIZE_CLASS,
10};
11use solana_program::{
12    instruction::{AccountMeta, Instruction},
13    pubkey::Pubkey,
14};
15
16/// Builds a call handler instruction.
17/// See [dlp::processor::call_handler] for docs.
18#[deprecated(since = "1.1.4", note = "Use `call_handler_v2` instead")]
19pub fn call_handler(
20    validator: Pubkey,
21    destination_program: Pubkey,
22    escrow_authority: Pubkey,
23    other_accounts: Vec<AccountMeta>,
24    args: CallHandlerArgs,
25) -> Instruction {
26    let validator_fees_vault_pda =
27        validator_fees_vault_pda_from_validator(&validator);
28
29    // handler accounts
30    let escrow_account =
31        ephemeral_balance_pda_from_payer(&escrow_authority, args.escrow_index);
32    let mut accounts = vec![
33        AccountMeta::new(validator, true),
34        AccountMeta::new(validator_fees_vault_pda, false),
35        AccountMeta::new_readonly(destination_program, false),
36        AccountMeta::new(escrow_authority, false),
37        AccountMeta::new(escrow_account, false),
38    ];
39    // append other accounts at the end
40    accounts.extend(other_accounts);
41
42    Instruction {
43        program_id: dlp::id(),
44        accounts,
45        data: [
46            DlpDiscriminator::CallHandler.to_vec(),
47            to_vec(&args).unwrap(),
48        ]
49        .concat(),
50    }
51}
52
53///
54/// Returns accounts-data-size budget for call_handler instruction.
55///
56/// This value can be used with ComputeBudgetInstruction::SetLoadedAccountsDataSizeLimit
57///
58pub fn call_handler_size_budget(
59    destination_program: AccountSizeClass,
60    other_accounts: u32,
61) -> u32 {
62    total_size_budget(&[
63        DLP_PROGRAM_DATA_SIZE_CLASS,
64        AccountSizeClass::Tiny, // validator
65        AccountSizeClass::Tiny, // validator_fees_vault_pda
66        destination_program,
67        AccountSizeClass::Tiny, // escrow_authority
68        AccountSizeClass::Tiny, // escrow_account
69    ]) + other_accounts
70}