ddk_manager/contract/
offered_contract.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//! #OfferedContract

use crate::conversion_utils::{
    get_contract_info_and_announcements, get_tx_input_infos, BITCOIN_CHAINHASH, PROTOCOL_VERSION,
};
use crate::utils::get_new_serial_id;

use super::contract_info::ContractInfo;
use super::contract_input::ContractInput;
use super::ContractDescriptor;
use crate::{ContractId, KeysId};
use ddk_dlc::PartyParams;
use ddk_messages::oracle_msgs::OracleAnnouncement;
use ddk_messages::{FundingInput, OfferDlc};
use secp256k1_zkp::PublicKey;

/// Contains information about a contract that was offered.
#[derive(Clone, Debug)]
#[cfg_attr(
    feature = "use-serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "camelCase")
)]
pub struct OfferedContract {
    /// The temporary id of the contract.
    pub id: [u8; 32],
    /// Indicated whether the contract was proposed or received.
    pub is_offer_party: bool,
    /// The set of contract information that are used to generate CET and
    /// adaptor signatures.
    pub contract_info: Vec<ContractInfo>,
    /// The public key of the counter-party's node.
    pub counter_party: PublicKey,
    /// The parameters of the offering party.
    pub offer_params: PartyParams,
    /// The sum of both parties collateral.
    pub total_collateral: u64,
    /// Information about the offering party's funding inputs.
    pub funding_inputs: Vec<FundingInput>,
    /// The serial id of the fund output used for output ordering.
    pub fund_output_serial_id: u64,
    /// The fee rate to be used to construct the DLC transactions.
    pub fee_rate_per_vb: u64,
    /// The time at which the contract is expected to be closeable.
    pub cet_locktime: u32,
    /// The time at which the contract becomes refundable.
    pub refund_locktime: u32,
    /// Keys Id for generating the signers
    pub(crate) keys_id: KeysId,
}

impl OfferedContract {
    /// Validate that the contract info covers all the possible outcomes that
    /// can be attested by the oracle(s).
    pub fn validate(&self) -> Result<(), crate::error::Error> {
        ddk_dlc::util::validate_fee_rate(self.fee_rate_per_vb).map_err(|_| {
            crate::error::Error::InvalidParameters("Fee rate is too high".to_string())
        })?;

        for info in &self.contract_info {
            info.validate()?;
            let payouts = match &info.contract_descriptor {
                ContractDescriptor::Enum(e) => e.get_payouts(),
                ContractDescriptor::Numerical(e) => e.get_payouts(self.total_collateral)?,
            };
            let valid = payouts
                .iter()
                .all(|p| p.accept + p.offer == self.total_collateral);
            if !valid {
                return Err(crate::error::Error::InvalidParameters(
                    "Sum of payout doesn't equal total collateral".to_string(),
                ));
            }
        }

        Ok(())
    }

    /// Creates a new [`OfferedContract`] from the given parameters.
    pub fn new(
        id: ContractId,
        contract: &ContractInput,
        oracle_announcements: Vec<Vec<OracleAnnouncement>>,
        offer_params: &PartyParams,
        funding_inputs: &[FundingInput],
        counter_party: &PublicKey,
        refund_delay: u32,
        cet_locktime: u32,
        keys_id: KeysId,
    ) -> Self {
        let total_collateral = contract.offer_collateral + contract.accept_collateral;

        assert_eq!(contract.contract_infos.len(), oracle_announcements.len());

        let latest_maturity = crate::utils::get_latest_maturity_date(&oracle_announcements)
            .expect("to be able to retrieve latest maturity date");

        let fund_output_serial_id = get_new_serial_id();
        let contract_info = contract
            .contract_infos
            .iter()
            .zip(oracle_announcements)
            .map(|(x, y)| ContractInfo {
                contract_descriptor: x.contract_descriptor.clone(),
                oracle_announcements: y,
                threshold: x.oracles.threshold as usize,
            })
            .collect::<Vec<ContractInfo>>();
        OfferedContract {
            id,
            is_offer_party: true,
            contract_info,
            offer_params: offer_params.clone(),
            total_collateral,
            funding_inputs: funding_inputs.to_vec(),
            fund_output_serial_id,
            fee_rate_per_vb: contract.fee_rate,
            cet_locktime,
            refund_locktime: latest_maturity + refund_delay,
            counter_party: *counter_party,
            keys_id,
        }
    }

    /// Convert an [`OfferDlc`] message to an [`OfferedContract`].
    pub fn try_from_offer_dlc(
        offer_dlc: &OfferDlc,
        counter_party: PublicKey,
        keys_id: KeysId,
    ) -> Result<OfferedContract, crate::conversion_utils::Error> {
        let contract_info = get_contract_info_and_announcements(&offer_dlc.contract_info)?;

        let (inputs, input_amount) = get_tx_input_infos(&offer_dlc.funding_inputs)?;

        Ok(OfferedContract {
            id: offer_dlc.temporary_contract_id,
            is_offer_party: false,
            contract_info,
            offer_params: PartyParams {
                fund_pubkey: offer_dlc.funding_pubkey,
                change_script_pubkey: offer_dlc.change_spk.clone(),
                change_serial_id: offer_dlc.change_serial_id,
                payout_script_pubkey: offer_dlc.payout_spk.clone(),
                payout_serial_id: offer_dlc.payout_serial_id,
                collateral: offer_dlc.offer_collateral,
                inputs,
                input_amount,
            },
            cet_locktime: offer_dlc.cet_locktime,
            refund_locktime: offer_dlc.refund_locktime,
            fee_rate_per_vb: offer_dlc.fee_rate_per_vb,
            fund_output_serial_id: offer_dlc.fund_output_serial_id,
            funding_inputs: offer_dlc.funding_inputs.clone(),
            total_collateral: offer_dlc.contract_info.get_total_collateral(),
            counter_party,
            keys_id,
        })
    }
}

impl From<&OfferedContract> for OfferDlc {
    fn from(offered_contract: &OfferedContract) -> OfferDlc {
        OfferDlc {
            protocol_version: PROTOCOL_VERSION,
            temporary_contract_id: offered_contract.id,
            contract_flags: 0,
            chain_hash: BITCOIN_CHAINHASH,
            contract_info: offered_contract.into(),
            funding_pubkey: offered_contract.offer_params.fund_pubkey,
            payout_spk: offered_contract.offer_params.payout_script_pubkey.clone(),
            payout_serial_id: offered_contract.offer_params.payout_serial_id,
            offer_collateral: offered_contract.offer_params.collateral,
            funding_inputs: offered_contract.funding_inputs.clone(),
            change_spk: offered_contract.offer_params.change_script_pubkey.clone(),
            change_serial_id: offered_contract.offer_params.change_serial_id,
            cet_locktime: offered_contract.cet_locktime,
            refund_locktime: offered_contract.refund_locktime,
            fee_rate_per_vb: offered_contract.fee_rate_per_vb,
            fund_output_serial_id: offered_contract.fund_output_serial_id,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn validate_offer_test_common(input: &str) {
        let offer: OfferedContract = serde_json::from_str(input).unwrap();
        assert!(offer.validate().is_err());
    }

    #[test]
    fn offer_enum_missing_payout() {
        validate_offer_test_common(include_str!(
            "../../test_inputs/offer_enum_missing_payout.json"
        ));
    }

    #[test]
    fn offer_enum_oracle_with_diff_payout() {
        validate_offer_test_common(include_str!(
            "../../test_inputs/offer_enum_oracle_with_diff_payout.json"
        ));
    }

    #[test]
    fn offer_numerical_bad_first_payout() {
        validate_offer_test_common(include_str!(
            "../../test_inputs/offer_numerical_bad_first_payout.json"
        ));
    }

    #[test]
    fn offer_numerical_bad_last_payout() {
        validate_offer_test_common(include_str!(
            "../../test_inputs/offer_numerical_bad_last_payout.json"
        ));
    }

    #[test]
    fn offer_numerical_non_continuous() {
        validate_offer_test_common(include_str!(
            "../../test_inputs/offer_numerical_non_continuous.json"
        ));
    }

    #[test]
    fn offer_enum_collateral_not_equal_payout() {
        validate_offer_test_common(include_str!(
            "../../test_inputs/offer_enum_collateral_not_equal_payout.json"
        ));
    }

    #[test]
    fn offer_numerical_collateral_less_than_payout() {
        validate_offer_test_common(include_str!(
            "../../test_inputs/offer_numerical_collateral_less_than_payout.json"
        ));
    }

    #[test]
    fn offer_numerical_invalid_rounding_interval() {
        validate_offer_test_common(include_str!(
            "../../test_inputs/offer_numerical_invalid_rounding_interval.json"
        ));
    }

    #[test]
    fn offer_numerical_empty_rounding_interval() {
        validate_offer_test_common(include_str!(
            "../../test_inputs/offer_numerical_empty_rounding_interval.json"
        ));
    }
}