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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#![deny(missing_docs)]
use anchor_lang::prelude::*;
use anchor_lang::solana_program;
use vipers::prelude::*;
use vipers::program_err;
#[account]
#[derive(Default, Debug, PartialEq)]
pub struct SmartWallet {
    
    pub base: Pubkey,
    
    pub bump: u8,
    
    pub threshold: u64,
    
    pub minimum_delay: i64,
    
    pub grace_period: i64,
    
    
    
    
    
    
    pub owner_set_seqno: u32,
    
    pub num_transactions: u64,
    
    pub owners: Vec<Pubkey>,
    
    pub reserved: [u64; 16],
}
impl SmartWallet {
    
    pub fn space(max_owners: u8) -> usize {
        4 
            + std::mem::size_of::<SmartWallet>()
            + 4 
            + std::mem::size_of::<Pubkey>() * (max_owners as usize)
    }
    
    pub fn owner_index_opt(&self, key: Pubkey) -> Option<usize> {
        self.owners.iter().position(|a| *a == key)
    }
    
    pub fn owner_index(&self, key: Pubkey) -> Result<usize> {
        Ok(unwrap_opt!(self.owner_index_opt(key), InvalidOwner))
    }
}
#[account]
#[derive(Debug, Default, PartialEq)]
pub struct Transaction {
    
    pub smart_wallet: Pubkey,
    
    
    
    pub index: u64,
    
    pub bump: u8,
    
    pub proposer: Pubkey,
    
    pub instructions: Vec<TXInstruction>,
    
    pub signers: Vec<bool>,
    
    pub owner_set_seqno: u32,
    
    
    
    
    pub eta: i64,
    
    pub executor: Pubkey,
    
    pub executed_at: i64,
}
impl Transaction {
    
    pub fn space(instructions: Vec<TXInstruction>) -> usize {
        4  
            + std::mem::size_of::<Transaction>()
            + 4 
            + (instructions.iter().map(|ix| ix.space()).sum::<usize>())
    }
    
    pub fn num_signers(&self) -> usize {
        self.signers.iter().filter(|&did_sign| *did_sign).count()
    }
}
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug, Default, PartialEq)]
pub struct TXInstruction {
    
    pub program_id: Pubkey,
    
    pub keys: Vec<TXAccountMeta>,
    
    pub data: Vec<u8>,
}
impl TXInstruction {
    
    pub fn space(&self) -> usize {
        std::mem::size_of::<Pubkey>()
            + (self.keys.len() as usize) * std::mem::size_of::<TXAccountMeta>()
            + (self.data.len() as usize)
    }
}
#[derive(AnchorSerialize, AnchorDeserialize, Debug, PartialEq, Copy, Clone)]
pub struct TXAccountMeta {
    
    pub pubkey: Pubkey,
    
    pub is_signer: bool,
    
    pub is_writable: bool,
}
impl From<&TXInstruction> for solana_program::instruction::Instruction {
    fn from(tx: &TXInstruction) -> solana_program::instruction::Instruction {
        solana_program::instruction::Instruction {
            program_id: tx.program_id,
            accounts: tx.keys.clone().into_iter().map(Into::into).collect(),
            data: tx.data.clone(),
        }
    }
}
impl From<TXAccountMeta> for solana_program::instruction::AccountMeta {
    fn from(
        TXAccountMeta {
            pubkey,
            is_signer,
            is_writable,
        }: TXAccountMeta,
    ) -> solana_program::instruction::AccountMeta {
        solana_program::instruction::AccountMeta {
            pubkey,
            is_signer,
            is_writable,
        }
    }
}
#[derive(
    AnchorSerialize, AnchorDeserialize, Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord,
)]
#[repr(u8)]
pub enum SubaccountType {
    
    Derived = 0,
    
    OwnerInvoker = 1,
}
impl Default for SubaccountType {
    fn default() -> Self {
        SubaccountType::Derived
    }
}
#[account]
#[derive(Copy, Default, Debug, PartialEq, Eq)]
pub struct SubaccountInfo {
    
    pub smart_wallet: Pubkey,
    
    pub subaccount_type: SubaccountType,
    
    pub index: u64,
}
#[account]
#[derive(Default, Debug, PartialEq)]
pub struct InstructionBuffer {
    
    
    
    pub owner_set_seqno: u32,
    
    
    pub eta: i64,
    
    pub authority: Pubkey,
    
    pub executor: Pubkey,
    
    pub smart_wallet: Pubkey,
    
    pub bundles: Vec<InstructionBundle>,
}
impl InstructionBuffer {
    
    pub fn is_finalized(&self) -> bool {
        self.authority == Pubkey::default()
    }
    
    pub fn get_bundle(&self, bundle_index: u8) -> Option<InstructionBundle> {
        let (_, right) = self.bundles.split_at(usize::from(bundle_index));
        if right.is_empty() {
            None
        } else {
            Some(right[0].clone())
        }
    }
    
    pub fn set_bundle(&mut self, bundle_index: u8, new_bundle: &InstructionBundle) -> Result<()> {
        let bundles = &mut self.bundles;
        if let Some(mut_bundle_ref) = bundles.get_mut(usize::from(bundle_index)) {
            *mut_bundle_ref = new_bundle.clone();
        } else if usize::from(bundle_index) == bundles.len() {
            bundles.push(new_bundle.clone())
        } else {
            return program_err!(BufferBundleNotFound);
        }
        Ok(())
    }
}
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug, Default, PartialEq)]
pub struct InstructionBundle {
    
    pub is_executed: bool,
    
    pub instructions: Vec<TXInstruction>,
}