Skip to content

Commit ea9f60a

Browse files
authored
Merge pull request #104 from kwsantiago/kyle/fix-issue-70
2 parents ca3fe51 + 9ad2148 commit ea9f60a

5 files changed

Lines changed: 681 additions & 490 deletions

File tree

ddk-node/src/cli_opts.rs

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,34 @@ pub enum OracleCommand {
7676
#[arg(help = "The outcomes of the event. Separate by spaces.")]
7777
outcomes: Vec<String>,
7878
},
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+
},
79105
}
106+
80107
#[derive(Parser, Clone, Debug)]
81108
pub struct Accept {
82109
// The contract id string to accept.
@@ -88,3 +115,112 @@ pub struct Connect {
88115
#[arg(help = "The public key to connect to.")]
89116
pub pubkey: String,
90117
}
118+
119+
#[cfg(test)]
120+
mod 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+
}

ddk-node/src/command.rs

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use std::sync::Arc;
22

33
use crate::cli_opts::{CliCommand, OracleCommand, WalletCommand};
4-
// use crate::convert::*;
54
use crate::ddkrpc::ddk_rpc_client::DdkRpcClient;
65
use crate::ddkrpc::{
7-
AcceptOfferRequest, ConnectRequest, CreateEnumRequest, GetWalletTransactionsRequest,
8-
InfoRequest, ListContractsRequest, ListOffersRequest, ListPeersRequest, ListUtxosRequest,
9-
NewAddressRequest, OracleAnnouncementsRequest, SendOfferRequest, SendRequest, SyncRequest,
10-
WalletBalanceRequest, WalletSyncRequest,
6+
sign_request, AcceptOfferRequest, ConnectRequest, CreateEnumRequest, CreateNumericRequest,
7+
GetWalletTransactionsRequest, InfoRequest, ListContractsRequest, ListOffersRequest,
8+
ListPeersRequest, ListUtxosRequest, NewAddressRequest, OracleAnnouncementsRequest,
9+
SendOfferRequest, SendRequest, SignRequest, SyncRequest, WalletBalanceRequest,
10+
WalletSyncRequest,
1111
};
1212
use anyhow::anyhow;
1313
use bitcoin::{Amount, Transaction};
@@ -44,7 +44,6 @@ pub async fn cli_command(
4444
} else {
4545
interactive_contract_input(client).await?
4646
};
47-
4847
let contract_input = serde_json::to_vec(&contract_input)?;
4948
let offer = client
5049
.send_offer(SendOfferRequest {
@@ -136,7 +135,6 @@ pub async fn cli_command(
136135
.iter()
137136
.map(|utxo| serde_json::from_slice(utxo).unwrap())
138137
.collect::<Vec<LocalOutput>>();
139-
140138
print!("{}", serde_json::to_string_pretty(&local_outputs).unwrap())
141139
}
142140
WalletCommand::Send {
@@ -184,6 +182,51 @@ pub async fn cli_command(
184182
serde_json::to_string_pretty(&oracle_announcement).unwrap()
185183
)
186184
}
185+
OracleCommand::CreateNumeric {
186+
maturity,
187+
nb_digits,
188+
} => {
189+
let response = client
190+
.create_numeric(CreateNumericRequest {
191+
maturity,
192+
nb_digits,
193+
})
194+
.await?
195+
.into_inner();
196+
let oracle_announcement: OracleAnnouncement =
197+
serde_json::from_slice(&response.announcement)?;
198+
print!(
199+
"{}",
200+
serde_json::to_string_pretty(&oracle_announcement).unwrap()
201+
)
202+
}
203+
OracleCommand::Sign {
204+
r#enum: enum_flag,
205+
numeric,
206+
outcome,
207+
event_id,
208+
} => {
209+
if enum_flag && numeric {
210+
return Err(anyhow!("Cannot specify both --enum and --numeric"));
211+
}
212+
if !enum_flag && !numeric {
213+
return Err(anyhow!("Must specify either --enum or --numeric"));
214+
}
215+
let outcome_variant = if enum_flag {
216+
sign_request::Outcome::EnumOutcome(outcome)
217+
} else {
218+
let numeric_outcome = outcome.parse::<i64>().map_err(|_| {
219+
anyhow!("Outcome must be a valid integer for numeric events")
220+
})?;
221+
sign_request::Outcome::NumericOutcome(numeric_outcome)
222+
};
223+
let request = SignRequest {
224+
event_id,
225+
outcome: Some(outcome_variant),
226+
};
227+
let response = client.sign_announcement(request).await?.into_inner();
228+
print!("{}", serde_json::to_string_pretty(&response.signature)?);
229+
}
187230
},
188231
CliCommand::Peers => {
189232
let peers_response = client
@@ -208,7 +251,6 @@ pub async fn cli_command(
208251
println!("Synced.")
209252
}
210253
}
211-
212254
Ok(())
213255
}
214256

0 commit comments

Comments
 (0)