miraland_config_program/
lib.rs

1#![allow(clippy::arithmetic_side_effects)]
2pub mod config_instruction;
3pub mod config_processor;
4pub mod date_instruction;
5
6pub use miraland_sdk::config::program::id;
7#[allow(deprecated)]
8use miraland_sdk::stake::config::Config as StakeConfig;
9use {
10    bincode::{deserialize, serialize, serialized_size},
11    serde_derive::{Deserialize, Serialize},
12    miraland_sdk::{
13        account::{Account, AccountSharedData},
14        pubkey::Pubkey,
15        short_vec,
16    },
17};
18
19pub trait ConfigState: serde::Serialize + Default {
20    /// Maximum space that the serialized representation will require
21    fn max_space() -> u64;
22}
23
24// TODO move ConfigState into `miraland_program` to implement trait locally
25#[allow(deprecated)]
26impl ConfigState for StakeConfig {
27    fn max_space() -> u64 {
28        serialized_size(&StakeConfig::default()).unwrap()
29    }
30}
31
32/// A collection of keys to be stored in Config account data.
33#[derive(Debug, Default, Deserialize, Serialize)]
34pub struct ConfigKeys {
35    // Each key tuple comprises a unique `Pubkey` identifier,
36    // and `bool` whether that key is a signer of the data
37    #[serde(with = "short_vec")]
38    pub keys: Vec<(Pubkey, bool)>,
39}
40
41impl ConfigKeys {
42    pub fn serialized_size(keys: Vec<(Pubkey, bool)>) -> u64 {
43        serialized_size(&ConfigKeys { keys }).unwrap()
44    }
45}
46
47pub fn get_config_data(bytes: &[u8]) -> Result<&[u8], bincode::Error> {
48    deserialize::<ConfigKeys>(bytes)
49        .and_then(|keys| serialized_size(&keys))
50        .map(|offset| &bytes[offset as usize..])
51}
52
53// utility for pre-made Accounts
54pub fn create_config_account<T: ConfigState>(
55    keys: Vec<(Pubkey, bool)>,
56    config_data: &T,
57    lamports: u64,
58) -> AccountSharedData {
59    let mut data = serialize(&ConfigKeys { keys }).unwrap();
60    data.extend_from_slice(&serialize(config_data).unwrap());
61    AccountSharedData::from(Account {
62        lamports,
63        data,
64        owner: id(),
65        ..Account::default()
66    })
67}