1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use crate::error::Error;
use super::ContractDescriptor;
use secp256k1_zkp::XOnlyPublicKey;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Debug)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(rename_all = "camelCase")
)]
pub struct OracleInput {
pub public_keys: Vec<XOnlyPublicKey>,
pub event_id: String,
pub threshold: u16,
}
impl OracleInput {
pub fn validate(&self) -> Result<(), Error> {
if self.public_keys.is_empty() {
return Err(Error::InvalidParameters(
"OracleInput must have at least one public key.".to_string(),
));
}
if self.threshold > self.public_keys.len() as u16 {
return Err(Error::InvalidParameters(
"Threshold cannot be larger than number of oracles.".to_string(),
));
}
Ok(())
}
}
#[derive(Debug)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(rename_all = "camelCase")
)]
pub struct ContractInputInfo {
pub contract_descriptor: ContractDescriptor,
pub oracles: OracleInput,
}
#[derive(Debug)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(rename_all = "camelCase")
)]
pub struct ContractInput {
pub offer_collateral: u64,
pub accept_collateral: u64,
pub fee_rate: u64,
pub contract_infos: Vec<ContractInputInfo>,
}
impl ContractInput {
pub fn validate(&self) -> Result<(), Error> {
if self.contract_infos.is_empty() {
return Err(Error::InvalidParameters(
"Need at least one contract info".to_string(),
));
}
for contract_info in &self.contract_infos {
contract_info.oracles.validate()?;
}
dlc::util::validate_fee_rate(self.fee_rate)
.map_err(|_| Error::InvalidParameters("Fee rate too high.".to_string()))
}
}
#[cfg(test)]
mod tests {
use dlc::{EnumerationPayout, Payout};
use secp256k1_zkp::{KeyPair, SECP256K1};
use crate::contract::enum_descriptor::EnumDescriptor;
use super::*;
fn get_base_input() -> ContractInput {
ContractInput {
offer_collateral: 1000000,
accept_collateral: 2000000,
fee_rate: 1234,
contract_infos: vec![ContractInputInfo {
contract_descriptor: ContractDescriptor::Enum(EnumDescriptor {
outcome_payouts: vec![
EnumerationPayout {
outcome: "A".to_string(),
payout: Payout {
offer: 3000000,
accept: 0,
},
},
EnumerationPayout {
outcome: "B".to_string(),
payout: Payout {
offer: 0,
accept: 3000000,
},
},
],
}),
oracles: OracleInput {
public_keys: vec![
XOnlyPublicKey::from_keypair(&KeyPair::from_secret_key(
SECP256K1,
&secp256k1_zkp::ONE_KEY,
))
.0,
],
event_id: "1234".to_string(),
threshold: 1,
},
}],
}
}
#[test]
fn valid_contract_input_is_valid() {
let input = get_base_input();
input.validate().expect("the contract input to be valid.");
}
#[test]
fn no_contract_info_contract_input_is_not_valid() {
let mut input = get_base_input();
input.contract_infos.clear();
input
.validate()
.expect_err("the contract input to be invalid.");
}
#[test]
fn invalid_fee_rate_contract_input_is_not_valid() {
let mut input = get_base_input();
input.fee_rate = 251 * 25;
input
.validate()
.expect_err("the contract input to be invalid.");
}
#[test]
fn no_public_keys_oracle_input_contract_input_is_not_valid() {
let mut input = get_base_input();
input.contract_infos[0].oracles.public_keys.clear();
input
.validate()
.expect_err("the contract input to be invalid.");
}
#[test]
fn invalid_oracle_info_threshold_oracle_input_contract_input_is_not_valid() {
let mut input = get_base_input();
input.contract_infos[0].oracles.threshold = 2;
input
.validate()
.expect_err("the contract input to be invalid.");
}
}