|
| 1 | +use steel::*; |
| 2 | + |
| 3 | +use crate::consts::ADDRESS_INFO_SEED; |
| 4 | + |
| 5 | +#[repr(C)] |
| 6 | +#[derive(Clone, Copy, Debug, Pod, Zeroable, PartialEq)] |
| 7 | +pub struct AddressInfoData { |
| 8 | + pub name: [u8; 64], |
| 9 | + pub house_number: [u8; 8], |
| 10 | + pub street: [u8; 64], |
| 11 | + pub city: [u8; 64], |
| 12 | +} |
| 13 | + |
| 14 | +fn string_to_bytes(s: &str) -> [u8; 64] { |
| 15 | + let mut bytes = [0; 64]; |
| 16 | + let s_bytes = s.as_bytes(); |
| 17 | + let len = s_bytes.len().min(64); |
| 18 | + bytes[..len].copy_from_slice(&s_bytes[..len]); |
| 19 | + bytes |
| 20 | +} |
| 21 | + |
| 22 | +impl AddressInfoData { |
| 23 | + pub fn new(name: String, house_number: u64, street: String, city: String) -> Self { |
| 24 | + Self { |
| 25 | + name: string_to_bytes(&name), |
| 26 | + house_number: house_number.to_le_bytes(), |
| 27 | + street: string_to_bytes(&street), |
| 28 | + city: string_to_bytes(&city), |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +#[repr(u8)] |
| 34 | +#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)] |
| 35 | +pub enum AddressInfoAccount { |
| 36 | + AddressInfo = 0, |
| 37 | +} |
| 38 | + |
| 39 | +#[repr(C)] |
| 40 | +#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)] |
| 41 | +pub struct AddressInfo { |
| 42 | + pub data: AddressInfoData, |
| 43 | +} |
| 44 | + |
| 45 | +account!(AddressInfoAccount, AddressInfo); |
| 46 | + |
| 47 | +/// Fetch PDA of the address info account. |
| 48 | +pub fn account_pda() -> (Pubkey, u8) { |
| 49 | + Pubkey::find_program_address(&[ADDRESS_INFO_SEED], &crate::id()) |
| 50 | +} |
0 commit comments