Skip to main content

ddk_node/
cli_opts.rs

1use clap::{Parser, Subcommand};
2
3#[derive(Debug, Clone, Subcommand)]
4pub enum CliCommand {
5    /// Gets information about the DDK instance
6    Info,
7    /// Pass a contract input to send an offer
8    OfferContract(Offer),
9    /// Retrieve the offers that ddk-node has received.
10    Offers,
11    /// Accept a DLC offer with the contract id string.
12    AcceptOffer(Accept),
13    /// List contracts.
14    Contracts,
15    #[command(about = "Get the wallet balance.")]
16    Balance,
17    /// Wallet commands
18    #[clap(subcommand)]
19    Wallet(WalletCommand),
20    /// Interface with the oracle
21    #[clap(subcommand)]
22    Oracle(OracleCommand),
23    /// Get the peers connected to the node.
24    Peers,
25    /// Connect to another DDK node.
26    Connect {
27        #[arg(help = "The counter party to connect to. <PUBKEY>@<HOST>")]
28        connect_string: String,
29    },
30    /// Sync the wallet and contracts.
31    #[command(about = "Sync the wallet and contracts.")]
32    Sync,
33}
34
35#[derive(Parser, Clone, Debug)]
36pub struct Offer {
37    #[arg(help = "Generate a contract automatically with peer.")]
38    #[arg(short = 'g', long = "generate", default_value = "false")]
39    pub generate: bool,
40    #[arg(help = "The contract counterparty to send to.")]
41    pub counter_party: String,
42}
43
44#[derive(Clone, Debug, Subcommand)]
45pub enum WalletCommand {
46    #[command(about = "Generate a new, unused address from the wallet.")]
47    NewAddress,
48    #[command(about = "Get the wallet transactions.")]
49    Transactions,
50    #[command(about = "Get the wallet utxos.")]
51    Utxos,
52    #[command(about = "Send a Bitcoin amount to an address")]
53    Send {
54        /// Address to send to.
55        address: String,
56        /// Amount in sats to send to.
57        amount: u64,
58        /// Fee rate in sats/vbyte
59        fee_rate: u64,
60    },
61    #[command(about = "Sync the on-chain wallet.")]
62    Sync,
63}
64
65#[derive(Clone, Debug, Subcommand)]
66pub enum OracleCommand {
67    #[command(about = "Get all known oracle announcements.")]
68    Announcements {
69        #[arg(help = "The announcement id to get.")]
70        event_id: String,
71    },
72    #[command(about = "Create an enum oracle event.")]
73    CreateEnum {
74        #[arg(help = "The maturity of the event.")]
75        maturity: u32,
76        #[arg(help = "The outcomes of the event. Separate by spaces.")]
77        outcomes: Vec<String>,
78    },
79    #[command(about = "Create a numeric oracle event.")]
80    CreateNumeric {
81        #[arg(help = "The maturity of the event.")]
82        maturity: u32,
83        #[arg(help = "Number of digits for the numeric event.")]
84        nb_digits: u32,
85    },
86    #[command(about = "Sign an oracle announcement.")]
87    Sign {
88        #[arg(
89            long,
90            help = "Specify if the event is enum.",
91            conflicts_with = "numeric"
92        )]
93        r#enum: bool,
94        #[arg(
95            long,
96            help = "Specify if the event is numeric.",
97            conflicts_with = "enum"
98        )]
99        numeric: bool,
100        #[arg(long, help = "The outcome to sign.")]
101        outcome: String,
102        #[arg(long, help = "The event id to sign.")]
103        event_id: String,
104    },
105}
106
107#[derive(Parser, Clone, Debug)]
108pub struct Accept {
109    // The contract id string to accept.
110    pub contract_id: String,
111}
112
113#[derive(Parser, Clone, Debug)]
114pub struct Connect {
115    #[arg(help = "The public key to connect to.")]
116    pub pubkey: String,
117}
118
119#[cfg(test)]
120mod tests {
121    use super::*;
122
123    #[test]
124    fn test_oracle_create_enum_structure() {
125        // Test creating an enum oracle command
126        let cmd = OracleCommand::CreateEnum {
127            maturity: 1234567890,
128            outcomes: vec!["YES".to_string(), "NO".to_string()],
129        };
130
131        match cmd {
132            OracleCommand::CreateEnum { maturity, outcomes } => {
133                assert_eq!(maturity, 1234567890);
134                assert_eq!(outcomes, vec!["YES", "NO"]);
135            }
136            _ => panic!("Wrong variant"),
137        }
138    }
139
140    #[test]
141    fn test_oracle_create_numeric_structure() {
142        // Test creating a numeric oracle command
143        let cmd = OracleCommand::CreateNumeric {
144            maturity: 1234567890,
145            nb_digits: 5,
146        };
147
148        match cmd {
149            OracleCommand::CreateNumeric {
150                maturity,
151                nb_digits,
152            } => {
153                assert_eq!(maturity, 1234567890);
154                assert_eq!(nb_digits, 5);
155            }
156            _ => panic!("Wrong variant"),
157        }
158    }
159
160    #[test]
161    fn test_oracle_sign_enum_structure() {
162        // Test creating a sign command with enum flag
163        let cmd = OracleCommand::Sign {
164            r#enum: true,
165            numeric: false,
166            outcome: "YES".to_string(),
167            event_id: "test123".to_string(),
168        };
169
170        match cmd {
171            OracleCommand::Sign {
172                r#enum,
173                numeric,
174                outcome,
175                event_id,
176            } => {
177                assert!(r#enum);
178                assert!(!numeric);
179                assert_eq!(outcome, "YES");
180                assert_eq!(event_id, "test123");
181            }
182            _ => panic!("Wrong variant"),
183        }
184    }
185
186    #[test]
187    fn test_oracle_sign_numeric_structure() {
188        // Test creating a sign command with numeric flag
189        let cmd = OracleCommand::Sign {
190            r#enum: false,
191            numeric: true,
192            outcome: "42".to_string(),
193            event_id: "test123".to_string(),
194        };
195
196        match cmd {
197            OracleCommand::Sign {
198                r#enum,
199                numeric,
200                outcome,
201                event_id,
202            } => {
203                assert!(!r#enum);
204                assert!(numeric);
205                assert_eq!(outcome, "42");
206                assert_eq!(event_id, "test123");
207            }
208            _ => panic!("Wrong variant"),
209        }
210    }
211
212    #[test]
213    fn test_oracle_announcements_structure() {
214        // Test creating an announcements command
215        let cmd = OracleCommand::Announcements {
216            event_id: "event123".to_string(),
217        };
218
219        match cmd {
220            OracleCommand::Announcements { event_id } => {
221                assert_eq!(event_id, "event123");
222            }
223            _ => panic!("Wrong variant"),
224        }
225    }
226}