snops_common/
action_models.rs

1use std::str::FromStr;
2
3use indexmap::{IndexMap, IndexSet};
4use serde::{Deserialize, Serialize};
5
6use crate::{
7    key_source::KeySource,
8    node_targets::{NodeTarget, NodeTargets},
9    state::{CannonId, DocHeightRequest, InternedId},
10};
11
12#[derive(Deserialize, Serialize, Clone)]
13pub struct WithTargets<T = ()>
14where
15    T: Serialize,
16{
17    pub nodes: NodeTargets,
18    #[serde(flatten)]
19    pub data: T,
20}
21
22impl From<Vec<NodeTarget>> for WithTargets<()> {
23    fn from(nodes: Vec<NodeTarget>) -> Self {
24        Self {
25            nodes: NodeTargets::from(nodes),
26            data: (),
27        }
28    }
29}
30impl From<NodeTargets> for WithTargets<()> {
31    fn from(nodes: NodeTargets) -> Self {
32        Self { nodes, data: () }
33    }
34}
35
36fn committee_0_key() -> KeySource {
37    KeySource::Committee(Some(0))
38}
39
40fn credits_aleo() -> String {
41    "credits.aleo".to_owned()
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize)]
45#[serde(rename_all = "snake_case")]
46pub struct ExecuteAction {
47    /// The private key to use for the transaction. If not provided, the
48    /// transaction will be signed with the committee member 0's key.
49    #[serde(default = "committee_0_key")]
50    pub private_key: KeySource,
51    /// A private key to use for the fee. If not provided, the fee will be paid
52    /// from the `private_key`
53    pub fee_private_key: Option<KeySource>,
54    /// The program to execute. Defaults to `credits.aleo`
55    #[serde(default = "credits_aleo")]
56    pub program: String,
57    /// The function to call
58    pub function: String,
59    /// The cannon id of who to execute the transaction
60    #[serde(default)]
61    pub cannon: CannonId,
62    /// The inputs to the function
63    pub inputs: Vec<AleoValue>,
64    /// The optional priority fee
65    #[serde(default)]
66    pub priority_fee: Option<u64>,
67    /// The optional fee record for a private fee
68    #[serde(default)]
69    pub fee_record: Option<String>,
70}
71
72#[derive(Debug, Clone, Serialize, Deserialize)]
73#[serde(rename_all = "snake_case")]
74pub struct DeployAction {
75    /// The private key to use for the transaction. If not provided, the
76    /// transaction will be signed with the committee member 0's key.
77    #[serde(default = "committee_0_key")]
78    pub private_key: KeySource,
79    /// A private key to use for the fee. If not provided, the fee will be paid
80    /// from the `private_key`
81    pub fee_private_key: Option<KeySource>,
82    /// The program to deploy
83    pub program: String,
84    /// The cannon id of who to execute the transaction
85    #[serde(default)]
86    pub cannon: CannonId,
87    /// The optional priority fee
88    #[serde(default)]
89    pub priority_fee: Option<u64>,
90    /// The optional fee record for a private fee
91    #[serde(default)]
92    pub fee_record: Option<String>,
93}
94
95#[derive(Debug, Clone, Serialize, Deserialize)]
96#[serde(untagged)]
97pub enum AleoValue {
98    // Public keys
99    Key(KeySource),
100    // Other values (u8, fields, etc.)
101    Other(String),
102}
103
104impl FromStr for AleoValue {
105    type Err = String;
106
107    fn from_str(s: &str) -> Result<Self, Self::Err> {
108        Ok(KeySource::from_str(s)
109            .map(AleoValue::Key)
110            .unwrap_or_else(|_| AleoValue::Other(s.to_owned())))
111    }
112}
113
114#[derive(Serialize, Deserialize, Debug, Clone)]
115pub struct Reconfig {
116    #[serde(default, skip_serializing_if = "Option::is_none")]
117    pub online: Option<bool>,
118    #[serde(default, skip_serializing_if = "Option::is_none")]
119    pub height: Option<DocHeightRequest>,
120    #[serde(default, skip_serializing_if = "Option::is_none")]
121    pub peers: Option<NodeTargets>,
122    #[serde(default, skip_serializing_if = "Option::is_none")]
123    pub validators: Option<NodeTargets>,
124    #[serde(default, skip_serializing_if = "Option::is_none")]
125    pub binary: Option<InternedId>,
126    #[serde(default, skip_serializing_if = "Option::is_none")]
127    pub private_key: Option<KeySource>,
128    #[serde(default, skip_serializing_if = "Option::is_none")]
129    pub set_env: Option<IndexMap<String, String>>,
130    #[serde(default, skip_serializing_if = "Option::is_none")]
131    pub del_env: Option<IndexSet<String>>,
132}