Skip to main content

rialo_s_config_program/
lib.rs

1// Copyright (c) Subzero Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3// This file is either (a) original to Subzero Labs, Inc. or (b) derived from the Anza codebase and modified by Subzero Labs, Inc.
4
5#![allow(clippy::arithmetic_side_effects)]
6pub mod config_instruction;
7pub mod config_processor;
8#[deprecated(
9    since = "2.0.0",
10    note = "The config program API no longer supports date instructions."
11)]
12pub mod date_instruction;
13
14use bincode::{deserialize, serialize, serialized_size};
15use rialo_s_account::{Account, AccountSharedData};
16use rialo_s_pubkey::Pubkey;
17pub use rialo_s_sdk_ids::config::id;
18use rialo_s_short_vec as short_vec;
19use serde_derive::{Deserialize, Serialize};
20
21pub trait ConfigState: serde::Serialize + Default {
22    /// Maximum space that the serialized representation will require
23    fn max_space() -> u64;
24}
25
26/// A collection of keys to be stored in Config account data.
27#[derive(Debug, Default, Deserialize, Serialize)]
28pub struct ConfigKeys {
29    // Each key tuple comprises a unique `Pubkey` identifier,
30    // and `bool` whether that key is a signer of the data
31    #[serde(with = "short_vec")]
32    pub keys: Vec<(Pubkey, bool)>,
33}
34
35impl ConfigKeys {
36    pub fn serialized_size(keys: Vec<(Pubkey, bool)>) -> u64 {
37        serialized_size(&ConfigKeys { keys }).unwrap()
38    }
39}
40
41pub fn get_config_data(bytes: &[u8]) -> Result<&[u8], bincode::Error> {
42    deserialize::<ConfigKeys>(bytes)
43        .and_then(|keys| serialized_size(&keys))
44        .map(|offset| &bytes[offset as usize..])
45}
46
47// utility for pre-made Accounts
48pub fn create_config_account<T: ConfigState>(
49    keys: Vec<(Pubkey, bool)>,
50    config_data: &T,
51    kelvins: u64,
52) -> AccountSharedData {
53    let mut data = serialize(&ConfigKeys { keys }).unwrap();
54    data.extend_from_slice(&serialize(config_data).unwrap());
55    AccountSharedData::from(Account {
56        kelvins,
57        data,
58        owner: id(),
59        ..Account::default()
60    })
61}