switchboard_solana/attestation_program/instructions/
request_trigger.rs

1use crate::prelude::*;
2
3#[derive(Accounts)]
4#[instruction(params:FunctionRequestTriggerParams)]
5pub struct FunctionRequestTrigger<'info> {
6    #[account(mut)]
7    pub request: AccountInfo<'info>,
8    #[account(signer)]
9    pub authority: AccountInfo<'info>,
10    #[account(mut)]
11    pub escrow: AccountInfo<'info>,
12    #[account(mut)]
13    pub function: AccountInfo<'info>,
14    pub state: AccountInfo<'info>,
15    pub attestation_queue: AccountInfo<'info>,
16    #[account(mut, signer)]
17    pub payer: AccountInfo<'info>,
18    #[account(address = anchor_spl::token::ID)]
19    pub token_program: AccountInfo<'info>,
20    #[account(address = solana_program::system_program::ID)]
21    pub system_program: AccountInfo<'info>,
22}
23
24#[derive(Clone, AnchorSerialize, AnchorDeserialize)]
25pub struct FunctionRequestTriggerParams {
26    pub bounty: Option<u64>,
27    pub slots_until_expiration: Option<u64>,
28    pub valid_after_slot: Option<u64>,
29}
30
31impl InstructionData for FunctionRequestTriggerParams {}
32
33impl Discriminator for FunctionRequestTriggerParams {
34    const DISCRIMINATOR: [u8; 8] = [74, 35, 78, 67, 196, 102, 78, 153];
35}
36
37impl Discriminator for FunctionRequestTrigger<'_> {
38    const DISCRIMINATOR: [u8; 8] = [74, 35, 78, 67, 196, 102, 78, 153];
39}
40
41impl<'info> FunctionRequestTrigger<'info> {
42    pub fn get_instruction(
43        &self,
44        program_id: Pubkey,
45        params: FunctionRequestTriggerParams,
46    ) -> anchor_lang::Result<Instruction> {
47        let accounts = self.to_account_metas(None);
48
49        let mut data: Vec<u8> = FunctionRequestTrigger::discriminator().try_to_vec()?;
50        let mut param_vec: Vec<u8> = params.try_to_vec()?;
51        data.append(&mut param_vec);
52
53        let instruction = Instruction::new_with_bytes(program_id, &data, accounts);
54        Ok(instruction)
55    }
56
57    pub fn invoke(
58        &self,
59        program: AccountInfo<'info>,
60        bounty: Option<u64>,
61        slots_until_expiration: Option<u64>,
62        valid_after_slot: Option<u64>,
63    ) -> ProgramResult {
64        let instruction = self.get_instruction(
65            *program.key,
66            FunctionRequestTriggerParams {
67                bounty,
68                slots_until_expiration,
69                valid_after_slot,
70            },
71        )?;
72        let account_infos = self.to_account_infos();
73
74        invoke(&instruction, &account_infos[..])
75    }
76
77    pub fn invoke_signed(
78        &self,
79        program: AccountInfo<'info>,
80        bounty: Option<u64>,
81        slots_until_expiration: Option<u64>,
82        valid_after_slot: Option<u64>,
83        signer_seeds: &[&[&[u8]]],
84    ) -> ProgramResult {
85        let instruction = self.get_instruction(
86            *program.key,
87            FunctionRequestTriggerParams {
88                bounty,
89                slots_until_expiration,
90                valid_after_slot,
91            },
92        )?;
93        let account_infos = self.to_account_infos();
94
95        invoke_signed(&instruction, &account_infos[..], signer_seeds)
96    }
97
98    fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
99        let mut account_infos = Vec::new();
100        account_infos.extend(self.request.to_account_infos());
101        account_infos.extend(self.authority.to_account_infos());
102        account_infos.extend(self.escrow.to_account_infos());
103        account_infos.extend(self.function.to_account_infos());
104        account_infos.extend(self.state.to_account_infos());
105        account_infos.extend(self.attestation_queue.to_account_infos());
106        account_infos.extend(self.payer.to_account_infos());
107        account_infos.extend(self.token_program.to_account_infos());
108        account_infos.extend(self.system_program.to_account_infos());
109        account_infos
110    }
111
112    #[allow(unused_variables)]
113    fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta> {
114        let mut account_metas = Vec::new();
115        account_metas.extend(self.request.to_account_metas(None));
116        account_metas.extend(self.authority.to_account_metas(Some(true)));
117        account_metas.extend(self.escrow.to_account_metas(None));
118        account_metas.extend(self.function.to_account_metas(None));
119        account_metas.extend(self.state.to_account_metas(None));
120        account_metas.extend(self.attestation_queue.to_account_metas(None));
121        account_metas.extend(self.payer.to_account_metas(Some(true)));
122        account_metas.extend(self.token_program.to_account_metas(None));
123        account_metas.extend(self.system_program.to_account_metas(None));
124        account_metas
125    }
126}