ddk_manager/contract/
contract_input.rs

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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//! #ContractInput

use crate::error::Error;

use super::ContractDescriptor;
use secp256k1_zkp::XOnlyPublicKey;
#[cfg(feature = "use-serde")]
use serde::{Deserialize, Serialize};

/// Oracle information required for the initial creation of a contract.
#[derive(Debug, Clone)]
#[cfg_attr(
    feature = "use-serde",
    derive(Serialize, Deserialize),
    serde(rename_all = "camelCase")
)]
pub struct OracleInput {
    /// The set of public keys for each of the used oracles.
    pub public_keys: Vec<XOnlyPublicKey>,
    /// The id of the event being used for the contract. Note that at the moment
    /// a single event id is used, while multiple ids would be preferable.
    pub event_id: String,
    /// The number of oracles that need to provide attestations satisfying the
    /// contract conditions to be able to close the contract.
    pub threshold: u16,
}

impl OracleInput {
    /// Checks whether the data within the struct is consistent.
    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(),
            ));
        }

        if self.threshold == 0 {
            return Err(Error::InvalidParameters(
                "Threshold cannot be zero.".to_string(),
            ));
        }

        Ok(())
    }
}

/// Represents the contract specifications.
#[derive(Debug, Clone)]
#[cfg_attr(
    feature = "use-serde",
    derive(Serialize, Deserialize),
    serde(rename_all = "camelCase")
)]
pub struct ContractInputInfo {
    /// The contract conditions.
    pub contract_descriptor: ContractDescriptor,
    /// The oracle information.
    pub oracles: OracleInput,
}

#[derive(Debug, Clone)]
#[cfg_attr(
    feature = "use-serde",
    derive(Serialize, Deserialize),
    serde(rename_all = "camelCase")
)]
/// Contains all the information necessary for the initialization of a DLC.
pub struct ContractInput {
    /// The collateral for the offering party.
    pub offer_collateral: u64,
    /// The collateral for the accepting party.
    pub accept_collateral: u64,
    /// The fee rate used to construct the transactions.
    pub fee_rate: u64,
    /// The set of contract that make up the DLC (a single DLC can be based
    /// on multiple contracts).
    pub contract_infos: Vec<ContractInputInfo>,
}

impl ContractInput {
    /// Validate the contract input parameters
    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()?;
        }

        ddk_dlc::util::validate_fee_rate(self.fee_rate)
            .map_err(|_| Error::InvalidParameters("Fee rate too high.".to_string()))
    }
}

#[cfg(test)]
mod tests {
    use ddk_dlc::{EnumerationPayout, Payout};
    use secp256k1_zkp::{Keypair, SecretKey, 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,
                            &SecretKey::from_slice(&secp256k1_zkp::constants::ONE).unwrap(),
                        ))
                        .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.");
    }

    #[test]
    fn invalid_oracle_info_threshold_zero() {
        let mut input = get_base_input();
        input.contract_infos[0].oracles.threshold = 0;
        input
            .validate()
            .expect_err("the contract input to be invalid.");
    }
}