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
use ethers::types::U256;
use eyre::Result;
use fixedpointmath::{fixed, FixedPoint};
use crate::State;
impl State {
/// Calculates the curve fee paid when opening shorts with a given bond amount.
///
/// The open short curve fee, `$\Phi_{c,os}(\Delta y)$`, is paid in base and
/// is given by:
///
/// ```math
/// \Phi_{c,os}(\Delta y) = \phi_c \cdot (1 - p) \cdot \Delta y
/// ```
pub fn open_short_curve_fee(&self, bond_amount: FixedPoint<U256>) -> Result<FixedPoint<U256>> {
// NOTE: Round up to overestimate the curve fee.
Ok(self
.curve_fee()
.mul_up(fixed!(1e18) - self.calculate_spot_price()?)
.mul_up(bond_amount))
}
/// Calculates the governance fee paid when opening shorts with a given bond
/// amount.
///
/// The open short governance fee, `$\Phi_{g,os}(\Delta y)$`, is paid in
/// base and is given by:
///
/// ```math
/// \Phi_{g,os}(\Delta y) = \phi_g \cdot \Phi_{c,os}(\Delta y)
/// ```
pub fn open_short_governance_fee(
&self,
bond_amount: FixedPoint<U256>,
maybe_curve_fee: Option<FixedPoint<U256>>,
) -> Result<FixedPoint<U256>> {
let curve_fee = match maybe_curve_fee {
Some(maybe_curve_fee) => maybe_curve_fee,
None => self.open_short_curve_fee(bond_amount)?,
};
// NOTE: Round down to underestimate the governance fee.
Ok(curve_fee.mul_down(self.governance_lp_fee()))
}
/// Calculates the curve fee paid when opening shorts with a given bond
/// amount.
///
/// The close short curve fee, `$\Phi_{c,cs}(\Delta y)$`, is paid in shares
/// and is given by:
///
/// ```math
/// \Phi_{c,cs}(\Delta y) = \frac{\phi_c \cdot (1-p) \cdot \Delta y \cdot t}{c}
/// ```
///
/// where $t$ is the normalized time remaining until bond maturity.
pub fn close_short_curve_fee(
&self,
bond_amount: FixedPoint<U256>,
maturity_time: U256,
current_time: U256,
) -> Result<FixedPoint<U256>> {
let normalized_time_remaining =
self.calculate_normalized_time_remaining(maturity_time, current_time);
// NOTE: Round up to overestimate the curve fee.
Ok(self
.curve_fee()
.mul_up(fixed!(1e18) - self.calculate_spot_price()?)
.mul_up(bond_amount)
.mul_div_up(normalized_time_remaining, self.vault_share_price()))
}
/// Calculate the governance fee paid when closing shorts with a given bond
/// amount.
///
/// The close short governance fee, `$\Phi_{g,cs}(\Delta y)$`, is paid in
/// shares and is given by:
///
/// ```math
/// \Phi_{g,cs}(\Delta y) = \Phi_{c,cs}(\Delta y) * \phi_g
/// ```
///
/// NOTE: Round down to underestimate the governance curve fee
pub fn close_short_governance_fee(
&self,
bond_amount: FixedPoint<U256>,
maturity_time: U256,
current_time: U256,
maybe_curve_fee: Option<FixedPoint<U256>>,
) -> Result<FixedPoint<U256>> {
let curve_fee = match maybe_curve_fee {
Some(maybe_curve_fee) => maybe_curve_fee,
None => self.close_short_curve_fee(bond_amount, maturity_time, current_time)?,
};
// NOTE: Round down to underestimate the governance fee.
Ok(curve_fee.mul_down(self.governance_lp_fee()))
}
/// Calculate the flat fee paid when closing shorts with a given bond
/// amount.
///
/// The close short flat fee, `$\Phi_{f,cs}(\Delta y)$`, is paid in shares
/// and is given by:
///
/// ```math
/// \Phi_{f,cs}(\Delta y) = \frac{\Delta y \cdot (1 - t) \cdot \phi_f}{c}
/// ```
///
/// where `$t$` is the normalized time remaining until bond maturity.
pub fn close_short_flat_fee(
&self,
bond_amount: FixedPoint<U256>,
maturity_time: U256,
current_time: U256,
) -> FixedPoint<U256> {
let normalized_time_remaining =
self.calculate_normalized_time_remaining(maturity_time, current_time);
// NOTE: Round up to overestimate the flat fee.
bond_amount
.mul_div_up(
fixed!(1e18) - normalized_time_remaining,
self.vault_share_price(),
)
.mul_up(self.flat_fee())
}
}