use std::str::FromStr;
use indexmap::{IndexMap, IndexSet};
use serde::{Deserialize, Serialize};
use crate::{
key_source::KeySource,
node_targets::{NodeTarget, NodeTargets},
state::{CannonId, DocHeightRequest, InternedId},
};
#[derive(Deserialize, Serialize, Clone)]
pub struct WithTargets<T = ()>
where
T: Serialize,
{
pub nodes: NodeTargets,
#[serde(flatten)]
pub data: T,
}
impl From<Vec<NodeTarget>> for WithTargets<()> {
fn from(nodes: Vec<NodeTarget>) -> Self {
Self {
nodes: NodeTargets::from(nodes),
data: (),
}
}
}
impl From<NodeTargets> for WithTargets<()> {
fn from(nodes: NodeTargets) -> Self {
Self { nodes, data: () }
}
}
fn committee_0_key() -> KeySource {
KeySource::Committee(Some(0))
}
fn credits_aleo() -> String {
"credits.aleo".to_owned()
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct ExecuteAction {
#[serde(default = "committee_0_key")]
pub private_key: KeySource,
pub fee_private_key: Option<KeySource>,
#[serde(default = "credits_aleo")]
pub program: String,
pub function: String,
#[serde(default)]
pub cannon: CannonId,
pub inputs: Vec<AleoValue>,
#[serde(default)]
pub priority_fee: Option<u64>,
#[serde(default)]
pub fee_record: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct DeployAction {
#[serde(default = "committee_0_key")]
pub private_key: KeySource,
pub fee_private_key: Option<KeySource>,
pub program: String,
#[serde(default)]
pub cannon: CannonId,
#[serde(default)]
pub priority_fee: Option<u64>,
#[serde(default)]
pub fee_record: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum AleoValue {
Key(KeySource),
Other(String),
}
impl FromStr for AleoValue {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(KeySource::from_str(s)
.map(AleoValue::Key)
.unwrap_or_else(|_| AleoValue::Other(s.to_owned())))
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Reconfig {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub online: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub height: Option<DocHeightRequest>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub peers: Option<NodeTargets>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub validators: Option<NodeTargets>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub binary: Option<InternedId>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub private_key: Option<KeySource>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub set_env: Option<IndexMap<String, String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub del_env: Option<IndexSet<String>>,
}