You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pyth-sdk/src/price.rs
+44-64Lines changed: 44 additions & 64 deletions
Original file line number
Diff line number
Diff line change
@@ -110,39 +110,25 @@ impl Price {
110
110
/// Args
111
111
/// deposits: u64, quantity of token deposited in the protocol
112
112
/// deposits_endpoint: u64, deposits right endpoint for the affine combination
113
-
/// discount_initial: u64, initial discount rate at 0 deposits (units given by discount_exponent)
114
-
/// discount_final: u64, final discount rate at deposits_endpoint deposits (units given by discount_exponent)
113
+
/// rate_discount_initial: u64, initial discounted rate at 0 deposits (units given by discount_exponent)
114
+
/// rate_discount_final: u64, final discounted rate at deposits_endpoint deposits (units given by discount_exponent)
115
115
/// discount_exponent: u64, the exponent to apply to the discounts above (e.g. if discount_final is 10 but meant to express 0.1/10%, exponent would be -2)
116
116
/// note that if discount_initial is bigger than 100% per the discount exponent scale, then the initial valuation of the collateral will be higher than the oracle price
117
-
///
118
-
/// affine_combination yields us error <= 2/PD_SCALE for discount_interpolated
119
-
/// We then multiply this with the price to yield price_discounted before scaling this back to the original expo
120
-
/// Output of affine_combination has expo >= -18, price (self) has arbitrary expo
121
-
/// Scaling this back to the original expo then has error bounded by the expo (10^expo).
122
-
/// This is because reverting a potentially finer expo to a coarser grid has the potential to be off by
123
-
/// the order of the atomic unit of the coarser grid.
124
-
/// This scaling error combines with the previous error additively: Err <= 2/PD_SCALE + 10^expo
125
-
///
126
-
/// The practical error is based on the original expo:
127
-
/// if it is big, then the 10^expo loss dominates;
128
-
/// otherwise, the 2/PD_SCALE error dominates.
129
-
///
130
-
/// Thus, we expect the computed collateral valuation price to be no more than 2/PD_SCALE + 10^expo off of the mathematically true value
131
-
/// For this reason, we encourage using this function with prices that have high expos, to minimize the potential error.
/// borrows: u64, quantity of token borrowed from the protocol
198
184
/// borrows_endpoint: u64, borrows right endpoint for the affine combination
199
-
/// premium_initial: u64, initial premium at 0 borrows (units given by premium_exponent)
200
-
/// premium_final: u64, final premium at borrows_endpoint borrows (units given by premium_exponent)
185
+
/// rate_premium_initial: u64, initial premium at 0 borrows (units given by premium_exponent)
186
+
/// rate_premium_final: u64, final premium at borrows_endpoint borrows (units given by premium_exponent)
201
187
/// premium_exponent: u64, the exponent to apply to the premiums above (e.g. if premium_final is 50 but meant to express 0.05/5%, exponent would be -3)
202
188
/// note that if premium_initial is less than 100% per the premium exponent scale, then the initial valuation of the borrow will be lower than the oracle price
203
-
///
204
-
/// affine_combination yields us error <= 2/PD_SCALE for premium_interpolated
205
-
/// We then multiply this with the price to yield price_premium before scaling this back to the original expo
206
-
/// Output of affine_combination has expo >= -18, price (self) has arbitrary expo
207
-
/// Scaling this back to the original expo then has error bounded by the expo (10^expo).
208
-
/// This is because reverting a potentially finer expo to a coarser grid has the potential to be off by
209
-
/// the order of the atomic unit of the coarser grid.
210
-
/// This scaling error combines with the previous error additively: Err <= 2/PD_SCALE + 10^expo
211
-
///
212
-
/// The practical error is based on the original expo:
213
-
/// if it is big, then the 10^expo loss dominates;
214
-
/// otherwise, the 2/PD_SCALE error dominates.
215
-
///
216
-
/// Thus, we expect the computed borrow valuation price to be no more than 2/PD_SCALE + 10^expo off of the mathematically true value
217
-
/// For this reason, we encourage using this function with prices that have high expos, to minimize the potential error.
// generating xs and prices from i32 to limit the range to reasonable values and guard against overflow/bespoke constraint setting for quickcheck
@@ -2013,17 +1989,24 @@ mod test {
2013
1989
returnTestResult::discard()
2014
1990
}
2015
1991
1992
+
// original result
2016
1993
let result_orig = Price::affine_combination(x1, y1, x2, y2, x_query, pre_add_expo).unwrap();
2017
1994
2018
1995
let y1_norm = y1.normalize().unwrap();
2019
1996
let y2_norm = y2.normalize().unwrap();
2020
1997
1998
+
// result with normalized price inputs
2021
1999
let result_norm = Price::affine_combination(x1, y1_norm, x2, y2_norm, x_query, pre_add_expo).unwrap();
2022
2000
2001
+
// results should match exactly
2023
2002
TestResult::from_bool(result_norm == result_orig)
2024
2003
}
2025
2004
2026
2005
// quickcheck to confirm affine_combination introduces bounded error if close fraction x/y passed in first
2006
+
// this quickcheck calls affine_combination with two sets of similar inputs:
2007
+
// the first set has xs generated by the quickcheck generation process, leading to potentially inexact fractions that don't fit within 8 digits of precision
2008
+
// the second set "normalizes" down to 8 digits of precision by setting x1 to 0, x2 to 100_000_000, and xquery proportionally
2009
+
// based on the bounds described in the docstring of affine_combination, we expect error due to this to be leq 4*10^-7
0 commit comments