-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathoptions.rs
More file actions
196 lines (181 loc) · 6.1 KB
/
options.rs
File metadata and controls
196 lines (181 loc) · 6.1 KB
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
use simplex::either::{Left, Right};
use simplex::program::Program;
use simplex::provider::SimplicityNetwork;
use simplex::simplicityhl::elements::{AssetId, secp256k1_zkp::XOnlyPublicKey};
use crate::artifacts::options::OptionsProgram;
use crate::artifacts::options::derived_options::{OptionsArguments, OptionsWitness};
use crate::programs::program::SimplexProgram;
#[derive(Debug, Clone, Copy)]
pub struct OptionsParameters {
pub start_time: u32,
pub expiry_time: u32,
pub collateral_per_contract: u64,
pub settlement_per_contract: u64,
pub collateral_asset_id: AssetId,
pub settlement_asset_id: AssetId,
pub option_token_asset: AssetId,
pub option_reissuance_token_asset: AssetId,
pub grantor_token_asset: AssetId,
pub grantor_reissuance_token_asset: AssetId,
pub network: SimplicityNetwork,
}
impl From<OptionsParameters> for OptionsArguments {
fn from(value: OptionsParameters) -> Self {
Self {
start_time: value.start_time,
expiry_time: value.expiry_time,
collateral_per_contract: value.collateral_per_contract,
settlement_per_contract: value.settlement_per_contract,
collateral_asset_id: value.collateral_asset_id.into_inner().0,
settlement_asset_id: value.settlement_asset_id.into_inner().0,
option_token_asset: value.option_token_asset.into_inner().0,
option_reissuance_token_asset: value.option_reissuance_token_asset.into_inner().0,
grantor_token_asset: value.grantor_token_asset.into_inner().0,
grantor_reissuance_token_asset: value.grantor_reissuance_token_asset.into_inner().0,
}
}
}
pub struct Options {
program: OptionsProgram,
pub parameters: OptionsParameters,
}
#[derive(Debug, Clone, Copy, Default)]
pub struct OptionsFundingBlinders {
pub input_option_abf: [u8; 32],
pub input_option_vbf: [u8; 32],
pub input_grantor_abf: [u8; 32],
pub input_grantor_vbf: [u8; 32],
pub output_option_abf: [u8; 32],
pub output_option_vbf: [u8; 32],
pub output_grantor_abf: [u8; 32],
pub output_grantor_vbf: [u8; 32],
}
impl OptionsFundingBlinders {
#[must_use]
pub const fn zero() -> Self {
Self {
input_option_abf: [0; 32],
input_option_vbf: [0; 32],
input_grantor_abf: [0; 32],
input_grantor_vbf: [0; 32],
output_option_abf: [0; 32],
output_option_vbf: [0; 32],
output_grantor_abf: [0; 32],
output_grantor_vbf: [0; 32],
}
}
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, Copy)]
pub enum OptionsBranch {
Fund {
expected_settlement_amount: u64,
blinders: OptionsFundingBlinders,
},
Exercise {
is_change_needed: bool,
amount_to_burn: u64,
collateral_amount: u64,
settlement_amount: u64,
},
Settlement {
is_change_needed: bool,
amount_to_burn: u64,
settlement_amount: u64,
},
Expiry {
is_change_needed: bool,
amount_to_burn: u64,
collateral_amount: u64,
},
Cancel {
is_change_needed: bool,
amount_to_burn: u64,
collateral_amount: u64,
},
}
impl Options {
#[must_use]
pub fn new(parameters: OptionsParameters) -> Self {
Self {
program: OptionsProgram::new(OptionsArguments::from(parameters)),
parameters,
}
}
#[must_use]
pub fn from_internal_key(internal_key: XOnlyPublicKey, parameters: OptionsParameters) -> Self {
Self {
program: OptionsProgram::new(OptionsArguments::from(parameters))
.with_pub_key(internal_key),
parameters,
}
}
#[must_use]
pub const fn calculate_per_contract_params(
total_collateral: u64,
expected_settlement: u64,
contract_count: u64,
) -> (Option<u64>, Option<u64>) {
let collateral_per_contract = total_collateral.checked_div(contract_count);
let settlement_per_contract = expected_settlement.checked_div(contract_count);
(collateral_per_contract, settlement_per_contract)
}
#[must_use]
pub const fn get_witness(branch: OptionsBranch) -> OptionsWitness {
let path = match branch {
OptionsBranch::Fund {
expected_settlement_amount,
blinders,
} => Left(Left((
expected_settlement_amount,
blinders.input_option_abf,
blinders.input_option_vbf,
blinders.input_grantor_abf,
blinders.input_grantor_vbf,
blinders.output_option_abf,
blinders.output_option_vbf,
blinders.output_grantor_abf,
blinders.output_grantor_vbf,
))),
OptionsBranch::Exercise {
is_change_needed,
amount_to_burn,
collateral_amount,
settlement_amount,
} => Left(Right(Left((
is_change_needed,
amount_to_burn,
collateral_amount,
settlement_amount,
)))),
OptionsBranch::Settlement {
is_change_needed,
amount_to_burn,
settlement_amount,
} => Left(Right(Right((
is_change_needed,
amount_to_burn,
settlement_amount,
)))),
OptionsBranch::Expiry {
is_change_needed,
amount_to_burn,
collateral_amount,
} => Right(Left((is_change_needed, amount_to_burn, collateral_amount))),
OptionsBranch::Cancel {
is_change_needed,
amount_to_burn,
collateral_amount,
} => Right(Right((is_change_needed, amount_to_burn, collateral_amount))),
};
OptionsWitness { path }
}
}
impl SimplexProgram for Options {
fn get_program(&self) -> &Program {
self.program.as_ref()
}
fn get_network(&self) -> &SimplicityNetwork {
&self.parameters.network
}
}