-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallowed_mint.rs
More file actions
223 lines (187 loc) · 6.49 KB
/
allowed_mint.rs
File metadata and controls
223 lines (187 loc) · 6.49 KB
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
use alloc::vec;
use alloc::vec::Vec;
use codama::CodamaAccount;
use pinocchio::{account::AccountView, cpi::Seed, error::ProgramError, Address};
use crate::assert_no_padding;
use crate::traits::{
AccountDeserialize, AccountSerialize, AccountSize, Discriminator, EscrowAccountDiscriminators, PdaSeeds, Versioned,
};
/// AllowedMint account state
///
/// Represents a mint that is allowed for deposits into a specific escrow.
/// The existence of this PDA indicates the mint is allowed.
/// The PDA seeds themselves validate the escrow and mint relationship.
///
/// # PDA Seeds
/// `[b"allowed_mint", escrow.as_ref(), mint.as_ref()]`
#[derive(Clone, Debug, PartialEq, CodamaAccount)]
#[codama(field("discriminator", number(u8), default_value = 4))]
#[codama(discriminator(field = "discriminator"))]
#[codama(seed(type = string(utf8), value = "allowed_mint"))]
#[codama(seed(name = "escrow", type = public_key))]
#[codama(seed(name = "mint", type = public_key))]
#[repr(C)]
pub struct AllowedMint {
pub bump: u8,
}
assert_no_padding!(AllowedMint, 1);
impl Discriminator for AllowedMint {
const DISCRIMINATOR: u8 = EscrowAccountDiscriminators::AllowedMintDiscriminator as u8;
}
impl Versioned for AllowedMint {
const VERSION: u8 = 1;
}
impl AccountSize for AllowedMint {
const DATA_LEN: usize = 1; // bump only
}
impl AccountDeserialize for AllowedMint {}
impl AccountSerialize for AllowedMint {
#[inline(always)]
fn to_bytes_inner(&self) -> Vec<u8> {
vec![self.bump]
}
}
impl AllowedMint {
#[inline(always)]
pub fn new(bump: u8) -> Self {
Self { bump }
}
#[inline(always)]
pub fn from_account<'a>(
data: &'a [u8],
account: &AccountView,
program_id: &Address,
escrow: &Address,
mint: &Address,
) -> Result<&'a Self, ProgramError> {
let state = Self::from_bytes(data)?;
let derived = Address::derive_address(
&[AllowedMintPda::PREFIX, escrow.as_ref(), mint.as_ref()],
Some(state.bump),
program_id,
);
if account.address() != &derived {
return Err(ProgramError::InvalidSeeds);
}
Ok(state)
}
}
/// PDA context for AllowedMint - holds escrow and mint addresses for seed derivation
///
/// Since the AllowedMint account only stores the bump, we use this helper
/// to derive and validate PDAs using external escrow and mint addresses.
///
/// Implements `PdaSeeds` trait for consistent PDA handling across codebase.
pub struct AllowedMintPda<'a> {
pub escrow: &'a Address,
pub mint: &'a Address,
}
impl<'a> AllowedMintPda<'a> {
#[inline(always)]
pub fn new(escrow: &'a Address, mint: &'a Address) -> Self {
Self { escrow, mint }
}
}
impl PdaSeeds for AllowedMintPda<'_> {
const PREFIX: &'static [u8] = b"allowed_mint";
#[inline(always)]
fn seeds(&self) -> Vec<&[u8]> {
vec![Self::PREFIX, self.escrow.as_ref(), self.mint.as_ref()]
}
#[inline(always)]
fn seeds_with_bump<'a>(&'a self, bump: &'a [u8; 1]) -> Vec<Seed<'a>> {
vec![
Seed::from(Self::PREFIX),
Seed::from(self.escrow.as_ref()),
Seed::from(self.mint.as_ref()),
Seed::from(bump.as_slice()),
]
}
}
#[cfg(test)]
mod tests {
use super::*;
fn create_test_allowed_mint() -> AllowedMint {
AllowedMint::new(255)
}
fn create_test_seeds() -> (Address, Address) {
let escrow = Address::new_from_array([1u8; 32]);
let mint = Address::new_from_array([2u8; 32]);
(escrow, mint)
}
#[test]
fn test_allowed_mint_new() {
let allowed_mint = AllowedMint::new(200);
assert_eq!(allowed_mint.bump, 200);
}
#[test]
fn test_allowed_mint_to_bytes_inner() {
let allowed_mint = create_test_allowed_mint();
let bytes = allowed_mint.to_bytes_inner();
assert_eq!(bytes.len(), AllowedMint::DATA_LEN);
assert_eq!(bytes[0], 255); // bump
}
#[test]
fn test_allowed_mint_to_bytes() {
let allowed_mint = create_test_allowed_mint();
let bytes = allowed_mint.to_bytes();
assert_eq!(bytes.len(), AllowedMint::LEN);
assert_eq!(bytes[0], AllowedMint::DISCRIMINATOR);
assert_eq!(bytes[1], AllowedMint::VERSION); // version auto-prepended
assert_eq!(bytes[2], 255); // bump
}
#[test]
fn test_allowed_mint_from_bytes() {
let allowed_mint = create_test_allowed_mint();
let bytes = allowed_mint.to_bytes();
let deserialized = AllowedMint::from_bytes(&bytes).unwrap();
assert_eq!(deserialized.bump, allowed_mint.bump);
}
#[test]
fn test_allowed_mint_from_bytes_too_short() {
let data = [0u8; 2]; // Only discriminator + version, no bump
let result = AllowedMint::from_bytes(&data);
assert_eq!(result, Err(ProgramError::InvalidInstructionData));
}
#[test]
fn test_allowed_mint_from_bytes_wrong_discriminator() {
let mut bytes = [0u8; 3];
bytes[0] = 99; // wrong discriminator
let result = AllowedMint::from_bytes(&bytes);
assert_eq!(result, Err(ProgramError::InvalidAccountData));
}
#[test]
fn test_allowed_mint_pda_seeds() {
let (escrow, mint) = create_test_seeds();
let pda = AllowedMintPda::new(&escrow, &mint);
let seeds = pda.seeds();
assert_eq!(seeds.len(), 3);
assert_eq!(seeds[0], AllowedMintPda::PREFIX);
assert_eq!(seeds[1], escrow.as_ref());
assert_eq!(seeds[2], mint.as_ref());
}
#[test]
fn test_allowed_mint_pda_seeds_with_bump() {
let (escrow, mint) = create_test_seeds();
let pda = AllowedMintPda::new(&escrow, &mint);
let bump = [255u8];
let seeds = pda.seeds_with_bump(&bump);
assert_eq!(seeds.len(), 4);
}
#[test]
fn test_allowed_mint_write_to_slice() {
let allowed_mint = create_test_allowed_mint();
let mut dest = [0u8; 100];
assert!(allowed_mint.write_to_slice(&mut dest).is_ok());
assert_eq!(dest[0], AllowedMint::DISCRIMINATOR);
assert_eq!(dest[1], AllowedMint::VERSION); // version
assert_eq!(dest[2], allowed_mint.bump);
}
#[test]
fn test_allowed_mint_write_to_slice_too_small() {
let allowed_mint = create_test_allowed_mint();
let mut dest = [0u8; 2]; // Only room for discriminator + version
let result = allowed_mint.write_to_slice(&mut dest);
assert_eq!(result, Err(ProgramError::AccountDataTooSmall));
}
}