Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serum health: use lower-precision division #750

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions programs/mango-v4/src/health/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use anchor_lang::prelude::*;
use fixed::types::I80F48;

use crate::error::*;
use crate::i80f48::LowPrecisionDivision;
use crate::serum3_cpi::{OpenOrdersAmounts, OpenOrdersSlim};
use crate::state::{
Bank, MangoAccountRef, PerpMarket, PerpMarketIndex, PerpPosition, Serum3MarketIndex,
Expand Down Expand Up @@ -293,8 +294,9 @@ impl Serum3Info {
) -> I80F48 {
let quote_asset = quote_info.prices.asset(health_type);
let base_liab = base_info.prices.liab(health_type);
// OPTIMIZATION: These divisions can be extremely expensive (up to 5k CU each)
let reserved_quote_as_base_oracle = self.reserved_quote * quote_asset / base_liab;
let reserved_quote_as_base_oracle = (self.reserved_quote * quote_asset)
.checked_div_f64_precision(base_liab)
.unwrap();
if self.reserved_quote_as_base_highest_bid != 0 {
self.reserved_base
+ reserved_quote_as_base_oracle.min(self.reserved_quote_as_base_highest_bid)
Expand All @@ -312,8 +314,9 @@ impl Serum3Info {
) -> I80F48 {
let base_asset = base_info.prices.asset(health_type);
let quote_liab = quote_info.prices.liab(health_type);
// OPTIMIZATION: These divisions can be extremely expensive (up to 5k CU each)
let reserved_base_as_quote_oracle = self.reserved_base * base_asset / quote_liab;
let reserved_base_as_quote_oracle = (self.reserved_base * base_asset)
.checked_div_f64_precision(quote_liab)
.unwrap();
if self.reserved_base_as_quote_lowest_ask != 0 {
self.reserved_quote
+ reserved_base_as_quote_oracle.min(self.reserved_base_as_quote_lowest_ask)
Expand Down
4 changes: 2 additions & 2 deletions programs/mango-v4/tests/cases/test_health_compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async fn test_health_compute_tokens() -> Result<(), TransportError> {
#[tokio::test]
async fn test_health_compute_serum() -> Result<(), TransportError> {
let mut test_builder = TestContextBuilder::new();
test_builder.test().set_compute_max_units(150_000);
test_builder.test().set_compute_max_units(130_000);
let context = test_builder.start_default().await;
let solana = &context.solana.clone();

Expand Down Expand Up @@ -244,7 +244,7 @@ async fn test_health_compute_serum() -> Result<(), TransportError> {
let avg_cu_increase = cu_measurements.windows(2).map(|p| p[1] - p[0]).sum::<u64>()
/ (cu_measurements.len() - 1) as u64;
println!("average cu increase: {avg_cu_increase}");
assert!(avg_cu_increase < 10000);
assert!(avg_cu_increase < 7000);

Ok(())
}
Expand Down