diff --git a/Cargo.lock b/Cargo.lock index c6337d630f..711a28e6f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -728,9 +728,10 @@ dependencies = [ [[package]] name = "drift-macros" version = "0.1.0" -source = "git+https://github.com/drift-labs/drift-macros.git?rev=c57d87#c57d87e073d13d43f4d1fb09fe6822915a4ccc11" +source = "git+https://github.com/drift-labs/drift-macros?rev=92192a1#92192a1efdfa0ddef3ae06e20e24462ea3bbdc0f" dependencies = [ "quote", + "static_assertions", "syn 1.0.109", ] @@ -1713,7 +1714,7 @@ dependencies = [ [[package]] name = "serum_dex" version = "0.5.6" -source = "git+https://github.com/jordy25519/serum-dex?branch=master#2347e4810ace967ab186202e22b6188b9cab03ba" +source = "git+https://github.com/drift-labs/serum-dex?branch=master#2347e4810ace967ab186202e22b6188b9cab03ba" dependencies = [ "arrayref", "bincode", diff --git a/programs/drift/Cargo.toml b/programs/drift/Cargo.toml index 3fb45115f0..6ab9c48c48 100644 --- a/programs/drift/Cargo.toml +++ b/programs/drift/Cargo.toml @@ -27,19 +27,19 @@ pyth-lazer-solana-contract = { git = "https://github.com/jordy25519/pyth-crossch pythnet-sdk = { git = "https://github.com/jordy25519/pyth-crosschain", branch = "master"} pyth-solana-receiver-sdk = { git = "https://github.com/jordy25519/pyth-crosschain", branch = "master"} bytemuck = { version = "1.4.0" } -borsh = "0.10.3" +borsh = "0.10.4" hex = "0.4.3" uint = { version = "0.9.1", default-features = false } num-integer = "0.1.44" num-traits = "0.2" arrayref = "0.3.6" base64 = "0.13.0" -serum_dex = { git = "https://github.com/jordy25519/serum-dex", branch = "master", features = ["no-entrypoint"] } +serum_dex = { git = "https://github.com/drift-labs/serum-dex", branch = "master", features = ["no-entrypoint"] } enumflags2 = "0.6.4" phoenix-v1 = { git = "https://github.com/jordy25519/phoenix-v1", branch = "master", features = ["no-entrypoint"] } solana-security-txt = "1.1.0" static_assertions = "1.1.0" -drift-macros = { git = "https://github.com/drift-labs/drift-macros.git", rev = "c57d87" } +drift-macros = { git = "https://github.com/drift-labs/drift-macros", rev = "92192a1" } switchboard = { path = "../switchboard", features = ["no-entrypoint"] } openbook-v2-light = { path = "../openbook_v2", features = ["no-entrypoint"] } switchboard-on-demand = { path = "../switchboard-on-demand", features = ["no-entrypoint"] } diff --git a/programs/drift/src/controller/amm.rs b/programs/drift/src/controller/amm.rs index e25a578bf6..534d7b612f 100644 --- a/programs/drift/src/controller/amm.rs +++ b/programs/drift/src/controller/amm.rs @@ -87,8 +87,8 @@ pub fn swap_base_asset( quote_asset_amount_surplus, ) = calculate_base_swap_output_with_spread(&market.amm, base_asset_swap_amount, direction)?; - market.amm.base_asset_reserve = new_base_asset_reserve; - market.amm.quote_asset_reserve = new_quote_asset_reserve; + market.amm.set_base_asset_reserve(new_base_asset_reserve); + market.amm.set_quote_asset_reserve(new_quote_asset_reserve); Ok(( quote_asset_amount, @@ -114,33 +114,33 @@ pub fn calculate_base_swap_output_with_spread( base_asset_swap_amount.cast()?, base_asset_reserve_with_spread, direction, - amm.sqrt_k, + amm.sqrt_k(), )?; let quote_asset_amount = calculate_quote_asset_amount_swapped( quote_asset_reserve_with_spread, new_quote_asset_reserve_with_spread, direction, - amm.peg_multiplier, + amm.peg_multiplier(), )?; let (new_quote_asset_reserve, new_base_asset_reserve) = amm::calculate_swap_output( base_asset_swap_amount.cast()?, - amm.base_asset_reserve, + amm.base_asset_reserve(), direction, - amm.sqrt_k, + amm.sqrt_k(), )?; // calculate the quote asset surplus by taking the difference between what quote_asset_amount is // with and without spread let quote_asset_amount_surplus = calculate_quote_asset_amount_surplus( new_quote_asset_reserve, - amm.quote_asset_reserve, + amm.quote_asset_reserve(), match direction { SwapDirection::Remove => SwapDirection::Add, SwapDirection::Add => SwapDirection::Remove, }, - amm.peg_multiplier, + amm.peg_multiplier(), quote_asset_amount, direction == SwapDirection::Remove, )?; @@ -160,19 +160,31 @@ pub fn update_spread_reserves(market: &mut PerpMarket) -> DriftResult { calculate_spread_reserves(market, PositionDirection::Short)?; if market.amm.reference_price_offset == 0 { - market.amm.ask_base_asset_reserve = - new_ask_base_asset_reserve.min(market.amm.base_asset_reserve); - market.amm.bid_base_asset_reserve = - new_bid_base_asset_reserve.max(market.amm.base_asset_reserve); - market.amm.ask_quote_asset_reserve = - new_ask_quote_asset_reserve.max(market.amm.quote_asset_reserve); - market.amm.bid_quote_asset_reserve = - new_bid_quote_asset_reserve.min(market.amm.quote_asset_reserve); + market.amm.set_ask_base_asset_reserve( + new_ask_base_asset_reserve.min(market.amm.base_asset_reserve()), + ); + market.amm.set_bid_base_asset_reserve( + new_bid_base_asset_reserve.max(market.amm.base_asset_reserve()), + ); + market.amm.set_ask_quote_asset_reserve( + new_ask_quote_asset_reserve.max(market.amm.quote_asset_reserve()), + ); + market.amm.set_bid_quote_asset_reserve( + new_bid_quote_asset_reserve.min(market.amm.quote_asset_reserve()), + ); } else { - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); } Ok(()) @@ -188,10 +200,10 @@ pub fn update_spreads( let reference_price_offset = if max_ref_offset > 0 { let liquidity_ratio = amm_spread::calculate_inventory_liquidity_ratio_for_reference_price_offset( - market.amm.base_asset_amount_with_amm, - market.amm.base_asset_reserve, - market.amm.min_base_asset_reserve, - market.amm.max_base_asset_reserve, + market.amm.base_asset_amount_with_amm(), + market.amm.base_asset_reserve(), + market.amm.min_base_asset_reserve(), + market.amm.max_base_asset_reserve(), )?; let signed_liquidity_ratio = @@ -233,16 +245,16 @@ pub fn update_spreads( market.amm.last_oracle_reserve_price_spread_pct, market.amm.last_oracle_conf_pct, market.amm.max_spread, - market.amm.quote_asset_reserve, - market.amm.terminal_quote_asset_reserve, - market.amm.peg_multiplier, - market.amm.base_asset_amount_with_amm, + market.amm.quote_asset_reserve(), + market.amm.terminal_quote_asset_reserve(), + market.amm.peg_multiplier(), + market.amm.base_asset_amount_with_amm(), reserve_price, - market.amm.total_fee_minus_distributions, + market.amm.total_fee_minus_distributions(), market.amm.net_revenue_since_last_funding, - market.amm.base_asset_reserve, - market.amm.min_base_asset_reserve, - market.amm.max_base_asset_reserve, + market.amm.base_asset_reserve(), + market.amm.min_base_asset_reserve(), + market.amm.max_base_asset_reserve(), market.amm.mark_std, market.amm.oracle_std, market.amm.long_intensity_volume, @@ -356,13 +368,13 @@ pub fn update_concentration_coef(market: &mut PerpMarket, scale: u128) -> DriftR "invalid new_concentration_coef", )?; - market.amm.concentration_coef = new_concentration_coef; + market.amm.set_concentration_coef(new_concentration_coef); let (_, terminal_quote_reserves, terminal_base_reserves) = amm::calculate_terminal_price_and_reserves(&market.amm)?; validate!( - terminal_quote_reserves == market.amm.terminal_quote_asset_reserve, + terminal_quote_reserves == market.amm.terminal_quote_asset_reserve(), ErrorCode::InvalidAmmDetected, "invalid terminal_quote_reserves", )?; @@ -371,17 +383,21 @@ pub fn update_concentration_coef(market: &mut PerpMarket, scale: u128) -> DriftR // doing so adds ability to improve amm constant product curve's slippage // by increasing k as same factor as scale w/o increasing imbalance risk let (min_base_asset_reserve, max_base_asset_reserve) = - amm::calculate_bid_ask_bounds(market.amm.concentration_coef, terminal_base_reserves)?; + amm::calculate_bid_ask_bounds(market.amm.concentration_coef(), terminal_base_reserves)?; - market.amm.max_base_asset_reserve = max_base_asset_reserve; - market.amm.min_base_asset_reserve = min_base_asset_reserve; + market + .amm + .set_max_base_asset_reserve(max_base_asset_reserve); + market + .amm + .set_min_base_asset_reserve(min_base_asset_reserve); let reserve_price_after = market.amm.reserve_price()?; update_spreads(market, reserve_price_after, None)?; let (max_bids, max_asks) = amm::calculate_market_open_bids_asks(&market.amm)?; validate!( - max_bids > market.amm.base_asset_amount_with_amm && max_asks < market.amm.base_asset_amount_with_amm, + max_bids > market.amm.base_asset_amount_with_amm() && max_asks < market.amm.base_asset_amount_with_amm(), ErrorCode::InvalidConcentrationCoef, "amm.base_asset_amount_with_amm exceeds the unload liquidity available after concentration adjustment" )?; @@ -395,10 +411,10 @@ pub fn formulaic_update_k( funding_imbalance_cost: i128, now: i64, ) -> DriftResult { - let peg_multiplier_before = market.amm.peg_multiplier; - let base_asset_reserve_before = market.amm.base_asset_reserve; - let quote_asset_reserve_before = market.amm.quote_asset_reserve; - let sqrt_k_before = market.amm.sqrt_k; + let peg_multiplier_before = market.amm.peg_multiplier(); + let base_asset_reserve_before = market.amm.base_asset_reserve(); + let quote_asset_reserve_before = market.amm.quote_asset_reserve(); + let sqrt_k_before = market.amm.sqrt_k(); let funding_imbalance_cost_i64 = funding_imbalance_cost.cast::()?; @@ -419,7 +435,9 @@ pub fn formulaic_update_k( 0 }; - if (budget > 0 && market.amm.sqrt_k < MAX_SQRT_K) || (budget < 0 && market.amm.can_lower_k()?) { + if (budget > 0 && market.amm.sqrt_k() < MAX_SQRT_K) + || (budget < 0 && market.amm.can_lower_k()?) + { // single k scale is capped by .1% increase and .1% decrease (regardless of budget) let k_pct_upper_bound = K_BPS_UPDATE_SCALE + MAX_K_BPS_INCREASE * (market.amm.curve_update_intensity as i128) / 100; @@ -433,7 +451,7 @@ pub fn formulaic_update_k( k_pct_lower_bound, )?; - let new_sqrt_k = bn::U192::from(market.amm.sqrt_k) + let new_sqrt_k = bn::U192::from(market.amm.sqrt_k()) .safe_mul(bn::U192::from(k_scale_numerator))? .safe_div(bn::U192::from(k_scale_denominator))?; @@ -446,10 +464,10 @@ pub fn formulaic_update_k( if cost_applied { cp_curve::update_k(market, &update_k_result)?; - let peg_multiplier_after = market.amm.peg_multiplier; - let base_asset_reserve_after = market.amm.base_asset_reserve; - let quote_asset_reserve_after = market.amm.quote_asset_reserve; - let sqrt_k_after = market.amm.sqrt_k; + let peg_multiplier_after = market.amm.peg_multiplier(); + let base_asset_reserve_after = market.amm.base_asset_reserve(); + let quote_asset_reserve_after = market.amm.quote_asset_reserve(); + let sqrt_k_after = market.amm.sqrt_k(); emit!(CurveRecord { ts: now, @@ -463,13 +481,13 @@ pub fn formulaic_update_k( base_asset_reserve_after, quote_asset_reserve_after, sqrt_k_after, - base_asset_amount_long: market.amm.base_asset_amount_long.unsigned_abs(), - base_asset_amount_short: market.amm.base_asset_amount_short.unsigned_abs(), - base_asset_amount_with_amm: market.amm.base_asset_amount_with_amm, + base_asset_amount_long: market.amm.base_asset_amount_long().unsigned_abs(), + base_asset_amount_short: market.amm.base_asset_amount_short().unsigned_abs(), + base_asset_amount_with_amm: market.amm.base_asset_amount_with_amm(), number_of_users: market.number_of_users, adjustment_cost, - total_fee: market.amm.total_fee, - total_fee_minus_distributions: market.amm.total_fee_minus_distributions, + total_fee: market.amm.total_fee(), + total_fee_minus_distributions: market.amm.total_fee_minus_distributions(), oracle_price: market.amm.historical_oracle_data.last_oracle_price, fill_record: market.next_fill_record_id as u128, }); @@ -511,14 +529,14 @@ fn calculate_revenue_pool_transfer( let fee_pool_threshold = amm_fee_pool_token_amount_after .saturating_sub( FEE_POOL_TO_REVENUE_POOL_THRESHOLD - .safe_add(market.amm.total_social_loss)? + .safe_add(market.amm.total_social_loss())? .cast()?, ) .cast()?; let total_liq_fees_for_revenue_pool = market .amm - .total_liquidation_fee + .total_liquidation_fee() .min( market .insurance_claim @@ -544,7 +562,7 @@ fn calculate_revenue_pool_transfer( let revenue_pool_transfer = total_fee_for_if .safe_add(total_liq_fees_for_revenue_pool)? - .saturating_sub(market.amm.total_fee_withdrawn.cast()?) + .saturating_sub(market.amm.total_fee_withdrawn().cast()?) .max(0) .min(fee_pool_threshold) .min(max_revenue_to_settle.cast()?); @@ -566,7 +584,7 @@ fn calculate_revenue_pool_transfer( .cast::()? .min( get_token_amount( - spot_market.revenue_pool.scaled_balance, + spot_market.revenue_pool.scaled_balance(), spot_market, &SpotBalanceType::Deposit, )? @@ -611,9 +629,9 @@ pub fn update_pool_balances( let amm_target_max_fee_pool_token_amount = market .amm - .total_fee_minus_distributions - .safe_add(market.amm.total_liquidation_fee.cast()?)? - .safe_sub(market.amm.total_fee_withdrawn.cast()?)?; + .total_fee_minus_distributions() + .safe_add(market.amm.total_liquidation_fee().cast()?)? + .safe_sub(market.amm.total_fee_withdrawn().cast()?)?; if amm_target_max_fee_pool_token_amount <= amm_fee_pool_token_amount { // owe the market pnl pool before settling user @@ -634,8 +652,8 @@ pub fn update_pool_balances( { let amm_target_min_fee_pool_token_amount = get_total_fee_lower_bound(market)? - .safe_add(market.amm.total_liquidation_fee)? - .safe_sub(market.amm.total_fee_withdrawn)?; + .safe_add(market.amm.total_liquidation_fee())? + .safe_sub(market.amm.total_fee_withdrawn())?; let amm_fee_pool_token_amount = get_token_amount( market.amm.fee_pool.balance(), @@ -672,8 +690,8 @@ pub fn update_pool_balances( let terminal_state_surplus = market .amm - .total_fee_minus_distributions - .safe_sub(market.amm.total_fee_withdrawn.cast()?)?; + .total_fee_minus_distributions() + .safe_sub(market.amm.total_fee_withdrawn().cast()?)?; // market can perform withdraw from revenue pool if spot_market.insurance_fund.last_revenue_settle_ts @@ -704,10 +722,12 @@ pub fn update_pool_balances( &mut market.amm.fee_pool, )?; - market.amm.total_fee_withdrawn = market - .amm - .total_fee_withdrawn - .safe_add(revenue_pool_transfer.unsigned_abs())?; + market.amm.set_total_fee_withdrawn( + market + .amm + .total_fee_withdrawn() + .safe_add(revenue_pool_transfer.unsigned_abs())?, + ); } Ordering::Less => { transfer_revenue_pool_to_spot_balance( @@ -720,10 +740,12 @@ pub fn update_pool_balances( } if revenue_pool_transfer != 0 { - market.amm.total_fee_minus_distributions = market - .amm - .total_fee_minus_distributions - .safe_sub(revenue_pool_transfer)?; + market.amm.set_total_fee_minus_distributions( + market + .amm + .total_fee_minus_distributions() + .safe_sub(revenue_pool_transfer)?, + ); market.insurance_claim.revenue_withdraw_since_last_settle = market .insurance_claim @@ -795,7 +817,7 @@ pub fn update_pnl_pool_and_user_balance( let pnl_to_settle_with_user = if unrealized_pnl_with_fee > 0 { unrealized_pnl_with_fee.min( get_token_amount( - market.pnl_pool.scaled_balance, + market.pnl_pool.scaled_balance(), bank, market.pnl_pool.balance_type(), )? @@ -845,35 +867,42 @@ pub fn move_price( quote_asset_reserve: u128, sqrt_k: u128, ) -> DriftResult { - market.amm.base_asset_reserve = base_asset_reserve; + market.amm.set_base_asset_reserve(base_asset_reserve); let k = bn::U256::from(sqrt_k).safe_mul(bn::U256::from(sqrt_k))?; - market.amm.quote_asset_reserve = k - .safe_div(bn::U256::from(base_asset_reserve))? - .try_to_u128()?; + market.amm.set_quote_asset_reserve( + k.safe_div(bn::U256::from(base_asset_reserve))? + .try_to_u128()?, + ); validate!( - (quote_asset_reserve.cast::()? - market.amm.quote_asset_reserve.cast::()?) + (quote_asset_reserve.cast::()? - market.amm.quote_asset_reserve().cast::()?) .abs() < 100, ErrorCode::InvalidAmmDetected, "quote_asset_reserve passed doesnt reconcile enough {} vs {}", quote_asset_reserve.cast::()?, - market.amm.quote_asset_reserve.cast::()? + market.amm.quote_asset_reserve().cast::()? )?; - market.amm.sqrt_k = sqrt_k; + market.amm.set_sqrt_k(sqrt_k); let (_, terminal_quote_reserves, terminal_base_reserves) = amm::calculate_terminal_price_and_reserves(&market.amm)?; - market.amm.terminal_quote_asset_reserve = terminal_quote_reserves; + market + .amm + .set_terminal_quote_asset_reserve(terminal_quote_reserves); let (min_base_asset_reserve, max_base_asset_reserve) = - amm::calculate_bid_ask_bounds(market.amm.concentration_coef, terminal_base_reserves)?; + amm::calculate_bid_ask_bounds(market.amm.concentration_coef(), terminal_base_reserves)?; - market.amm.max_base_asset_reserve = max_base_asset_reserve; - market.amm.min_base_asset_reserve = min_base_asset_reserve; + market + .amm + .set_max_base_asset_reserve(max_base_asset_reserve); + market + .amm + .set_min_base_asset_reserve(min_base_asset_reserve); let reserve_price_after = market.amm.reserve_price()?; update_spreads(market, reserve_price_after, None)?; @@ -888,48 +917,56 @@ pub fn recenter_perp_market_amm( sqrt_k: u128, ) -> DriftResult { // calculate base/quote reserves for balanced terminal reserves - let swap_direction = if market.amm.base_asset_amount_with_amm > 0 { + let swap_direction = if market.amm.base_asset_amount_with_amm() > 0 { SwapDirection::Remove } else { SwapDirection::Add }; let (new_quote_asset_amount, new_base_asset_amount) = amm::calculate_swap_output( - market.amm.base_asset_amount_with_amm.unsigned_abs(), + market.amm.base_asset_amount_with_amm().unsigned_abs(), sqrt_k, swap_direction, sqrt_k, )?; - market.amm.base_asset_reserve = new_base_asset_amount; + market.amm.set_base_asset_reserve(new_base_asset_amount); let k = bn::U256::from(sqrt_k).safe_mul(bn::U256::from(sqrt_k))?; - market.amm.quote_asset_reserve = k - .safe_div(bn::U256::from(new_base_asset_amount))? - .try_to_u128()?; + market.amm.set_quote_asset_reserve( + k.safe_div(bn::U256::from(new_base_asset_amount))? + .try_to_u128()?, + ); validate!( - (new_quote_asset_amount.cast::()? - market.amm.quote_asset_reserve.cast::()?) - .abs() + (new_quote_asset_amount.cast::()? + - market.amm.quote_asset_reserve().cast::()?) + .abs() < 100, ErrorCode::InvalidAmmDetected, "quote_asset_reserve passed doesnt reconcile enough" )?; - market.amm.sqrt_k = sqrt_k; + market.amm.set_sqrt_k(sqrt_k); // todo: ensure correct terminal state cost for altering sqrt_k - market.amm.peg_multiplier = peg_multiplier; + market.amm.set_peg_multiplier(peg_multiplier); let (_, terminal_quote_reserves, terminal_base_reserves) = amm::calculate_terminal_price_and_reserves(&market.amm)?; - market.amm.terminal_quote_asset_reserve = terminal_quote_reserves; + market + .amm + .set_terminal_quote_asset_reserve(terminal_quote_reserves); let (min_base_asset_reserve, max_base_asset_reserve) = - amm::calculate_bid_ask_bounds(market.amm.concentration_coef, terminal_base_reserves)?; + amm::calculate_bid_ask_bounds(market.amm.concentration_coef(), terminal_base_reserves)?; - market.amm.max_base_asset_reserve = max_base_asset_reserve; - market.amm.min_base_asset_reserve = min_base_asset_reserve; + market + .amm + .set_max_base_asset_reserve(max_base_asset_reserve); + market + .amm + .set_min_base_asset_reserve(min_base_asset_reserve); let reserve_price_after = market.amm.reserve_price()?; update_spreads(market, reserve_price_after, None)?; @@ -945,13 +982,13 @@ pub fn calculate_perp_market_amm_summary_stats( exclude_liquidation_fee: bool, ) -> DriftResult { let pnl_pool_token_amount = get_token_amount( - perp_market.pnl_pool.scaled_balance, + perp_market.pnl_pool.scaled_balance(), spot_market, perp_market.pnl_pool.balance_type(), )?; let fee_pool_token_amount = get_token_amount( - perp_market.amm.fee_pool.scaled_balance, + perp_market.amm.fee_pool.scaled_balance(), spot_market, perp_market.amm.fee_pool.balance_type(), )?; @@ -967,7 +1004,7 @@ pub fn calculate_perp_market_amm_summary_stats( if exclude_liquidation_fee { new_total_fee_minus_distributions = new_total_fee_minus_distributions - .safe_sub(perp_market.amm.total_liquidation_fee.cast()?)?; + .safe_sub(perp_market.amm.total_liquidation_fee().cast()?)?; } Ok(new_total_fee_minus_distributions) diff --git a/programs/drift/src/controller/amm/tests.rs b/programs/drift/src/controller/amm/tests.rs index ba51779f5e..6eb0a5c2ac 100644 --- a/programs/drift/src/controller/amm/tests.rs +++ b/programs/drift/src/controller/amm/tests.rs @@ -10,14 +10,14 @@ use crate::state::perp_market::{InsuranceClaim, PoolBalance}; fn concentration_coef_tests() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 512295081967, - quote_asset_reserve: 488 * AMM_RESERVE_PRECISION, - terminal_quote_asset_reserve: 500 * AMM_RESERVE_PRECISION, - sqrt_k: 500 * AMM_RESERVE_PRECISION, - peg_multiplier: 50000000, - concentration_coef: MAX_CONCENTRATION_COEFFICIENT, - base_asset_amount_with_amm: -12295081967, - total_fee_minus_distributions: 1000 * QUOTE_PRECISION as i128, + base_asset_reserve: 512295081967.into(), + quote_asset_reserve: (488 * AMM_RESERVE_PRECISION).into(), + terminal_quote_asset_reserve: (500 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (500 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 50000000.into(), + concentration_coef: MAX_CONCENTRATION_COEFFICIENT.into(), + base_asset_amount_with_amm: (-12295081967).into(), + total_fee_minus_distributions: (1000 * QUOTE_PRECISION as i128).into(), curve_update_intensity: 100, ..AMM::default() }, @@ -28,8 +28,8 @@ fn concentration_coef_tests() { let new_scale = 1; update_concentration_coef(&mut market, new_scale).unwrap(); - assert_eq!(market.amm.min_base_asset_reserve, 353556781219); - assert_eq!(market.amm.max_base_asset_reserve, 707100000000); + assert_eq!(market.amm.min_base_asset_reserve(), 353556781219); + assert_eq!(market.amm.max_base_asset_reserve(), 707100000000); let (orig_open_bids, orig_open_asks) = amm::calculate_market_open_bids_asks(&market.amm).unwrap(); @@ -38,20 +38,20 @@ fn concentration_coef_tests() { let new_scale = 2; update_concentration_coef(&mut market, new_scale).unwrap(); - assert_eq!(market.amm.min_base_asset_reserve, 414215889321); - assert_eq!(market.amm.max_base_asset_reserve, 603550000000); + assert_eq!(market.amm.min_base_asset_reserve(), 414215889321); + assert_eq!(market.amm.max_base_asset_reserve(), 603550000000); let new_scale = 5; update_concentration_coef(&mut market, new_scale).unwrap(); - assert_eq!(market.amm.min_base_asset_reserve, 461748734808); - assert_eq!(market.amm.max_base_asset_reserve, 541420000000); - let new_sqrt_k = market.amm.sqrt_k * new_scale; + assert_eq!(market.amm.min_base_asset_reserve(), 461748734808); + assert_eq!(market.amm.max_base_asset_reserve(), 541420000000); + let new_sqrt_k = market.amm.sqrt_k() * new_scale; let update_k_result = get_update_k_result(&market, bn::U192::from(new_sqrt_k), false).unwrap(); let adjustment_cost = cp_curve::adjust_k_cost(&mut market, &update_k_result).unwrap(); assert_eq!(adjustment_cost, 11_575_563); cp_curve::update_k(&mut market, &update_k_result).unwrap(); - assert_eq!(market.amm.sqrt_k, new_sqrt_k); + assert_eq!(market.amm.sqrt_k(), new_sqrt_k); let (open_bids, open_asks) = amm::calculate_market_open_bids_asks(&market.amm).unwrap(); assert_eq!(open_bids, 207313827452); @@ -66,32 +66,32 @@ fn concentration_coef_tests() { // different default market let mut market_balanced = PerpMarket::default_test(); - assert_eq!(market_balanced.amm.base_asset_amount_with_amm, 0); - assert_eq!(market_balanced.amm.sqrt_k, 100000000000); + assert_eq!(market_balanced.amm.base_asset_amount_with_amm(), 0); + assert_eq!(market_balanced.amm.sqrt_k(), 100000000000); let new_scale = 20; update_concentration_coef(&mut market_balanced, new_scale).unwrap(); - assert_eq!(market_balanced.amm.min_base_asset_reserve, 97971020172); - assert_eq!(market_balanced.amm.max_base_asset_reserve, 102071000000); + assert_eq!(market_balanced.amm.min_base_asset_reserve(), 97971020172); + assert_eq!(market_balanced.amm.max_base_asset_reserve(), 102071000000); let new_scale = AMM_RESERVE_PRECISION; // too large, err assert!(update_concentration_coef(&mut market_balanced, new_scale).is_err()); - assert_eq!(market_balanced.amm.min_base_asset_reserve, 97971020172); - assert_eq!(market_balanced.amm.max_base_asset_reserve, 102071000000); + assert_eq!(market_balanced.amm.min_base_asset_reserve(), 97971020172); + assert_eq!(market_balanced.amm.max_base_asset_reserve(), 102071000000); let new_scale = 140000; // near limit, very little liquidity update_concentration_coef(&mut market_balanced, new_scale).unwrap(); - assert_eq!(market_balanced.amm.min_base_asset_reserve, 99999800000); - assert_eq!(market_balanced.amm.max_base_asset_reserve, 100000200000); + assert_eq!(market_balanced.amm.min_base_asset_reserve(), 99999800000); + assert_eq!(market_balanced.amm.max_base_asset_reserve(), 100000200000); - let new_sqrt_k = market_balanced.amm.sqrt_k * new_scale; + let new_sqrt_k = market_balanced.amm.sqrt_k() * new_scale; let update_k_result = get_update_k_result(&market_balanced, bn::U192::from(new_sqrt_k), false).unwrap(); let adjustment_cost = cp_curve::adjust_k_cost(&mut market_balanced, &update_k_result).unwrap(); assert_eq!(adjustment_cost, 0); cp_curve::update_k(&mut market_balanced, &update_k_result).unwrap(); - assert_eq!(market_balanced.amm.sqrt_k, new_sqrt_k); + assert_eq!(market_balanced.amm.sqrt_k(), new_sqrt_k); let (open_bids, open_asks) = amm::calculate_market_open_bids_asks(&market_balanced.amm).unwrap(); @@ -103,13 +103,13 @@ fn concentration_coef_tests() { fn formualic_k_tests() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 512295081967, - quote_asset_reserve: 488 * AMM_RESERVE_PRECISION, - sqrt_k: 500 * AMM_RESERVE_PRECISION, - peg_multiplier: 50000000, - concentration_coef: MAX_CONCENTRATION_COEFFICIENT, - base_asset_amount_with_amm: -12295081967, - total_fee_minus_distributions: 1000 * QUOTE_PRECISION as i128, + base_asset_reserve: 512295081967.into(), + quote_asset_reserve: (488 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (500 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 50000000.into(), + concentration_coef: MAX_CONCENTRATION_COEFFICIENT.into(), + base_asset_amount_with_amm: (-12295081967).into(), + total_fee_minus_distributions: (1000 * QUOTE_PRECISION as i128).into(), curve_update_intensity: 100, ..AMM::default() }, @@ -117,14 +117,20 @@ fn formualic_k_tests() { }; let (new_terminal_quote_reserve, new_terminal_base_reserve) = amm::calculate_terminal_reserves(&market.amm).unwrap(); - market.amm.terminal_quote_asset_reserve = new_terminal_quote_reserve; + market + .amm + .set_terminal_quote_asset_reserve(new_terminal_quote_reserve); let (min_base_asset_reserve, max_base_asset_reserve) = - amm::calculate_bid_ask_bounds(market.amm.concentration_coef, new_terminal_base_reserve) + amm::calculate_bid_ask_bounds(market.amm.concentration_coef(), new_terminal_base_reserve) .unwrap(); - market.amm.min_base_asset_reserve = min_base_asset_reserve; - market.amm.max_base_asset_reserve = max_base_asset_reserve; + market + .amm + .set_min_base_asset_reserve(min_base_asset_reserve); + market + .amm + .set_max_base_asset_reserve(max_base_asset_reserve); - let prev_sqrt_k = market.amm.sqrt_k; + let prev_sqrt_k = market.amm.sqrt_k(); // let reserve_price = market.amm.reserve_price().unwrap(); let now = 10000; @@ -139,46 +145,49 @@ fn formualic_k_tests() { // zero funding cost let funding_cost: i128 = 0; formulaic_update_k(&mut market, &oracle_price_data, funding_cost, now).unwrap(); - assert_eq!(prev_sqrt_k, market.amm.sqrt_k); + assert_eq!(prev_sqrt_k, market.amm.sqrt_k()); assert_eq!( - market.amm.total_fee_minus_distributions, + market.amm.total_fee_minus_distributions(), 1000 * QUOTE_PRECISION as i128 ); // positive means amm supossedly paid $500 in funding payments for interval let funding_cost_2: i128 = (500 * QUOTE_PRECISION) as i128; formulaic_update_k(&mut market, &oracle_price_data, funding_cost_2, now).unwrap(); - assert_eq!(market.amm.sqrt_k, 499500000000); // max k decrease (.1%) - assert!(prev_sqrt_k > market.amm.sqrt_k); - assert_eq!(market.amm.total_fee_minus_distributions, 1000014768); //$.014768 acquired from slippage increase + assert_eq!(market.amm.sqrt_k(), 499500000000); // max k decrease (.1%) + assert!(prev_sqrt_k > market.amm.sqrt_k()); + assert_eq!(market.amm.total_fee_minus_distributions(), 1000014768); //$.014768 acquired from slippage increase // negative means amm recieved $500 in funding payments for interval let funding_cost_2: i128 = -((500 * QUOTE_PRECISION) as i128); formulaic_update_k(&mut market, &oracle_price_data, funding_cost_2, now).unwrap(); - assert_eq!(market.amm.sqrt_k, 499999500000); // max k increase (.1%) - assert_eq!(market.amm.total_fee_minus_distributions, 1000000013); //almost full spent from slippage decrease + assert_eq!(market.amm.sqrt_k(), 499999500000); // max k increase (.1%) + assert_eq!(market.amm.total_fee_minus_distributions(), 1000000013); //almost full spent from slippage decrease // negative means amm recieved $.001 in funding payments for interval let funding_cost_2: i128 = -((QUOTE_PRECISION / 1000) as i128); formulaic_update_k(&mut market, &oracle_price_data, funding_cost_2, now).unwrap(); // new numbers bc of increased sqrt_k precision - assert_eq!(market.amm.sqrt_k, 500015999983); // increase k by 1.000033x - assert_eq!(market.amm.total_fee_minus_distributions - 1000000013, -486); // ~$0.000486 spent from slippage decrease + assert_eq!(market.amm.sqrt_k(), 500015999983); // increase k by 1.000033x + assert_eq!( + market.amm.total_fee_minus_distributions() - 1000000013, + -486 + ); // ~$0.000486 spent from slippage decrease } #[test] fn iterative_bounds_formualic_k_tests() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 512295081967, - quote_asset_reserve: 488 * AMM_RESERVE_PRECISION, - sqrt_k: 500 * AMM_RESERVE_PRECISION, - peg_multiplier: 50000000, - concentration_coef: MAX_CONCENTRATION_COEFFICIENT, - base_asset_amount_with_amm: -12295081967, - total_fee_minus_distributions: 1000 * QUOTE_PRECISION as i128, + base_asset_reserve: 512295081967.into(), + quote_asset_reserve: (488 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (500 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 50000000.into(), + concentration_coef: MAX_CONCENTRATION_COEFFICIENT.into(), + base_asset_amount_with_amm: (-12295081967).into(), + total_fee_minus_distributions: (1000 * QUOTE_PRECISION as i128).into(), curve_update_intensity: 100, ..AMM::default() }, @@ -198,32 +207,32 @@ fn iterative_bounds_formualic_k_tests() { // negative funding cost let mut count = 0; - let mut prev_k = market.amm.sqrt_k; + let mut prev_k = market.amm.sqrt_k(); let mut new_k = 0; while prev_k != new_k && count < 10000 { let funding_cost = -(QUOTE_PRECISION as i128); - prev_k = market.amm.sqrt_k; + prev_k = market.amm.sqrt_k(); formulaic_update_k(&mut market, &oracle_price_data, funding_cost, now).unwrap(); - new_k = market.amm.sqrt_k; + new_k = market.amm.sqrt_k(); count += 1 } - assert_eq!(market.amm.base_asset_amount_with_amm, -12295081967); - assert_eq!(market.amm.sqrt_k, 10958340658498292); - assert_eq!(market.amm.total_fee_minus_distributions, 985_612_320); + assert_eq!(market.amm.base_asset_amount_with_amm(), -12295081967); + assert_eq!(market.amm.sqrt_k(), 10958340658498292); + assert_eq!(market.amm.total_fee_minus_distributions(), 985_612_320); } #[test] fn iterative_no_bounds_formualic_k_tests() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 512295081967, - quote_asset_reserve: 488 * AMM_RESERVE_PRECISION, - sqrt_k: 500 * AMM_RESERVE_PRECISION, - peg_multiplier: 50000000, - concentration_coef: MAX_CONCENTRATION_COEFFICIENT, - base_asset_amount_with_amm: -12295081967, - total_fee_minus_distributions: 1000 * QUOTE_PRECISION as i128, + base_asset_reserve: 512295081967.into(), + quote_asset_reserve: (488 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (500 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 50000000.into(), + concentration_coef: MAX_CONCENTRATION_COEFFICIENT.into(), + base_asset_amount_with_amm: (-12295081967).into(), + total_fee_minus_distributions: (1000 * QUOTE_PRECISION as i128).into(), curve_update_intensity: 100, ..AMM::default() }, @@ -243,31 +252,31 @@ fn iterative_no_bounds_formualic_k_tests() { // negative funding cost let mut count = 0; - let mut prev_k = market.amm.sqrt_k; + let mut prev_k = market.amm.sqrt_k(); let mut new_k = 0; while prev_k != new_k && count < 100000 && prev_k < MAX_SQRT_K * 99 / 100 { let funding_cost = -((QUOTE_PRECISION * 100000) as i128); - prev_k = market.amm.sqrt_k; + prev_k = market.amm.sqrt_k(); formulaic_update_k(&mut market, &oracle_price_data, funding_cost, now).unwrap(); - new_k = market.amm.sqrt_k; + new_k = market.amm.sqrt_k(); count += 1 } - assert_eq!(market.amm.base_asset_amount_with_amm, -12295081967); - assert_eq!(market.amm.sqrt_k, 991917456633894384209); // below MAX_SQRT_K - assert_eq!(market.amm.total_fee_minus_distributions, 985625029); + assert_eq!(market.amm.base_asset_amount_with_amm(), -12295081967); + assert_eq!(market.amm.sqrt_k(), 991917456633894384209); // below MAX_SQRT_K + assert_eq!(market.amm.total_fee_minus_distributions(), 985625029); } #[test] fn update_pool_balances_test_high_util_borrow() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 5122950819670000, - quote_asset_reserve: 488 * AMM_RESERVE_PRECISION, - sqrt_k: 500 * AMM_RESERVE_PRECISION, - peg_multiplier: 50000, - base_asset_amount_with_amm: -122950819670000, - total_fee_minus_distributions: 1000 * QUOTE_PRECISION as i128, + base_asset_reserve: 5122950819670000.into(), + quote_asset_reserve: (488 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (500 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 50000.into(), + base_asset_amount_with_amm: (-122950819670000).into(), + total_fee_minus_distributions: (1000 * QUOTE_PRECISION as i128).into(), curve_update_intensity: 100, ..AMM::default() }, @@ -276,13 +285,13 @@ fn update_pool_balances_test_high_util_borrow() { let now = 33928058; let mut spot_market = SpotMarket { - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), ..SpotMarket::default() }; // 100% util - spot_market.deposit_balance = 10_u128.pow(19_u32); - spot_market.borrow_balance = 10_u128.pow(19_u32); + spot_market.set_deposit_balance(10_u128.pow(19_u32)); + spot_market.set_borrow_balance(10_u128.pow(19_u32)); spot_market.deposit_token_twap = 10_u64.pow(16_u32); // would lead to a borrow @@ -300,7 +309,7 @@ fn update_pool_balances_test_high_util_borrow() { assert_eq!(to_settle_with_user, 0); // util is low => neg settle ok - spot_market.borrow_balance = 0; + spot_market.set_borrow_balance(0); let unsettled_pnl = -100; let to_settle_with_user = update_pool_balances( &mut market, @@ -313,7 +322,7 @@ fn update_pool_balances_test_high_util_borrow() { assert_eq!(to_settle_with_user, unsettled_pnl); // util is high - spot_market.borrow_balance = 10_u128.pow(19_u32); + spot_market.set_borrow_balance(10_u128.pow(19_u32)); // user has a little bit deposited => settle how much they have deposited update_spot_balances( 50, @@ -357,12 +366,12 @@ fn update_pool_balances_test_high_util_borrow() { fn update_pool_balances_test() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 5122950819670000, - quote_asset_reserve: 488 * AMM_RESERVE_PRECISION, - sqrt_k: 500 * AMM_RESERVE_PRECISION, - peg_multiplier: 50000, - base_asset_amount_with_amm: -122950819670000, - total_fee_minus_distributions: 1000 * QUOTE_PRECISION as i128, + base_asset_reserve: 5122950819670000.into(), + quote_asset_reserve: (488 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (500 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 50000.into(), + base_asset_amount_with_amm: (-122950819670000).into(), + total_fee_minus_distributions: (1000 * QUOTE_PRECISION as i128).into(), curve_update_intensity: 100, ..AMM::default() }, @@ -371,11 +380,11 @@ fn update_pool_balances_test() { let now = 33928058; let mut spot_market = SpotMarket { - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), ..SpotMarket::default() }; - spot_market.deposit_balance = 10_u128.pow(19_u32); + spot_market.set_deposit_balance(10_u128.pow(19_u32)); spot_market.deposit_token_twap = 10_u64.pow(16_u32); let spot_position = SpotPosition::default(); @@ -422,7 +431,7 @@ fn update_pool_balances_test() { assert_eq!(pnl_pool_token_amount, 0); assert_eq!(amm_fee_pool_token_amount, 1); - market.amm.total_fee_minus_distributions = 0; + market.amm.set_total_fee_minus_distributions(0); update_pool_balances(&mut market, &mut spot_market, &spot_position, -1, now).unwrap(); let amm_fee_pool_token_amount = get_token_amount( market.amm.fee_pool.balance(), @@ -439,7 +448,9 @@ fn update_pool_balances_test() { assert_eq!(pnl_pool_token_amount, 2); assert_eq!(amm_fee_pool_token_amount, 0); - market.amm.total_fee_minus_distributions = 90_000 * QUOTE_PRECISION as i128; + market + .amm + .set_total_fee_minus_distributions(90_000 * QUOTE_PRECISION as i128); update_pool_balances( &mut market, &mut spot_market, @@ -464,7 +475,7 @@ fn update_pool_balances_test() { assert_eq!(amm_fee_pool_token_amount, 33333333); // negative fee pool - market.amm.total_fee_minus_distributions = -8_008_123_456; + market.amm.set_total_fee_minus_distributions(-8_008_123_456); update_pool_balances( &mut market, @@ -494,29 +505,29 @@ fn update_pool_balances_test() { fn update_pool_balances_fee_to_revenue_test() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 5122950819670000, - quote_asset_reserve: 488 * AMM_RESERVE_PRECISION, - sqrt_k: 500 * AMM_RESERVE_PRECISION, - peg_multiplier: 50000, - base_asset_amount_with_amm: -122950819670000, - - total_exchange_fee: 10 * QUOTE_PRECISION, - total_fee: 10 * QUOTE_PRECISION as i128, - total_mm_fee: 990 * QUOTE_PRECISION as i128, - total_fee_minus_distributions: 1000 * QUOTE_PRECISION as i128, - total_liquidation_fee: QUOTE_PRECISION, + base_asset_reserve: 5122950819670000.into(), + quote_asset_reserve: (488 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (500 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 50000.into(), + base_asset_amount_with_amm: (-122950819670000).into(), + + total_exchange_fee: (10 * QUOTE_PRECISION).into(), + total_fee: (10 * QUOTE_PRECISION as i128).into(), + total_mm_fee: (990 * QUOTE_PRECISION as i128).into(), + total_fee_minus_distributions: (1000 * QUOTE_PRECISION as i128).into(), + total_liquidation_fee: QUOTE_PRECISION.into(), net_revenue_since_last_funding: 10000 * QUOTE_PRECISION as i64, curve_update_intensity: 100, fee_pool: PoolBalance { - scaled_balance: 50 * QUOTE_PRECISION * SPOT_BALANCE_PRECISION, + scaled_balance: (50 * QUOTE_PRECISION * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, ..AMM::default() }, pnl_pool: PoolBalance { - scaled_balance: 50 * QUOTE_PRECISION * SPOT_BALANCE_PRECISION, + scaled_balance: (50 * QUOTE_PRECISION * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -530,18 +541,18 @@ fn update_pool_balances_fee_to_revenue_test() { let now = 33928058; let mut spot_market = SpotMarket { - deposit_balance: 100 * QUOTE_PRECISION * SPOT_BALANCE_PRECISION, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + deposit_balance: (100 * QUOTE_PRECISION * SPOT_BALANCE_PRECISION).into(), + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), revenue_pool: PoolBalance::default(), ..SpotMarket::default() }; - let prev_fee_pool = market.amm.fee_pool.scaled_balance; - let prev_pnl_pool = market.amm.fee_pool.scaled_balance; - let prev_rev_pool = spot_market.revenue_pool.scaled_balance; + let prev_fee_pool = market.amm.fee_pool.scaled_balance(); + let prev_pnl_pool = market.amm.fee_pool.scaled_balance(); + let prev_rev_pool = spot_market.revenue_pool.scaled_balance(); - assert_eq!(market.amm.total_fee_withdrawn, 0); + assert_eq!(market.amm.total_fee_withdrawn(), 0); assert_eq!( get_token_amount( @@ -555,7 +566,7 @@ fn update_pool_balances_fee_to_revenue_test() { assert_eq!( get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), &spot_market, &SpotBalanceType::Deposit ) @@ -566,68 +577,68 @@ fn update_pool_balances_fee_to_revenue_test() { let spot_position = SpotPosition::default(); update_pool_balances(&mut market, &mut spot_market, &spot_position, 0, now).unwrap(); - assert_eq!(market.amm.fee_pool.scaled_balance, 50000000000000000); // under FEE_POOL_TO_REVENUE_POOL_THRESHOLD - assert_eq!(market.pnl_pool.scaled_balance, 50000000000000000); - assert_eq!(spot_market.revenue_pool.scaled_balance, 0); - assert_eq!(market.amm.total_fee_withdrawn, 0); + assert_eq!(market.amm.fee_pool.scaled_balance(), 50000000000000000); // under FEE_POOL_TO_REVENUE_POOL_THRESHOLD + assert_eq!(market.pnl_pool.scaled_balance(), 50000000000000000); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 0); + assert_eq!(market.amm.total_fee_withdrawn(), 0); - assert!(market.amm.fee_pool.scaled_balance == prev_fee_pool); - assert_eq!(market.pnl_pool.scaled_balance, prev_pnl_pool); - assert!(spot_market.revenue_pool.scaled_balance == prev_rev_pool); + assert!(market.amm.fee_pool.scaled_balance() == prev_fee_pool); + assert_eq!(market.pnl_pool.scaled_balance(), prev_pnl_pool); + assert!(spot_market.revenue_pool.scaled_balance() == prev_rev_pool); // add FEE_POOL_TO_REVENUE_POOL_THRESHOLD let prev_fee_pool_2 = (FEE_POOL_TO_REVENUE_POOL_THRESHOLD + 50 * QUOTE_PRECISION) * SPOT_BALANCE_PRECISION; - market.amm.fee_pool.scaled_balance = prev_fee_pool_2; + market.amm.fee_pool.set_scaled_balance(prev_fee_pool_2); update_pool_balances(&mut market, &mut spot_market, &spot_position, 0, now).unwrap(); - assert_eq!(market.pnl_pool.scaled_balance, 50000000000000000); - assert_eq!(market.amm.total_fee_withdrawn, 5000000); - assert_eq!(spot_market.revenue_pool.scaled_balance, 5000000000000000); - assert_eq!(market.amm.fee_pool.scaled_balance, 295000000000000000); // > FEE_POOL_TO_REVENUE_POOL_THRESHOLD + assert_eq!(market.pnl_pool.scaled_balance(), 50000000000000000); + assert_eq!(market.amm.total_fee_withdrawn(), 5000000); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 5000000000000000); + assert_eq!(market.amm.fee_pool.scaled_balance(), 295000000000000000); // > FEE_POOL_TO_REVENUE_POOL_THRESHOLD - assert!(market.amm.fee_pool.scaled_balance < prev_fee_pool_2); - assert_eq!(market.pnl_pool.scaled_balance, prev_pnl_pool); - assert!(spot_market.revenue_pool.scaled_balance > prev_rev_pool); + assert!(market.amm.fee_pool.scaled_balance() < prev_fee_pool_2); + assert_eq!(market.pnl_pool.scaled_balance(), prev_pnl_pool); + assert!(spot_market.revenue_pool.scaled_balance() > prev_rev_pool); market.insurance_claim.quote_max_insurance = 1; // add min insurance update_pool_balances(&mut market, &mut spot_market, &spot_position, 0, now).unwrap(); - assert_eq!(market.amm.total_fee_withdrawn, 5000001); - assert_eq!(spot_market.revenue_pool.scaled_balance, 5000001000000000); + assert_eq!(market.amm.total_fee_withdrawn(), 5000001); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 5000001000000000); market.insurance_claim.quote_max_insurance = 100000000; // add lots of insurance update_pool_balances(&mut market, &mut spot_market, &spot_position, 0, now).unwrap(); - assert_eq!(market.amm.total_fee_withdrawn, 6000000); - assert_eq!(spot_market.revenue_pool.scaled_balance, 6000000000000000); + assert_eq!(market.amm.total_fee_withdrawn(), 6000000); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 6000000000000000); } #[test] fn update_pool_balances_fee_to_revenue_low_amm_revenue_test() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 5122950819670000, - quote_asset_reserve: 488 * AMM_RESERVE_PRECISION, - sqrt_k: 500 * AMM_RESERVE_PRECISION, - peg_multiplier: 50000, - base_asset_amount_with_amm: -122950819670000, - - total_exchange_fee: 10 * QUOTE_PRECISION, - total_fee: 10 * QUOTE_PRECISION as i128, - total_mm_fee: 990 * QUOTE_PRECISION as i128, - total_fee_minus_distributions: 1000 * QUOTE_PRECISION as i128, - total_liquidation_fee: QUOTE_PRECISION, + base_asset_reserve: 5122950819670000.into(), + quote_asset_reserve: (488 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (500 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 50000.into(), + base_asset_amount_with_amm: (-122950819670000).into(), + + total_exchange_fee: (10 * QUOTE_PRECISION).into(), + total_fee: (10 * QUOTE_PRECISION as i128).into(), + total_mm_fee: (990 * QUOTE_PRECISION as i128).into(), + total_fee_minus_distributions: (1000 * QUOTE_PRECISION as i128).into(), + total_liquidation_fee: QUOTE_PRECISION.into(), net_revenue_since_last_funding: QUOTE_PRECISION as i64, curve_update_intensity: 100, fee_pool: PoolBalance { - scaled_balance: 50 * QUOTE_PRECISION * SPOT_BALANCE_PRECISION, + scaled_balance: (50 * QUOTE_PRECISION * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, ..AMM::default() }, pnl_pool: PoolBalance { - scaled_balance: 50 * QUOTE_PRECISION * SPOT_BALANCE_PRECISION, + scaled_balance: (50 * QUOTE_PRECISION * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -641,18 +652,18 @@ fn update_pool_balances_fee_to_revenue_low_amm_revenue_test() { let now = 33928058; let mut spot_market = SpotMarket { - deposit_balance: 100 * QUOTE_PRECISION * SPOT_BALANCE_PRECISION, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + deposit_balance: (100 * QUOTE_PRECISION * SPOT_BALANCE_PRECISION).into(), + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), revenue_pool: PoolBalance::default(), ..SpotMarket::default() }; - let prev_fee_pool = market.amm.fee_pool.scaled_balance; - let prev_pnl_pool = market.amm.fee_pool.scaled_balance; - let prev_rev_pool = spot_market.revenue_pool.scaled_balance; + let prev_fee_pool = market.amm.fee_pool.scaled_balance(); + let prev_pnl_pool = market.amm.fee_pool.scaled_balance(); + let prev_rev_pool = spot_market.revenue_pool.scaled_balance(); - assert_eq!(market.amm.total_fee_withdrawn, 0); + assert_eq!(market.amm.total_fee_withdrawn(), 0); assert_eq!( get_token_amount( @@ -666,7 +677,7 @@ fn update_pool_balances_fee_to_revenue_low_amm_revenue_test() { assert_eq!( get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), &spot_market, &SpotBalanceType::Deposit ) @@ -677,71 +688,71 @@ fn update_pool_balances_fee_to_revenue_low_amm_revenue_test() { let spot_position = SpotPosition::default(); update_pool_balances(&mut market, &mut spot_market, &spot_position, 0, now).unwrap(); - assert_eq!(market.amm.fee_pool.scaled_balance, 50000000000000000); // under FEE_POOL_TO_REVENUE_POOL_THRESHOLD - assert_eq!(market.pnl_pool.scaled_balance, 50000000000000000); - assert_eq!(spot_market.revenue_pool.scaled_balance, 0); - assert_eq!(market.amm.total_fee_withdrawn, 0); + assert_eq!(market.amm.fee_pool.scaled_balance(), 50000000000000000); // under FEE_POOL_TO_REVENUE_POOL_THRESHOLD + assert_eq!(market.pnl_pool.scaled_balance(), 50000000000000000); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 0); + assert_eq!(market.amm.total_fee_withdrawn(), 0); - assert!(market.amm.fee_pool.scaled_balance == prev_fee_pool); - assert_eq!(market.pnl_pool.scaled_balance, prev_pnl_pool); - assert!(spot_market.revenue_pool.scaled_balance == prev_rev_pool); + assert!(market.amm.fee_pool.scaled_balance() == prev_fee_pool); + assert_eq!(market.pnl_pool.scaled_balance(), prev_pnl_pool); + assert!(spot_market.revenue_pool.scaled_balance() == prev_rev_pool); // add FEE_POOL_TO_REVENUE_POOL_THRESHOLD let prev_fee_pool_2 = (FEE_POOL_TO_REVENUE_POOL_THRESHOLD + 50 * QUOTE_PRECISION) * SPOT_BALANCE_PRECISION; - market.amm.fee_pool.scaled_balance = prev_fee_pool_2; + market.amm.fee_pool.set_scaled_balance(prev_fee_pool_2); update_pool_balances(&mut market, &mut spot_market, &spot_position, 0, now).unwrap(); - assert_eq!(market.pnl_pool.scaled_balance, 50000000000000000); - assert_eq!(market.amm.total_fee_withdrawn, 1000000); - assert_eq!(spot_market.revenue_pool.scaled_balance, 1000000000000000); - assert_eq!(market.amm.fee_pool.scaled_balance, 299000000000000000); // > FEE_POOL_TO_REVENUE_POOL_THRESHOLD + assert_eq!(market.pnl_pool.scaled_balance(), 50000000000000000); + assert_eq!(market.amm.total_fee_withdrawn(), 1000000); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 1000000000000000); + assert_eq!(market.amm.fee_pool.scaled_balance(), 299000000000000000); // > FEE_POOL_TO_REVENUE_POOL_THRESHOLD - assert!(market.amm.fee_pool.scaled_balance < prev_fee_pool_2); - assert_eq!(market.pnl_pool.scaled_balance, prev_pnl_pool); - assert!(spot_market.revenue_pool.scaled_balance > prev_rev_pool); + assert!(market.amm.fee_pool.scaled_balance() < prev_fee_pool_2); + assert_eq!(market.pnl_pool.scaled_balance(), prev_pnl_pool); + assert!(spot_market.revenue_pool.scaled_balance() > prev_rev_pool); market.insurance_claim.quote_max_insurance = 1; // add min insurance market.amm.net_revenue_since_last_funding = 1; update_pool_balances(&mut market, &mut spot_market, &spot_position, 0, now).unwrap(); - assert_eq!(market.amm.total_fee_withdrawn, 1000001); - assert_eq!(spot_market.revenue_pool.scaled_balance, 1000001000000000); + assert_eq!(market.amm.total_fee_withdrawn(), 1000001); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 1000001000000000); market.insurance_claim.quote_max_insurance = 100000000; // add lots of insurance market.amm.net_revenue_since_last_funding = 100000000; update_pool_balances(&mut market, &mut spot_market, &spot_position, 0, now).unwrap(); - assert_eq!(market.amm.total_fee_withdrawn, 6000000); - assert_eq!(spot_market.revenue_pool.scaled_balance, 6000000000000000); + assert_eq!(market.amm.total_fee_withdrawn(), 6000000); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 6000000000000000); } #[test] fn update_pool_balances_revenue_to_fee_test() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 5122950819670000, - quote_asset_reserve: 488 * AMM_RESERVE_PRECISION, - sqrt_k: 500 * AMM_RESERVE_PRECISION, - peg_multiplier: 50000, - base_asset_amount_with_amm: -122950819670000, + base_asset_reserve: 5122950819670000.into(), + quote_asset_reserve: (488 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (500 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 50000.into(), + base_asset_amount_with_amm: (-122950819670000).into(), - total_exchange_fee: 10 * QUOTE_PRECISION, - total_fee: 10 * QUOTE_PRECISION as i128, - total_mm_fee: 990 * QUOTE_PRECISION as i128, - total_fee_minus_distributions: -(10000 * QUOTE_PRECISION as i128), + total_exchange_fee: (10 * QUOTE_PRECISION).into(), + total_fee: (10 * QUOTE_PRECISION as i128).into(), + total_mm_fee: (990 * QUOTE_PRECISION as i128).into(), + total_fee_minus_distributions: (-(10000 * QUOTE_PRECISION as i128)).into(), curve_update_intensity: 100, fee_pool: PoolBalance { - scaled_balance: 50 * SPOT_BALANCE_PRECISION, + scaled_balance: (50 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, ..AMM::default() }, pnl_pool: PoolBalance { - scaled_balance: 50 * SPOT_BALANCE_PRECISION, + scaled_balance: (50 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -750,12 +761,12 @@ fn update_pool_balances_revenue_to_fee_test() { let mut now = 33928058; let mut spot_market = SpotMarket { - deposit_balance: 200 * SPOT_BALANCE_PRECISION, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), revenue_pool: PoolBalance { market_index: 0, - scaled_balance: 100 * SPOT_BALANCE_PRECISION, + scaled_balance: (100 * SPOT_BALANCE_PRECISION).into(), ..PoolBalance::default() }, decimals: 6, @@ -763,12 +774,12 @@ fn update_pool_balances_revenue_to_fee_test() { }; let spot_position = SpotPosition::default(); - let prev_fee_pool = market.amm.fee_pool.scaled_balance; - let prev_pnl_pool = market.amm.fee_pool.scaled_balance; - let prev_rev_pool = spot_market.revenue_pool.scaled_balance; - let prev_tfmd = market.amm.total_fee_minus_distributions; + let prev_fee_pool = market.amm.fee_pool.scaled_balance(); + let prev_pnl_pool = market.amm.fee_pool.scaled_balance(); + let prev_rev_pool = spot_market.revenue_pool.scaled_balance(); + let prev_tfmd = market.amm.total_fee_minus_distributions(); - assert_eq!(market.amm.total_fee_withdrawn, 0); + assert_eq!(market.amm.total_fee_withdrawn(), 0); assert_eq!(spot_market.insurance_fund.revenue_settle_period, 0); spot_market.insurance_fund.revenue_settle_period = 0; @@ -776,11 +787,13 @@ fn update_pool_balances_revenue_to_fee_test() { assert_eq!(res, 0); spot_market.insurance_fund.revenue_settle_period = 1; - spot_market.revenue_pool.scaled_balance = 0; + spot_market.revenue_pool.set_scaled_balance(0); let res = settle_revenue_to_insurance_fund(200000000, 0, &mut spot_market, now + 1, false).unwrap(); assert_eq!(res, 0); - spot_market.revenue_pool.scaled_balance = 100 * SPOT_BALANCE_PRECISION; + spot_market + .revenue_pool + .set_scaled_balance(100 * SPOT_BALANCE_PRECISION); now += 2; assert_eq!( @@ -795,7 +808,7 @@ fn update_pool_balances_revenue_to_fee_test() { assert_eq!( get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), &spot_market, &SpotBalanceType::Deposit ) @@ -803,51 +816,57 @@ fn update_pool_balances_revenue_to_fee_test() { 200 * QUOTE_PRECISION ); assert_eq!( - spot_market.revenue_pool.scaled_balance, + spot_market.revenue_pool.scaled_balance(), 100 * SPOT_BALANCE_PRECISION ); update_pool_balances(&mut market, &mut spot_market, &spot_position, 0, now).unwrap(); assert_eq!( - market.amm.fee_pool.scaled_balance, + market.amm.fee_pool.scaled_balance(), 5 * SPOT_BALANCE_PRECISION ); - assert_eq!(market.pnl_pool.scaled_balance, 95 * SPOT_BALANCE_PRECISION); assert_eq!( - spot_market.revenue_pool.scaled_balance, + market.pnl_pool.scaled_balance(), + 95 * SPOT_BALANCE_PRECISION + ); + assert_eq!( + spot_market.revenue_pool.scaled_balance(), 100 * SPOT_BALANCE_PRECISION ); - assert_eq!(market.amm.total_fee_withdrawn, 0); - assert_eq!(market.amm.total_fee_minus_distributions, prev_tfmd); + assert_eq!(market.amm.total_fee_withdrawn(), 0); + assert_eq!(market.amm.total_fee_minus_distributions(), prev_tfmd); - assert!(market.amm.fee_pool.scaled_balance < prev_fee_pool); - assert_eq!(market.pnl_pool.scaled_balance > prev_pnl_pool, true); + assert!(market.amm.fee_pool.scaled_balance() < prev_fee_pool); + assert_eq!(market.pnl_pool.scaled_balance() > prev_pnl_pool, true); assert_eq!( - spot_market.revenue_pool.scaled_balance == prev_rev_pool, + spot_market.revenue_pool.scaled_balance() == prev_rev_pool, true ); assert_eq!(market.insurance_claim.revenue_withdraw_since_last_settle, 0); assert_eq!(market.insurance_claim.last_revenue_withdraw_ts, 0); market.insurance_claim.max_revenue_withdraw_per_period = 100000000 * 2; - assert_eq!(spot_market.deposit_balance, 200 * SPOT_BALANCE_PRECISION); + assert_eq!(spot_market.deposit_balance(), 200 * SPOT_BALANCE_PRECISION); assert_eq!( - spot_market.revenue_pool.scaled_balance, + spot_market.revenue_pool.scaled_balance(), 100 * SPOT_BALANCE_PRECISION ); - assert_eq!(market.amm.total_fee_minus_distributions, -10000000000); + assert_eq!(market.amm.total_fee_minus_distributions(), -10000000000); update_pool_balances(&mut market, &mut spot_market, &spot_position, 0, now).unwrap(); assert_eq!( - market.amm.fee_pool.scaled_balance, + market.amm.fee_pool.scaled_balance(), 105 * SPOT_BALANCE_PRECISION ); - assert_eq!(market.pnl_pool.scaled_balance, 95 * SPOT_BALANCE_PRECISION); - assert_eq!(spot_market.revenue_pool.scaled_balance, 0); - assert_eq!(market.amm.total_fee_withdrawn, 0); - assert_eq!(market.amm.total_fee_minus_distributions, -9900000000); + assert_eq!( + market.pnl_pool.scaled_balance(), + 95 * SPOT_BALANCE_PRECISION + ); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 0); + assert_eq!(market.amm.total_fee_withdrawn(), 0); + assert_eq!(market.amm.total_fee_minus_distributions(), -9900000000); assert_eq!( market.insurance_claim.revenue_withdraw_since_last_settle, 100000000 @@ -855,7 +874,7 @@ fn update_pool_balances_revenue_to_fee_test() { assert_eq!(market.insurance_claim.last_revenue_withdraw_ts, now); let spot_market_vault_amount = get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), &spot_market, &SpotBalanceType::Deposit, ) @@ -865,50 +884,61 @@ fn update_pool_balances_revenue_to_fee_test() { // calling multiple times doesnt effect other than fee pool -> pnl pool update_pool_balances(&mut market, &mut spot_market, &spot_position, 0, now).unwrap(); assert_eq!( - market.amm.fee_pool.scaled_balance, + market.amm.fee_pool.scaled_balance(), 5 * SPOT_BALANCE_PRECISION ); - assert_eq!(market.pnl_pool.scaled_balance, 195 * SPOT_BALANCE_PRECISION); - assert_eq!(market.amm.total_fee_minus_distributions, -9900000000); - assert_eq!(market.amm.total_fee_withdrawn, 0); - assert_eq!(spot_market.revenue_pool.scaled_balance, 0); + assert_eq!( + market.pnl_pool.scaled_balance(), + 195 * SPOT_BALANCE_PRECISION + ); + assert_eq!(market.amm.total_fee_minus_distributions(), -9900000000); + assert_eq!(market.amm.total_fee_withdrawn(), 0); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 0); update_pool_balances(&mut market, &mut spot_market, &spot_position, 0, now).unwrap(); assert_eq!( - market.amm.fee_pool.scaled_balance, + market.amm.fee_pool.scaled_balance(), 5 * SPOT_BALANCE_PRECISION ); - assert_eq!(market.pnl_pool.scaled_balance, 195 * SPOT_BALANCE_PRECISION); - assert_eq!(market.amm.total_fee_minus_distributions, -9900000000); - assert_eq!(market.amm.total_fee_withdrawn, 0); - assert_eq!(spot_market.revenue_pool.scaled_balance, 0); + assert_eq!( + market.pnl_pool.scaled_balance(), + 195 * SPOT_BALANCE_PRECISION + ); + assert_eq!(market.amm.total_fee_minus_distributions(), -9900000000); + assert_eq!(market.amm.total_fee_withdrawn(), 0); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 0); // add deposits and revenue to pool - assert_eq!(spot_market.deposit_balance, 200 * SPOT_BALANCE_PRECISION); - spot_market.revenue_pool.scaled_balance = 9900000001000; + assert_eq!(spot_market.deposit_balance(), 200 * SPOT_BALANCE_PRECISION); + spot_market.revenue_pool.set_scaled_balance(9900000001000); let spot_market_backup = spot_market; let market_backup = market; assert!(update_pool_balances(&mut market, &mut spot_market, &spot_position, 0, now).is_err()); // assert is_err if any way has revenue pool above deposit balances spot_market = spot_market_backup; market = market_backup; - spot_market.deposit_balance += 9900000001000; + spot_market.set_deposit_balance( + spot_market + .deposit_balance() + .safe_add(9900000001000) + .unwrap(), + ); let spot_market_vault_amount = get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), &spot_market, &SpotBalanceType::Deposit, ) .unwrap() as u64; - assert_eq!(spot_market.deposit_balance, 10100000001000); + assert_eq!(spot_market.deposit_balance(), 10100000001000); assert_eq!(spot_market_vault_amount, 10100000001); update_pool_balances(&mut market, &mut spot_market, &spot_position, 0, now).unwrap(); - assert_eq!(spot_market.deposit_balance, 10100000001000); - assert_eq!(spot_market.revenue_pool.scaled_balance, 9800000001000); - assert_eq!(market.amm.fee_pool.scaled_balance, 105000000000); - assert_eq!(market.pnl_pool.scaled_balance, 195000000000); - assert_eq!(market.amm.total_fee_minus_distributions, -9800000000); - assert_eq!(market.amm.total_fee_withdrawn, 0); + assert_eq!(spot_market.deposit_balance(), 10100000001000); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 9800000001000); + assert_eq!(market.amm.fee_pool.scaled_balance(), 105000000000); + assert_eq!(market.pnl_pool.scaled_balance(), 195000000000); + assert_eq!(market.amm.total_fee_minus_distributions(), -9800000000); + assert_eq!(market.amm.total_fee_withdrawn(), 0); assert_eq!( market.insurance_claim.revenue_withdraw_since_last_settle, market.insurance_claim.max_revenue_withdraw_per_period as i64 @@ -917,11 +947,11 @@ fn update_pool_balances_revenue_to_fee_test() { // calling again only does fee -> pnl pool update_pool_balances(&mut market, &mut spot_market, &spot_position, 0, now).unwrap(); - assert_eq!(market.amm.fee_pool.scaled_balance, 5000000000); - assert_eq!(market.pnl_pool.scaled_balance, 295000000000); - assert_eq!(market.amm.total_fee_minus_distributions, -9800000000); - assert_eq!(market.amm.total_fee_withdrawn, 0); - assert_eq!(spot_market.revenue_pool.scaled_balance, 9800000001000); + assert_eq!(market.amm.fee_pool.scaled_balance(), 5000000000); + assert_eq!(market.pnl_pool.scaled_balance(), 295000000000); + assert_eq!(market.amm.total_fee_minus_distributions(), -9800000000); + assert_eq!(market.amm.total_fee_withdrawn(), 0); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 9800000001000); assert_eq!( market.insurance_claim.revenue_withdraw_since_last_settle, market.insurance_claim.max_revenue_withdraw_per_period as i64 @@ -930,11 +960,11 @@ fn update_pool_balances_revenue_to_fee_test() { // calling again does nothing update_pool_balances(&mut market, &mut spot_market, &spot_position, 0, now).unwrap(); - assert_eq!(market.amm.fee_pool.scaled_balance, 5000000000); - assert_eq!(market.pnl_pool.scaled_balance, 295000000000); - assert_eq!(market.amm.total_fee_minus_distributions, -9800000000); - assert_eq!(market.amm.total_fee_withdrawn, 0); - assert_eq!(spot_market.revenue_pool.scaled_balance, 9800000001000); + assert_eq!(market.amm.fee_pool.scaled_balance(), 5000000000); + assert_eq!(market.pnl_pool.scaled_balance(), 295000000000); + assert_eq!(market.amm.total_fee_minus_distributions(), -9800000000); + assert_eq!(market.amm.total_fee_withdrawn(), 0); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 9800000001000); assert_eq!( market.insurance_claim.revenue_withdraw_since_last_settle, market.insurance_claim.max_revenue_withdraw_per_period as i64 @@ -943,7 +973,7 @@ fn update_pool_balances_revenue_to_fee_test() { // do a revenue settlement to allow up to max again assert_eq!(spot_market.insurance_fund.last_revenue_settle_ts, 33928059); - assert_eq!(spot_market.deposit_balance, 10100000001000); + assert_eq!(spot_market.deposit_balance(), 10100000001000); spot_market.insurance_fund.total_factor = 1; spot_market.insurance_fund.revenue_settle_period = 1; @@ -958,24 +988,24 @@ fn update_pool_balances_revenue_to_fee_test() { assert_eq!(res, 9800000001); let spot_market_vault_amount = get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), &spot_market, &SpotBalanceType::Deposit, ) .unwrap() as u64; - assert_eq!(spot_market.deposit_balance, 300000000000); // 100000000 was added to market fee/pnl pool - assert_eq!(spot_market.borrow_balance, 0); + assert_eq!(spot_market.deposit_balance(), 300000000000); // 100000000 was added to market fee/pnl pool + assert_eq!(spot_market.borrow_balance(), 0); assert_eq!(spot_market_vault_amount, 300000000); - assert_eq!(spot_market.revenue_pool.scaled_balance, 0); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 0); assert_eq!( spot_market.insurance_fund.last_revenue_settle_ts, now + 3600 ); // add deposits and revenue to pool - spot_market.revenue_pool.scaled_balance = 9800000001000; + spot_market.revenue_pool.set_scaled_balance(9800000001000); let market_backup = market; let spot_market_backup = spot_market; assert!( @@ -983,13 +1013,18 @@ fn update_pool_balances_revenue_to_fee_test() { ); // assert is_err if any way has revenue pool above deposit balances market = market_backup; spot_market = spot_market_backup; - spot_market.deposit_balance += 9800000000001; + spot_market.set_deposit_balance( + spot_market + .deposit_balance() + .safe_add(9800000000001) + .unwrap(), + ); - assert_eq!(market.amm.fee_pool.scaled_balance, 5000000000); - assert_eq!(market.pnl_pool.scaled_balance, 295000000000); - assert_eq!(market.amm.total_fee_minus_distributions, -9800000000); - assert_eq!(market.amm.total_fee_withdrawn, 0); - assert_eq!(spot_market.revenue_pool.scaled_balance, 9800000001000); + assert_eq!(market.amm.fee_pool.scaled_balance(), 5000000000); + assert_eq!(market.pnl_pool.scaled_balance(), 295000000000); + assert_eq!(market.amm.total_fee_minus_distributions(), -9800000000); + assert_eq!(market.amm.total_fee_withdrawn(), 0); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 9800000001000); assert_eq!(market.insurance_claim.last_revenue_withdraw_ts, 33928060); assert_eq!( spot_market.insurance_fund.last_revenue_settle_ts, @@ -1001,11 +1036,11 @@ fn update_pool_balances_revenue_to_fee_test() { assert_eq!(market.insurance_claim.last_revenue_withdraw_ts, 33931660); assert_eq!(spot_market.insurance_fund.last_revenue_settle_ts, 33931660); - assert_eq!(market.amm.fee_pool.scaled_balance, 205000000000); - assert_eq!(market.pnl_pool.scaled_balance, 295000000000); - assert_eq!(market.amm.total_fee_minus_distributions, -9600000000); - assert_eq!(market.amm.total_fee_withdrawn, 0); - assert_eq!(spot_market.revenue_pool.scaled_balance, 9600000001000); + assert_eq!(market.amm.fee_pool.scaled_balance(), 205000000000); + assert_eq!(market.pnl_pool.scaled_balance(), 295000000000); + assert_eq!(market.amm.total_fee_minus_distributions(), -9600000000); + assert_eq!(market.amm.total_fee_withdrawn(), 0); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 9600000001000); assert_eq!( market.insurance_claim.revenue_withdraw_since_last_settle, market.insurance_claim.max_revenue_withdraw_per_period as i64 @@ -1016,26 +1051,26 @@ fn update_pool_balances_revenue_to_fee_test() { fn update_pool_balances_revenue_to_fee_devnet_state_test() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 916769960813655, - quote_asset_reserve: 932609131198775, - sqrt_k: 924655631391254, - peg_multiplier: 20242531, - base_asset_amount_with_amm: 7563264495267, - - quote_asset_amount: -90559143969, - - total_exchange_fee: 18223810834, - total_fee: 130757047337, - total_mm_fee: 112696236155, - total_fee_minus_distributions: 338762376993, - total_fee_withdrawn: 161959731500, - total_liquidation_fee: 152847899222, - total_social_loss: 74768391959, + base_asset_reserve: 916769960813655.into(), + quote_asset_reserve: 932609131198775.into(), + sqrt_k: 924655631391254.into(), + peg_multiplier: 20242531.into(), + base_asset_amount_with_amm: 7563264495267.into(), + + quote_asset_amount: (-90559143969).into(), + + total_exchange_fee: 18223810834.into(), + total_fee: 130757047337.into(), + total_mm_fee: 112696236155.into(), + total_fee_minus_distributions: 338762376993.into(), + total_fee_withdrawn: 161959731500.into(), + total_liquidation_fee: 152847899222.into(), + total_social_loss: 74768391959.into(), curve_update_intensity: 100, net_revenue_since_last_funding: 229827181, fee_pool: PoolBalance { - scaled_balance: 1821 * SPOT_BALANCE_PRECISION, + scaled_balance: (1821 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -1043,7 +1078,7 @@ fn update_pool_balances_revenue_to_fee_devnet_state_test() { ..AMM::default() }, pnl_pool: PoolBalance { - scaled_balance: 381047 * SPOT_BALANCE_PRECISION, + scaled_balance: (381047 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -1057,12 +1092,12 @@ fn update_pool_balances_revenue_to_fee_devnet_state_test() { let now = 33928058; let mut spot_market = SpotMarket { - deposit_balance: 200 * SPOT_BALANCE_PRECISION, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), revenue_pool: PoolBalance { market_index: 0, - scaled_balance: 100 * SPOT_BALANCE_PRECISION, + scaled_balance: (100 * SPOT_BALANCE_PRECISION).into(), ..PoolBalance::default() }, decimals: 6, @@ -1070,35 +1105,35 @@ fn update_pool_balances_revenue_to_fee_devnet_state_test() { }; let spot_position = SpotPosition::default(); - let prev_fee_pool = market.amm.fee_pool.scaled_balance; - let prev_pnl_pool = market.amm.fee_pool.scaled_balance; - let prev_rev_pool = spot_market.revenue_pool.scaled_balance; - let prev_tfmd = market.amm.total_fee_minus_distributions; + let prev_fee_pool = market.amm.fee_pool.scaled_balance(); + let prev_pnl_pool = market.amm.fee_pool.scaled_balance(); + let prev_rev_pool = spot_market.revenue_pool.scaled_balance(); + let prev_tfmd = market.amm.total_fee_minus_distributions(); update_pool_balances(&mut market, &mut spot_market, &spot_position, 0, now).unwrap(); - assert_eq!(market.amm.fee_pool.scaled_balance, 1821000000000); - assert_eq!(market.pnl_pool.scaled_balance, 381047000000000); + assert_eq!(market.amm.fee_pool.scaled_balance(), 1821000000000); + assert_eq!(market.pnl_pool.scaled_balance(), 381047000000000); assert_eq!( - spot_market.revenue_pool.scaled_balance, + spot_market.revenue_pool.scaled_balance(), 100 * SPOT_BALANCE_PRECISION ); - assert_eq!(market.amm.total_fee_withdrawn, 161959731500); - assert_eq!(market.amm.total_fee_minus_distributions, prev_tfmd); + assert_eq!(market.amm.total_fee_withdrawn(), 161959731500); + assert_eq!(market.amm.total_fee_minus_distributions(), prev_tfmd); - assert_eq!(market.amm.fee_pool.scaled_balance, prev_fee_pool); - assert_eq!(market.pnl_pool.scaled_balance > prev_pnl_pool, true); + assert_eq!(market.amm.fee_pool.scaled_balance(), prev_fee_pool); + assert_eq!(market.pnl_pool.scaled_balance() > prev_pnl_pool, true); assert_eq!( - spot_market.revenue_pool.scaled_balance == prev_rev_pool, + spot_market.revenue_pool.scaled_balance() == prev_rev_pool, true ); assert_eq!(market.insurance_claim.revenue_withdraw_since_last_settle, 0); assert_eq!(market.insurance_claim.last_revenue_withdraw_ts, 0); market.insurance_claim.max_revenue_withdraw_per_period = 100000000 * 2; - assert_eq!(spot_market.deposit_balance, 200 * SPOT_BALANCE_PRECISION); + assert_eq!(spot_market.deposit_balance(), 200 * SPOT_BALANCE_PRECISION); assert_eq!( - spot_market.revenue_pool.scaled_balance, + spot_market.revenue_pool.scaled_balance(), 100 * SPOT_BALANCE_PRECISION ); } @@ -1107,26 +1142,26 @@ fn update_pool_balances_revenue_to_fee_devnet_state_test() { fn update_pool_balances_revenue_to_fee_new_market() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 6165301473685, - quote_asset_reserve: 6165301473685, - sqrt_k: 6165301473685, - peg_multiplier: 324000000, - base_asset_amount_with_amm: 0, - - quote_asset_amount: 0, - - total_exchange_fee: 26000, - total_fee: 26000, - total_mm_fee: 0, - total_fee_minus_distributions: 26000, - total_fee_withdrawn: 0, - total_liquidation_fee: 0, - total_social_loss: 0, + base_asset_reserve: 6165301473685.into(), + quote_asset_reserve: 6165301473685.into(), + sqrt_k: 6165301473685.into(), + peg_multiplier: 324000000.into(), + base_asset_amount_with_amm: 0.into(), + + quote_asset_amount: 0.into(), + + total_exchange_fee: 26000.into(), + total_fee: 26000.into(), + total_mm_fee: 0.into(), + total_fee_minus_distributions: 26000.into(), + total_fee_withdrawn: 0.into(), + total_liquidation_fee: 0.into(), + total_social_loss: 0.into(), curve_update_intensity: 100, net_revenue_since_last_funding: 0, fee_pool: PoolBalance { - scaled_balance: 0, + scaled_balance: 0.into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -1134,7 +1169,7 @@ fn update_pool_balances_revenue_to_fee_new_market() { ..AMM::default() }, pnl_pool: PoolBalance { - scaled_balance: 0, + scaled_balance: 0.into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -1148,12 +1183,12 @@ fn update_pool_balances_revenue_to_fee_new_market() { let now = 33928058; let mut spot_market = SpotMarket { - deposit_balance: 200 * SPOT_BALANCE_PRECISION, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), revenue_pool: PoolBalance { market_index: 0, - scaled_balance: 100 * SPOT_BALANCE_PRECISION, + scaled_balance: (100 * SPOT_BALANCE_PRECISION).into(), ..PoolBalance::default() }, decimals: 6, @@ -1162,22 +1197,22 @@ fn update_pool_balances_revenue_to_fee_new_market() { let spot_position = SpotPosition::default(); // let prev_fee_pool = market.amm.fee_pool.scaled_balance; - let prev_pnl_pool = market.amm.fee_pool.scaled_balance; - let prev_rev_pool = spot_market.revenue_pool.scaled_balance; + let prev_pnl_pool = market.amm.fee_pool.scaled_balance(); + let prev_rev_pool = spot_market.revenue_pool.scaled_balance(); // let prev_tfmd = market.amm.total_fee_minus_distributions; update_pool_balances(&mut market, &mut spot_market, &spot_position, 0, now).unwrap(); - assert_eq!(market.amm.fee_pool.scaled_balance, 50000000000); // $50 + assert_eq!(market.amm.fee_pool.scaled_balance(), 50000000000); // $50 - assert_eq!(market.pnl_pool.scaled_balance, 0); - assert_eq!(spot_market.revenue_pool.scaled_balance, 50000000000); - assert_eq!(market.amm.total_fee_withdrawn, 0); - assert_eq!(market.amm.total_fee_minus_distributions, 50026000); + assert_eq!(market.pnl_pool.scaled_balance(), 0); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 50000000000); + assert_eq!(market.amm.total_fee_withdrawn(), 0); + assert_eq!(market.amm.total_fee_minus_distributions(), 50026000); - assert_eq!(market.pnl_pool.scaled_balance, prev_pnl_pool); + assert_eq!(market.pnl_pool.scaled_balance(), prev_pnl_pool); assert_eq!( - spot_market.revenue_pool.scaled_balance < prev_rev_pool, + spot_market.revenue_pool.scaled_balance() < prev_rev_pool, true ); assert_eq!( @@ -1187,8 +1222,8 @@ fn update_pool_balances_revenue_to_fee_new_market() { assert_eq!(market.insurance_claim.last_revenue_withdraw_ts, 33928058); market.insurance_claim.max_revenue_withdraw_per_period = 100000000 * 2; - assert_eq!(spot_market.deposit_balance, 200 * SPOT_BALANCE_PRECISION); - assert_eq!(spot_market.revenue_pool.scaled_balance, 50000000000); + assert_eq!(spot_market.deposit_balance(), 200 * SPOT_BALANCE_PRECISION); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 50000000000); } mod revenue_pool_transfer_tests { @@ -1204,10 +1239,10 @@ mod revenue_pool_transfer_tests { // Set up input parameters let mut market = PerpMarket { amm: AMM { - total_social_loss: 0, - total_liquidation_fee: 0, + total_social_loss: 0.into(), + total_liquidation_fee: 0.into(), net_revenue_since_last_funding: 0, - total_fee_withdrawn: 0, + total_fee_withdrawn: 0.into(), ..AMM::default() }, insurance_claim: InsuranceClaim { @@ -1220,12 +1255,12 @@ mod revenue_pool_transfer_tests { ..PerpMarket::default() }; let mut spot_market = SpotMarket { - deposit_balance: 20020 * SPOT_BALANCE_PRECISION, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + deposit_balance: (20020 * SPOT_BALANCE_PRECISION).into(), + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), revenue_pool: PoolBalance { market_index: 0, - scaled_balance: 100 * SPOT_BALANCE_PRECISION, + scaled_balance: (100 * SPOT_BALANCE_PRECISION).into(), ..PoolBalance::default() }, decimals: 6, @@ -1294,8 +1329,8 @@ mod revenue_pool_transfer_tests { // Test case 2: When amm_budget_surplus is greater than zero and max_revenue_to_settle is greater than zero, revenue_pool_transfer should be greater than zero market.amm.net_revenue_since_last_funding = 1000 * QUOTE_PRECISION_I64; - market.amm.total_fee_withdrawn = 500 * QUOTE_PRECISION; - market.amm.total_liquidation_fee = 300 * QUOTE_PRECISION; + market.amm.set_total_fee_withdrawn(500 * QUOTE_PRECISION); + market.amm.set_total_liquidation_fee(300 * QUOTE_PRECISION); market.insurance_claim.quote_max_insurance = 100 * QUOTE_PRECISION_U64; market.insurance_claim.quote_settled_insurance = 50 * QUOTE_PRECISION_U64; market.insurance_claim.revenue_withdraw_since_last_settle = 200 * QUOTE_PRECISION_I64; @@ -1338,8 +1373,8 @@ mod revenue_pool_transfer_tests { let amm_fee_pool_token_amount_after = 500 * QUOTE_PRECISION; let terminal_state_surplus = 1000 * QUOTE_PRECISION_I128; market.insurance_claim.max_revenue_withdraw_per_period = 20 * QUOTE_PRECISION_U64; - market.amm.total_social_loss = 100 * QUOTE_PRECISION; - market.amm.total_exchange_fee = 3000 * QUOTE_PRECISION; + market.amm.set_total_social_loss(100 * QUOTE_PRECISION); + market.amm.set_total_exchange_fee(3000 * QUOTE_PRECISION); let result = calculate_revenue_pool_transfer( &market, &spot_market, @@ -1352,7 +1387,7 @@ mod revenue_pool_transfer_tests { let amm_fee_pool_token_amount_after = 500 * QUOTE_PRECISION; let terminal_state_surplus = 1000 * QUOTE_PRECISION_I128; market.insurance_claim.max_revenue_withdraw_per_period = 1000 * QUOTE_PRECISION_U64; - market.amm.total_social_loss = 100 * QUOTE_PRECISION; + market.amm.set_total_social_loss(100 * QUOTE_PRECISION); let result = calculate_revenue_pool_transfer( &market, &spot_market, @@ -1365,8 +1400,8 @@ mod revenue_pool_transfer_tests { let amm_fee_pool_token_amount_after = 500 * QUOTE_PRECISION; let terminal_state_surplus = 1000 * QUOTE_PRECISION_I128; market.insurance_claim.max_revenue_withdraw_per_period = 1000 * QUOTE_PRECISION_U64; - market.amm.total_social_loss = 100 * QUOTE_PRECISION; - market.amm.total_liquidation_fee = 800 * QUOTE_PRECISION; + market.amm.set_total_social_loss(100 * QUOTE_PRECISION); + market.amm.set_total_liquidation_fee(800 * QUOTE_PRECISION); let result = calculate_revenue_pool_transfer( &market, &spot_market, @@ -1379,8 +1414,8 @@ mod revenue_pool_transfer_tests { let amm_fee_pool_token_amount_after = 500 * QUOTE_PRECISION; let terminal_state_surplus = 1000 * QUOTE_PRECISION_I128; market.insurance_claim.max_revenue_withdraw_per_period = 1000 * QUOTE_PRECISION_U64; - market.amm.total_social_loss = 100 * QUOTE_PRECISION; - market.amm.total_liquidation_fee = 200 * QUOTE_PRECISION; + market.amm.set_total_social_loss(100 * QUOTE_PRECISION); + market.amm.set_total_liquidation_fee(200 * QUOTE_PRECISION); let result = calculate_revenue_pool_transfer( &market, &spot_market, @@ -1388,7 +1423,7 @@ mod revenue_pool_transfer_tests { terminal_state_surplus, ); let expected_result: i128 = (amm_fee_pool_token_amount_after - - market.amm.total_social_loss + - market.amm.total_social_loss() - FEE_POOL_TO_REVENUE_POOL_THRESHOLD) as i128; assert_eq!(result.unwrap(), expected_result); @@ -1396,7 +1431,7 @@ mod revenue_pool_transfer_tests { let amm_fee_pool_token_amount_after = 500 * QUOTE_PRECISION; let terminal_state_surplus = 1000 * QUOTE_PRECISION_I128; market.insurance_claim.max_revenue_withdraw_per_period = 1000 * QUOTE_PRECISION_U64; - market.amm.total_social_loss = 600 * QUOTE_PRECISION; + market.amm.set_total_social_loss(600 * QUOTE_PRECISION); let result = calculate_revenue_pool_transfer( &market, &spot_market, @@ -1409,7 +1444,7 @@ mod revenue_pool_transfer_tests { let amm_fee_pool_token_amount_after: u128 = 500 * QUOTE_PRECISION; let terminal_state_surplus = 1000 * QUOTE_PRECISION_I128; market.insurance_claim.max_revenue_withdraw_per_period = 40 * QUOTE_PRECISION_U64; - market.amm.total_social_loss = 100 * QUOTE_PRECISION; + market.amm.set_total_social_loss(100 * QUOTE_PRECISION); let result = calculate_revenue_pool_transfer( &market, &spot_market, @@ -1422,7 +1457,7 @@ mod revenue_pool_transfer_tests { let amm_fee_pool_token_amount_after = 500 * QUOTE_PRECISION; let terminal_state_surplus = 1000 * QUOTE_PRECISION_I128; market.insurance_claim.max_revenue_withdraw_per_period = 1000 * QUOTE_PRECISION_U64; - market.amm.total_social_loss = 100 * QUOTE_PRECISION; + market.amm.set_total_social_loss(100 * QUOTE_PRECISION); let result = calculate_revenue_pool_transfer( &market, &spot_market, @@ -1431,13 +1466,15 @@ mod revenue_pool_transfer_tests { ); assert_eq!(result.unwrap(), 150000000); - spot_market.revenue_pool.scaled_balance = 15000 * SPOT_BALANCE_PRECISION; + spot_market + .revenue_pool + .set_scaled_balance(15000 * SPOT_BALANCE_PRECISION); //Test case 11: claim max_revenue_withdraw_per_period let amm_fee_pool_token_amount_after = 500 * QUOTE_PRECISION; let terminal_state_surplus = -1000 * QUOTE_PRECISION_I128; market.insurance_claim.max_revenue_withdraw_per_period = 1000 * QUOTE_PRECISION_U64; - market.amm.total_social_loss = 100 * QUOTE_PRECISION; + market.amm.set_total_social_loss(100 * QUOTE_PRECISION); let result = calculate_revenue_pool_transfer( &market, &spot_market, @@ -1450,7 +1487,7 @@ mod revenue_pool_transfer_tests { let amm_fee_pool_token_amount_after = 500 * QUOTE_PRECISION; let terminal_state_surplus = -1000 * QUOTE_PRECISION_I128; market.insurance_claim.max_revenue_withdraw_per_period = 2000 * QUOTE_PRECISION_U64; - market.amm.total_social_loss = 100 * QUOTE_PRECISION; + market.amm.set_total_social_loss(100 * QUOTE_PRECISION); let result = calculate_revenue_pool_transfer( &market, &spot_market, @@ -1465,20 +1502,20 @@ mod revenue_pool_transfer_tests { // Set up input parameters let mut market = PerpMarket { amm: AMM { - total_social_loss: 0, - total_liquidation_fee: 0, - total_fee_withdrawn: 0, + total_social_loss: 0.into(), + total_liquidation_fee: 0.into(), + total_fee_withdrawn: 0.into(), net_revenue_since_last_funding: 169 * QUOTE_PRECISION_I64, - total_fee_minus_distributions: 1420420420420, - total_exchange_fee: 420420420420, + total_fee_minus_distributions: 1420420420420.into(), + total_exchange_fee: 420420420420.into(), fee_pool: PoolBalance { - scaled_balance: 81000 * SPOT_BALANCE_PRECISION, + scaled_balance: (81000 * SPOT_BALANCE_PRECISION).into(), ..PoolBalance::default() }, ..AMM::default() }, pnl_pool: PoolBalance { - scaled_balance: 10000 * SPOT_BALANCE_PRECISION, + scaled_balance: (10000 * SPOT_BALANCE_PRECISION).into(), ..PoolBalance::default() }, insurance_claim: InsuranceClaim { @@ -1491,13 +1528,13 @@ mod revenue_pool_transfer_tests { ..PerpMarket::default() }; let mut spot_market = SpotMarket { - deposit_balance: 20020 * SPOT_BALANCE_PRECISION, + deposit_balance: (20020 * SPOT_BALANCE_PRECISION).into(), deposit_token_twap: 20020 * QUOTE_PRECISION_U64, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), revenue_pool: PoolBalance { market_index: 0, - scaled_balance: 10000 * SPOT_BALANCE_PRECISION, + scaled_balance: (10000 * SPOT_BALANCE_PRECISION).into(), ..PoolBalance::default() }, insurance_fund: InsuranceFund { @@ -1523,7 +1560,7 @@ mod revenue_pool_transfer_tests { assert_eq!(to_settle_with_user, -100); assert_eq!(market.insurance_claim.last_revenue_withdraw_ts, 100); - assert_eq!(spot_market.revenue_pool.scaled_balance, 10065000000000); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 10065000000000); // revenue pool not yet settled let now = 10000; @@ -1538,7 +1575,7 @@ mod revenue_pool_transfer_tests { assert_eq!(to_settle_with_user, -100); assert_eq!(market.insurance_claim.last_revenue_withdraw_ts, 100); - assert_eq!(spot_market.revenue_pool.scaled_balance, 10065000000000); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 10065000000000); // revenue pool settled but negative revenue for hour spot_market.insurance_fund.last_revenue_settle_ts = 3600 + 100; @@ -1556,7 +1593,7 @@ mod revenue_pool_transfer_tests { assert_eq!(to_settle_with_user, -100); assert_eq!(market.insurance_claim.last_revenue_withdraw_ts, 100); - assert_eq!(spot_market.revenue_pool.scaled_balance, 10065000000000); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 10065000000000); // revenue pool settled and positive revenue for hour spot_market.insurance_fund.last_revenue_settle_ts = 3600 + 100; @@ -1574,7 +1611,7 @@ mod revenue_pool_transfer_tests { assert_eq!(to_settle_with_user, -100); assert_eq!(market.insurance_claim.last_revenue_withdraw_ts, 10000); - assert_eq!(spot_market.revenue_pool.scaled_balance, 10065000169000); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 10065000169000); } #[test] @@ -1582,20 +1619,20 @@ mod revenue_pool_transfer_tests { // Set up input parameters let mut market = PerpMarket { amm: AMM { - total_social_loss: 0, - total_liquidation_fee: 0, - total_fee_withdrawn: 0, + total_social_loss: 0.into(), + total_liquidation_fee: 0.into(), + total_fee_withdrawn: 0.into(), net_revenue_since_last_funding: 169 * QUOTE_PRECISION_I64, - total_fee_minus_distributions: -6969696969, - total_exchange_fee: 420420420420, + total_fee_minus_distributions: (-6969696969).into(), + total_exchange_fee: 420420420420.into(), fee_pool: PoolBalance { - scaled_balance: 81000 * SPOT_BALANCE_PRECISION, + scaled_balance: (81000 * SPOT_BALANCE_PRECISION).into(), ..PoolBalance::default() }, ..AMM::default() }, pnl_pool: PoolBalance { - scaled_balance: 10000 * SPOT_BALANCE_PRECISION, + scaled_balance: (10000 * SPOT_BALANCE_PRECISION).into(), ..PoolBalance::default() }, insurance_claim: InsuranceClaim { @@ -1608,13 +1645,13 @@ mod revenue_pool_transfer_tests { ..PerpMarket::default() }; let mut spot_market = SpotMarket { - deposit_balance: 20020000 * SPOT_BALANCE_PRECISION, + deposit_balance: (20020000 * SPOT_BALANCE_PRECISION).into(), deposit_token_twap: 20020000 * QUOTE_PRECISION_U64, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), revenue_pool: PoolBalance { market_index: 0, - scaled_balance: 10000 * SPOT_BALANCE_PRECISION, + scaled_balance: (10000 * SPOT_BALANCE_PRECISION).into(), ..PoolBalance::default() }, insurance_fund: InsuranceFund { @@ -1640,7 +1677,7 @@ mod revenue_pool_transfer_tests { assert_eq!(to_settle_with_user, -100); assert_eq!(market.insurance_claim.last_revenue_withdraw_ts, 100); - assert_eq!(spot_market.revenue_pool.scaled_balance, 9935000000000); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 9935000000000); // revenue pool not yet settled let now = 10000; @@ -1655,7 +1692,7 @@ mod revenue_pool_transfer_tests { assert_eq!(to_settle_with_user, -100); assert_eq!(market.insurance_claim.last_revenue_withdraw_ts, 100); - assert_eq!(spot_market.revenue_pool.scaled_balance, 9935000000000); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 9935000000000); // revenue pool settled and negative/positive revenue for hour irrelevant for withdraw spot_market.insurance_fund.last_revenue_settle_ts = 3600 + 100; @@ -1673,6 +1710,6 @@ mod revenue_pool_transfer_tests { assert_eq!(to_settle_with_user, -100); assert_eq!(market.insurance_claim.last_revenue_withdraw_ts, 10000); - assert_eq!(spot_market.revenue_pool.scaled_balance, 9870000000000); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 9870000000000); } } diff --git a/programs/drift/src/controller/funding.rs b/programs/drift/src/controller/funding.rs index eaa2dd9833..3293bf6d2b 100644 --- a/programs/drift/src/controller/funding.rs +++ b/programs/drift/src/controller/funding.rs @@ -46,9 +46,9 @@ pub fn settle_funding_payment( let amm: &AMM = &market.amm; let amm_cumulative_funding_rate = if user.perp_positions[position_index].base_asset_amount > 0 { - amm.cumulative_funding_rate_long + amm.cumulative_funding_rate_long() } else { - amm.cumulative_funding_rate_short + amm.cumulative_funding_rate_short() }; if amm_cumulative_funding_rate @@ -72,8 +72,8 @@ pub fn settle_funding_payment( market_index: market_position.market_index, funding_payment: market_funding_payment, //10e13 user_last_cumulative_funding: market_position.last_cumulative_funding_rate, //10e14 - amm_cumulative_funding_long: amm.cumulative_funding_rate_long, //10e14 - amm_cumulative_funding_short: amm.cumulative_funding_rate_short, //10e14 + amm_cumulative_funding_long: amm.cumulative_funding_rate_long(), //10e14 + amm_cumulative_funding_short: amm.cumulative_funding_rate_short(), //10e14 base_asset_amount: market_position.base_asset_amount, //10e13 }); @@ -105,9 +105,9 @@ pub fn settle_funding_payments( let amm_cumulative_funding_rate = if user.perp_positions[position_index].base_asset_amount > 0 { - amm.cumulative_funding_rate_long + amm.cumulative_funding_rate_long() } else { - amm.cumulative_funding_rate_short + amm.cumulative_funding_rate_short() }; if amm_cumulative_funding_rate @@ -131,8 +131,8 @@ pub fn settle_funding_payments( market_index: market_position.market_index, funding_payment: market_funding_payment, //1e6 user_last_cumulative_funding: market_position.last_cumulative_funding_rate, //1e9 - amm_cumulative_funding_long: amm.cumulative_funding_rate_long, //1e9 - amm_cumulative_funding_short: amm.cumulative_funding_rate_short, //1e9 + amm_cumulative_funding_long: amm.cumulative_funding_rate_long(), //1e9 + amm_cumulative_funding_short: amm.cumulative_funding_rate_short(), //1e9 base_asset_amount: market_position.base_asset_amount, //1e9 }); @@ -260,15 +260,19 @@ pub fn update_funding_rate( formulaic_update_k(market, oracle_price_data, funding_imbalance_cost, now)?; } - market.amm.cumulative_funding_rate_long = market - .amm - .cumulative_funding_rate_long - .safe_add(funding_rate_long)?; + market.amm.set_cumulative_funding_rate_long( + market + .amm + .cumulative_funding_rate_long() + .safe_add(funding_rate_long)?, + ); - market.amm.cumulative_funding_rate_short = market - .amm - .cumulative_funding_rate_short - .safe_add(funding_rate_short)?; + market.amm.set_cumulative_funding_rate_short( + market + .amm + .cumulative_funding_rate_short() + .safe_add(funding_rate_short)?, + ); market.amm.last_funding_rate = funding_rate; market.amm.last_funding_oracle_twap = oracle_price_twap; @@ -296,13 +300,13 @@ pub fn update_funding_rate( funding_rate, funding_rate_long, funding_rate_short, - cumulative_funding_rate_long: market.amm.cumulative_funding_rate_long, - cumulative_funding_rate_short: market.amm.cumulative_funding_rate_short, + cumulative_funding_rate_long: market.amm.cumulative_funding_rate_long(), + cumulative_funding_rate_short: market.amm.cumulative_funding_rate_short(), mark_price_twap: mid_price_twap, oracle_price_twap, period_revenue: market.amm.net_revenue_since_last_funding, - base_asset_amount_with_amm: market.amm.base_asset_amount_with_amm, - base_asset_amount_with_unsettled_lp: market.amm.base_asset_amount_with_unsettled_lp, + base_asset_amount_with_amm: market.amm.base_asset_amount_with_amm(), + base_asset_amount_with_unsettled_lp: market.amm.base_asset_amount_with_unsettled_lp(), }); market.amm.net_revenue_since_last_funding = 0; diff --git a/programs/drift/src/controller/insurance.rs b/programs/drift/src/controller/insurance.rs index fa53164733..4268ae7043 100644 --- a/programs/drift/src/controller/insurance.rs +++ b/programs/drift/src/controller/insurance.rs @@ -63,13 +63,13 @@ pub fn update_user_stats_if_stake_amount( let if_stake_amount = if if_stake_amount_delta >= 0 { if_shares_to_vault_amount( insurance_fund_stake.checked_if_shares(spot_market)?, - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.total_shares(), insurance_vault_amount.safe_add(if_stake_amount_delta.unsigned_abs())?, )? } else { if_shares_to_vault_amount( insurance_fund_stake.checked_if_shares(spot_market)?, - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.total_shares(), insurance_vault_amount.safe_sub(if_stake_amount_delta.unsigned_abs())?, )? }; @@ -115,7 +115,7 @@ pub fn add_insurance_fund_stake( admin_deposit: bool, ) -> DriftResult { validate!( - !(insurance_vault_amount == 0 && spot_market.insurance_fund.total_shares != 0), + !(insurance_vault_amount == 0 && spot_market.insurance_fund.total_shares() != 0), ErrorCode::InvalidIFForNewStakes, "Insurance Fund balance should be non-zero for new stakers to enter" )?; @@ -124,12 +124,12 @@ pub fn add_insurance_fund_stake( apply_rebase_to_insurance_fund_stake(insurance_fund_stake, spot_market)?; let if_shares_before = insurance_fund_stake.checked_if_shares(spot_market)?; - let total_if_shares_before = spot_market.insurance_fund.total_shares; - let user_if_shares_before = spot_market.insurance_fund.user_shares; + let total_if_shares_before = spot_market.insurance_fund.total_shares(); + let user_if_shares_before = spot_market.insurance_fund.user_shares(); let n_shares = vault_amount_to_if_shares( amount, - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.total_shares(), insurance_vault_amount, )?; @@ -142,11 +142,19 @@ pub fn add_insurance_fund_stake( insurance_fund_stake.increase_if_shares(n_shares, spot_market)?; - spot_market.insurance_fund.total_shares = - spot_market.insurance_fund.total_shares.safe_add(n_shares)?; + spot_market.insurance_fund.set_total_shares( + spot_market + .insurance_fund + .total_shares() + .safe_add(n_shares)?, + ); - spot_market.insurance_fund.user_shares = - spot_market.insurance_fund.user_shares.safe_add(n_shares)?; + spot_market.insurance_fund.set_user_shares( + spot_market + .insurance_fund + .user_shares() + .safe_add(n_shares)?, + ); update_user_stats_if_stake_amount( amount.cast()?, @@ -174,8 +182,8 @@ pub fn add_insurance_fund_stake( user_if_shares_before, total_if_shares_before, if_shares_after, - total_if_shares_after: spot_market.insurance_fund.total_shares, - user_if_shares_after: spot_market.insurance_fund.user_shares, + total_if_shares_after: spot_market.insurance_fund.total_shares(), + user_if_shares_after: spot_market.insurance_fund.user_shares(), }); Ok(()) @@ -186,31 +194,39 @@ pub fn apply_rebase_to_insurance_fund( spot_market: &mut SpotMarket, ) -> DriftResult { if insurance_fund_vault_balance != 0 - && insurance_fund_vault_balance.cast::()? < spot_market.insurance_fund.total_shares + && insurance_fund_vault_balance.cast::()? < spot_market.insurance_fund.total_shares() { let (expo_diff, rebase_divisor) = calculate_rebase_info( - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.total_shares(), insurance_fund_vault_balance, )?; - spot_market.insurance_fund.total_shares = spot_market - .insurance_fund - .total_shares - .safe_div(rebase_divisor)?; - spot_market.insurance_fund.user_shares = spot_market - .insurance_fund - .user_shares - .safe_div(rebase_divisor)?; - spot_market.insurance_fund.shares_base = spot_market - .insurance_fund - .shares_base - .safe_add(expo_diff.cast::()?)?; + spot_market.insurance_fund.set_total_shares( + spot_market + .insurance_fund + .total_shares() + .safe_div(rebase_divisor)?, + ); + spot_market.insurance_fund.set_user_shares( + spot_market + .insurance_fund + .user_shares() + .safe_div(rebase_divisor)?, + ); + spot_market.insurance_fund.set_shares_base( + spot_market + .insurance_fund + .shares_base() + .safe_add(expo_diff.cast::()?)?, + ); msg!("rebasing insurance fund: expo_diff={}", expo_diff); } - if insurance_fund_vault_balance != 0 && spot_market.insurance_fund.total_shares == 0 { - spot_market.insurance_fund.total_shares = insurance_fund_vault_balance.cast::()?; + if insurance_fund_vault_balance != 0 && spot_market.insurance_fund.total_shares() == 0 { + spot_market + .insurance_fund + .set_total_shares(insurance_fund_vault_balance.cast::()?); } Ok(()) @@ -220,25 +236,25 @@ pub fn apply_rebase_to_insurance_fund_stake( insurance_fund_stake: &mut InsuranceFundStake, spot_market: &mut SpotMarket, ) -> DriftResult { - if spot_market.insurance_fund.shares_base != insurance_fund_stake.if_base { + if spot_market.insurance_fund.shares_base() != insurance_fund_stake.if_base() { validate!( - spot_market.insurance_fund.shares_base > insurance_fund_stake.if_base, + spot_market.insurance_fund.shares_base() > insurance_fund_stake.if_base(), ErrorCode::InvalidIFRebase, "Rebase expo out of bounds" )?; - let expo_diff = (spot_market.insurance_fund.shares_base - insurance_fund_stake.if_base) + let expo_diff = (spot_market.insurance_fund.shares_base() - insurance_fund_stake.if_base()) .cast::()?; let rebase_divisor = 10_u128.pow(expo_diff); msg!( "rebasing insurance fund stake: base: {} -> {} ", - insurance_fund_stake.if_base, - spot_market.insurance_fund.shares_base, + insurance_fund_stake.if_base(), + spot_market.insurance_fund.shares_base(), ); - insurance_fund_stake.if_base = spot_market.insurance_fund.shares_base; + insurance_fund_stake.set_if_base(spot_market.insurance_fund.shares_base()); let old_if_shares = insurance_fund_stake.unchecked_if_shares(); let new_if_shares = old_if_shares.safe_div(rebase_divisor)?; @@ -250,9 +266,11 @@ pub fn apply_rebase_to_insurance_fund_stake( insurance_fund_stake.update_if_shares(new_if_shares, spot_market)?; - insurance_fund_stake.last_withdraw_request_shares = insurance_fund_stake - .last_withdraw_request_shares - .safe_div(rebase_divisor)?; + insurance_fund_stake.set_last_withdraw_request_shares( + insurance_fund_stake + .last_withdraw_request_shares() + .safe_div(rebase_divisor)?, + ); } Ok(()) @@ -267,33 +285,33 @@ pub fn request_remove_insurance_fund_stake( now: i64, ) -> DriftResult { msg!("n_shares {}", n_shares); - insurance_fund_stake.last_withdraw_request_shares = n_shares; + insurance_fund_stake.set_last_withdraw_request_shares(n_shares); apply_rebase_to_insurance_fund(insurance_vault_amount, spot_market)?; apply_rebase_to_insurance_fund_stake(insurance_fund_stake, spot_market)?; let if_shares_before = insurance_fund_stake.checked_if_shares(spot_market)?; - let total_if_shares_before = spot_market.insurance_fund.total_shares; - let user_if_shares_before = spot_market.insurance_fund.user_shares; + let total_if_shares_before = spot_market.insurance_fund.total_shares(); + let user_if_shares_before = spot_market.insurance_fund.user_shares(); validate!( - insurance_fund_stake.last_withdraw_request_shares + insurance_fund_stake.last_withdraw_request_shares() <= insurance_fund_stake.checked_if_shares(spot_market)?, ErrorCode::InvalidInsuranceUnstakeSize, "last_withdraw_request_shares exceeds if_shares {} > {}", - insurance_fund_stake.last_withdraw_request_shares, + insurance_fund_stake.last_withdraw_request_shares(), insurance_fund_stake.checked_if_shares(spot_market)? )?; validate!( - insurance_fund_stake.if_base == spot_market.insurance_fund.shares_base, + insurance_fund_stake.if_base() == spot_market.insurance_fund.shares_base(), ErrorCode::InvalidIFRebase, "if stake base != spot market base" )?; insurance_fund_stake.last_withdraw_request_value = if_shares_to_vault_amount( - insurance_fund_stake.last_withdraw_request_shares, - spot_market.insurance_fund.total_shares, + insurance_fund_stake.last_withdraw_request_shares(), + spot_market.insurance_fund.total_shares(), insurance_vault_amount, )? .min(insurance_vault_amount.saturating_sub(1)); @@ -327,8 +345,8 @@ pub fn request_remove_insurance_fund_stake( user_if_shares_before, total_if_shares_before, if_shares_after, - total_if_shares_after: spot_market.insurance_fund.total_shares, - user_if_shares_after: spot_market.insurance_fund.user_shares, + total_if_shares_after: spot_market.insurance_fund.total_shares(), + user_if_shares_after: spot_market.insurance_fund.user_shares(), }); insurance_fund_stake.last_withdraw_request_ts = now; @@ -347,17 +365,17 @@ pub fn cancel_request_remove_insurance_fund_stake( apply_rebase_to_insurance_fund_stake(insurance_fund_stake, spot_market)?; let if_shares_before = insurance_fund_stake.checked_if_shares(spot_market)?; - let total_if_shares_before = spot_market.insurance_fund.total_shares; - let user_if_shares_before = spot_market.insurance_fund.user_shares; + let total_if_shares_before = spot_market.insurance_fund.total_shares(); + let user_if_shares_before = spot_market.insurance_fund.user_shares(); validate!( - insurance_fund_stake.if_base == spot_market.insurance_fund.shares_base, + insurance_fund_stake.if_base() == spot_market.insurance_fund.shares_base(), ErrorCode::InvalidIFRebase, "if stake base != spot market base" )?; validate!( - insurance_fund_stake.last_withdraw_request_shares != 0, + insurance_fund_stake.last_withdraw_request_shares() != 0, ErrorCode::InvalidIFUnstakeCancel, "No withdraw request in progress" )?; @@ -367,15 +385,19 @@ pub fn cancel_request_remove_insurance_fund_stake( insurance_fund_stake.decrease_if_shares(if_shares_lost, spot_market)?; - spot_market.insurance_fund.total_shares = spot_market - .insurance_fund - .total_shares - .safe_sub(if_shares_lost)?; + spot_market.insurance_fund.set_total_shares( + spot_market + .insurance_fund + .total_shares() + .safe_sub(if_shares_lost)?, + ); - spot_market.insurance_fund.user_shares = spot_market - .insurance_fund - .user_shares - .safe_sub(if_shares_lost)?; + spot_market.insurance_fund.set_user_shares( + spot_market + .insurance_fund + .user_shares() + .safe_sub(if_shares_lost)?, + ); let if_shares_after = insurance_fund_stake.checked_if_shares(spot_market)?; @@ -399,11 +421,11 @@ pub fn cancel_request_remove_insurance_fund_stake( user_if_shares_before, total_if_shares_before, if_shares_after, - total_if_shares_after: spot_market.insurance_fund.total_shares, - user_if_shares_after: spot_market.insurance_fund.user_shares, + total_if_shares_after: spot_market.insurance_fund.total_shares(), + user_if_shares_after: spot_market.insurance_fund.user_shares(), }); - insurance_fund_stake.last_withdraw_request_shares = 0; + insurance_fund_stake.set_last_withdraw_request_shares(0); insurance_fund_stake.last_withdraw_request_value = 0; insurance_fund_stake.last_withdraw_request_ts = now; @@ -429,10 +451,10 @@ pub fn remove_insurance_fund_stake( apply_rebase_to_insurance_fund_stake(insurance_fund_stake, spot_market)?; let if_shares_before = insurance_fund_stake.checked_if_shares(spot_market)?; - let total_if_shares_before = spot_market.insurance_fund.total_shares; - let user_if_shares_before = spot_market.insurance_fund.user_shares; + let total_if_shares_before = spot_market.insurance_fund.total_shares(); + let user_if_shares_before = spot_market.insurance_fund.user_shares(); - let n_shares = insurance_fund_stake.last_withdraw_request_shares; + let n_shares = insurance_fund_stake.last_withdraw_request_shares(); validate!( n_shares > 0, @@ -447,7 +469,7 @@ pub fn remove_insurance_fund_stake( let amount = if_shares_to_vault_amount( n_shares, - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.total_shares(), insurance_vault_amount, )?; @@ -462,14 +484,22 @@ pub fn remove_insurance_fund_stake( .cost_basis .safe_sub(withdraw_amount.cast()?)?; - spot_market.insurance_fund.total_shares = - spot_market.insurance_fund.total_shares.safe_sub(n_shares)?; + spot_market.insurance_fund.set_total_shares( + spot_market + .insurance_fund + .total_shares() + .safe_sub(n_shares)?, + ); - spot_market.insurance_fund.user_shares = - spot_market.insurance_fund.user_shares.safe_sub(n_shares)?; + spot_market.insurance_fund.set_user_shares( + spot_market + .insurance_fund + .user_shares() + .safe_sub(n_shares)?, + ); // reset insurance_fund_stake withdraw request info - insurance_fund_stake.last_withdraw_request_shares = 0; + insurance_fund_stake.set_last_withdraw_request_shares(0); insurance_fund_stake.last_withdraw_request_value = 0; insurance_fund_stake.last_withdraw_request_ts = now; @@ -495,8 +525,8 @@ pub fn remove_insurance_fund_stake( user_if_shares_before, total_if_shares_before, if_shares_after, - total_if_shares_after: spot_market.insurance_fund.total_shares, - user_if_shares_after: spot_market.insurance_fund.user_shares, + total_if_shares_after: spot_market.insurance_fund.total_shares(), + user_if_shares_after: spot_market.insurance_fund.user_shares(), }); Ok(withdraw_amount) @@ -511,8 +541,8 @@ pub fn admin_remove_insurance_fund_stake( ) -> DriftResult { apply_rebase_to_insurance_fund(insurance_vault_amount, spot_market)?; - let total_if_shares_before = spot_market.insurance_fund.total_shares; - let user_if_shares_before = spot_market.insurance_fund.user_shares; + let total_if_shares_before = spot_market.insurance_fund.total_shares(); + let user_if_shares_before = spot_market.insurance_fund.user_shares(); let if_shares_before = total_if_shares_before.safe_sub(user_if_shares_before)?; @@ -526,16 +556,20 @@ pub fn admin_remove_insurance_fund_stake( let withdraw_amount = if_shares_to_vault_amount( n_shares, - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.total_shares(), insurance_vault_amount, )?; - spot_market.insurance_fund.total_shares = - spot_market.insurance_fund.total_shares.safe_sub(n_shares)?; + spot_market.insurance_fund.set_total_shares( + spot_market + .insurance_fund + .total_shares() + .safe_sub(n_shares)?, + ); let if_shares_after = spot_market .insurance_fund - .total_shares + .total_shares() .safe_sub(user_if_shares_before)?; emit!(InsuranceFundStakeRecord { @@ -549,8 +583,8 @@ pub fn admin_remove_insurance_fund_stake( user_if_shares_before, total_if_shares_before, if_shares_after, - total_if_shares_after: spot_market.insurance_fund.total_shares, - user_if_shares_after: spot_market.insurance_fund.user_shares, + total_if_shares_after: spot_market.insurance_fund.total_shares(), + user_if_shares_after: spot_market.insurance_fund.user_shares(), }); Ok(withdraw_amount) @@ -567,8 +601,8 @@ pub fn transfer_protocol_insurance_fund_stake( ) -> DriftResult { apply_rebase_to_insurance_fund(insurance_vault_amount, spot_market)?; - let total_if_shares_before = spot_market.insurance_fund.total_shares; - let user_if_shares_before = spot_market.insurance_fund.user_shares; + let total_if_shares_before = spot_market.insurance_fund.total_shares(); + let user_if_shares_before = spot_market.insurance_fund.user_shares(); let if_shares_before = total_if_shares_before.safe_sub(user_if_shares_before)?; let target_if_shares_before = target_insurance_fund_stake.checked_if_shares(spot_market)?; @@ -580,8 +614,12 @@ pub fn transfer_protocol_insurance_fund_stake( n_shares )?; - spot_market.insurance_fund.user_shares = - spot_market.insurance_fund.user_shares.safe_add(n_shares)?; + spot_market.insurance_fund.set_user_shares( + spot_market + .insurance_fund + .user_shares() + .safe_add(n_shares)?, + ); target_insurance_fund_stake.increase_if_shares(n_shares, spot_market)?; @@ -590,27 +628,27 @@ pub fn transfer_protocol_insurance_fund_stake( if spot_market.market_index == QUOTE_SPOT_MARKET_INDEX { user_stats.if_staked_quote_asset_amount = if_shares_to_vault_amount( target_if_shares_after, - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.total_shares(), insurance_vault_amount, )?; } else if spot_market.market_index == GOV_SPOT_MARKET_INDEX { user_stats.if_staked_gov_token_amount = if_shares_to_vault_amount( target_if_shares_after, - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.total_shares(), insurance_vault_amount, )?; } let withdraw_amount = if_shares_to_vault_amount( n_shares, - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.total_shares(), insurance_vault_amount, )?; - let user_if_shares_after = spot_market.insurance_fund.user_shares; + let user_if_shares_after = spot_market.insurance_fund.user_shares(); let protocol_if_shares_after = spot_market .insurance_fund - .total_shares + .total_shares() .safe_sub(user_if_shares_after)?; emit!(InsuranceFundStakeRecord { @@ -624,8 +662,8 @@ pub fn transfer_protocol_insurance_fund_stake( user_if_shares_before, total_if_shares_before, if_shares_after: protocol_if_shares_after, - total_if_shares_after: spot_market.insurance_fund.total_shares, - user_if_shares_after: spot_market.insurance_fund.user_shares, + total_if_shares_after: spot_market.insurance_fund.total_shares(), + user_if_shares_after: spot_market.insurance_fund.user_shares(), }); emit!(InsuranceFundStakeRecord { @@ -639,8 +677,8 @@ pub fn transfer_protocol_insurance_fund_stake( user_if_shares_before, total_if_shares_before, if_shares_after: target_insurance_fund_stake.checked_if_shares(spot_market)?, - total_if_shares_after: spot_market.insurance_fund.total_shares, - user_if_shares_after: spot_market.insurance_fund.user_shares, + total_if_shares_after: spot_market.insurance_fund.total_shares(), + user_if_shares_after: spot_market.insurance_fund.user_shares(), }); Ok(withdraw_amount) @@ -735,7 +773,7 @@ pub fn settle_revenue_to_insurance_fund( validate_spot_market_vault_amount(spot_market, spot_market_vault_amount)?; let mut token_amount = get_token_amount( - spot_market.revenue_pool.scaled_balance, + spot_market.revenue_pool.scaled_balance(), spot_market, &SpotBalanceType::Deposit, )?; @@ -745,7 +783,7 @@ pub fn settle_revenue_to_insurance_fund( token_amount = depositors_claim.max(0).cast::()?.safe_div(2)?; } - if spot_market.insurance_fund.user_shares > 0 { + if spot_market.insurance_fund.user_shares() > 0 { // only allow MAX_APR_PER_REVENUE_SETTLE_TO_INSURANCE_FUND_VAULT or 1/10th of revenue pool to be settled let max_apr_per_revenue_settle: u128 = if spot_market.market_index == GOV_SPOT_MARKET_INDEX { @@ -795,15 +833,19 @@ pub fn settle_revenue_to_insurance_fund( insurance_fund_token_amount .safe_mul(protocol_if_factor.cast()?)? .safe_div(spot_market.insurance_fund.total_factor.cast()?)?, - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.total_shares(), insurance_vault_amount, )?; - spot_market.insurance_fund.total_shares = - spot_market.insurance_fund.total_shares.safe_add(n_shares)?; + spot_market.insurance_fund.set_total_shares( + spot_market + .insurance_fund + .total_shares() + .safe_add(n_shares)?, + ); } - let total_if_shares_before = spot_market.insurance_fund.total_shares; + let total_if_shares_before = spot_market.insurance_fund.total_shares(); update_revenue_pool_balances( insurance_fund_token_amount.cast::()?, @@ -822,7 +864,7 @@ pub fn settle_revenue_to_insurance_fund( vault_amount_before: spot_market_vault_amount, insurance_vault_amount_before: insurance_vault_amount, total_if_shares_before, - total_if_shares_after: spot_market.insurance_fund.total_shares, + total_if_shares_after: spot_market.insurance_fund.total_shares(), }); insurance_fund_token_amount.cast() @@ -836,14 +878,14 @@ pub fn resolve_perp_pnl_deficit( now: i64, ) -> DriftResult { validate!( - market.amm.total_fee_minus_distributions < 0, + market.amm.total_fee_minus_distributions() < 0, ErrorCode::NoAmmPerpPnlDeficit, "market.amm.total_fee_minus_distributions={} must be negative", - market.amm.total_fee_minus_distributions + market.amm.total_fee_minus_distributions() )?; let pnl_pool_token_amount = get_token_amount( - market.pnl_pool.scaled_balance, + market.pnl_pool.scaled_balance(), spot_market, &SpotBalanceType::Deposit, )?; @@ -866,7 +908,7 @@ pub fn resolve_perp_pnl_deficit( update_spot_market_cumulative_interest(spot_market, None, now)?; - let total_if_shares_before = spot_market.insurance_fund.total_shares; + let total_if_shares_before = spot_market.insurance_fund.total_shares(); let excess_user_pnl_imbalance = if market.unrealized_pnl_max_imbalance > 0 { let net_unsettled_pnl = calculate_net_user_pnl( @@ -931,10 +973,12 @@ pub fn resolve_perp_pnl_deficit( excess_user_pnl_imbalance )?; - market.amm.total_fee_minus_distributions = market - .amm - .total_fee_minus_distributions - .safe_add(insurance_withdraw)?; + market.amm.set_total_fee_minus_distributions( + market + .amm + .total_fee_minus_distributions() + .safe_add(insurance_withdraw)?, + ); market.insurance_claim.revenue_withdraw_since_last_settle = market .insurance_claim @@ -975,7 +1019,7 @@ pub fn resolve_perp_pnl_deficit( vault_amount_before: vault_amount, insurance_vault_amount_before: insurance_vault_amount, total_if_shares_before, - total_if_shares_after: spot_market.insurance_fund.total_shares, + total_if_shares_after: spot_market.insurance_fund.total_shares(), }); insurance_withdraw.cast() @@ -1021,28 +1065,28 @@ pub fn handle_if_end_swap( let out_insurance_fund_vault_amount_before = out_insurance_fund_vault_amount_after.safe_sub(out_amount)?; - let in_if_total_shares_before = in_spot_market.insurance_fund.total_shares; - let out_if_total_shares_before = out_spot_market.insurance_fund.total_shares; - let in_if_user_shares_before = in_spot_market.insurance_fund.user_shares; - let out_if_user_shares_before = out_spot_market.insurance_fund.user_shares; + let in_if_total_shares_before = in_spot_market.insurance_fund.total_shares(); + let out_if_total_shares_before = out_spot_market.insurance_fund.total_shares(); + let in_if_user_shares_before = in_spot_market.insurance_fund.user_shares(); + let out_if_user_shares_before = out_spot_market.insurance_fund.user_shares(); let in_share_price_before = calculate_share_price( - in_spot_market.insurance_fund.total_shares, + in_spot_market.insurance_fund.total_shares(), in_insurance_fund_vault_amount_before, )?; let out_share_price_before = calculate_share_price( - out_spot_market.insurance_fund.total_shares, + out_spot_market.insurance_fund.total_shares(), out_insurance_fund_vault_amount_before, )?; let in_shares = vault_amount_to_if_shares( in_amount, - in_spot_market.insurance_fund.total_shares, + in_spot_market.insurance_fund.total_shares(), in_insurance_fund_vault_amount_before, )?; let out_shares = vault_amount_to_if_shares( out_amount, - out_spot_market.insurance_fund.total_shares, + out_spot_market.insurance_fund.total_shares(), out_insurance_fund_vault_amount_before, )?; @@ -1056,21 +1100,25 @@ pub fn handle_if_end_swap( )?; // increment spot market insurance funds total shares - in_spot_market.insurance_fund.total_shares = in_spot_market - .insurance_fund - .total_shares - .safe_sub(in_shares)?; - out_spot_market.insurance_fund.total_shares = out_spot_market - .insurance_fund - .total_shares - .safe_add(out_shares)?; + in_spot_market.insurance_fund.set_total_shares( + in_spot_market + .insurance_fund + .total_shares() + .safe_sub(in_shares)?, + ); + out_spot_market.insurance_fund.set_total_shares( + out_spot_market + .insurance_fund + .total_shares() + .safe_add(out_shares)?, + ); let in_share_price_after = calculate_share_price( - in_spot_market.insurance_fund.total_shares, + in_spot_market.insurance_fund.total_shares(), in_insurance_fund_vault_amount_after, )?; let out_share_price_after = calculate_share_price( - out_spot_market.insurance_fund.total_shares, + out_spot_market.insurance_fund.total_shares(), out_insurance_fund_vault_amount_after, )?; @@ -1163,10 +1211,10 @@ pub fn handle_if_end_swap( out_if_total_shares_before, in_if_user_shares_before, out_if_user_shares_before, - in_if_total_shares_after: in_spot_market.insurance_fund.total_shares, - out_if_total_shares_after: out_spot_market.insurance_fund.total_shares, - in_if_user_shares_after: in_spot_market.insurance_fund.user_shares, - out_if_user_shares_after: out_spot_market.insurance_fund.user_shares, + in_if_total_shares_after: in_spot_market.insurance_fund.total_shares(), + out_if_total_shares_after: out_spot_market.insurance_fund.total_shares(), + in_if_user_shares_after: in_spot_market.insurance_fund.user_shares(), + out_if_user_shares_after: out_spot_market.insurance_fund.user_shares(), }); Ok(()) @@ -1183,7 +1231,7 @@ pub fn transfer_protocol_if_shares_to_revenue_pool( let shares = vault_amount_to_if_shares( amount, - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.total_shares(), insurance_fund_vault_amount_before, )?; @@ -1205,8 +1253,9 @@ pub fn transfer_protocol_if_shares_to_revenue_pool( if_rebalance_config.max_transfer_amount()? )?; - spot_market.insurance_fund.total_shares = - spot_market.insurance_fund.total_shares.safe_sub(shares)?; + spot_market + .insurance_fund + .set_total_shares(spot_market.insurance_fund.total_shares().safe_sub(shares)?); update_revenue_pool_balances(amount.cast()?, &SpotBalanceType::Deposit, spot_market)?; diff --git a/programs/drift/src/controller/insurance/tests.rs b/programs/drift/src/controller/insurance/tests.rs index c71948c3d3..04150329b0 100644 --- a/programs/drift/src/controller/insurance/tests.rs +++ b/programs/drift/src/controller/insurance/tests.rs @@ -25,8 +25,8 @@ pub fn basic_stake_if_test() { }; let amount = QUOTE_PRECISION as u64; // $1 let mut spot_market = SpotMarket { - deposit_balance: 0, - cumulative_deposit_interest: 1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000, + deposit_balance: 0.into(), + cumulative_deposit_interest: (1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000).into(), insurance_fund: InsuranceFund { unstaking_period: 0, ..InsuranceFund::default() @@ -58,8 +58,8 @@ pub fn basic_stake_if_test() { .is_err()); assert_eq!(if_stake.unchecked_if_shares(), amount as u128); - assert_eq!(spot_market.insurance_fund.total_shares, amount as u128); - assert_eq!(spot_market.insurance_fund.shares_base, 0); + assert_eq!(spot_market.insurance_fund.total_shares(), amount as u128); + assert_eq!(spot_market.insurance_fund.shares_base(), 0); request_remove_insurance_fund_stake( if_stake.unchecked_if_shares(), @@ -71,13 +71,13 @@ pub fn basic_stake_if_test() { ) .unwrap(); assert_eq!( - if_stake.last_withdraw_request_shares, + if_stake.last_withdraw_request_shares(), if_stake.unchecked_if_shares() ); assert_eq!(if_stake.last_withdraw_request_value, if_balance - 1); //rounding in favor assert_eq!(if_stake.unchecked_if_shares(), amount as u128); - assert_eq!(spot_market.insurance_fund.total_shares, amount as u128); - assert_eq!(spot_market.insurance_fund.shares_base, 0); + assert_eq!(spot_market.insurance_fund.total_shares(), amount as u128); + assert_eq!(spot_market.insurance_fund.shares_base(), 0); let amount_returned = (remove_insurance_fund_stake( if_balance, @@ -92,10 +92,10 @@ pub fn basic_stake_if_test() { assert_eq!(if_stake.unchecked_if_shares(), 0); assert_eq!(if_stake.cost_basis, 1); - assert_eq!(if_stake.last_withdraw_request_shares, 0); + assert_eq!(if_stake.last_withdraw_request_shares(), 0); assert_eq!(if_stake.last_withdraw_request_value, 0); - assert_eq!(spot_market.insurance_fund.total_shares, 0); - assert_eq!(spot_market.insurance_fund.shares_base, 0); + assert_eq!(spot_market.insurance_fund.total_shares(), 0); + assert_eq!(spot_market.insurance_fund.shares_base(), 0); assert_eq!(if_balance, 1); add_insurance_fund_stake( @@ -109,9 +109,9 @@ pub fn basic_stake_if_test() { ) .unwrap(); assert_eq!(if_stake.cost_basis, 1234); - assert_eq!(spot_market.insurance_fund.user_shares, 1234); - assert_eq!(spot_market.insurance_fund.total_shares, 1235); // protocol claims the 1 balance - assert_eq!(spot_market.insurance_fund.shares_base, 0); + assert_eq!(spot_market.insurance_fund.user_shares(), 1234); + assert_eq!(spot_market.insurance_fund.total_shares(), 1235); // protocol claims the 1 balance + assert_eq!(spot_market.insurance_fund.shares_base(), 0); } #[test] @@ -124,8 +124,8 @@ pub fn basic_seeded_stake_if_test() { }; let amount = QUOTE_PRECISION as u64; // $1 let mut spot_market = SpotMarket { - deposit_balance: 0, - cumulative_deposit_interest: 1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000, + deposit_balance: 0.into(), + cumulative_deposit_interest: (1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000).into(), insurance_fund: InsuranceFund { unstaking_period: 0, ..InsuranceFund::default() @@ -133,8 +133,8 @@ pub fn basic_seeded_stake_if_test() { ..SpotMarket::default() }; - assert_eq!(spot_market.insurance_fund.total_shares, 0); - assert_eq!(spot_market.insurance_fund.user_shares, 0); + assert_eq!(spot_market.insurance_fund.total_shares(), 0); + assert_eq!(spot_market.insurance_fund.user_shares(), 0); add_insurance_fund_stake( amount, @@ -148,10 +148,10 @@ pub fn basic_seeded_stake_if_test() { .unwrap(); assert_eq!( - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.total_shares(), (1001 * QUOTE_PRECISION) ); // seeded works - assert_eq!(spot_market.insurance_fund.user_shares, QUOTE_PRECISION); + assert_eq!(spot_market.insurance_fund.user_shares(), QUOTE_PRECISION); assert_eq!(if_stake.unchecked_if_shares(), amount as u128); if_balance += amount; @@ -176,7 +176,7 @@ pub fn basic_seeded_stake_if_test() { ) .unwrap(); assert_eq!( - if_stake.last_withdraw_request_shares, + if_stake.last_withdraw_request_shares(), if_stake.unchecked_if_shares() ); assert_eq!(if_stake.last_withdraw_request_value, 1000000); @@ -194,7 +194,7 @@ pub fn basic_seeded_stake_if_test() { assert_eq!(if_stake.unchecked_if_shares(), 0); assert_eq!(if_stake.cost_basis, 0); - assert_eq!(if_stake.last_withdraw_request_shares, 0); + assert_eq!(if_stake.last_withdraw_request_shares(), 0); assert_eq!(if_stake.last_withdraw_request_value, 0); assert_eq!(if_balance, 1000000000); @@ -224,8 +224,8 @@ pub fn large_num_seeded_stake_if_test() { // all funds in revenue pool let mut spot_market = SpotMarket { - deposit_balance: 100 * SPOT_BALANCE_PRECISION, - cumulative_deposit_interest: 1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), + cumulative_deposit_interest: (1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000).into(), insurance_fund: InsuranceFund { unstaking_period: 0, revenue_settle_period: 1, @@ -233,14 +233,14 @@ pub fn large_num_seeded_stake_if_test() { }, revenue_pool: PoolBalance { market_index: 0, - scaled_balance: 100 * SPOT_BALANCE_PRECISION, + scaled_balance: (100 * SPOT_BALANCE_PRECISION).into(), ..PoolBalance::default() }, ..SpotMarket::default() }; - assert_eq!(spot_market.insurance_fund.total_shares, 0); - assert_eq!(spot_market.insurance_fund.user_shares, 0); + assert_eq!(spot_market.insurance_fund.total_shares(), 0); + assert_eq!(spot_market.insurance_fund.user_shares(), 0); add_insurance_fund_stake( amount, @@ -253,8 +253,8 @@ pub fn large_num_seeded_stake_if_test() { ) .unwrap(); - assert_eq!(spot_market.insurance_fund.total_shares, 199000199000001); // seeded works - assert_eq!(spot_market.insurance_fund.user_shares, 199000001); + assert_eq!(spot_market.insurance_fund.total_shares(), 199000199000001); // seeded works + assert_eq!(spot_market.insurance_fund.user_shares(), 199000001); assert_eq!(if_stake.unchecked_if_shares(), amount as u128); if_balance += amount; @@ -269,7 +269,7 @@ pub fn large_num_seeded_stake_if_test() { .is_err()); assert_eq!(if_stake.unchecked_if_shares(), amount as u128); let spot_market_vault_amount = get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), &spot_market, &SpotBalanceType::Deposit, ) @@ -285,9 +285,9 @@ pub fn large_num_seeded_stake_if_test() { ) .unwrap(); assert_eq!(flow, 11); - assert_eq!(spot_market.revenue_pool.scaled_balance, 90099009901); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 90099009901); let spot_market_vault_amount = get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), &spot_market, &SpotBalanceType::Deposit, ) @@ -306,7 +306,7 @@ pub fn large_num_seeded_stake_if_test() { ) .unwrap(); assert_eq!( - if_stake.last_withdraw_request_shares, + if_stake.last_withdraw_request_shares(), if_stake.unchecked_if_shares() ); assert_eq!(if_stake.last_withdraw_request_value, 199000001); @@ -324,13 +324,15 @@ pub fn large_num_seeded_stake_if_test() { assert_eq!(if_stake.unchecked_if_shares(), 0); assert_eq!(if_stake.cost_basis, 0); - assert_eq!(if_stake.last_withdraw_request_shares, 0); + assert_eq!(if_stake.last_withdraw_request_shares(), 0); assert_eq!(if_stake.last_withdraw_request_value, 0); assert_eq!(if_balance, 199000000000011); - assert_eq!(spot_market.insurance_fund.user_shares, 0); - assert_eq!(spot_market.insurance_fund.total_shares, 199000000000000); + assert_eq!(spot_market.insurance_fund.user_shares(), 0); + assert_eq!(spot_market.insurance_fund.total_shares(), 199000000000000); - spot_market.revenue_pool.scaled_balance = 100 * SPOT_BALANCE_PRECISION; + spot_market + .revenue_pool + .set_scaled_balance(100 * SPOT_BALANCE_PRECISION); add_insurance_fund_stake( 199033744205760, @@ -343,7 +345,7 @@ pub fn large_num_seeded_stake_if_test() { ) .unwrap(); assert_eq!(if_stake.cost_basis, 199033744205760); - assert_eq!(spot_market.insurance_fund.user_shares, 199033744205748); + assert_eq!(spot_market.insurance_fund.user_shares(), 199033744205748); add_insurance_fund_stake( 199033744205760, @@ -356,7 +358,7 @@ pub fn large_num_seeded_stake_if_test() { ) .unwrap(); assert_eq!(if_stake.cost_basis, 398067488411520); - assert_eq!(spot_market.insurance_fund.user_shares, 597134982544960); + assert_eq!(spot_market.insurance_fund.user_shares(), 597134982544960); } #[test] @@ -369,8 +371,8 @@ pub fn gains_stake_if_test() { }; let amount = QUOTE_PRECISION as u64; // $1 let mut spot_market = SpotMarket { - deposit_balance: 0, - cumulative_deposit_interest: 1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000, + deposit_balance: 0.into(), + cumulative_deposit_interest: (1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000).into(), insurance_fund: InsuranceFund { unstaking_period: 0, ..InsuranceFund::default() @@ -494,8 +496,8 @@ pub fn losses_stake_if_test() { }; let amount = QUOTE_PRECISION as u64; // $1 let mut spot_market = SpotMarket { - deposit_balance: 0, - cumulative_deposit_interest: 1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000, + deposit_balance: 0.into(), + cumulative_deposit_interest: (1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000).into(), insurance_fund: InsuranceFund { unstaking_period: 0, ..InsuranceFund::default() @@ -622,8 +624,8 @@ pub fn escrow_losses_stake_if_test() { }; let amount = (QUOTE_PRECISION * 100_000) as u64; // $100k let mut spot_market = SpotMarket { - deposit_balance: 0, - cumulative_deposit_interest: 1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000, + deposit_balance: 0.into(), + cumulative_deposit_interest: (1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000).into(), insurance_fund: InsuranceFund { unstaking_period: 60 * 60 * 24 * 7, // 7 weeks ..InsuranceFund::default() @@ -654,11 +656,11 @@ pub fn escrow_losses_stake_if_test() { let o = if_shares_to_vault_amount( n_shares / 3, - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.total_shares(), if_balance, ) .unwrap(); - assert_eq!(if_stake.last_withdraw_request_shares, 0); + assert_eq!(if_stake.last_withdraw_request_shares(), 0); request_remove_insurance_fund_stake( n_shares / 3, @@ -669,7 +671,7 @@ pub fn escrow_losses_stake_if_test() { now, ) .unwrap(); - assert_eq!(if_stake.last_withdraw_request_shares, 33333333333); + assert_eq!(if_stake.last_withdraw_request_shares(), 33333333333); assert_eq!( if_stake.last_withdraw_request_value, expected_amount_returned @@ -719,12 +721,12 @@ pub fn escrow_gains_stake_if_test() { }; let amount = 100_000_384_939_u64; // $100k + change let mut spot_market = SpotMarket { - deposit_balance: 0, - cumulative_deposit_interest: 1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000, + deposit_balance: 0.into(), + cumulative_deposit_interest: (1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000).into(), insurance_fund: InsuranceFund { unstaking_period: 60 * 60 * 24 * 7, // 7 weeks - total_shares: 1, - user_shares: 0, + total_shares: 1.into(), + user_shares: 0.into(), ..InsuranceFund::default() }, ..SpotMarket::default() @@ -767,11 +769,11 @@ pub fn escrow_gains_stake_if_test() { let n_shares = if_stake.unchecked_if_shares(); let expected_amount_returned = - (if_balance as u128 * n_shares / spot_market.insurance_fund.total_shares) as u64; + (if_balance as u128 * n_shares / spot_market.insurance_fund.total_shares()) as u64; let o = if_shares_to_vault_amount( n_shares, - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.total_shares(), if_balance, ) .unwrap(); @@ -802,7 +804,7 @@ pub fn escrow_gains_stake_if_test() { if_balance = if_balance + if_balance / 412; let ideal_amount_returned = - (if_balance as u128 * n_shares / spot_market.insurance_fund.total_shares) as u64; + (if_balance as u128 * n_shares / spot_market.insurance_fund.total_shares()) as u64; // appropriate time for withdraw let amount_returned = (remove_insurance_fund_stake( @@ -813,7 +815,7 @@ pub fn escrow_gains_stake_if_test() { now + 60 * 60 * 24 * 7 + 3254, )) .unwrap(); - assert_eq!(if_stake.last_withdraw_request_shares, 0); + assert_eq!(if_stake.last_withdraw_request_shares(), 0); assert_eq!(if_stake.last_withdraw_request_value, 0); if_balance -= amount_returned; @@ -839,12 +841,12 @@ pub fn drained_stake_if_test_rebase_on_new_add() { let amount = 100_000_384_939_u64; // $100k + change let mut spot_market = SpotMarket { - deposit_balance: 0, - cumulative_deposit_interest: 1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000, + deposit_balance: 0.into(), + cumulative_deposit_interest: (1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000).into(), insurance_fund: InsuranceFund { unstaking_period: 60 * 60 * 24 * 7, // 7 weeks - total_shares: 100_000 * QUOTE_PRECISION, - user_shares: 80_000 * QUOTE_PRECISION, + total_shares: (100_000 * QUOTE_PRECISION).into(), + user_shares: (80_000 * QUOTE_PRECISION).into(), ..InsuranceFund::default() }, ..SpotMarket::default() @@ -875,9 +877,9 @@ pub fn drained_stake_if_test_rebase_on_new_add() { .is_err()); assert_eq!(if_stake.unchecked_if_shares(), 0); - assert_eq!(spot_market.insurance_fund.total_shares, 100_000_000_000); + assert_eq!(spot_market.insurance_fund.total_shares(), 100_000_000_000); assert_eq!( - spot_market.insurance_fund.user_shares, + spot_market.insurance_fund.user_shares(), 80_000 * QUOTE_PRECISION ); @@ -896,27 +898,28 @@ pub fn drained_stake_if_test_rebase_on_new_add() { if_balance += amount; // check rebase math - assert_eq!(spot_market.insurance_fund.total_shares, 1000003849400); - assert_eq!(spot_market.insurance_fund.user_shares, 1000003849398); + assert_eq!(spot_market.insurance_fund.total_shares(), 1000003849400); + assert_eq!(spot_market.insurance_fund.user_shares(), 1000003849398); assert_eq!(if_stake.unchecked_if_shares(), 1000003849390); assert_eq!( - if_stake.unchecked_if_shares() < spot_market.insurance_fund.user_shares, + if_stake.unchecked_if_shares() < spot_market.insurance_fund.user_shares(), true ); assert_eq!( - spot_market.insurance_fund.user_shares - if_stake.unchecked_if_shares(), + spot_market.insurance_fund.user_shares() - if_stake.unchecked_if_shares(), 8 ); - assert_eq!(spot_market.insurance_fund.shares_base, 10); - assert_eq!(if_stake.if_base, 10); + assert_eq!(spot_market.insurance_fund.shares_base(), 10); + assert_eq!(if_stake.if_base(), 10); // check orig if stake is good (on add) - assert_eq!(orig_if_stake.if_base, 0); + assert_eq!(orig_if_stake.if_base(), 0); assert_eq!(orig_if_stake.unchecked_if_shares(), 80000000000); let expected_shares_for_amount = - vault_amount_to_if_shares(1, spot_market.insurance_fund.total_shares, if_balance).unwrap(); + vault_amount_to_if_shares(1, spot_market.insurance_fund.total_shares(), if_balance) + .unwrap(); assert_eq!(expected_shares_for_amount, 10); add_insurance_fund_stake( @@ -930,8 +933,8 @@ pub fn drained_stake_if_test_rebase_on_new_add() { ) .unwrap(); - assert_eq!(spot_market.insurance_fund.shares_base, 10); - assert_eq!(orig_if_stake.if_base, 10); + assert_eq!(spot_market.insurance_fund.shares_base(), 10); + assert_eq!(orig_if_stake.if_base(), 10); assert_eq!( orig_if_stake.unchecked_if_shares(), 80000000000 / 10000000000 + expected_shares_for_amount @@ -947,12 +950,12 @@ pub fn drained_stake_if_test_rebase_on_old_remove_all() { let mut if_balance = 0; let mut spot_market = SpotMarket { - deposit_balance: 0, - cumulative_deposit_interest: 1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000, + deposit_balance: 0.into(), + cumulative_deposit_interest: (1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000).into(), insurance_fund: InsuranceFund { unstaking_period: 0, - total_shares: 100_000 * QUOTE_PRECISION, - user_shares: 80_000 * QUOTE_PRECISION, + total_shares: (100_000 * QUOTE_PRECISION).into(), + user_shares: (80_000 * QUOTE_PRECISION).into(), ..InsuranceFund::default() }, ..SpotMarket::default() @@ -971,9 +974,9 @@ pub fn drained_stake_if_test_rebase_on_old_remove_all() { assert_eq!(if_balance, 0); // right now other users have claim on a zero balance IF... should not give them your money here - assert_eq!(spot_market.insurance_fund.total_shares, 100_000_000_000); + assert_eq!(spot_market.insurance_fund.total_shares(), 100_000_000_000); assert_eq!( - spot_market.insurance_fund.user_shares, + spot_market.insurance_fund.user_shares(), 80_000 * QUOTE_PRECISION ); @@ -998,8 +1001,8 @@ pub fn drained_stake_if_test_rebase_on_old_remove_all() { // check rebase math assert_eq!(amount_returned, 0); - assert_eq!(spot_market.insurance_fund.total_shares, 20000000000); - assert_eq!(spot_market.insurance_fund.user_shares, 0); + assert_eq!(spot_market.insurance_fund.total_shares(), 20000000000); + assert_eq!(spot_market.insurance_fund.user_shares(), 0); // make non-zero if_balance = 1; @@ -1029,9 +1032,9 @@ pub fn drained_stake_if_test_rebase_on_old_remove_all() { ) .unwrap(); - assert_eq!(spot_market.insurance_fund.shares_base, 9); - assert_eq!(spot_market.insurance_fund.total_shares, 200000000000020); - assert_eq!(spot_market.insurance_fund.user_shares, 200000000000000); + assert_eq!(spot_market.insurance_fund.shares_base(), 9); + assert_eq!(spot_market.insurance_fund.total_shares(), 200000000000020); + assert_eq!(spot_market.insurance_fund.user_shares(), 200000000000000); if_balance += 10_000_000_000_000; assert_eq!(if_balance, 10000000000001); } @@ -1041,12 +1044,12 @@ pub fn drained_stake_if_test_rebase_on_old_remove_all_2() { let mut if_balance = 0; let mut spot_market = SpotMarket { - deposit_balance: 0, - cumulative_deposit_interest: 1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000, + deposit_balance: 0.into(), + cumulative_deposit_interest: (1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000).into(), insurance_fund: InsuranceFund { unstaking_period: 0, - total_shares: 100_930_021_053, - user_shares: 83_021 * QUOTE_PRECISION + 135723, + total_shares: 100_930_021_053.into(), + user_shares: (83_021 * QUOTE_PRECISION + 135723).into(), ..InsuranceFund::default() }, ..SpotMarket::default() @@ -1085,15 +1088,15 @@ pub fn drained_stake_if_test_rebase_on_old_remove_all_2() { // check rebase math assert_eq!(amount_returned, 0); - assert_eq!(spot_market.insurance_fund.total_shares, 60930021053); - assert_eq!(spot_market.insurance_fund.user_shares, 43021135723); - assert_eq!(spot_market.insurance_fund.shares_base, 0); + assert_eq!(spot_market.insurance_fund.total_shares(), 60930021053); + assert_eq!(spot_market.insurance_fund.user_shares(), 43021135723); + assert_eq!(spot_market.insurance_fund.shares_base(), 0); if_balance = QUOTE_PRECISION as u64; let unstake_amt = if_stake.unchecked_if_shares() / 2; assert_eq!(unstake_amt, 20000000000); - assert_eq!(if_stake.last_withdraw_request_shares, 0); + assert_eq!(if_stake.last_withdraw_request_shares(), 0); assert_eq!(if_stake.last_withdraw_request_value, 0); assert_eq!(if_stake.last_withdraw_request_ts, 0); @@ -1108,20 +1111,20 @@ pub fn drained_stake_if_test_rebase_on_old_remove_all_2() { .unwrap(); // rebase occurs in request - assert_eq!(if_stake.last_withdraw_request_shares, unstake_amt / 1000); + assert_eq!(if_stake.last_withdraw_request_shares(), unstake_amt / 1000); // (that rebase occurs when you pass in shares you wanna unstake) :/ assert_eq!(if_stake.unchecked_if_shares(), 40000000); assert_eq!(if_stake.last_withdraw_request_value, 328245); assert_eq!(if_stake.last_withdraw_request_ts, 10); - assert_eq!(spot_market.insurance_fund.total_shares, 60930021); - assert_eq!(spot_market.insurance_fund.user_shares, 43021135); + assert_eq!(spot_market.insurance_fund.total_shares(), 60930021); + assert_eq!(spot_market.insurance_fund.user_shares(), 43021135); - assert_eq!(spot_market.insurance_fund.shares_base, 3); + assert_eq!(spot_market.insurance_fund.shares_base(), 3); let expected_amount_for_shares = if_shares_to_vault_amount( if_stake.unchecked_if_shares() / 2, - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.total_shares(), if_balance, ) .unwrap(); @@ -1131,15 +1134,15 @@ pub fn drained_stake_if_test_rebase_on_old_remove_all_2() { ); let user_expected_amount_for_shares_before_double = if_shares_to_vault_amount( - spot_market.insurance_fund.user_shares, - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.user_shares(), + spot_market.insurance_fund.total_shares(), if_balance, ) .unwrap(); let protocol_expected_amount_for_shares_before_double = if_shares_to_vault_amount( - spot_market.insurance_fund.total_shares - spot_market.insurance_fund.user_shares, - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.total_shares() - spot_market.insurance_fund.user_shares(), + spot_market.insurance_fund.total_shares(), if_balance, ) .unwrap(); @@ -1155,15 +1158,15 @@ pub fn drained_stake_if_test_rebase_on_old_remove_all_2() { if_balance *= 2; // double the IF vault before withdraw let protocol_expected_amount_for_shares_after_double = if_shares_to_vault_amount( - spot_market.insurance_fund.total_shares - spot_market.insurance_fund.user_shares, - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.total_shares() - spot_market.insurance_fund.user_shares(), + spot_market.insurance_fund.total_shares(), if_balance, ) .unwrap(); let user_expected_amount_for_shares_after_double = if_shares_to_vault_amount( - spot_market.insurance_fund.user_shares, - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.user_shares(), + spot_market.insurance_fund.total_shares(), if_balance, ) .unwrap(); @@ -1178,22 +1181,22 @@ pub fn drained_stake_if_test_rebase_on_old_remove_all_2() { .unwrap(); let protocol_expected_amount_for_shares_after_user_withdraw = if_shares_to_vault_amount( - spot_market.insurance_fund.total_shares - spot_market.insurance_fund.user_shares, - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.total_shares() - spot_market.insurance_fund.user_shares(), + spot_market.insurance_fund.total_shares(), if_balance, ) .unwrap(); // check rebase math assert_eq!(if_stake.unchecked_if_shares(), 20000000); - assert_eq!(if_stake.if_base, spot_market.insurance_fund.shares_base); - assert_eq!(if_stake.last_withdraw_request_shares, 0); + assert_eq!(if_stake.if_base(), spot_market.insurance_fund.shares_base()); + assert_eq!(if_stake.last_withdraw_request_shares(), 0); assert_eq!(if_stake.last_withdraw_request_value, 0); assert_eq!(amount_returned, 328245); - assert_eq!(spot_market.insurance_fund.total_shares, 40930021); - assert_eq!(spot_market.insurance_fund.user_shares, 23021135); - assert_eq!(spot_market.insurance_fund.shares_base, 3); + assert_eq!(spot_market.insurance_fund.total_shares(), 40930021); + assert_eq!(spot_market.insurance_fund.user_shares(), 23021135); + assert_eq!(spot_market.insurance_fund.shares_base(), 3); assert_eq!( protocol_expected_amount_for_shares_after_double, @@ -1231,9 +1234,9 @@ pub fn drained_stake_if_test_rebase_on_old_remove_all_2() { .unwrap(); if_balance += 10_000_000_000_000; - assert_eq!(spot_market.insurance_fund.total_shares, 204650145930021); - assert_eq!(spot_market.insurance_fund.user_shares, 204650128021135); - assert_eq!(spot_market.insurance_fund.shares_base, 3); + assert_eq!(spot_market.insurance_fund.total_shares(), 204650145930021); + assert_eq!(spot_market.insurance_fund.user_shares(), 204650128021135); + assert_eq!(spot_market.insurance_fund.shares_base(), 3); assert_eq!(if_balance, 10000002000000); } @@ -1255,8 +1258,8 @@ pub fn multiple_if_stakes_and_rebase() { let amount = (QUOTE_PRECISION * 100_000) as u64; // $100k let mut spot_market = SpotMarket { - deposit_balance: 0, - cumulative_deposit_interest: 1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000, + deposit_balance: 0.into(), + cumulative_deposit_interest: (1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000).into(), insurance_fund: InsuranceFund { unstaking_period: 0, ..InsuranceFund::default() @@ -1290,8 +1293,8 @@ pub fn multiple_if_stakes_and_rebase() { // if gets drained if_balance = QUOTE_PRECISION as u64; - assert_eq!(if_stake_1.if_base, 0); - assert_eq!(spot_market.insurance_fund.shares_base, 0); + assert_eq!(if_stake_1.if_base(), 0); + assert_eq!(spot_market.insurance_fund.shares_base(), 0); request_remove_insurance_fund_stake( if_stake_1.unchecked_if_shares(), @@ -1302,8 +1305,8 @@ pub fn multiple_if_stakes_and_rebase() { 0, ) .unwrap(); - assert_eq!(if_stake_1.if_base, 4); - assert_eq!(spot_market.insurance_fund.shares_base, 4); + assert_eq!(if_stake_1.if_base(), 4); + assert_eq!(spot_market.insurance_fund.shares_base(), 4); let amount_returned = (remove_insurance_fund_stake( if_balance, @@ -1316,8 +1319,8 @@ pub fn multiple_if_stakes_and_rebase() { assert_eq!(amount_returned, 500000); if_balance -= amount_returned; - assert_eq!(if_stake_2.if_base, 0); - assert_eq!(spot_market.insurance_fund.shares_base, 4); + assert_eq!(if_stake_2.if_base(), 0); + assert_eq!(spot_market.insurance_fund.shares_base(), 4); request_remove_insurance_fund_stake( if_stake_2.unchecked_if_shares(), if_balance, @@ -1327,15 +1330,15 @@ pub fn multiple_if_stakes_and_rebase() { 0, ) .unwrap(); - assert_eq!(if_stake_2.if_base, 4); - assert_eq!(spot_market.insurance_fund.shares_base, 4); + assert_eq!(if_stake_2.if_base(), 4); + assert_eq!(spot_market.insurance_fund.shares_base(), 4); assert_eq!( - if_stake_2.if_base < spot_market.insurance_fund.total_shares, + if_stake_2.if_base() < spot_market.insurance_fund.total_shares(), true ); assert_eq!( if_stake_2.unchecked_if_shares(), - spot_market.insurance_fund.user_shares + spot_market.insurance_fund.user_shares() ); assert_eq!(if_balance, 500000); @@ -1352,8 +1355,8 @@ pub fn multiple_if_stakes_and_rebase() { if_balance -= amount_returned; assert_eq!(if_balance, 1); - assert_eq!(spot_market.insurance_fund.user_shares, 0); - assert_eq!(spot_market.insurance_fund.total_shares, 0); + assert_eq!(spot_market.insurance_fund.user_shares(), 0); + assert_eq!(spot_market.insurance_fund.total_shares(), 0); } #[test] @@ -1374,8 +1377,8 @@ pub fn multiple_if_stakes_and_rebase_and_admin_remove() { let amount = (QUOTE_PRECISION * 100_000) as u64; // $100k let mut spot_market = SpotMarket { - deposit_balance: 0, - cumulative_deposit_interest: 1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000, + deposit_balance: 0.into(), + cumulative_deposit_interest: (1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000).into(), insurance_fund: InsuranceFund { unstaking_period: 0, ..InsuranceFund::default() @@ -1395,9 +1398,9 @@ pub fn multiple_if_stakes_and_rebase_and_admin_remove() { if_balance -= amount_returned; assert_eq!(amount_returned, (50 * QUOTE_PRECISION) as u64); - assert_eq!(spot_market.insurance_fund.user_shares, 0); + assert_eq!(spot_market.insurance_fund.user_shares(), 0); assert_eq!( - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.total_shares(), 50 * QUOTE_PRECISION ); @@ -1430,8 +1433,8 @@ pub fn multiple_if_stakes_and_rebase_and_admin_remove() { // if gets drained if_balance = QUOTE_PRECISION as u64; - assert_eq!(if_stake_1.if_base, 0); - assert_eq!(spot_market.insurance_fund.shares_base, 0); + assert_eq!(if_stake_1.if_base(), 0); + assert_eq!(spot_market.insurance_fund.shares_base(), 0); request_remove_insurance_fund_stake( if_stake_1.unchecked_if_shares(), @@ -1442,8 +1445,8 @@ pub fn multiple_if_stakes_and_rebase_and_admin_remove() { 0, ) .unwrap(); - assert_eq!(if_stake_1.if_base, 4); - assert_eq!(spot_market.insurance_fund.shares_base, 4); + assert_eq!(if_stake_1.if_base(), 4); + assert_eq!(spot_market.insurance_fund.shares_base(), 4); let amount_returned = (remove_insurance_fund_stake( if_balance, @@ -1456,8 +1459,8 @@ pub fn multiple_if_stakes_and_rebase_and_admin_remove() { assert_eq!(amount_returned, 499750); if_balance -= amount_returned; - assert_eq!(if_stake_2.if_base, 0); - assert_eq!(spot_market.insurance_fund.shares_base, 4); + assert_eq!(if_stake_2.if_base(), 0); + assert_eq!(spot_market.insurance_fund.shares_base(), 4); request_remove_insurance_fund_stake( if_stake_2.unchecked_if_shares(), if_balance, @@ -1467,22 +1470,22 @@ pub fn multiple_if_stakes_and_rebase_and_admin_remove() { 0, ) .unwrap(); - assert_eq!(if_stake_2.if_base, 4); - assert_eq!(spot_market.insurance_fund.shares_base, 4); + assert_eq!(if_stake_2.if_base(), 4); + assert_eq!(spot_market.insurance_fund.shares_base(), 4); assert_eq!( - if_stake_2.if_base < spot_market.insurance_fund.total_shares, + if_stake_2.if_base() < spot_market.insurance_fund.total_shares(), true ); assert_eq!( if_stake_2.unchecked_if_shares(), - spot_market.insurance_fund.user_shares + spot_market.insurance_fund.user_shares() ); assert_eq!(if_balance, 500250); // withdraw all let amount_returned = admin_remove_insurance_fund_stake( if_balance, - spot_market.insurance_fund.total_shares - spot_market.insurance_fund.user_shares, + spot_market.insurance_fund.total_shares() - spot_market.insurance_fund.user_shares(), &mut spot_market, 1, Pubkey::default(), @@ -1492,8 +1495,8 @@ pub fn multiple_if_stakes_and_rebase_and_admin_remove() { assert_eq!(amount_returned, 499); assert_eq!( - spot_market.insurance_fund.user_shares, - spot_market.insurance_fund.total_shares + spot_market.insurance_fund.user_shares(), + spot_market.insurance_fund.total_shares() ); // half of it back @@ -1512,8 +1515,8 @@ pub fn multiple_if_stakes_and_rebase_and_admin_remove() { if_balance -= amount_returned; assert_eq!(if_balance, 250); - assert_eq!(spot_market.insurance_fund.user_shares, 0); - assert_eq!(spot_market.insurance_fund.total_shares, 0); + assert_eq!(spot_market.insurance_fund.user_shares(), 0); + assert_eq!(spot_market.insurance_fund.total_shares(), 0); let amount_returned = admin_remove_insurance_fund_stake(if_balance, 250, &mut spot_market, 1, Pubkey::default()) @@ -1521,8 +1524,8 @@ pub fn multiple_if_stakes_and_rebase_and_admin_remove() { // if_balance -= amount_returned; assert_eq!(amount_returned, 250); - assert_eq!(spot_market.insurance_fund.user_shares, 0); - assert_eq!(spot_market.insurance_fund.total_shares, 0); + assert_eq!(spot_market.insurance_fund.user_shares(), 0); + assert_eq!(spot_market.insurance_fund.total_shares(), 0); } #[test] @@ -1536,23 +1539,23 @@ fn test_transfer_protocol_owned_stake() { }; let mut spot_market = SpotMarket { - deposit_balance: 0, - cumulative_deposit_interest: 1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000, + deposit_balance: 0.into(), + cumulative_deposit_interest: (1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000).into(), insurance_fund: InsuranceFund { unstaking_period: 0, ..InsuranceFund::default() }, ..SpotMarket::default() }; - assert_eq!(spot_market.insurance_fund.user_shares, 0); - assert_eq!(spot_market.insurance_fund.total_shares, 0); + assert_eq!(spot_market.insurance_fund.user_shares(), 0); + assert_eq!(spot_market.insurance_fund.total_shares(), 0); - spot_market.insurance_fund.total_shares = 42210407198; // make price != 1 + spot_market.insurance_fund.set_total_shares(42210407198); // make price != 1 // withdraw half let amount_returned = admin_remove_insurance_fund_stake( if_balance, - spot_market.insurance_fund.total_shares / 2, + spot_market.insurance_fund.total_shares() / 2, &mut spot_market, 1, Pubkey::default(), @@ -1561,8 +1564,8 @@ fn test_transfer_protocol_owned_stake() { if_balance -= amount_returned; assert_eq!(amount_returned, 99500000000_u64); - assert_eq!(spot_market.insurance_fund.user_shares, 0); - assert_eq!(spot_market.insurance_fund.total_shares, 21105203599); + assert_eq!(spot_market.insurance_fund.user_shares(), 0); + assert_eq!(spot_market.insurance_fund.total_shares(), 21105203599); let now = 6969696969; @@ -1576,7 +1579,7 @@ fn test_transfer_protocol_owned_stake() { Pubkey::default(), ) .unwrap(); - assert_eq!(0, spot_market.insurance_fund.user_shares); + assert_eq!(0, spot_market.insurance_fund.user_shares()); assert_eq!(transfer_num_0, 0); let transfer_num_1 = transfer_protocol_insurance_fund_stake( @@ -1589,13 +1592,13 @@ fn test_transfer_protocol_owned_stake() { Pubkey::default(), ) .unwrap(); - assert_eq!(1, spot_market.insurance_fund.user_shares); - assert_eq!(21105203599, spot_market.insurance_fund.total_shares); + assert_eq!(1, spot_market.insurance_fund.user_shares()); + assert_eq!(21105203599, spot_market.insurance_fund.total_shares()); assert_eq!(transfer_num_1, 4); assert!(transfer_protocol_insurance_fund_stake( if_balance, - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.total_shares(), &mut if_stake_2, &mut user_stats_2, &mut spot_market, @@ -1606,7 +1609,7 @@ fn test_transfer_protocol_owned_stake() { let transfer_num_2 = transfer_protocol_insurance_fund_stake( if_balance, - spot_market.insurance_fund.total_shares - spot_market.insurance_fund.user_shares, + spot_market.insurance_fund.total_shares() - spot_market.insurance_fund.user_shares(), &mut if_stake_2, &mut user_stats_2, &mut spot_market, @@ -1616,8 +1619,8 @@ fn test_transfer_protocol_owned_stake() { .unwrap(); assert_eq!( - spot_market.insurance_fund.total_shares, - spot_market.insurance_fund.user_shares + spot_market.insurance_fund.total_shares(), + spot_market.insurance_fund.user_shares() ); assert_eq!(transfer_num_2, 99499999995); @@ -1631,7 +1634,7 @@ fn test_transfer_protocol_owned_stake() { assert!(transfer_protocol_insurance_fund_stake( if_balance, - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.total_shares(), &mut if_stake_2, &mut user_stats_2, &mut spot_market, diff --git a/programs/drift/src/controller/liquidation.rs b/programs/drift/src/controller/liquidation.rs index 4e5543d313..f695366951 100644 --- a/programs/drift/src/controller/liquidation.rs +++ b/programs/drift/src/controller/liquidation.rs @@ -531,10 +531,10 @@ pub fn liquidate_perp( market.amm.order_step_size )?; - market.amm.total_liquidation_fee = market + let tlf = market.amm.total_liquidation_fee(); + market .amm - .total_liquidation_fee - .safe_add(if_fee.unsigned_abs().cast()?)?; + .set_total_liquidation_fee(tlf.safe_add(if_fee.unsigned_abs().cast()?)?); ( user_existing_position_direction, @@ -672,10 +672,10 @@ pub fn liquidate_perp( maker_order_cumulative_quote_asset_amount_filled: Some(base_asset_value), oracle_price, bit_flags: 0, - taker_existing_quote_entry_amount: taker_existing_quote_entry_amount, - taker_existing_base_asset_amount: taker_existing_base_asset_amount, - maker_existing_quote_entry_amount: maker_existing_quote_entry_amount, - maker_existing_base_asset_amount: maker_existing_base_asset_amount, + taker_existing_quote_entry_amount, + taker_existing_base_asset_amount, + maker_existing_quote_entry_amount, + maker_existing_base_asset_amount, trigger_price: None, builder_idx: None, builder_fee: None, @@ -1103,10 +1103,10 @@ pub fn liquidate_perp_with_fill( let user_position = user.get_perp_position_mut(market_index)?; update_quote_asset_and_break_even_amount(user_position, &mut market, if_fee)?; - market.amm.total_liquidation_fee = market - .amm - .total_liquidation_fee - .safe_add(if_fee.unsigned_abs().cast()?)?; + let total_liquidation_fee = market.amm.total_liquidation_fee(); + market.amm.set_total_liquidation_fee( + total_liquidation_fee.safe_add(if_fee.unsigned_abs().cast()?)?, + ); } let (margin_freed_for_perp_position, margin_calculation_after) = calculate_margin_freed( @@ -3427,20 +3427,20 @@ pub fn resolve_perp_bankruptcy( if loss_to_socialize < 0 { let mut market = perp_market_map.get_ref_mut(&market_index)?; - market.amm.total_social_loss = market + let total_social_loss = market.amm.total_social_loss(); + market .amm - .total_social_loss - .safe_add(loss_to_socialize.unsigned_abs())?; + .set_total_social_loss(total_social_loss.safe_add(loss_to_socialize.unsigned_abs())?); - market.amm.cumulative_funding_rate_long = market - .amm - .cumulative_funding_rate_long - .safe_add(cumulative_funding_rate_delta)?; + let cumulative_funding_rate_long = market.amm.cumulative_funding_rate_long(); + market.amm.set_cumulative_funding_rate_long( + cumulative_funding_rate_long.safe_add(cumulative_funding_rate_delta)?, + ); - market.amm.cumulative_funding_rate_short = market - .amm - .cumulative_funding_rate_short - .safe_sub(cumulative_funding_rate_delta)?; + let cumulative_funding_rate_short = market.amm.cumulative_funding_rate_short(); + market.amm.set_cumulative_funding_rate_short( + cumulative_funding_rate_short.safe_sub(cumulative_funding_rate_delta)?, + ); } // clear bad debt @@ -3600,17 +3600,18 @@ pub fn resolve_spot_bankruptcy( None, )?; - spot_market.cumulative_deposit_interest = spot_market - .cumulative_deposit_interest - .safe_sub(cumulative_deposit_interest_delta)?; + let cumulative_deposit_interest = spot_market.cumulative_deposit_interest(); + spot_market.set_cumulative_deposit_interest( + cumulative_deposit_interest.safe_sub(cumulative_deposit_interest_delta)?, + ); - spot_market.total_social_loss = spot_market - .total_social_loss - .safe_add(borrow_amount.cast()?)?; + let total_social_loss = spot_market.total_social_loss(); + spot_market.set_total_social_loss(total_social_loss.safe_add(borrow_amount.cast()?)?); - spot_market.total_quote_social_loss = spot_market - .total_quote_social_loss - .safe_add(quote_social_loss.unsigned_abs().cast()?)?; + let total_quote_social_loss = spot_market.total_quote_social_loss(); + spot_market.set_total_quote_social_loss( + total_quote_social_loss.safe_add(quote_social_loss.unsigned_abs().cast()?)?, + ); } // exit bankruptcy diff --git a/programs/drift/src/controller/liquidation/tests.rs b/programs/drift/src/controller/liquidation/tests.rs index e8cf21acde..27e29eaf48 100644 --- a/programs/drift/src/controller/liquidation/tests.rs +++ b/programs/drift/src/controller/liquidation/tests.rs @@ -55,19 +55,19 @@ pub mod liquidate_perp { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), ..AMM::default() @@ -86,7 +86,7 @@ pub mod liquidate_perp { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, historical_oracle_data: HistoricalOracleData { @@ -182,7 +182,7 @@ pub mod liquidate_perp { ); let market_after = perp_market_map.get_ref(&0).unwrap(); - assert_eq!(market_after.amm.total_liquidation_fee, 0); + assert_eq!(market_after.amm.total_liquidation_fee(), 0); } #[test] @@ -204,19 +204,19 @@ pub mod liquidate_perp { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 50 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (50 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), funding_period: 3600, @@ -236,7 +236,7 @@ pub mod liquidate_perp { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, historical_oracle_data: HistoricalOracleData { @@ -332,7 +332,7 @@ pub mod liquidate_perp { ); let market_after = perp_market_map.get_ref(&0).unwrap(); - assert_eq!(market_after.amm.total_liquidation_fee, 0); + assert_eq!(market_after.amm.total_liquidation_fee(), 0); } #[test] @@ -354,19 +354,19 @@ pub mod liquidate_perp { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 50 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (50 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), funding_period: 3600, @@ -385,7 +385,7 @@ pub mod liquidate_perp { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -488,19 +488,19 @@ pub mod liquidate_perp { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), funding_period: ONE_HOUR, @@ -520,7 +520,7 @@ pub mod liquidate_perp { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, historical_oracle_data: HistoricalOracleData { @@ -615,7 +615,7 @@ pub mod liquidate_perp { assert_eq!(liquidator.perp_positions[0].quote_asset_amount, -49500000); let market_after = perp_market_map.get_ref(&0).unwrap(); - assert_eq!(market_after.amm.total_liquidation_fee, 0) + assert_eq!(market_after.amm.total_liquidation_fee(), 0) } #[test] @@ -637,19 +637,19 @@ pub mod liquidate_perp { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), funding_period: ONE_HOUR, @@ -669,7 +669,7 @@ pub mod liquidate_perp { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -794,7 +794,7 @@ pub mod liquidate_perp { assert_eq!(liquidator.perp_positions[0].quote_asset_amount, -178200000); let market_after = perp_market_map.get_ref(&0).unwrap(); - assert_eq!(market_after.amm.total_liquidation_fee, 1800000) + assert_eq!(market_after.amm.total_liquidation_fee(), 1800000) } #[test] @@ -816,19 +816,19 @@ pub mod liquidate_perp { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), funding_period: ONE_HOUR, @@ -849,7 +849,7 @@ pub mod liquidate_perp { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, historical_oracle_data: HistoricalOracleData { @@ -994,7 +994,7 @@ pub mod liquidate_perp { ); let market_after = perp_market_map.get_ref(&0).unwrap(); - assert_eq!(market_after.amm.total_liquidation_fee, QUOTE_PRECISION); + assert_eq!(market_after.amm.total_liquidation_fee(), QUOTE_PRECISION); } #[test] @@ -1016,19 +1016,19 @@ pub mod liquidate_perp { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), funding_period: ONE_HOUR, @@ -1048,7 +1048,7 @@ pub mod liquidate_perp { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, historical_oracle_data: HistoricalOracleData { @@ -1148,19 +1148,19 @@ pub mod liquidate_perp { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 50 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (50 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), funding_period: ONE_HOUR, @@ -1180,7 +1180,7 @@ pub mod liquidate_perp { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, historical_oracle_data: HistoricalOracleData { @@ -1280,19 +1280,19 @@ pub mod liquidate_perp { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), funding_period: ONE_HOUR, @@ -1312,7 +1312,7 @@ pub mod liquidate_perp { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, historical_oracle_data: HistoricalOracleData { @@ -1402,7 +1402,7 @@ pub mod liquidate_perp { let market_after = perp_market_map.get_ref(&0).unwrap(); assert_eq!( - market_after.amm.total_liquidation_fee, + market_after.amm.total_liquidation_fee(), QUOTE_PRECISION / 100 ); } @@ -1426,19 +1426,19 @@ pub mod liquidate_perp { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), funding_period: ONE_HOUR, @@ -1458,7 +1458,7 @@ pub mod liquidate_perp { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -1703,19 +1703,19 @@ pub mod liquidate_perp { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), funding_period: ONE_HOUR, @@ -1735,7 +1735,7 @@ pub mod liquidate_perp { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -1840,19 +1840,19 @@ pub mod liquidate_perp { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), funding_period: ONE_HOUR, @@ -1872,7 +1872,7 @@ pub mod liquidate_perp { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -1964,19 +1964,19 @@ pub mod liquidate_perp { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 50 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (50 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), funding_period: 3600, @@ -1997,7 +1997,7 @@ pub mod liquidate_perp { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, historical_oracle_data: HistoricalOracleData { @@ -2070,7 +2070,7 @@ pub mod liquidate_perp { let market_after = perp_market_map.get_ref(&0).unwrap(); // .5% * 100 * .95 =$0.475 - assert_eq!(market_after.amm.total_liquidation_fee, 475000); + assert_eq!(market_after.amm.total_liquidation_fee(), 475000); } #[test] @@ -2092,19 +2092,19 @@ pub mod liquidate_perp { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 50 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (50 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), funding_period: 3600, @@ -2125,7 +2125,7 @@ pub mod liquidate_perp { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, historical_oracle_data: HistoricalOracleData { @@ -2198,7 +2198,7 @@ pub mod liquidate_perp { let market_after = perp_market_map.get_ref(&0).unwrap(); assert!(!user.is_being_liquidated()); - assert_eq!(market_after.amm.total_liquidation_fee, 41787043); + assert_eq!(market_after.amm.total_liquidation_fee(), 41787043); } #[test] @@ -2220,19 +2220,19 @@ pub mod liquidate_perp { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), funding_period: ONE_HOUR, @@ -2254,7 +2254,7 @@ pub mod liquidate_perp { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -2373,7 +2373,7 @@ pub mod liquidate_perp { assert_eq!(liquidator.perp_positions[0].quote_asset_amount, -149250000); let market_after = perp_market_map.get_ref(&0).unwrap(); - assert_eq!(market_after.amm.total_liquidation_fee, 750000) + assert_eq!(market_after.amm.total_liquidation_fee(), 750000) } } @@ -2431,21 +2431,21 @@ pub mod liquidate_perp_with_fill { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - terminal_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + terminal_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, order_tick_size: 1, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: 0, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: 0.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), ..AMM::default() @@ -2464,7 +2464,7 @@ pub mod liquidate_perp_with_fill { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, historical_oracle_data: HistoricalOracleData { @@ -2618,7 +2618,7 @@ pub mod liquidate_perp_with_fill { assert_eq!(liquidator.perp_positions[0].quote_asset_amount, 3600); let market_after = perp_market_map.get_ref(&0).unwrap(); - assert_eq!(market_after.amm.total_liquidation_fee, 360000); + assert_eq!(market_after.amm.total_liquidation_fee(), 360000); } #[test] @@ -2640,21 +2640,21 @@ pub mod liquidate_perp_with_fill { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - terminal_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + terminal_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, order_tick_size: 1, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: 0, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: 0.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), ..AMM::default() @@ -2673,7 +2673,7 @@ pub mod liquidate_perp_with_fill { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, historical_oracle_data: HistoricalOracleData { @@ -2827,7 +2827,7 @@ pub mod liquidate_perp_with_fill { assert_eq!(liquidator.perp_positions[0].quote_asset_amount, 3600); let market_after = perp_market_map.get_ref(&0).unwrap(); - assert_eq!(market_after.amm.total_liquidation_fee, 360000); + assert_eq!(market_after.amm.total_liquidation_fee(), 360000); } #[test] @@ -2851,21 +2851,21 @@ pub mod liquidate_perp_with_fill { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - terminal_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + terminal_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, order_tick_size: 1, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: 0, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: 0.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), ..AMM::default() @@ -2879,15 +2879,15 @@ pub mod liquidate_perp_with_fill { ..PerpMarket::default() }; market.amm.max_fill_reserve_fraction = 1; - market.amm.max_base_asset_reserve = u64::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u64::MAX as u128); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let perp_market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, historical_oracle_data: HistoricalOracleData { @@ -2998,7 +2998,7 @@ pub mod liquidate_perp_with_fill { assert_eq!(liquidator.perp_positions[0].quote_asset_amount, 3587); let market_after = perp_market_map.get_ref(&0).unwrap(); - assert_eq!(market_after.amm.total_liquidation_fee, 358708); + assert_eq!(market_after.amm.total_liquidation_fee(), 358708); } #[test] @@ -3022,21 +3022,21 @@ pub mod liquidate_perp_with_fill { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - terminal_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + terminal_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, order_tick_size: 1, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: 0, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: 0.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), ..AMM::default() @@ -3050,15 +3050,15 @@ pub mod liquidate_perp_with_fill { ..PerpMarket::default() }; market.amm.max_fill_reserve_fraction = 1; - market.amm.max_base_asset_reserve = u64::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u64::MAX as u128); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let perp_market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, historical_oracle_data: HistoricalOracleData { @@ -3169,7 +3169,7 @@ pub mod liquidate_perp_with_fill { assert_eq!(liquidator.perp_positions[0].quote_asset_amount, 3613); let market_after = perp_market_map.get_ref(&0).unwrap(); - assert_eq!(market_after.amm.total_liquidation_fee, 361300); + assert_eq!(market_after.amm.total_liquidation_fee(), 361300); } } @@ -3226,11 +3226,11 @@ pub mod liquidate_spot { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -3244,15 +3244,15 @@ pub mod liquidate_spot { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: SPOT_BALANCE_PRECISION.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: (sol_oracle_price.agg.price * 99 / 100), @@ -3369,11 +3369,11 @@ pub mod liquidate_spot { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -3387,15 +3387,15 @@ pub mod liquidate_spot { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: SPOT_BALANCE_PRECISION.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: (sol_oracle_price.agg.price * 1442 / 10000), @@ -3543,11 +3543,11 @@ pub mod liquidate_spot { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -3561,15 +3561,15 @@ pub mod liquidate_spot { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: SPOT_BALANCE_PRECISION.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, if_liquidation_fee: LIQUIDATION_FEE_PRECISION / 1000, historical_oracle_data: HistoricalOracleData { @@ -3725,7 +3725,7 @@ pub mod liquidate_spot { let market_after = spot_market_map.get_ref(&1).unwrap(); let market_revenue = get_token_amount( - market_after.revenue_pool.scaled_balance, + market_after.revenue_pool.scaled_balance(), &market_after, &SpotBalanceType::Deposit, ) @@ -3734,7 +3734,7 @@ pub mod liquidate_spot { assert_eq!(market_revenue, 593); assert_eq!( liquidator.spot_positions[1].scaled_balance + user.spot_positions[1].scaled_balance - - market_after.revenue_pool.scaled_balance as u64, + - market_after.revenue_pool.scaled_balance() as u64, SPOT_BALANCE_PRECISION_U64 ); } @@ -3762,11 +3762,11 @@ pub mod liquidate_spot { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -3780,15 +3780,15 @@ pub mod liquidate_spot { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: SPOT_BALANCE_PRECISION.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: (sol_oracle_price.agg.price * 99 / 100), @@ -3889,11 +3889,11 @@ pub mod liquidate_spot { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -3907,15 +3907,15 @@ pub mod liquidate_spot { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: SPOT_BALANCE_PRECISION.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: (sol_oracle_price.agg.price * 99 / 100), @@ -4016,11 +4016,11 @@ pub mod liquidate_spot { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -4034,15 +4034,15 @@ pub mod liquidate_spot { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: SPOT_BALANCE_PRECISION.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, if_liquidation_fee: LIQUIDATION_FEE_PRECISION / 1000, historical_oracle_data: HistoricalOracleData { @@ -4148,11 +4148,11 @@ pub mod liquidate_spot { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -4166,15 +4166,15 @@ pub mod liquidate_spot { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: 10 * SPOT_BALANCE_PRECISION, - borrow_balance: 10 * SPOT_BALANCE_PRECISION, + deposit_balance: (10 * SPOT_BALANCE_PRECISION).into(), + borrow_balance: (10 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, if_liquidation_fee: LIQUIDATION_FEE_PRECISION / 1000, historical_oracle_data: HistoricalOracleData { @@ -4378,11 +4378,11 @@ pub mod liquidate_spot { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -4396,15 +4396,15 @@ pub mod liquidate_spot { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: SPOT_BALANCE_PRECISION.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 100, if_liquidation_fee: LIQUIDATION_FEE_PRECISION / 20, historical_oracle_data: HistoricalOracleData { @@ -4418,11 +4418,11 @@ pub mod liquidate_spot { let mut usdt_market = SpotMarket { market_index: 2, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 100, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -4506,7 +4506,7 @@ pub mod liquidate_spot { let liability_market = spot_market_map.get_ref(&1).unwrap(); let revenue_pool_token_amount = get_token_amount( - liability_market.revenue_pool.scaled_balance, + liability_market.revenue_pool.scaled_balance(), &liability_market, &SpotBalanceType::Deposit, ) @@ -4579,19 +4579,19 @@ pub mod liquidate_borrow_for_perp_pnl { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: sol_oracle_price_key, ..AMM::default() }, @@ -4610,11 +4610,11 @@ pub mod liquidate_borrow_for_perp_pnl { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -4628,15 +4628,15 @@ pub mod liquidate_borrow_for_perp_pnl { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: SPOT_BALANCE_PRECISION.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: (sol_oracle_price.agg.price * 99 / 100), @@ -4734,19 +4734,19 @@ pub mod liquidate_borrow_for_perp_pnl { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: sol_oracle_price_key, ..AMM::default() }, @@ -4765,11 +4765,11 @@ pub mod liquidate_borrow_for_perp_pnl { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -4783,15 +4783,15 @@ pub mod liquidate_borrow_for_perp_pnl { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: SPOT_BALANCE_PRECISION.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, if_liquidation_fee: LIQUIDATION_FEE_PRECISION / 1000, historical_oracle_data: HistoricalOracleData { @@ -4906,7 +4906,7 @@ pub mod liquidate_borrow_for_perp_pnl { let market_after = spot_market_map.get_ref(&1).unwrap(); let market_revenue = get_token_amount( - market_after.revenue_pool.scaled_balance, + market_after.revenue_pool.scaled_balance(), &market_after, &SpotBalanceType::Deposit, ) @@ -4934,19 +4934,19 @@ pub mod liquidate_borrow_for_perp_pnl { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: sol_oracle_price_key, ..AMM::default() }, @@ -4964,11 +4964,11 @@ pub mod liquidate_borrow_for_perp_pnl { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -4982,15 +4982,15 @@ pub mod liquidate_borrow_for_perp_pnl { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: SPOT_BALANCE_PRECISION.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: (sol_oracle_price.agg.price * 99 / 100), @@ -5088,19 +5088,19 @@ pub mod liquidate_borrow_for_perp_pnl { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: sol_oracle_price_key, ..AMM::default() }, @@ -5119,11 +5119,11 @@ pub mod liquidate_borrow_for_perp_pnl { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -5137,15 +5137,15 @@ pub mod liquidate_borrow_for_perp_pnl { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: SPOT_BALANCE_PRECISION.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: (sol_oracle_price.agg.price * 99 / 100), @@ -5235,19 +5235,19 @@ pub mod liquidate_borrow_for_perp_pnl { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: sol_oracle_price_key, ..AMM::default() }, @@ -5266,11 +5266,11 @@ pub mod liquidate_borrow_for_perp_pnl { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -5284,15 +5284,15 @@ pub mod liquidate_borrow_for_perp_pnl { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: SPOT_BALANCE_PRECISION.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: (sol_oracle_price.agg.price * 99 / 100), @@ -5382,19 +5382,19 @@ pub mod liquidate_borrow_for_perp_pnl { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: sol_oracle_price_key, ..AMM::default() }, @@ -5413,11 +5413,11 @@ pub mod liquidate_borrow_for_perp_pnl { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -5431,15 +5431,15 @@ pub mod liquidate_borrow_for_perp_pnl { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION / 50, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: (SPOT_BALANCE_PRECISION / 50).into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, if_liquidation_fee: LIQUIDATION_FEE_PRECISION / 1000, historical_oracle_data: HistoricalOracleData { @@ -5539,19 +5539,19 @@ pub mod liquidate_borrow_for_perp_pnl { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: sol_oracle_price_key, ..AMM::default() }, @@ -5570,11 +5570,11 @@ pub mod liquidate_borrow_for_perp_pnl { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -5588,15 +5588,15 @@ pub mod liquidate_borrow_for_perp_pnl { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: 100 * SPOT_BALANCE_PRECISION, - borrow_balance: 11 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), + borrow_balance: (11 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, if_liquidation_fee: LIQUIDATION_FEE_PRECISION / 1000, historical_oracle_data: HistoricalOracleData { @@ -5813,19 +5813,19 @@ pub mod liquidate_perp_pnl_for_deposit { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: sol_oracle_price_key, ..AMM::default() }, @@ -5844,11 +5844,11 @@ pub mod liquidate_perp_pnl_for_deposit { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -5862,15 +5862,15 @@ pub mod liquidate_perp_pnl_for_deposit { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: 0, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: 0.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: (sol_oracle_price.agg.price * 99 / 100), @@ -5968,19 +5968,19 @@ pub mod liquidate_perp_pnl_for_deposit { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: sol_oracle_price_key, ..AMM::default() }, @@ -6000,11 +6000,11 @@ pub mod liquidate_perp_pnl_for_deposit { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -6018,15 +6018,15 @@ pub mod liquidate_perp_pnl_for_deposit { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: 0, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: 0.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: (sol_oracle_price.agg.price * 99 / 100), @@ -6105,7 +6105,7 @@ pub mod liquidate_perp_pnl_for_deposit { assert_eq!(liquidator.perp_positions[0].quote_asset_amount, -25636363); let market_after = market_map.get_ref(&0).unwrap(); - assert_eq!(market_after.amm.total_liquidation_fee, 0); + assert_eq!(market_after.amm.total_liquidation_fee(), 0); } #[test] @@ -6127,19 +6127,19 @@ pub mod liquidate_perp_pnl_for_deposit { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: sol_oracle_price_key, ..AMM::default() }, @@ -6158,11 +6158,11 @@ pub mod liquidate_perp_pnl_for_deposit { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -6176,15 +6176,15 @@ pub mod liquidate_perp_pnl_for_deposit { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: 0, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: 0.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: (sol_oracle_price.agg.price * 99 / 100), @@ -6282,19 +6282,19 @@ pub mod liquidate_perp_pnl_for_deposit { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: sol_oracle_price_key, ..AMM::default() }, @@ -6313,11 +6313,11 @@ pub mod liquidate_perp_pnl_for_deposit { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -6331,15 +6331,15 @@ pub mod liquidate_perp_pnl_for_deposit { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: 0, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: 0.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: (sol_oracle_price.agg.price * 99 / 100), @@ -6429,19 +6429,19 @@ pub mod liquidate_perp_pnl_for_deposit { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: sol_oracle_price_key, ..AMM::default() }, @@ -6460,11 +6460,11 @@ pub mod liquidate_perp_pnl_for_deposit { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -6478,15 +6478,15 @@ pub mod liquidate_perp_pnl_for_deposit { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: 0, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: 0.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: (sol_oracle_price.agg.price * 99 / 100), @@ -6576,19 +6576,19 @@ pub mod liquidate_perp_pnl_for_deposit { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: sol_oracle_price_key, ..AMM::default() }, @@ -6608,11 +6608,11 @@ pub mod liquidate_perp_pnl_for_deposit { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -6626,15 +6626,15 @@ pub mod liquidate_perp_pnl_for_deposit { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: 0, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: 0.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: (sol_oracle_price.agg.price * 99 / 100), @@ -6732,19 +6732,19 @@ pub mod liquidate_perp_pnl_for_deposit { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: sol_oracle_price_key, ..AMM::default() }, @@ -6764,11 +6764,11 @@ pub mod liquidate_perp_pnl_for_deposit { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -6782,15 +6782,15 @@ pub mod liquidate_perp_pnl_for_deposit { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: 0, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: 0.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: (sol_oracle_price.agg.price * 99 / 100), @@ -6965,19 +6965,19 @@ pub mod liquidate_perp_pnl_for_deposit { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: sol_oracle_price_key, ..AMM::default() }, @@ -6996,11 +6996,11 @@ pub mod liquidate_perp_pnl_for_deposit { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, asset_tier: AssetTier::Collateral, historical_oracle_data: HistoricalOracleData { @@ -7016,15 +7016,15 @@ pub mod liquidate_perp_pnl_for_deposit { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: 10 * SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION, + deposit_balance: (10 * SPOT_BALANCE_PRECISION).into(), + borrow_balance: SPOT_BALANCE_PRECISION.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: (sol_oracle_price.agg.price * 99 / 100), @@ -7197,19 +7197,19 @@ pub mod liquidate_perp_pnl_for_deposit { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: sol_oracle_price_key, ..AMM::default() }, @@ -7228,19 +7228,19 @@ pub mod liquidate_perp_pnl_for_deposit { let mut bonk_market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 8000, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 8000.into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: sol_oracle_price_key, ..AMM::default() }, @@ -7266,11 +7266,11 @@ pub mod liquidate_perp_pnl_for_deposit { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -7284,15 +7284,15 @@ pub mod liquidate_perp_pnl_for_deposit { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: 0, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: 0.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: (sol_oracle_price.agg.price * 99 / 100), @@ -7476,24 +7476,24 @@ pub mod resolve_perp_bankruptcy { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_long: 5 * BASE_PRECISION_I128, - base_asset_amount_short: -5 * BASE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_long: (5 * BASE_PRECISION_I128).into(), + base_asset_amount_short: (-5 * BASE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: oracle_price_key, - cumulative_funding_rate_long: 1000 * FUNDING_RATE_PRECISION_I128, - cumulative_funding_rate_short: -1000 * FUNDING_RATE_PRECISION_I128, + cumulative_funding_rate_long: (1000 * FUNDING_RATE_PRECISION_I128).into(), + cumulative_funding_rate_short: (-1000 * FUNDING_RATE_PRECISION_I128).into(), ..AMM::default() }, margin_ratio_initial: 1000, @@ -7509,7 +7509,7 @@ pub mod resolve_perp_bankruptcy { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, historical_oracle_data: HistoricalOracleData { @@ -7567,10 +7567,16 @@ pub mod resolve_perp_bankruptcy { expected_user.total_social_loss = 100000000; let mut expected_market = market; - expected_market.amm.cumulative_funding_rate_long = 1010 * FUNDING_RATE_PRECISION_I128; - expected_market.amm.cumulative_funding_rate_short = -1010 * FUNDING_RATE_PRECISION_I128; - expected_market.amm.total_social_loss = 100000000; - expected_market.amm.quote_asset_amount = -50 * QUOTE_PRECISION_I128; + expected_market + .amm + .set_cumulative_funding_rate_long(1010 * FUNDING_RATE_PRECISION_I128); + expected_market + .amm + .set_cumulative_funding_rate_short(-1010 * FUNDING_RATE_PRECISION_I128); + expected_market.amm.set_total_social_loss(100000000); + expected_market + .amm + .set_quote_asset_amount(-50 * QUOTE_PRECISION_I128); expected_market.number_of_users = 0; resolve_perp_bankruptcy( @@ -7686,26 +7692,26 @@ pub mod resolve_perp_bankruptcy { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_long: 5 * BASE_PRECISION_I128, - base_asset_amount_short: -5 * BASE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_long: (5 * BASE_PRECISION_I128).into(), + base_asset_amount_short: (-5 * BASE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: oracle_price_key, - cumulative_funding_rate_long: 1000 * FUNDING_RATE_PRECISION_I128, - cumulative_funding_rate_short: -1000 * FUNDING_RATE_PRECISION_I128, + cumulative_funding_rate_long: (1000 * FUNDING_RATE_PRECISION_I128).into(), + cumulative_funding_rate_short: (-1000 * FUNDING_RATE_PRECISION_I128).into(), fee_pool: PoolBalance { - scaled_balance: 50 * SPOT_BALANCE_PRECISION, + scaled_balance: (50 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -7723,9 +7729,9 @@ pub mod resolve_perp_bankruptcy { let mut spot_market = SpotMarket { market_index: 0, - deposit_balance: 500 * SPOT_BALANCE_PRECISION, + deposit_balance: (500 * SPOT_BALANCE_PRECISION).into(), oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, ..SpotMarket::default() @@ -7778,12 +7784,18 @@ pub mod resolve_perp_bankruptcy { expected_user.total_social_loss = 100000000; let mut expected_market = market; - expected_market.amm.cumulative_funding_rate_long = 1005 * FUNDING_RATE_PRECISION_I128; - expected_market.amm.cumulative_funding_rate_short = -1005 * FUNDING_RATE_PRECISION_I128; - expected_market.amm.total_social_loss = 50000000; - expected_market.amm.quote_asset_amount = -50 * QUOTE_PRECISION_I128; + expected_market + .amm + .set_cumulative_funding_rate_long(1005 * FUNDING_RATE_PRECISION_I128); + expected_market + .amm + .set_cumulative_funding_rate_short(-1005 * FUNDING_RATE_PRECISION_I128); + expected_market.amm.set_total_social_loss(50000000); + expected_market + .amm + .set_quote_asset_amount(-50 * QUOTE_PRECISION_I128); expected_market.number_of_users = 0; - expected_market.amm.fee_pool.scaled_balance = 0; + expected_market.amm.fee_pool.set_scaled_balance(0); resolve_perp_bankruptcy( 0, @@ -7929,24 +7941,24 @@ pub mod resolve_spot_bankruptcy { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, - base_asset_amount_long: 5 * BASE_PRECISION_I128, - base_asset_amount_short: -5 * BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), + base_asset_amount_long: (5 * BASE_PRECISION_I128).into(), + base_asset_amount_short: (-5 * BASE_PRECISION_I128).into(), oracle: oracle_price_key, - cumulative_funding_rate_long: 1000 * FUNDING_RATE_PRECISION_I128, - cumulative_funding_rate_short: -1000 * FUNDING_RATE_PRECISION_I128, + cumulative_funding_rate_long: (1000 * FUNDING_RATE_PRECISION_I128).into(), + cumulative_funding_rate_short: (-1000 * FUNDING_RATE_PRECISION_I128).into(), ..AMM::default() }, margin_ratio_initial: 1000, @@ -7961,12 +7973,12 @@ pub mod resolve_spot_bankruptcy { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 1000 * SPOT_BALANCE_PRECISION, - borrow_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (1000 * SPOT_BALANCE_PRECISION).into(), + borrow_balance: (100 * SPOT_BALANCE_PRECISION).into(), historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64), ..SpotMarket::default() }; @@ -8015,11 +8027,11 @@ pub mod resolve_spot_bankruptcy { expected_user.total_social_loss = 100000000; let mut expected_spot_market = spot_market; - expected_spot_market.borrow_balance = 0; - expected_spot_market.cumulative_deposit_interest = - 9 * SPOT_CUMULATIVE_INTEREST_PRECISION / 10; - expected_spot_market.total_social_loss = 100 * QUOTE_PRECISION; - expected_spot_market.total_quote_social_loss = 100 * QUOTE_PRECISION; + expected_spot_market.set_borrow_balance(0); + expected_spot_market + .set_cumulative_deposit_interest(9 * SPOT_CUMULATIVE_INTEREST_PRECISION / 10); + expected_spot_market.set_total_social_loss(100 * QUOTE_PRECISION); + expected_spot_market.set_total_quote_social_loss(100 * QUOTE_PRECISION); resolve_spot_bankruptcy( 0, @@ -8039,7 +8051,7 @@ pub mod resolve_spot_bankruptcy { assert_eq!(expected_spot_market, *spot_market_map.get_ref(&0).unwrap()); let spot_market = spot_market_map.get_ref_mut(&0).unwrap(); - let deposit_balance = spot_market.deposit_balance; + let deposit_balance = spot_market.deposit_balance(); let deposit_token_amount = get_token_amount(deposit_balance, &spot_market, &SpotBalanceType::Deposit).unwrap(); @@ -8095,19 +8107,19 @@ pub mod set_user_status_to_being_liquidated { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), ..AMM::default() @@ -8126,7 +8138,7 @@ pub mod set_user_status_to_being_liquidated { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -8272,19 +8284,19 @@ pub mod set_user_status_to_being_liquidated { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), ..AMM::default() @@ -8303,7 +8315,7 @@ pub mod set_user_status_to_being_liquidated { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: PRICE_PRECISION_I64, @@ -8413,11 +8425,11 @@ pub mod liquidate_spot_with_swap { let mut usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 200 * SPOT_BALANCE_PRECISION, + deposit_balance: (200 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: QUOTE_PRECISION_I64, @@ -8431,15 +8443,15 @@ pub mod liquidate_spot_with_swap { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: SPOT_BALANCE_PRECISION.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 100, if_liquidation_fee: LIQUIDATION_FEE_PRECISION / 100, historical_oracle_data: HistoricalOracleData { @@ -8579,7 +8591,7 @@ pub mod liquidate_spot_with_swap { ); let market_revenue = get_token_amount( - sol_spot_market.revenue_pool.scaled_balance, + sol_spot_market.revenue_pool.scaled_balance(), &sol_spot_market, &SpotBalanceType::Deposit, ) diff --git a/programs/drift/src/controller/lp.rs b/programs/drift/src/controller/lp.rs index 9dc8e3b489..5f967dbbc9 100644 --- a/programs/drift/src/controller/lp.rs +++ b/programs/drift/src/controller/lp.rs @@ -40,25 +40,25 @@ pub fn apply_lp_rebase_to_perp_market( let rebase_divisor: i128 = 10_i128.pow(expo_diff.abs().cast()?); if expo_diff > 0 { - perp_market.amm.base_asset_amount_per_lp = perp_market + perp_market.amm.set_base_asset_amount_per_lp(perp_market .amm - .base_asset_amount_per_lp - .safe_mul(rebase_divisor)?; + .base_asset_amount_per_lp() + .safe_mul(rebase_divisor)?); - perp_market.amm.quote_asset_amount_per_lp = perp_market + perp_market.amm.set_quote_asset_amount_per_lp(perp_market .amm - .quote_asset_amount_per_lp - .safe_mul(rebase_divisor)?; + .quote_asset_amount_per_lp() + .safe_mul(rebase_divisor)?); } else { - perp_market.amm.base_asset_amount_per_lp = perp_market + perp_market.amm.set_base_asset_amount_per_lp(perp_market .amm - .base_asset_amount_per_lp - .safe_div(rebase_divisor)?; + .base_asset_amount_per_lp() + .safe_div(rebase_divisor)?); - perp_market.amm.quote_asset_amount_per_lp = perp_market + perp_market.amm.set_quote_asset_amount_per_lp(perp_market .amm - .quote_asset_amount_per_lp - .safe_div(rebase_divisor)?; + .quote_asset_amount_per_lp() + .safe_div(rebase_divisor)?); } msg!( @@ -260,10 +260,10 @@ pub fn burn_lp_shares( let base_asset_amount = position.remainder_base_asset_amount as i128; // user closes the dust - market.amm.base_asset_amount_with_amm = market + market.amm.set_base_asset_amount_with_amm(market .amm - .base_asset_amount_with_amm - .safe_sub(base_asset_amount)?; + .base_asset_amount_with_amm() + .safe_sub(base_asset_amount)?); market.amm.base_asset_amount_with_unsettled_lp = market .amm diff --git a/programs/drift/src/controller/orders.rs b/programs/drift/src/controller/orders.rs index bc8b3aec4c..da3ac1fca0 100644 --- a/programs/drift/src/controller/orders.rs +++ b/programs/drift/src/controller/orders.rs @@ -391,17 +391,17 @@ pub fn place_perp_order( )?; } - let max_oi = market.amm.max_open_interest; + let max_oi = market.amm.max_open_interest(); if max_oi != 0 && risk_increasing { let oi_plus_order = match params.direction { PositionDirection::Long => market .amm - .base_asset_amount_long + .base_asset_amount_long() .safe_add(order_base_asset_amount.cast()?)? .unsigned_abs(), PositionDirection::Short => market .amm - .base_asset_amount_short + .base_asset_amount_short() .safe_sub(order_base_asset_amount.cast()?)? .unsigned_abs(), }; @@ -1349,7 +1349,7 @@ pub fn fill_perp_order( let market = perp_market_map.get_ref(&market_index)?; let open_interest = market.get_open_interest(); - let max_open_interest = market.amm.max_open_interest; + let max_open_interest = market.amm.max_open_interest(); validate!( max_open_interest == 0 || max_open_interest > open_interest, @@ -2294,16 +2294,24 @@ pub fn fulfill_perp_order_with_amm( } // Increment the protocol's total fee variables - market.amm.total_fee = market.amm.total_fee.safe_add(fee_to_market.cast()?)?; - market.amm.total_exchange_fee = market.amm.total_exchange_fee.safe_add(user_fee.cast()?)?; - market.amm.total_mm_fee = market + market .amm - .total_mm_fee - .safe_add(quote_asset_amount_surplus.cast()?)?; - market.amm.total_fee_minus_distributions = market + .set_total_fee(market.amm.total_fee().safe_add(fee_to_market.cast()?)?); + market .amm - .total_fee_minus_distributions - .safe_add(fee_to_market.cast()?)?; + .set_total_exchange_fee(market.amm.total_exchange_fee().safe_add(user_fee.cast()?)?); + market.amm.set_total_mm_fee( + market + .amm + .total_mm_fee() + .safe_add(quote_asset_amount_surplus.cast()?)?, + ); + market.amm.set_total_fee_minus_distributions( + market + .amm + .total_fee_minus_distributions() + .safe_add(fee_to_market.cast()?)?, + ); market.amm.net_revenue_since_last_funding = market .amm .net_revenue_since_last_funding @@ -2807,15 +2815,21 @@ pub fn fulfill_perp_order_with_match( } // Increment the markets house's total fee variables - market.amm.total_fee = market.amm.total_fee.safe_add(fee_to_market.cast()?)?; - market.amm.total_exchange_fee = market - .amm - .total_exchange_fee - .safe_add(fee_to_market.cast()?)?; - market.amm.total_fee_minus_distributions = market + market .amm - .total_fee_minus_distributions - .safe_add(fee_to_market.cast()?)?; + .set_total_fee(market.amm.total_fee().safe_add(fee_to_market.cast()?)?); + market.amm.set_total_exchange_fee( + market + .amm + .total_exchange_fee() + .safe_add(fee_to_market.cast()?)?, + ); + market.amm.set_total_fee_minus_distributions( + market + .amm + .total_fee_minus_distributions() + .safe_add(fee_to_market.cast()?)?, + ); market.amm.net_revenue_since_last_funding = market .amm .net_revenue_since_last_funding @@ -4986,7 +5000,11 @@ pub fn fulfill_spot_order_with_match( } // Update base market - base_market.total_spot_fee = base_market.total_spot_fee.safe_add(fee_to_market.cast()?)?; + base_market.set_total_spot_fee( + base_market + .total_spot_fee() + .safe_add(fee_to_market.cast()?)?, + ); update_spot_balances( fee_to_market.cast()?, @@ -5154,7 +5172,7 @@ pub fn fulfill_spot_order_with_external_market( )?; let fee_pool_amount = get_token_amount( - base_market.spot_fee_pool.scaled_balance, + base_market.spot_fee_pool.scaled_balance(), quote_market, &SpotBalanceType::Deposit, )?; @@ -5275,7 +5293,11 @@ pub fn fulfill_spot_order_with_external_market( )?; } - base_market.total_spot_fee = base_market.total_spot_fee.safe_add(fee_to_market.cast()?)?; + base_market.set_total_spot_fee( + base_market + .total_spot_fee() + .safe_add(fee_to_market.cast()?)?, + ); let fill_record_id = get_then_update_id!(base_market, next_fill_record_id); let order_action_record = get_order_action_record( diff --git a/programs/drift/src/controller/orders/amm_jit_tests.rs b/programs/drift/src/controller/orders/amm_jit_tests.rs index 87c160b78d..2f4e94b867 100644 --- a/programs/drift/src/controller/orders/amm_jit_tests.rs +++ b/programs/drift/src/controller/orders/amm_jit_tests.rs @@ -115,12 +115,12 @@ pub mod amm_jit { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: (AMM_RESERVE_PRECISION / 2) as i128, - base_asset_amount_long: (AMM_RESERVE_PRECISION / 2) as i128, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + base_asset_amount_with_amm: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + base_asset_amount_long: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -145,9 +145,9 @@ pub mod amm_jit { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.base_asset_reserve = 0; - market.amm.max_base_asset_reserve = u64::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_base_asset_reserve(0); + market.amm.set_max_base_asset_reserve(u64::MAX as u128); + market.amm.set_min_base_asset_reserve(0); let (new_ask_base_asset_reserve, new_ask_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Long) @@ -155,10 +155,18 @@ pub mod amm_jit { let (new_bid_base_asset_reserve, new_bid_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Short) .unwrap(); - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); // shouldnt throw an error when bids/asks are zero crate::math::amm_jit::calculate_jit_base_asset_amount( @@ -191,12 +199,12 @@ pub mod amm_jit { // net users are short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: (AMM_RESERVE_PRECISION / 2) as i128, - base_asset_amount_long: (AMM_RESERVE_PRECISION / 2) as i128, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + base_asset_amount_with_amm: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + base_asset_amount_long: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -221,8 +229,8 @@ pub mod amm_jit { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u64::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u64::MAX as u128); + market.amm.set_min_base_asset_reserve(0); let (new_ask_base_asset_reserve, new_ask_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Long) @@ -230,10 +238,18 @@ pub mod amm_jit { let (new_bid_base_asset_reserve, new_bid_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Short) .unwrap(); - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); assert_eq!(new_bid_quote_asset_reserve, 99000000000); create_anchor_account_info!(market, PerpMarket, market_account_info); @@ -242,7 +258,7 @@ pub mod amm_jit { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -373,8 +389,8 @@ pub mod amm_jit { let market_after = market_map.get_ref(&0).unwrap(); // amm jit doesnt take anything assert_eq!( - market_after.amm.base_asset_amount_with_amm, - market.amm.base_asset_amount_with_amm + market_after.amm.base_asset_amount_with_amm(), + market.amm.base_asset_amount_with_amm() ); } @@ -398,16 +414,16 @@ pub mod amm_jit { // net users are short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: -((AMM_RESERVE_PRECISION / 2) as i128), - base_asset_amount_short: -((AMM_RESERVE_PRECISION / 2) as i128), - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 90 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + base_asset_amount_with_amm: (-((AMM_RESERVE_PRECISION / 2) as i128)).into(), + base_asset_amount_short: (-((AMM_RESERVE_PRECISION / 2) as i128)).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (90 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -421,8 +437,8 @@ pub mod amm_jit { ..HistoricalOracleData::default() }, - user_lp_shares: 10 * AMM_RESERVE_PRECISION, // some lps exist - concentration_coef: CONCENTRATION_PRECISION + 1, + user_lp_shares: (10 * AMM_RESERVE_PRECISION).into(), // some lps exist + concentration_coef: (CONCENTRATION_PRECISION + 1).into(), ..AMM::default() }, margin_ratio_initial: 1000, @@ -430,8 +446,8 @@ pub mod amm_jit { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u64::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u64::MAX as u128); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -439,7 +455,7 @@ pub mod amm_jit { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -527,11 +543,11 @@ pub mod amm_jit { let mut filler_stats = UserStats::default(); - assert_eq!(market.amm.total_fee, 0); - assert_eq!(market.amm.total_fee_minus_distributions, 0); + assert_eq!(market.amm.total_fee(), 0); + assert_eq!(market.amm.total_fee_minus_distributions(), 0); assert_eq!(market.amm.net_revenue_since_last_funding, 0); - assert_eq!(market.amm.total_mm_fee, 0); - assert_eq!(market.amm.total_fee_withdrawn, 0); + assert_eq!(market.amm.total_mm_fee(), 0); + assert_eq!(market.amm.total_fee_withdrawn(), 0); let order_index = 0; let min_auction_duration = 10; @@ -584,12 +600,12 @@ pub mod amm_jit { // nets to zero let market_after = market_map.get_ref(&0).unwrap(); assert_eq!( - market_after.amm.base_asset_amount_with_amm, + market_after.amm.base_asset_amount_with_amm(), BASE_PRECISION_I128 / 2 ); // make sure lps didnt get anything - assert_eq!(market_after.amm.base_asset_amount_per_lp, 0); + assert_eq!(market_after.amm.base_asset_amount_per_lp(), 0); } #[test] @@ -612,16 +628,16 @@ pub mod amm_jit { // net users are short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: -((AMM_RESERVE_PRECISION / 2) as i128), - base_asset_amount_short: -((AMM_RESERVE_PRECISION / 2) as i128), - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + base_asset_amount_with_amm: (-((AMM_RESERVE_PRECISION / 2) as i128)).into(), + base_asset_amount_short: (-((AMM_RESERVE_PRECISION / 2) as i128)).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -635,8 +651,8 @@ pub mod amm_jit { ..HistoricalOracleData::default() }, - user_lp_shares: 10 * AMM_RESERVE_PRECISION, // some lps exist - concentration_coef: CONCENTRATION_PRECISION + 1, + user_lp_shares: (10 * AMM_RESERVE_PRECISION).into(), // some lps exist + concentration_coef: (CONCENTRATION_PRECISION + 1).into(), ..AMM::default() }, margin_ratio_initial: 1000, @@ -644,8 +660,8 @@ pub mod amm_jit { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u64::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u64::MAX as u128); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -653,7 +669,7 @@ pub mod amm_jit { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -741,11 +757,11 @@ pub mod amm_jit { let mut filler_stats = UserStats::default(); - assert_eq!(market.amm.total_fee, 0); - assert_eq!(market.amm.total_fee_minus_distributions, 0); + assert_eq!(market.amm.total_fee(), 0); + assert_eq!(market.amm.total_fee_minus_distributions(), 0); assert_eq!(market.amm.net_revenue_since_last_funding, 0); - assert_eq!(market.amm.total_mm_fee, 0); - assert_eq!(market.amm.total_fee_withdrawn, 0); + assert_eq!(market.amm.total_mm_fee(), 0); + assert_eq!(market.amm.total_fee_withdrawn(), 0); let order_index = 0; let min_auction_duration = 10; @@ -789,17 +805,17 @@ pub mod amm_jit { let market_after = market_map.get_ref(&0).unwrap(); // nets to zero - assert_eq!(market_after.amm.base_asset_amount_with_amm, 0); + assert_eq!(market_after.amm.base_asset_amount_with_amm(), 0); // make sure lps didnt get anything - assert_eq!(market_after.amm.base_asset_amount_per_lp, 0); + assert_eq!(market_after.amm.base_asset_amount_per_lp(), 0); let maker = makers_and_referrers.get_ref_mut(&maker_key).unwrap(); let maker_position = &maker.perp_positions[0]; // maker got (full - net_baa) assert_eq!( maker_position.base_asset_amount as i128, - -BASE_PRECISION_I128 * 2 - market.amm.base_asset_amount_with_amm + -BASE_PRECISION_I128 * 2 - market.amm.base_asset_amount_with_amm() ); } @@ -823,16 +839,16 @@ pub mod amm_jit { // net users are short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: (AMM_RESERVE_PRECISION / 2) as i128, - base_asset_amount_long: (AMM_RESERVE_PRECISION / 2) as i128, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + base_asset_amount_with_amm: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + base_asset_amount_long: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -854,8 +870,8 @@ pub mod amm_jit { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u64::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u64::MAX as u128); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -863,7 +879,7 @@ pub mod amm_jit { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -951,11 +967,11 @@ pub mod amm_jit { let maker_and_referrer_stats = UserStatsMap::load_one(&maker_stats_account_info).unwrap(); let mut filler_stats = UserStats::default(); - assert_eq!(market.amm.total_fee, 0); - assert_eq!(market.amm.total_fee_minus_distributions, 0); + assert_eq!(market.amm.total_fee(), 0); + assert_eq!(market.amm.total_fee_minus_distributions(), 0); assert_eq!(market.amm.net_revenue_since_last_funding, 0); - assert_eq!(market.amm.total_mm_fee, 0); - assert_eq!(market.amm.total_fee_withdrawn, 0); + assert_eq!(market.amm.total_mm_fee(), 0); + assert_eq!(market.amm.total_fee_withdrawn(), 0); let order_index = 0; let min_auction_duration = 10; @@ -999,14 +1015,14 @@ pub mod amm_jit { let market_after = market_map.get_ref(&0).unwrap(); // nets to zero - assert_eq!(market_after.amm.base_asset_amount_with_amm, 0); + assert_eq!(market_after.amm.base_asset_amount_with_amm(), 0); let maker = makers_and_referrers.get_ref_mut(&maker_key).unwrap(); let maker_position = &maker.perp_positions[0]; // maker got (full - net_baa) assert_eq!( maker_position.base_asset_amount as i128, - BASE_PRECISION_I128 * taker_mul as i128 - market.amm.base_asset_amount_with_amm + BASE_PRECISION_I128 * taker_mul as i128 - market.amm.base_asset_amount_with_amm() ); } @@ -1030,16 +1046,16 @@ pub mod amm_jit { // amm is short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), // bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, // bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, // ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, // ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: -((AMM_RESERVE_PRECISION / 2) as i128), - base_asset_amount_short: -((AMM_RESERVE_PRECISION / 2) as i128), - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_amount_with_amm: (-((AMM_RESERVE_PRECISION / 2) as i128)).into(), + base_asset_amount_short: (-((AMM_RESERVE_PRECISION / 2) as i128)).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -1063,18 +1079,26 @@ pub mod amm_jit { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u64::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u64::MAX as u128); + market.amm.set_min_base_asset_reserve(0); let (new_ask_base_asset_reserve, new_ask_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Long) .unwrap(); let (new_bid_base_asset_reserve, new_bid_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Short) .unwrap(); - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); assert_eq!(new_bid_quote_asset_reserve, 99000000000); @@ -1084,7 +1108,7 @@ pub mod amm_jit { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -1225,7 +1249,7 @@ pub mod amm_jit { assert_eq!(maker_position.open_orders, 0); let market_after = market_map.get_ref(&0).unwrap(); - assert_eq!(market_after.amm.base_asset_amount_with_amm, -1000000000); + assert_eq!(market_after.amm.base_asset_amount_with_amm(), -1000000000); } #[test] @@ -1248,15 +1272,15 @@ pub mod amm_jit { // net users are short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), base_spread: 250, long_spread: 125, short_spread: 125, - base_asset_amount_with_amm: (AMM_RESERVE_PRECISION / 2) as i128, - base_asset_amount_long: (AMM_RESERVE_PRECISION / 2) as i128, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_amount_with_amm: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + base_asset_amount_long: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -1278,8 +1302,8 @@ pub mod amm_jit { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u64::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u64::MAX as u128); + market.amm.set_min_base_asset_reserve(0); let (new_ask_base_asset_reserve, new_ask_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Long) @@ -1287,10 +1311,18 @@ pub mod amm_jit { let (new_bid_base_asset_reserve, new_bid_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Short) .unwrap(); - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -1298,7 +1330,7 @@ pub mod amm_jit { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -1385,11 +1417,11 @@ pub mod amm_jit { let maker_and_referrer_stats = UserStatsMap::load_one(&maker_stats_account_info).unwrap(); let mut filler_stats = UserStats::default(); - assert_eq!(market.amm.total_fee, 0); - assert_eq!(market.amm.total_fee_minus_distributions, 0); + assert_eq!(market.amm.total_fee(), 0); + assert_eq!(market.amm.total_fee_minus_distributions(), 0); assert_eq!(market.amm.net_revenue_since_last_funding, 0); - assert_eq!(market.amm.total_mm_fee, 0); - assert_eq!(market.amm.total_fee_withdrawn, 0); + assert_eq!(market.amm.total_mm_fee(), 0); + assert_eq!(market.amm.total_fee_withdrawn(), 0); let order_index = 0; let min_auction_duration = 10; @@ -1436,11 +1468,12 @@ pub mod amm_jit { let market_after = market_map.get_ref(&0).unwrap(); assert!( - market_after.amm.base_asset_amount_with_amm.abs() - < market.amm.base_asset_amount_with_amm.abs() + market_after.amm.base_asset_amount_with_amm().abs() + < market.amm.base_asset_amount_with_amm().abs() ); - let quote_asset_amount_surplus = market_after.amm.total_mm_fee - market.amm.total_mm_fee; + let quote_asset_amount_surplus = + market_after.amm.total_mm_fee() - market.amm.total_mm_fee(); assert!(quote_asset_amount_surplus > 0); } @@ -1464,24 +1497,24 @@ pub mod amm_jit { // net users are short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), base_spread: 250, long_spread: 125, short_spread: 125, max_spread: 20000, - base_asset_amount_with_amm: (AMM_RESERVE_PRECISION / 2) as i128, - base_asset_amount_long: (AMM_RESERVE_PRECISION / 2) as i128, - user_lp_shares: 20 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_amount_with_amm: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + base_asset_amount_long: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + user_lp_shares: (20 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, order_tick_size: 1, oracle: oracle_price_key, amm_jit_intensity: 100, - concentration_coef: CONCENTRATION_PRECISION + 1, + concentration_coef: (CONCENTRATION_PRECISION + 1).into(), historical_oracle_data: HistoricalOracleData { last_oracle_price: (100 * PRICE_PRECISION) as i64, last_oracle_price_twap: (100 * PRICE_PRECISION) as i64, @@ -1497,8 +1530,8 @@ pub mod amm_jit { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u64::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u64::MAX as u128); + market.amm.set_min_base_asset_reserve(0); let (new_ask_base_asset_reserve, new_ask_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Long) @@ -1506,10 +1539,18 @@ pub mod amm_jit { let (new_bid_base_asset_reserve, new_bid_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Short) .unwrap(); - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -1517,7 +1558,7 @@ pub mod amm_jit { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -1604,11 +1645,11 @@ pub mod amm_jit { let maker_and_referrer_stats = UserStatsMap::load_one(&maker_stats_account_info).unwrap(); let mut filler_stats = UserStats::default(); - assert_eq!(market.amm.total_fee, 0); - assert_eq!(market.amm.total_fee_minus_distributions, 0); + assert_eq!(market.amm.total_fee(), 0); + assert_eq!(market.amm.total_fee_minus_distributions(), 0); assert_eq!(market.amm.net_revenue_since_last_funding, 0); - assert_eq!(market.amm.total_mm_fee, 0); - assert_eq!(market.amm.total_fee_withdrawn, 0); + assert_eq!(market.amm.total_mm_fee(), 0); + assert_eq!(market.amm.total_fee_withdrawn(), 0); let (base_asset_amount, _) = fulfill_perp_order( &mut taker, @@ -1643,7 +1684,8 @@ pub mod amm_jit { assert_eq!(base_asset_amount, 500000000); // 1/2 base (half of otherwise) let market_after = market_map.get_ref(&0).unwrap(); - let quote_asset_amount_surplus = market_after.amm.total_mm_fee - market.amm.total_mm_fee; + let quote_asset_amount_surplus = + market_after.amm.total_mm_fee() - market.amm.total_mm_fee(); assert_eq!(quote_asset_amount_surplus, 0); @@ -1671,16 +1713,16 @@ pub mod amm_jit { // net users are short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: -((AMM_RESERVE_PRECISION / 2) as i128), - base_asset_amount_short: -((AMM_RESERVE_PRECISION / 2) as i128), - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + base_asset_amount_with_amm: (-((AMM_RESERVE_PRECISION / 2) as i128)).into(), + base_asset_amount_short: (-((AMM_RESERVE_PRECISION / 2) as i128)).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -1702,8 +1744,8 @@ pub mod amm_jit { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u64::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u64::MAX as u128); + market.amm.set_min_base_asset_reserve(0); let (new_ask_base_asset_reserve, new_ask_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Long) @@ -1711,10 +1753,18 @@ pub mod amm_jit { let (new_bid_base_asset_reserve, new_bid_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Short) .unwrap(); - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -1722,7 +1772,7 @@ pub mod amm_jit { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -1854,8 +1904,8 @@ pub mod amm_jit { // net baa improves let market_after = market_map.get_ref(&0).unwrap(); assert!( - market_after.amm.base_asset_amount_with_amm.abs() - < market.amm.base_asset_amount_with_amm.abs() + market_after.amm.base_asset_amount_with_amm().abs() + < market.amm.base_asset_amount_with_amm().abs() ); } @@ -1879,16 +1929,16 @@ pub mod amm_jit { // net users are short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: -((AMM_RESERVE_PRECISION / 2) as i128), - base_asset_amount_short: -((AMM_RESERVE_PRECISION / 2) as i128), - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + base_asset_amount_with_amm: (-((AMM_RESERVE_PRECISION / 2) as i128)).into(), + base_asset_amount_short: (-((AMM_RESERVE_PRECISION / 2) as i128)).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -1910,8 +1960,8 @@ pub mod amm_jit { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u64::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u64::MAX as u128); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -1919,7 +1969,7 @@ pub mod amm_jit { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -2005,11 +2055,11 @@ pub mod amm_jit { let maker_and_referrer_stats = UserStatsMap::load_one(&maker_stats_account_info).unwrap(); let mut filler_stats = UserStats::default(); - assert_eq!(market.amm.total_fee, 0); - assert_eq!(market.amm.total_fee_minus_distributions, 0); + assert_eq!(market.amm.total_fee(), 0); + assert_eq!(market.amm.total_fee_minus_distributions(), 0); assert_eq!(market.amm.net_revenue_since_last_funding, 0); - assert_eq!(market.amm.total_mm_fee, 0); - assert_eq!(market.amm.total_fee_withdrawn, 0); + assert_eq!(market.amm.total_mm_fee(), 0); + assert_eq!(market.amm.total_fee_withdrawn(), 0); let order_index = 0; let min_auction_duration = 10; @@ -2059,16 +2109,17 @@ pub mod amm_jit { assert_eq!(maker_position.base_asset_amount, -BASE_PRECISION_I64 / 2); let market_after = market_map.get_ref(&0).unwrap(); - assert_eq!(market_after.amm.base_asset_amount_with_amm, -250000000); + assert_eq!(market_after.amm.base_asset_amount_with_amm(), -250000000); let taker_position = &taker.perp_positions[0]; assert_eq!( taker_position.base_asset_amount, - BASE_PRECISION_I64 + market_after.amm.base_asset_amount_with_amm as i64 + BASE_PRECISION_I64 + market_after.amm.base_asset_amount_with_amm() as i64 ); // market pays extra for trade - let quote_asset_amount_surplus = market_after.amm.total_mm_fee - market.amm.total_mm_fee; + let quote_asset_amount_surplus = + market_after.amm.total_mm_fee() - market.amm.total_mm_fee(); assert!(quote_asset_amount_surplus < 0); } @@ -2092,16 +2143,16 @@ pub mod amm_jit { // net users are short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: (AMM_RESERVE_PRECISION / 2) as i128, - base_asset_amount_long: (AMM_RESERVE_PRECISION / 2) as i128, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + base_asset_amount_with_amm: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + base_asset_amount_long: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 100, @@ -2123,8 +2174,8 @@ pub mod amm_jit { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u64::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u64::MAX as u128); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -2132,7 +2183,7 @@ pub mod amm_jit { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -2218,11 +2269,11 @@ pub mod amm_jit { let maker_and_referrer_stats = UserStatsMap::load_one(&maker_stats_account_info).unwrap(); let mut filler_stats = UserStats::default(); - assert_eq!(market.amm.total_fee, 0); - assert_eq!(market.amm.total_fee_minus_distributions, 0); + assert_eq!(market.amm.total_fee(), 0); + assert_eq!(market.amm.total_fee_minus_distributions(), 0); assert_eq!(market.amm.net_revenue_since_last_funding, 0); - assert_eq!(market.amm.total_mm_fee, 0); - assert_eq!(market.amm.total_fee_withdrawn, 0); + assert_eq!(market.amm.total_mm_fee(), 0); + assert_eq!(market.amm.total_fee_withdrawn(), 0); let order_index = 0; let min_auction_duration = 10; @@ -2276,7 +2327,8 @@ pub mod amm_jit { ); // mm gains from trade - let quote_asset_amount_surplus = market_after.amm.total_mm_fee - market.amm.total_mm_fee; + let quote_asset_amount_surplus = + market_after.amm.total_mm_fee() - market.amm.total_mm_fee(); assert!(quote_asset_amount_surplus < 0); } @@ -2302,12 +2354,12 @@ pub mod amm_jit { let reserves = 5 * AMM_RESERVE_PRECISION; let mut market = PerpMarket { amm: AMM { - base_asset_reserve: reserves, - quote_asset_reserve: reserves, - base_asset_amount_with_amm: -(100 * AMM_RESERVE_PRECISION as i128), - base_asset_amount_short: -(100 * AMM_RESERVE_PRECISION as i128), - sqrt_k: reserves, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: reserves.into(), + quote_asset_reserve: reserves.into(), + base_asset_amount_with_amm: (-(100 * AMM_RESERVE_PRECISION as i128)).into(), + base_asset_amount_short: (-(100 * AMM_RESERVE_PRECISION as i128)).into(), + sqrt_k: reserves.into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -2332,8 +2384,8 @@ pub mod amm_jit { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u64::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u64::MAX as u128); + market.amm.set_min_base_asset_reserve(0); let (new_ask_base_asset_reserve, new_ask_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Long) @@ -2341,10 +2393,18 @@ pub mod amm_jit { let (new_bid_base_asset_reserve, new_bid_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Short) .unwrap(); - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -2352,7 +2412,7 @@ pub mod amm_jit { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -2415,15 +2475,15 @@ pub mod amm_jit { let maker_and_referrer_stats = UserStatsMap::load_one(&maker_stats_account_info).unwrap(); let mut filler_stats = UserStats::default(); - assert_eq!(market.amm.total_fee, 0); - assert_eq!(market.amm.total_fee_minus_distributions, 0); + assert_eq!(market.amm.total_fee(), 0); + assert_eq!(market.amm.total_fee_minus_distributions(), 0); assert_eq!(market.amm.net_revenue_since_last_funding, 0); - assert_eq!(market.amm.total_mm_fee, 0); - assert_eq!(market.amm.total_fee_withdrawn, 0); + assert_eq!(market.amm.total_mm_fee(), 0); + assert_eq!(market.amm.total_fee_withdrawn(), 0); let (mut neg, mut pos, mut none) = (false, false, false); let mut prev_mm_fee = 0; - let mut prev_net_baa = market.amm.base_asset_amount_with_amm; + let mut prev_net_baa = market.amm.base_asset_amount_with_amm(); // track scaling let mut prev_qas = 0; let mut has_set_prev_qas = false; @@ -2519,12 +2579,12 @@ pub mod amm_jit { .unwrap(); let market_after = market_map.get_ref(&0).unwrap(); - let quote_asset_amount_surplus = market_after.amm.total_mm_fee - prev_mm_fee; - prev_mm_fee = market_after.amm.total_mm_fee; + let quote_asset_amount_surplus = market_after.amm.total_mm_fee() - prev_mm_fee; + prev_mm_fee = market_after.amm.total_mm_fee(); // imbalance decreases - assert!(market_after.amm.base_asset_amount_with_amm.abs() < prev_net_baa.abs()); - prev_net_baa = market_after.amm.base_asset_amount_with_amm; + assert!(market_after.amm.base_asset_amount_with_amm().abs() < prev_net_baa.abs()); + prev_net_baa = market_after.amm.base_asset_amount_with_amm(); println!( "slot {} auction: {} surplus: {}", @@ -2584,12 +2644,12 @@ pub mod amm_jit { let reserves = 5 * AMM_RESERVE_PRECISION; let mut market = PerpMarket { amm: AMM { - base_asset_reserve: reserves, - quote_asset_reserve: reserves, - base_asset_amount_with_amm: 100 * AMM_RESERVE_PRECISION as i128, - base_asset_amount_long: 100 * AMM_RESERVE_PRECISION as i128, - sqrt_k: reserves, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: reserves.into(), + quote_asset_reserve: reserves.into(), + base_asset_amount_with_amm: (100 * AMM_RESERVE_PRECISION as i128).into(), + base_asset_amount_long: (100 * AMM_RESERVE_PRECISION as i128).into(), + sqrt_k: reserves.into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1, @@ -2615,8 +2675,8 @@ pub mod amm_jit { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u64::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u64::MAX as u128); + market.amm.set_min_base_asset_reserve(0); let (new_ask_base_asset_reserve, new_ask_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Long) @@ -2624,10 +2684,18 @@ pub mod amm_jit { let (new_bid_base_asset_reserve, new_bid_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Short) .unwrap(); - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -2635,7 +2703,7 @@ pub mod amm_jit { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -2699,15 +2767,15 @@ pub mod amm_jit { let maker_and_referrer_stats = UserStatsMap::load_one(&maker_stats_account_info).unwrap(); let mut filler_stats = UserStats::default(); - assert_eq!(market.amm.total_fee, 0); - assert_eq!(market.amm.total_fee_minus_distributions, 0); + assert_eq!(market.amm.total_fee(), 0); + assert_eq!(market.amm.total_fee_minus_distributions(), 0); assert_eq!(market.amm.net_revenue_since_last_funding, 0); - assert_eq!(market.amm.total_mm_fee, 0); - assert_eq!(market.amm.total_fee_withdrawn, 0); + assert_eq!(market.amm.total_mm_fee(), 0); + assert_eq!(market.amm.total_fee_withdrawn(), 0); let (mut neg, mut pos, mut none) = (false, false, false); let mut prev_mm_fee = 0; - let mut prev_net_baa = market.amm.base_asset_amount_with_amm; + let mut prev_net_baa = market.amm.base_asset_amount_with_amm(); // track scaling let mut prev_qas = 0; let mut has_set_prev_qas = false; @@ -2818,12 +2886,12 @@ pub mod amm_jit { .unwrap(); let market_after = market_map.get_ref(&0).unwrap(); - let quote_asset_amount_surplus = market_after.amm.total_mm_fee - prev_mm_fee; - prev_mm_fee = market_after.amm.total_mm_fee; + let quote_asset_amount_surplus = market_after.amm.total_mm_fee() - prev_mm_fee; + prev_mm_fee = market_after.amm.total_mm_fee(); // imbalance decreases or remains the same (damm wont always take on positions) - assert!(market_after.amm.base_asset_amount_with_amm.abs() <= prev_net_baa.abs()); - prev_net_baa = market_after.amm.base_asset_amount_with_amm; + assert!(market_after.amm.base_asset_amount_with_amm().abs() <= prev_net_baa.abs()); + prev_net_baa = market_after.amm.base_asset_amount_with_amm(); println!( "slot {} auction: {} surplus: {}", @@ -2881,16 +2949,16 @@ pub mod amm_jit { // net users are short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: ((AMM_RESERVE_PRECISION / 2) as i128), - base_asset_amount_long: ((AMM_RESERVE_PRECISION / 2) as i128), - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + base_asset_amount_with_amm: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + base_asset_amount_long: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -2912,8 +2980,8 @@ pub mod amm_jit { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u64::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u64::MAX as u128); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -2921,7 +2989,7 @@ pub mod amm_jit { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -3011,11 +3079,11 @@ pub mod amm_jit { let maker_and_referrer_stats = UserStatsMap::load_one(&maker_stats_account_info).unwrap(); let mut filler_stats = UserStats::default(); - assert_eq!(market.amm.total_fee, 0); - assert_eq!(market.amm.total_fee_minus_distributions, 0); + assert_eq!(market.amm.total_fee(), 0); + assert_eq!(market.amm.total_fee_minus_distributions(), 0); assert_eq!(market.amm.net_revenue_since_last_funding, 0); - assert_eq!(market.amm.total_mm_fee, 0); - assert_eq!(market.amm.total_fee_withdrawn, 0); + assert_eq!(market.amm.total_mm_fee(), 0); + assert_eq!(market.amm.total_fee_withdrawn(), 0); assert_eq!(taker.perp_positions[0].open_orders, 1); // fulfill with match @@ -3064,7 +3132,7 @@ pub mod amm_jit { assert_eq!(base_asset_amount, 1000000000); let market_after = market_map.get_ref(&0).unwrap(); - assert_eq!(market_after.amm.total_mm_fee, 2033008); // jit occured even tho maker offered full amount - assert_eq!(market_after.amm.total_fee, 2057287); + assert_eq!(market_after.amm.total_mm_fee(), 2033008); // jit occured even tho maker offered full amount + assert_eq!(market_after.amm.total_fee(), 2057287); } } diff --git a/programs/drift/src/controller/orders/fuel_tests.rs b/programs/drift/src/controller/orders/fuel_tests.rs index 24959f9e3b..3b68848065 100644 --- a/programs/drift/src/controller/orders/fuel_tests.rs +++ b/programs/drift/src/controller/orders/fuel_tests.rs @@ -124,12 +124,12 @@ pub mod fuel_scoring { // net users are short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: (AMM_RESERVE_PRECISION / 2) as i128, - base_asset_amount_long: (AMM_RESERVE_PRECISION / 2) as i128, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + base_asset_amount_with_amm: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + base_asset_amount_long: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -157,8 +157,8 @@ pub mod fuel_scoring { fuel_boost_taker: 50, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u64::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u64::MAX as u128); + market.amm.set_min_base_asset_reserve(0); let (new_ask_base_asset_reserve, new_ask_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Long) @@ -166,10 +166,18 @@ pub mod fuel_scoring { let (new_bid_base_asset_reserve, new_bid_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Short) .unwrap(); - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); assert_eq!(new_bid_quote_asset_reserve, 99000000000); create_anchor_account_info!(market, PerpMarket, market_account_info); @@ -178,7 +186,7 @@ pub mod fuel_scoring { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -313,8 +321,8 @@ pub mod fuel_scoring { let market_after = market_map.get_ref(&0).unwrap(); assert_eq!( - market_after.amm.base_asset_amount_with_amm, - market.amm.base_asset_amount_with_amm + market_after.amm.base_asset_amount_with_amm(), + market.amm.base_asset_amount_with_amm() ); assert_ne!(taker.get_perp_position(0).unwrap().base_asset_amount, 0); let maker_after: std::cell::RefMut = @@ -400,12 +408,12 @@ pub mod fuel_scoring { // net users are short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: (AMM_RESERVE_PRECISION / 2) as i128, - base_asset_amount_long: (AMM_RESERVE_PRECISION / 2) as i128, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + base_asset_amount_with_amm: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + base_asset_amount_long: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -433,8 +441,8 @@ pub mod fuel_scoring { fuel_boost_taker: 50, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u64::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u64::MAX as u128); + market.amm.set_min_base_asset_reserve(0); let (new_ask_base_asset_reserve, new_ask_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Long) @@ -442,10 +450,18 @@ pub mod fuel_scoring { let (new_bid_base_asset_reserve, new_bid_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Short) .unwrap(); - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); assert_eq!(new_bid_quote_asset_reserve, 99000000000); create_anchor_account_info!(market, PerpMarket, market_account_info); @@ -454,7 +470,7 @@ pub mod fuel_scoring { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -471,8 +487,8 @@ pub mod fuel_scoring { market_index: 1, oracle_source: OracleSource::Pyth, oracle: oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION * 2, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION * 4, + cumulative_deposit_interest: (SPOT_CUMULATIVE_INTEREST_PRECISION * 2).into(), + cumulative_borrow_interest: (SPOT_CUMULATIVE_INTEREST_PRECISION * 4).into(), decimals: 9, initial_asset_weight: SPOT_WEIGHT_PRECISION * 8 / 10, maintenance_asset_weight: SPOT_WEIGHT_PRECISION * 9 / 10, @@ -586,8 +602,8 @@ pub mod fuel_scoring { let market_after = market_map.get_ref(&0).unwrap(); assert_eq!( - market_after.amm.base_asset_amount_with_amm, - market.amm.base_asset_amount_with_amm + market_after.amm.base_asset_amount_with_amm(), + market.amm.base_asset_amount_with_amm() ); assert_eq!(taker.get_perp_position(0).unwrap().base_asset_amount, 0); now += 86400; // one day @@ -670,12 +686,12 @@ pub mod fuel_scoring { // net users are short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: (AMM_RESERVE_PRECISION / 2) as i128, - base_asset_amount_long: (AMM_RESERVE_PRECISION / 2) as i128, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + base_asset_amount_with_amm: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + base_asset_amount_long: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -703,8 +719,8 @@ pub mod fuel_scoring { fuel_boost_taker: 50, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u64::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u64::MAX as u128); + market.amm.set_min_base_asset_reserve(0); let (new_ask_base_asset_reserve, new_ask_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Long) @@ -712,10 +728,18 @@ pub mod fuel_scoring { let (new_bid_base_asset_reserve, new_bid_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Short) .unwrap(); - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); assert_eq!(new_bid_quote_asset_reserve, 99000000000); create_anchor_account_info!(market, PerpMarket, market_account_info); @@ -724,7 +748,7 @@ pub mod fuel_scoring { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -817,8 +841,8 @@ pub mod fuel_scoring { let market_after = market_map.get_ref(&0).unwrap(); assert_eq!( - market_after.amm.base_asset_amount_with_amm, - market.amm.base_asset_amount_with_amm + market_after.amm.base_asset_amount_with_amm(), + market.amm.base_asset_amount_with_amm() ); assert_eq!(taker.get_perp_position(0).unwrap().base_asset_amount, 0); now += 86400; // one day diff --git a/programs/drift/src/controller/orders/tests.rs b/programs/drift/src/controller/orders/tests.rs index 5a51991cba..46428f1dbf 100644 --- a/programs/drift/src/controller/orders/tests.rs +++ b/programs/drift/src/controller/orders/tests.rs @@ -137,11 +137,11 @@ pub mod fill_order_protected_maker { let mut market = PerpMarket { paused_operations: PerpOperation::AmmFill as u8, amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - terminal_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + terminal_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 100, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -165,25 +165,33 @@ pub mod fill_order_protected_maker { ..PerpMarket::default() }; market.status = MarketStatus::Active; - market.amm.max_base_asset_reserve = i128::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(i128::MAX as u128); + market.amm.set_min_base_asset_reserve(0); let (new_ask_base_asset_reserve, new_ask_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Long) .unwrap(); let (new_bid_base_asset_reserve, new_bid_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Short) .unwrap(); - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -571,12 +579,12 @@ pub mod fulfill_order_with_maker_order { assert_eq!(maker_stats.maker_volume_30d, 100 * QUOTE_PRECISION_U64); assert!(maker.orders[0].is_available()); - assert_eq!(market.amm.base_asset_amount_with_amm, 0); - assert_eq!(market.amm.base_asset_amount_long, BASE_PRECISION_I128); - assert_eq!(market.amm.base_asset_amount_short, -BASE_PRECISION_I128); - assert_eq!(market.amm.quote_asset_amount, -20000); - assert_eq!(market.amm.total_fee, 20000); - assert_eq!(market.amm.total_fee_minus_distributions, 20000); + assert_eq!(market.amm.base_asset_amount_with_amm(), 0); + assert_eq!(market.amm.base_asset_amount_long(), BASE_PRECISION_I128); + assert_eq!(market.amm.base_asset_amount_short(), -BASE_PRECISION_I128); + assert_eq!(market.amm.quote_asset_amount(), -20000); + assert_eq!(market.amm.total_fee(), 20000); + assert_eq!(market.amm.total_fee_minus_distributions(), 20000); assert_eq!(market.amm.net_revenue_since_last_funding, 20000); } @@ -696,12 +704,12 @@ pub mod fulfill_order_with_maker_order { assert_eq!(maker_stats.maker_volume_30d, 160 * QUOTE_PRECISION_U64); assert!(maker.orders[0].is_available()); - assert_eq!(market.amm.base_asset_amount_with_amm, 0); - assert_eq!(market.amm.base_asset_amount_long, BASE_PRECISION_I128); - assert_eq!(market.amm.base_asset_amount_short, -BASE_PRECISION_I128); - assert_eq!(market.amm.quote_asset_amount, -32000); - assert_eq!(market.amm.total_fee, 32000); - assert_eq!(market.amm.total_fee_minus_distributions, 32000); + assert_eq!(market.amm.base_asset_amount_with_amm(), 0); + assert_eq!(market.amm.base_asset_amount_long(), BASE_PRECISION_I128); + assert_eq!(market.amm.base_asset_amount_short(), -BASE_PRECISION_I128); + assert_eq!(market.amm.quote_asset_amount(), -32000); + assert_eq!(market.amm.total_fee(), 32000); + assert_eq!(market.amm.total_fee_minus_distributions(), 32000); assert_eq!(market.amm.net_revenue_since_last_funding, 32000); } @@ -821,12 +829,12 @@ pub mod fulfill_order_with_maker_order { assert_eq!(maker_stats.maker_volume_30d, 180 * QUOTE_PRECISION_U64); assert!(maker.orders[0].is_available()); - assert_eq!(market.amm.base_asset_amount_with_amm, 0); - assert_eq!(market.amm.base_asset_amount_long, BASE_PRECISION_I128); - assert_eq!(market.amm.base_asset_amount_short, -BASE_PRECISION_I128); - assert_eq!(market.amm.quote_asset_amount, -36000); - assert_eq!(market.amm.total_fee, 36000); - assert_eq!(market.amm.total_fee_minus_distributions, 36000); + assert_eq!(market.amm.base_asset_amount_with_amm(), 0); + assert_eq!(market.amm.base_asset_amount_long(), BASE_PRECISION_I128); + assert_eq!(market.amm.base_asset_amount_short(), -BASE_PRECISION_I128); + assert_eq!(market.amm.quote_asset_amount(), -36000); + assert_eq!(market.amm.total_fee(), 36000); + assert_eq!(market.amm.total_fee_minus_distributions(), 36000); assert_eq!(market.amm.net_revenue_since_last_funding, 36000); } @@ -946,12 +954,12 @@ pub mod fulfill_order_with_maker_order { assert_eq!(maker_stats.maker_volume_30d, 140 * QUOTE_PRECISION_U64); assert!(maker.orders[0].is_available()); - assert_eq!(market.amm.base_asset_amount_with_amm, 0); - assert_eq!(market.amm.base_asset_amount_long, BASE_PRECISION_I128); - assert_eq!(market.amm.base_asset_amount_short, -BASE_PRECISION_I128); - assert_eq!(market.amm.quote_asset_amount, -28000); - assert_eq!(market.amm.total_fee, 28000); - assert_eq!(market.amm.total_fee_minus_distributions, 28000); + assert_eq!(market.amm.base_asset_amount_with_amm(), 0); + assert_eq!(market.amm.base_asset_amount_long(), BASE_PRECISION_I128); + assert_eq!(market.amm.base_asset_amount_short(), -BASE_PRECISION_I128); + assert_eq!(market.amm.quote_asset_amount(), -28000); + assert_eq!(market.amm.total_fee(), 28000); + assert_eq!(market.amm.total_fee_minus_distributions(), 28000); assert_eq!(market.amm.net_revenue_since_last_funding, 28000); } @@ -1427,10 +1435,10 @@ pub mod fulfill_order_with_maker_order { assert_eq!(maker_position.quote_break_even_amount, 120072000); assert_eq!(maker_stats.maker_volume_30d, 120 * QUOTE_PRECISION_U64); - assert_eq!(market.amm.base_asset_amount_with_amm, 0); - assert_eq!(market.amm.base_asset_amount_long, BASE_PRECISION_I128); - assert_eq!(market.amm.base_asset_amount_short, -BASE_PRECISION_I128); - assert_eq!(market.amm.quote_asset_amount, -48000); + assert_eq!(market.amm.base_asset_amount_with_amm(), 0); + assert_eq!(market.amm.base_asset_amount_long(), BASE_PRECISION_I128); + assert_eq!(market.amm.base_asset_amount_short(), -BASE_PRECISION_I128); + assert_eq!(market.amm.quote_asset_amount(), -48000); } #[test] @@ -1539,10 +1547,10 @@ pub mod fulfill_order_with_maker_order { assert_eq!(maker_position.quote_break_even_amount, 120072000); assert_eq!(maker_stats.maker_volume_30d, 120 * QUOTE_PRECISION_U64); - assert_eq!(market.amm.base_asset_amount_with_amm, 0); - assert_eq!(market.amm.base_asset_amount_long, BASE_PRECISION_I128); - assert_eq!(market.amm.base_asset_amount_short, -BASE_PRECISION_I128); - assert_eq!(market.amm.quote_asset_amount, -48000); + assert_eq!(market.amm.base_asset_amount_with_amm(), 0); + assert_eq!(market.amm.base_asset_amount_long(), BASE_PRECISION_I128); + assert_eq!(market.amm.base_asset_amount_short(), -BASE_PRECISION_I128); + assert_eq!(market.amm.quote_asset_amount(), -48000); } #[test] @@ -1666,12 +1674,12 @@ pub mod fulfill_order_with_maker_order { assert_eq!(maker_stats.maker_volume_30d, 150 * QUOTE_PRECISION_U64); assert!(maker.orders[0].is_available()); - assert_eq!(market.amm.base_asset_amount_with_amm, 0); - assert_eq!(market.amm.base_asset_amount_long, BASE_PRECISION_I128); - assert_eq!(market.amm.base_asset_amount_short, -BASE_PRECISION_I128); - assert_eq!(market.amm.quote_asset_amount, -30000); - assert_eq!(market.amm.total_fee, 30000); - assert_eq!(market.amm.total_fee_minus_distributions, 30000); + assert_eq!(market.amm.base_asset_amount_with_amm(), 0); + assert_eq!(market.amm.base_asset_amount_long(), BASE_PRECISION_I128); + assert_eq!(market.amm.base_asset_amount_short(), -BASE_PRECISION_I128); + assert_eq!(market.amm.quote_asset_amount(), -30000); + assert_eq!(market.amm.total_fee(), 30000); + assert_eq!(market.amm.total_fee_minus_distributions(), 30000); assert_eq!(market.amm.net_revenue_since_last_funding, 30000); } @@ -1788,12 +1796,12 @@ pub mod fulfill_order_with_maker_order { assert_eq!(taker_stats.taker_volume_30d, 100 * QUOTE_PRECISION_U64); assert!(taker.orders[0].is_available()); - assert_eq!(market.amm.base_asset_amount_with_amm, 0); - assert_eq!(market.amm.base_asset_amount_long, BASE_PRECISION_I128); - assert_eq!(market.amm.base_asset_amount_short, -BASE_PRECISION_I128); - assert_eq!(market.amm.quote_asset_amount, -20000); - assert_eq!(market.amm.total_fee, 20000); - assert_eq!(market.amm.total_fee_minus_distributions, 20000); + assert_eq!(market.amm.base_asset_amount_with_amm(), 0); + assert_eq!(market.amm.base_asset_amount_long(), BASE_PRECISION_I128); + assert_eq!(market.amm.base_asset_amount_short(), -BASE_PRECISION_I128); + assert_eq!(market.amm.quote_asset_amount(), -20000); + assert_eq!(market.amm.total_fee(), 20000); + assert_eq!(market.amm.total_fee_minus_distributions(), 20000); assert_eq!(market.amm.net_revenue_since_last_funding, 20000); } @@ -1911,12 +1919,12 @@ pub mod fulfill_order_with_maker_order { assert_eq!(taker_stats.taker_volume_30d, 100 * QUOTE_PRECISION_U64); assert!(taker.orders[0].is_available()); - assert_eq!(market.amm.base_asset_amount_with_amm, 0); - assert_eq!(market.amm.base_asset_amount_long, BASE_PRECISION_I128); - assert_eq!(market.amm.base_asset_amount_short, -BASE_PRECISION_I128); - assert_eq!(market.amm.quote_asset_amount, -20000); - assert_eq!(market.amm.total_fee, 20000); - assert_eq!(market.amm.total_fee_minus_distributions, 20000); + assert_eq!(market.amm.base_asset_amount_with_amm(), 0); + assert_eq!(market.amm.base_asset_amount_long(), BASE_PRECISION_I128); + assert_eq!(market.amm.base_asset_amount_short(), -BASE_PRECISION_I128); + assert_eq!(market.amm.quote_asset_amount(), -20000); + assert_eq!(market.amm.total_fee(), 20000); + assert_eq!(market.amm.total_fee_minus_distributions(), 20000); assert_eq!(market.amm.net_revenue_since_last_funding, 20000); } @@ -1963,16 +1971,16 @@ pub mod fulfill_order_with_maker_order { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), short_spread: (BID_ASK_SPREAD_PRECISION / 10) as u32, long_spread: (BID_ASK_SPREAD_PRECISION / 10) as u32, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -2077,16 +2085,16 @@ pub mod fulfill_order_with_maker_order { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), short_spread: (BID_ASK_SPREAD_PRECISION / 10) as u32, long_spread: (BID_ASK_SPREAD_PRECISION / 10) as u32, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -2314,12 +2322,12 @@ pub mod fulfill_order_with_maker_order { assert_eq!(taker_stats.taker_volume_30d, 100 * QUOTE_PRECISION_U64); assert!(taker.orders[0].is_available()); - assert_eq!(market.amm.base_asset_amount_with_amm, 0); - assert_eq!(market.amm.base_asset_amount_long, BASE_PRECISION_I128); - assert_eq!(market.amm.base_asset_amount_short, -BASE_PRECISION_I128); - assert_eq!(market.amm.quote_asset_amount, -20000); - assert_eq!(market.amm.total_fee, 20000); - assert_eq!(market.amm.total_fee_minus_distributions, 20000); + assert_eq!(market.amm.base_asset_amount_with_amm(), 0); + assert_eq!(market.amm.base_asset_amount_long(), BASE_PRECISION_I128); + assert_eq!(market.amm.base_asset_amount_short(), -BASE_PRECISION_I128); + assert_eq!(market.amm.quote_asset_amount(), -20000); + assert_eq!(market.amm.total_fee(), 20000); + assert_eq!(market.amm.total_fee_minus_distributions(), 20000); assert_eq!(market.amm.net_revenue_since_last_funding, 20000); } @@ -2467,12 +2475,12 @@ pub mod fulfill_order_with_maker_order { assert_eq!(taker_stats.taker_volume_30d, 100 * QUOTE_PRECISION_U64); assert!(taker.orders[0].is_available()); - assert_eq!(market.amm.base_asset_amount_with_amm, 0); - assert_eq!(market.amm.base_asset_amount_long, BASE_PRECISION_I128); - assert_eq!(market.amm.base_asset_amount_short, -BASE_PRECISION_I128); - assert_eq!(market.amm.quote_asset_amount, -20000); - assert_eq!(market.amm.total_fee, 20000); - assert_eq!(market.amm.total_fee_minus_distributions, 20000); + assert_eq!(market.amm.base_asset_amount_with_amm(), 0); + assert_eq!(market.amm.base_asset_amount_long(), BASE_PRECISION_I128); + assert_eq!(market.amm.base_asset_amount_short(), -BASE_PRECISION_I128); + assert_eq!(market.amm.quote_asset_amount(), -20000); + assert_eq!(market.amm.total_fee(), 20000); + assert_eq!(market.amm.total_fee_minus_distributions(), 20000); assert_eq!(market.amm.net_revenue_since_last_funding, 20000); } @@ -2618,12 +2626,12 @@ pub mod fulfill_order_with_maker_order { assert_eq!(taker_stats.taker_volume_30d, 100 * QUOTE_PRECISION_U64); assert!(taker.orders[0].is_available()); - assert_eq!(market.amm.base_asset_amount_with_amm, 0); - assert_eq!(market.amm.base_asset_amount_long, BASE_PRECISION_I128); - assert_eq!(market.amm.base_asset_amount_short, -BASE_PRECISION_I128); - assert_eq!(market.amm.quote_asset_amount, -20000); - assert_eq!(market.amm.total_fee, 20000); - assert_eq!(market.amm.total_fee_minus_distributions, 20000); + assert_eq!(market.amm.base_asset_amount_with_amm(), 0); + assert_eq!(market.amm.base_asset_amount_long(), BASE_PRECISION_I128); + assert_eq!(market.amm.base_asset_amount_short(), -BASE_PRECISION_I128); + assert_eq!(market.amm.quote_asset_amount(), -20000); + assert_eq!(market.amm.total_fee(), 20000); + assert_eq!(market.amm.total_fee_minus_distributions(), 20000); assert_eq!(market.amm.net_revenue_since_last_funding, 20000); } @@ -2770,12 +2778,12 @@ pub mod fulfill_order_with_maker_order { assert_eq!(taker_stats.taker_volume_30d, 100 * QUOTE_PRECISION_U64); assert!(taker.orders[0].is_available()); - assert_eq!(market.amm.base_asset_amount_with_amm, 0); - assert_eq!(market.amm.base_asset_amount_long, BASE_PRECISION_I128); - assert_eq!(market.amm.base_asset_amount_short, -BASE_PRECISION_I128); - assert_eq!(market.amm.quote_asset_amount, -20000); - assert_eq!(market.amm.total_fee, 20000); - assert_eq!(market.amm.total_fee_minus_distributions, 20000); + assert_eq!(market.amm.base_asset_amount_with_amm(), 0); + assert_eq!(market.amm.base_asset_amount_long(), BASE_PRECISION_I128); + assert_eq!(market.amm.base_asset_amount_short(), -BASE_PRECISION_I128); + assert_eq!(market.amm.quote_asset_amount(), -20000); + assert_eq!(market.amm.total_fee(), 20000); + assert_eq!(market.amm.total_fee_minus_distributions(), 20000); assert_eq!(market.amm.net_revenue_since_last_funding, 20000); } @@ -2903,12 +2911,12 @@ pub mod fulfill_order_with_maker_order { assert_eq!(taker_stats.taker_volume_30d, 100 * QUOTE_PRECISION_U64); assert!(taker.orders[0].is_available()); - assert_eq!(market.amm.base_asset_amount_with_amm, 0); - assert_eq!(market.amm.base_asset_amount_long, BASE_PRECISION_I128); - assert_eq!(market.amm.base_asset_amount_short, -BASE_PRECISION_I128); - assert_eq!(market.amm.quote_asset_amount, -20000); - assert_eq!(market.amm.total_fee, 20000); - assert_eq!(market.amm.total_fee_minus_distributions, 20000); + assert_eq!(market.amm.base_asset_amount_with_amm(), 0); + assert_eq!(market.amm.base_asset_amount_long(), BASE_PRECISION_I128); + assert_eq!(market.amm.base_asset_amount_short(), -BASE_PRECISION_I128); + assert_eq!(market.amm.quote_asset_amount(), -20000); + assert_eq!(market.amm.total_fee(), 20000); + assert_eq!(market.amm.total_fee_minus_distributions(), 20000); assert_eq!(market.amm.net_revenue_since_last_funding, 20000); } @@ -3035,12 +3043,12 @@ pub mod fulfill_order_with_maker_order { assert_eq!(taker_stats.taker_volume_30d, 100 * QUOTE_PRECISION_U64); assert!(taker.orders[0].is_available()); - assert_eq!(market.amm.base_asset_amount_with_amm, 0); - assert_eq!(market.amm.base_asset_amount_long, BASE_PRECISION_I128); - assert_eq!(market.amm.base_asset_amount_short, -BASE_PRECISION_I128); - assert_eq!(market.amm.quote_asset_amount, -20000); - assert_eq!(market.amm.total_fee, 20000); - assert_eq!(market.amm.total_fee_minus_distributions, 20000); + assert_eq!(market.amm.base_asset_amount_with_amm(), 0); + assert_eq!(market.amm.base_asset_amount_long(), BASE_PRECISION_I128); + assert_eq!(market.amm.base_asset_amount_short(), -BASE_PRECISION_I128); + assert_eq!(market.amm.quote_asset_amount(), -20000); + assert_eq!(market.amm.total_fee(), 20000); + assert_eq!(market.amm.total_fee_minus_distributions(), 20000); assert_eq!(market.amm.net_revenue_since_last_funding, 20000); } } @@ -3086,14 +3094,14 @@ pub mod fulfill_order { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -3115,8 +3123,8 @@ pub mod fulfill_order { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); let mut state = State { oracle_guard_rails: OracleGuardRails { @@ -3173,14 +3181,14 @@ pub mod fulfill_order { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -3201,8 +3209,8 @@ pub mod fulfill_order { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = i128::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(i128::MAX as u128); + market.amm.set_min_base_asset_reserve(0); let mut state = State { min_perp_auction_duration: 1, @@ -3242,14 +3250,14 @@ pub mod fulfill_order { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -3270,8 +3278,8 @@ pub mod fulfill_order { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -3279,7 +3287,7 @@ pub mod fulfill_order { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -3443,10 +3451,10 @@ pub mod fulfill_order { assert_eq!(filler.perp_positions[0].quote_asset_amount, 5012); let market_after = market_map.get_ref(&0).unwrap(); - assert_eq!(market_after.amm.base_asset_amount_with_amm, 500000000); - assert_eq!(market_after.amm.base_asset_amount_long, 1000000000); - assert_eq!(market_after.amm.base_asset_amount_short, -500000000); - assert_eq!(market_after.amm.quote_asset_amount, -50281374); + assert_eq!(market_after.amm.base_asset_amount_with_amm(), 500000000); + assert_eq!(market_after.amm.base_asset_amount_long(), 1000000000); + assert_eq!(market_after.amm.base_asset_amount_short(), -500000000); + assert_eq!(market_after.amm.quote_asset_amount(), -50281374); let expected_market_fee = ((taker_stats.fees.total_fee_paid - (maker_stats.fees.total_fee_rebate @@ -3455,9 +3463,9 @@ pub mod fulfill_order { + 1; //todo // assert_eq!(expected_market_fee, 35100); - assert_eq!(market_after.amm.total_fee, expected_market_fee); + assert_eq!(market_after.amm.total_fee(), expected_market_fee); assert_eq!( - market_after.amm.total_fee_minus_distributions, + market_after.amm.total_fee_minus_distributions(), expected_market_fee ); assert_eq!( @@ -3488,14 +3496,14 @@ pub mod fulfill_order { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -3516,8 +3524,8 @@ pub mod fulfill_order { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -3525,7 +3533,7 @@ pub mod fulfill_order { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -3705,14 +3713,14 @@ pub mod fulfill_order { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100_050 * PEG_PRECISION / 1000, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100_050 * PEG_PRECISION / 1000).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -3733,8 +3741,8 @@ pub mod fulfill_order { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -3742,7 +3750,7 @@ pub mod fulfill_order { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -3903,19 +3911,19 @@ pub mod fulfill_order { assert_eq!(filler.perp_positions[0].quote_asset_amount, 5013); let market_after = market_map.get_ref(&0).unwrap(); - assert_eq!(market_after.amm.base_asset_amount_with_amm, 500000000); - assert_eq!(market_after.amm.base_asset_amount_long, 1000000000); - assert_eq!(market_after.amm.base_asset_amount_short, -500000000); - assert_eq!(market_after.amm.quote_asset_amount, -50306510); + assert_eq!(market_after.amm.base_asset_amount_with_amm(), 500000000); + assert_eq!(market_after.amm.base_asset_amount_long(), 1000000000); + assert_eq!(market_after.amm.base_asset_amount_short(), -500000000); + assert_eq!(market_after.amm.quote_asset_amount(), -50306510); let expected_market_fee = (taker_stats.fees.total_fee_paid - (maker_stats.fees.total_fee_rebate + filler.perp_positions[0].quote_asset_amount as u64)) as i128; assert_eq!(expected_market_fee, 30128); - assert_eq!(market_after.amm.total_fee, expected_market_fee); + assert_eq!(market_after.amm.total_fee(), expected_market_fee); assert_eq!( - market_after.amm.total_fee_minus_distributions, + market_after.amm.total_fee_minus_distributions(), expected_market_fee ); assert_eq!( @@ -3931,14 +3939,14 @@ pub mod fulfill_order { fn fulfill_with_maker_with_auction_incomplete() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), order_step_size: 1, order_tick_size: 1, historical_oracle_data: HistoricalOracleData { @@ -3962,7 +3970,7 @@ pub mod fulfill_order { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -4117,12 +4125,12 @@ pub mod fulfill_order { assert_eq!(maker_stats.maker_volume_30d, 50 * QUOTE_PRECISION_U64); let market_after = market_map.get_ref(&0).unwrap(); - assert_eq!(market_after.amm.base_asset_amount_with_amm, 0); - assert_eq!(market_after.amm.base_asset_amount_long, 500000000); - assert_eq!(market_after.amm.base_asset_amount_short, -500000000); - assert_eq!(market_after.amm.quote_asset_amount, -10000); - assert_eq!(market_after.amm.total_fee, 10000); - assert_eq!(market_after.amm.total_fee_minus_distributions, 10000); + assert_eq!(market_after.amm.base_asset_amount_with_amm(), 0); + assert_eq!(market_after.amm.base_asset_amount_long(), 500000000); + assert_eq!(market_after.amm.base_asset_amount_short(), -500000000); + assert_eq!(market_after.amm.quote_asset_amount(), -10000); + assert_eq!(market_after.amm.total_fee(), 10000); + assert_eq!(market_after.amm.total_fee_minus_distributions(), 10000); assert_eq!(market_after.amm.net_revenue_since_last_funding, 10000); } @@ -4145,14 +4153,14 @@ pub mod fulfill_order { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 10, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -4173,8 +4181,8 @@ pub mod fulfill_order { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -4182,7 +4190,7 @@ pub mod fulfill_order { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -4283,12 +4291,12 @@ pub mod fulfill_order { assert!(taker.orders[0].is_available()); let market_after = market_map.get_ref(&0).unwrap(); - assert_eq!(market_after.amm.base_asset_amount_with_amm, 1000000000); - assert_eq!(market_after.amm.base_asset_amount_long, 1000000000); - assert_eq!(market_after.amm.base_asset_amount_short, 0); - assert_eq!(market_after.amm.quote_asset_amount, -104133674); - assert_eq!(market_after.amm.total_fee, 3123572); - assert_eq!(market_after.amm.total_fee_minus_distributions, 3123572); + assert_eq!(market_after.amm.base_asset_amount_with_amm(), 1000000000); + assert_eq!(market_after.amm.base_asset_amount_long(), 1000000000); + assert_eq!(market_after.amm.base_asset_amount_short(), 0); + assert_eq!(market_after.amm.quote_asset_amount(), -104133674); + assert_eq!(market_after.amm.total_fee(), 3123572); + assert_eq!(market_after.amm.total_fee_minus_distributions(), 3123572); assert_eq!(market_after.amm.net_revenue_since_last_funding, 3123572); } @@ -4312,14 +4320,14 @@ pub mod fulfill_order { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -4341,8 +4349,8 @@ pub mod fulfill_order { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -4350,7 +4358,7 @@ pub mod fulfill_order { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -4514,14 +4522,14 @@ pub mod fulfill_order { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -4542,8 +4550,8 @@ pub mod fulfill_order { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -4551,7 +4559,7 @@ pub mod fulfill_order { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -4699,14 +4707,14 @@ pub mod fulfill_order { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -4727,8 +4735,8 @@ pub mod fulfill_order { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u64::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u64::MAX as u128); + market.amm.set_min_base_asset_reserve(0); let reserve_price_before = market.amm.reserve_price().unwrap(); let bid_price = market.amm.bid_price(reserve_price_before).unwrap(); @@ -4740,7 +4748,7 @@ pub mod fulfill_order { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -4846,12 +4854,12 @@ pub mod fulfill_order { assert_eq!(taker_stats.maker_volume_30d, 3499697); let market_after = market_map.get_ref(&0).unwrap(); - assert_eq!(market_after.amm.base_asset_amount_with_amm, -35032000); - assert_eq!(market_after.amm.base_asset_amount_long, 0); - assert_eq!(market_after.amm.base_asset_amount_short, -35032000); - assert_eq!(market_after.amm.quote_asset_amount, 3500868); - assert_eq!(market_after.amm.total_fee, 1105); - assert_eq!(market_after.amm.total_fee_minus_distributions, 1105); + assert_eq!(market_after.amm.base_asset_amount_with_amm(), -35032000); + assert_eq!(market_after.amm.base_asset_amount_long(), 0); + assert_eq!(market_after.amm.base_asset_amount_short(), -35032000); + assert_eq!(market_after.amm.quote_asset_amount(), 3500868); + assert_eq!(market_after.amm.total_fee(), 1105); + assert_eq!(market_after.amm.total_fee_minus_distributions(), 1105); assert_eq!(market_after.amm.net_revenue_since_last_funding, 1105); let market_after = market_map.get_ref(&0).unwrap(); @@ -4879,14 +4887,14 @@ pub mod fulfill_order { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -4907,8 +4915,8 @@ pub mod fulfill_order { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u64::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u64::MAX as u128); + market.amm.set_min_base_asset_reserve(0); let reserve_price_before = market.amm.reserve_price().unwrap(); let bid_price = market.amm.bid_price(reserve_price_before).unwrap(); @@ -4920,7 +4928,7 @@ pub mod fulfill_order { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -5026,12 +5034,12 @@ pub mod fulfill_order { assert_eq!(taker_stats.maker_volume_30d, 3500096); let market_after = market_map.get_ref(&0).unwrap(); - assert_eq!(market_after.amm.base_asset_amount_with_amm, 34966000); - assert_eq!(market_after.amm.base_asset_amount_long, 34966000); - assert_eq!(market_after.amm.base_asset_amount_short, 0); - assert_eq!(market_after.amm.quote_asset_amount, -3498924); - assert_eq!(market_after.amm.total_fee, 1100); - assert_eq!(market_after.amm.total_fee_minus_distributions, 1100); + assert_eq!(market_after.amm.base_asset_amount_with_amm(), 34966000); + assert_eq!(market_after.amm.base_asset_amount_long(), 34966000); + assert_eq!(market_after.amm.base_asset_amount_short(), 0); + assert_eq!(market_after.amm.quote_asset_amount(), -3498924); + assert_eq!(market_after.amm.total_fee(), 1100); + assert_eq!(market_after.amm.total_fee_minus_distributions(), 1100); assert_eq!(market_after.amm.net_revenue_since_last_funding, 1100); let market_after = market_map.get_ref(&0).unwrap(); @@ -5067,15 +5075,15 @@ pub mod fulfill_order { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - terminal_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + terminal_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -5098,8 +5106,8 @@ pub mod fulfill_order { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); market.status = MarketStatus::Active; create_anchor_account_info!(market, PerpMarket, market_account_info); @@ -5108,7 +5116,7 @@ pub mod fulfill_order { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -5377,14 +5385,14 @@ pub mod fulfill_order { fn fulfill_users_with_multiple_orders_and_markets() { let mut sol_market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), order_step_size: 1, order_tick_size: 1, historical_oracle_data: HistoricalOracleData { @@ -5403,14 +5411,14 @@ pub mod fulfill_order { create_anchor_account_info!(sol_market, PerpMarket, sol_market_account_info); let mut btc_market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 20000 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (20000 * PEG_PRECISION).into(), order_step_size: 1, order_tick_size: 1, historical_oracle_data: HistoricalOracleData { @@ -5437,7 +5445,7 @@ pub mod fulfill_order { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -5657,12 +5665,12 @@ pub mod fulfill_order { assert_eq!(maker.orders[0], maker_before.orders[0]); let market_after = market_map.get_ref(&0).unwrap(); - assert_eq!(market_after.amm.base_asset_amount_with_amm, 0); - assert_eq!(market_after.amm.base_asset_amount_long, 500000000); - assert_eq!(market_after.amm.base_asset_amount_short, -500000000); - assert_eq!(market_after.amm.quote_asset_amount, -10000); - assert_eq!(market_after.amm.total_fee, 10000); - assert_eq!(market_after.amm.total_fee_minus_distributions, 10000); + assert_eq!(market_after.amm.base_asset_amount_with_amm(), 0); + assert_eq!(market_after.amm.base_asset_amount_long(), 500000000); + assert_eq!(market_after.amm.base_asset_amount_short(), -500000000); + assert_eq!(market_after.amm.quote_asset_amount(), -10000); + assert_eq!(market_after.amm.total_fee(), 10000); + assert_eq!(market_after.amm.total_fee_minus_distributions(), 10000); assert_eq!(market_after.amm.net_revenue_since_last_funding, 10000); assert_eq!(market_after.amm.last_mark_price_twap_ts, 1); @@ -5712,14 +5720,14 @@ pub mod fulfill_order { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -5740,8 +5748,8 @@ pub mod fulfill_order { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -5749,7 +5757,7 @@ pub mod fulfill_order { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -5960,11 +5968,11 @@ pub mod fill_order { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - terminal_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + terminal_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 100, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -5988,25 +5996,33 @@ pub mod fill_order { ..PerpMarket::default() }; market.status = MarketStatus::Active; - market.amm.max_base_asset_reserve = i128::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(i128::MAX as u128); + market.amm.set_min_base_asset_reserve(0); let (new_ask_base_asset_reserve, new_ask_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Long) .unwrap(); let (new_bid_base_asset_reserve, new_bid_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Short) .unwrap(); - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -6161,11 +6177,11 @@ pub mod fill_order { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - terminal_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + terminal_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 100, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -6189,25 +6205,33 @@ pub mod fill_order { ..PerpMarket::default() }; market.status = MarketStatus::Active; - market.amm.max_base_asset_reserve = i128::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(i128::MAX as u128); + market.amm.set_min_base_asset_reserve(0); let (new_ask_base_asset_reserve, new_ask_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Long) .unwrap(); let (new_bid_base_asset_reserve, new_bid_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Short) .unwrap(); - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -6341,21 +6365,21 @@ pub mod fill_order { fn expire_order() { let mut market = PerpMarket { amm: AMM { - terminal_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + terminal_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 100, max_fill_reserve_fraction: 100, order_step_size: 10000000, order_tick_size: 1, - max_base_asset_reserve: 200 * AMM_RESERVE_PRECISION, - min_base_asset_reserve: 50 * AMM_RESERVE_PRECISION, + max_base_asset_reserve: (200 * AMM_RESERVE_PRECISION).into(), + min_base_asset_reserve: (50 * AMM_RESERVE_PRECISION).into(), historical_oracle_data: HistoricalOracleData::default_price(PRICE_PRECISION_I64), ..AMM::default() }, @@ -6373,7 +6397,7 @@ pub mod fill_order { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, ..SpotMarket::default() @@ -6500,17 +6524,17 @@ pub mod fill_order { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - terminal_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + terminal_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 100, max_fill_reserve_fraction: 100, order_step_size: 1000, order_tick_size: 1, oracle: oracle_price_key, - max_open_interest: 100, + max_open_interest: 100.into(), max_spread: 1000, base_spread: 0, long_spread: 0, @@ -6529,25 +6553,33 @@ pub mod fill_order { ..PerpMarket::default() }; market.status = MarketStatus::Active; - market.amm.max_base_asset_reserve = i128::MAX as u128; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(i128::MAX as u128); + market.amm.set_min_base_asset_reserve(0); let (new_ask_base_asset_reserve, new_ask_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Long) .unwrap(); let (new_bid_base_asset_reserve, new_bid_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Short) .unwrap(); - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -6705,11 +6737,11 @@ pub mod fulfill_spot_order_with_match { }; let mut base_market = SpotMarket { - deposit_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), ..SpotMarket::default_base_market() }; let mut quote_market = SpotMarket { - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_quote_market() }; @@ -6769,8 +6801,8 @@ pub mod fulfill_spot_order_with_match { assert_eq!(maker_stats.maker_volume_30d, 100000000); assert_eq!(maker_stats.fees.total_fee_rebate, 30000); - assert_eq!(base_market.total_spot_fee, 20000); - assert_eq!(base_market.spot_fee_pool.scaled_balance, 20000000); + assert_eq!(base_market.total_spot_fee(), 20000); + assert_eq!(base_market.spot_fee_pool.scaled_balance(), 20000000); } #[test] @@ -6830,11 +6862,11 @@ pub mod fulfill_spot_order_with_match { }; let mut base_market = SpotMarket { - deposit_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), ..SpotMarket::default_base_market() }; let mut quote_market = SpotMarket { - deposit_balance: 161 * SPOT_BALANCE_PRECISION, + deposit_balance: (161 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_quote_market() }; @@ -6894,8 +6926,8 @@ pub mod fulfill_spot_order_with_match { assert_eq!(maker_stats.maker_volume_30d, 160000000); assert_eq!(maker_stats.fees.total_fee_rebate, 48000); - assert_eq!(base_market.total_spot_fee, 32000); - assert_eq!(base_market.spot_fee_pool.scaled_balance, 32000000); + assert_eq!(base_market.total_spot_fee(), 32000); + assert_eq!(base_market.spot_fee_pool.scaled_balance(), 32000000); } #[test] @@ -6955,11 +6987,11 @@ pub mod fulfill_spot_order_with_match { }; let mut base_market = SpotMarket { - deposit_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), ..SpotMarket::default_base_market() }; let mut quote_market = SpotMarket { - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_quote_market() }; @@ -7019,8 +7051,8 @@ pub mod fulfill_spot_order_with_match { assert_eq!(maker_stats.maker_volume_30d, 100000000); assert_eq!(maker_stats.fees.total_fee_rebate, 30000); - assert_eq!(base_market.total_spot_fee, 20000); - assert_eq!(base_market.spot_fee_pool.scaled_balance, 20000000); + assert_eq!(base_market.total_spot_fee(), 20000); + assert_eq!(base_market.spot_fee_pool.scaled_balance(), 20000000); } #[test] @@ -7080,11 +7112,11 @@ pub mod fulfill_spot_order_with_match { }; let mut base_market = SpotMarket { - deposit_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), ..SpotMarket::default_base_market() }; let mut quote_market = SpotMarket { - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_quote_market() }; @@ -7144,8 +7176,8 @@ pub mod fulfill_spot_order_with_match { assert_eq!(maker_stats.maker_volume_30d, 70000000); assert_eq!(maker_stats.fees.total_fee_rebate, 21000); - assert_eq!(base_market.total_spot_fee, 14000); - assert_eq!(base_market.spot_fee_pool.scaled_balance, 14000000); + assert_eq!(base_market.total_spot_fee(), 14000); + assert_eq!(base_market.spot_fee_pool.scaled_balance(), 14000000); } #[test] @@ -7205,11 +7237,11 @@ pub mod fulfill_spot_order_with_match { }; let mut base_market = SpotMarket { - deposit_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), ..SpotMarket::default_base_market() }; let mut quote_market = SpotMarket { - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_quote_market() }; @@ -7304,11 +7336,11 @@ pub mod fulfill_spot_order_with_match { }; let mut base_market = SpotMarket { - deposit_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), ..SpotMarket::default_base_market() }; let mut quote_market = SpotMarket { - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_quote_market() }; @@ -7403,11 +7435,11 @@ pub mod fulfill_spot_order_with_match { }; let mut base_market = SpotMarket { - deposit_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), ..SpotMarket::default_base_market() }; let mut quote_market = SpotMarket { - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_quote_market() }; @@ -7502,11 +7534,11 @@ pub mod fulfill_spot_order_with_match { }; let mut base_market = SpotMarket { - deposit_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), ..SpotMarket::default_base_market() }; let mut quote_market = SpotMarket { - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_quote_market() }; @@ -7601,11 +7633,11 @@ pub mod fulfill_spot_order_with_match { }; let mut base_market = SpotMarket { - deposit_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), ..SpotMarket::default_base_market() }; let mut quote_market = SpotMarket { - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_quote_market() }; @@ -7677,8 +7709,8 @@ pub mod fulfill_spot_order_with_match { assert_eq!(maker_stats.maker_volume_30d, 100000000); assert_eq!(maker_stats.fees.total_fee_rebate, 30000); - assert_eq!(base_market.total_spot_fee, 20000); - assert_eq!(base_market.spot_fee_pool.scaled_balance, 20000000); + assert_eq!(base_market.total_spot_fee(), 20000); + assert_eq!(base_market.spot_fee_pool.scaled_balance(), 20000000); } #[test] @@ -7738,11 +7770,11 @@ pub mod fulfill_spot_order_with_match { }; let mut base_market = SpotMarket { - deposit_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), ..SpotMarket::default_base_market() }; let mut quote_market = SpotMarket { - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_quote_market() }; @@ -7814,8 +7846,8 @@ pub mod fulfill_spot_order_with_match { assert_eq!(maker_stats.maker_volume_30d, 100000000); assert_eq!(maker_stats.fees.total_fee_rebate, 30000); - assert_eq!(base_market.total_spot_fee, 20000); - assert_eq!(base_market.spot_fee_pool.scaled_balance, 20000000); + assert_eq!(base_market.total_spot_fee(), 20000); + assert_eq!(base_market.spot_fee_pool.scaled_balance(), 20000000); } #[test] @@ -7877,11 +7909,11 @@ pub mod fulfill_spot_order_with_match { }; let mut base_market = SpotMarket { - deposit_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), ..SpotMarket::default_base_market() }; let mut quote_market = SpotMarket { - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_quote_market() }; @@ -7941,8 +7973,8 @@ pub mod fulfill_spot_order_with_match { assert_eq!(maker_stats.maker_volume_30d, 100000000); assert_eq!(maker_stats.fees.total_fee_rebate, 30000); - assert_eq!(base_market.total_spot_fee, 20000); - assert_eq!(base_market.spot_fee_pool.scaled_balance, 20000000); + assert_eq!(base_market.total_spot_fee(), 20000); + assert_eq!(base_market.spot_fee_pool.scaled_balance(), 20000000); } #[test] @@ -8000,11 +8032,11 @@ pub mod fulfill_spot_order_with_match { }; let mut base_market = SpotMarket { - deposit_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), ..SpotMarket::default_base_market() }; let mut quote_market = SpotMarket { - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_quote_market() }; @@ -8064,8 +8096,8 @@ pub mod fulfill_spot_order_with_match { assert_eq!(maker_stats.maker_volume_30d, 100000000); assert_eq!(maker_stats.fees.total_fee_rebate, 30000); - assert_eq!(base_market.total_spot_fee, 20000); - assert_eq!(base_market.spot_fee_pool.scaled_balance, 20000000); + assert_eq!(base_market.total_spot_fee(), 20000); + assert_eq!(base_market.spot_fee_pool.scaled_balance(), 20000000); } #[test] @@ -8123,11 +8155,11 @@ pub mod fulfill_spot_order_with_match { }; let mut base_market = SpotMarket { - deposit_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), ..SpotMarket::default_base_market() }; let mut quote_market = SpotMarket { - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_quote_market() }; @@ -8187,8 +8219,8 @@ pub mod fulfill_spot_order_with_match { assert_eq!(maker_stats.maker_volume_30d, 100000000); assert_eq!(maker_stats.fees.total_fee_rebate, 30000); - assert_eq!(base_market.total_spot_fee, 20000); - assert_eq!(base_market.spot_fee_pool.scaled_balance, 20000000); + assert_eq!(base_market.total_spot_fee(), 20000); + assert_eq!(base_market.spot_fee_pool.scaled_balance(), 20000000); } #[test] @@ -8247,11 +8279,11 @@ pub mod fulfill_spot_order_with_match { }; let mut base_market = SpotMarket { - deposit_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), ..SpotMarket::default_base_market() }; let mut quote_market = SpotMarket { - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_quote_market() }; @@ -8344,11 +8376,11 @@ pub mod fulfill_spot_order_with_match { }; let mut base_market = SpotMarket { - deposit_balance: 0, + deposit_balance: 0.into(), ..SpotMarket::default_base_market() }; let mut quote_market = SpotMarket { - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_quote_market() }; @@ -8441,12 +8473,12 @@ pub mod fulfill_spot_order_with_match { }; let mut base_market = SpotMarket { - deposit_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), deposit_token_twap: SPOT_BALANCE_PRECISION as u64, ..SpotMarket::default_base_market() }; let mut quote_market = SpotMarket { - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), deposit_token_twap: 101 * QUOTE_PRECISION_U64, ..SpotMarket::default_quote_market() @@ -8541,11 +8573,11 @@ pub mod fulfill_spot_order_with_match { }; let mut base_market = SpotMarket { - deposit_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), ..SpotMarket::default_base_market() }; let mut quote_market = SpotMarket { - deposit_balance: 0, + deposit_balance: 0.into(), ..SpotMarket::default_quote_market() }; @@ -8638,12 +8670,12 @@ pub mod fulfill_spot_order_with_match { }; let mut base_market = SpotMarket { - deposit_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), deposit_token_twap: SPOT_BALANCE_PRECISION as u64, ..SpotMarket::default_base_market() }; let mut quote_market = SpotMarket { - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), deposit_token_twap: 100 * QUOTE_PRECISION_U64, ..SpotMarket::default_quote_market() }; @@ -8737,12 +8769,12 @@ pub mod fulfill_spot_order_with_match { }; let mut base_market = SpotMarket { - deposit_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), deposit_token_twap: SPOT_BALANCE_PRECISION as u64, ..SpotMarket::default_base_market() }; let mut quote_market = SpotMarket { - deposit_balance: 0, + deposit_balance: 0.into(), deposit_token_twap: 0, ..SpotMarket::default_quote_market() }; @@ -8836,12 +8868,12 @@ pub mod fulfill_spot_order_with_match { }; let mut base_market = SpotMarket { - deposit_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), deposit_token_twap: SPOT_BALANCE_PRECISION as u64, ..SpotMarket::default_base_market() }; let mut quote_market = SpotMarket { - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), deposit_token_twap: 100 * QUOTE_PRECISION_U64, ..SpotMarket::default_quote_market() }; @@ -8935,11 +8967,11 @@ pub mod fulfill_spot_order_with_match { }; let mut base_market = SpotMarket { - deposit_balance: 0, + deposit_balance: 0.into(), ..SpotMarket::default_base_market() }; let mut quote_market = SpotMarket { - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_quote_market() }; @@ -9032,12 +9064,12 @@ pub mod fulfill_spot_order_with_match { }; let mut base_market = SpotMarket { - deposit_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), deposit_token_twap: SPOT_BALANCE_PRECISION as u64, ..SpotMarket::default_base_market() }; let mut quote_market = SpotMarket { - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), deposit_token_twap: 101 * QUOTE_PRECISION_U64, ..SpotMarket::default_quote_market() }; @@ -9131,14 +9163,14 @@ pub mod fulfill_spot_order_with_match { }; let mut base_market = SpotMarket { - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: SPOT_BALANCE_PRECISION.into(), utilization_twap: SPOT_UTILIZATION_PRECISION as u64, ..SpotMarket::default_base_market() }; let mut quote_market = SpotMarket { - deposit_balance: 101 * SPOT_BALANCE_PRECISION, - borrow_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), + borrow_balance: (101 * SPOT_BALANCE_PRECISION).into(), utilization_twap: SPOT_UTILIZATION_PRECISION as u64, ..SpotMarket::default_quote_market() }; @@ -9445,7 +9477,7 @@ pub mod fulfill_spot_order { let mut base_market = SpotMarket { market_index: 1, - deposit_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(100 * PRICE_PRECISION_I64), ..SpotMarket::default_base_market() @@ -9453,7 +9485,7 @@ pub mod fulfill_spot_order { create_anchor_account_info!(base_market, SpotMarket, base_market_account_info); let mut second_base_market = SpotMarket { market_index: 2, - deposit_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), historical_oracle_data: HistoricalOracleData::default_price(100 * PRICE_PRECISION_I64), ..SpotMarket::default_base_market() }; @@ -9464,7 +9496,7 @@ pub mod fulfill_spot_order { ); let mut quote_market = SpotMarket { market_index: 0, - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_quote_market() }; create_anchor_account_info!(quote_market, SpotMarket, quote_market_account_info); @@ -9717,7 +9749,7 @@ pub mod fulfill_spot_order { let mut base_market = SpotMarket { market_index: 1, - deposit_balance: 2 * SPOT_BALANCE_PRECISION, + deposit_balance: (2 * SPOT_BALANCE_PRECISION).into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(100 * PRICE_PRECISION_I64), ..SpotMarket::default_base_market() @@ -9725,7 +9757,7 @@ pub mod fulfill_spot_order { create_anchor_account_info!(base_market, SpotMarket, base_market_account_info); let mut quote_market = SpotMarket { market_index: 0, - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_quote_market() }; create_anchor_account_info!(quote_market, SpotMarket, quote_market_account_info); @@ -10004,7 +10036,7 @@ pub mod fulfill_spot_order { let mut base_market = SpotMarket { market_index: 1, - deposit_balance: 10 * SPOT_BALANCE_PRECISION, + deposit_balance: (10 * SPOT_BALANCE_PRECISION).into(), deposit_token_twap: 10 * LAMPORTS_PER_SOL_U64, oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(100 * PRICE_PRECISION_I64), @@ -10013,7 +10045,7 @@ pub mod fulfill_spot_order { create_anchor_account_info!(base_market, SpotMarket, base_market_account_info); let mut quote_market = SpotMarket { market_index: 0, - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_quote_market() }; create_anchor_account_info!(quote_market, SpotMarket, quote_market_account_info); @@ -10192,13 +10224,13 @@ pub mod fill_spot_order { let perp_market_map = PerpMarketMap::empty(); let mut base_market = SpotMarket { - deposit_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), historical_oracle_data: HistoricalOracleData::default_price(PRICE_PRECISION_I64), ..SpotMarket::default_base_market() }; create_anchor_account_info!(base_market, SpotMarket, base_market_account_info); let mut quote_market = SpotMarket { - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_quote_market() }; create_anchor_account_info!(quote_market, SpotMarket, quote_market_account_info); @@ -10374,15 +10406,15 @@ pub mod force_cancel_orders { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - terminal_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + terminal_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), // bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, // bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, // ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, // ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 100, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -10406,27 +10438,35 @@ pub mod force_cancel_orders { ..PerpMarket::default() }; market.status = MarketStatus::Active; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); let (new_ask_base_asset_reserve, new_ask_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Long) .unwrap(); let (new_bid_base_asset_reserve, new_bid_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Short) .unwrap(); - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - deposit_balance: SPOT_BALANCE_PRECISION, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -10436,9 +10476,9 @@ pub mod force_cancel_orders { let mut sol_spot_market = SpotMarket { market_index: 1, - deposit_balance: SPOT_BALANCE_PRECISION, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), oracle: oracle_price_key, ..SpotMarket::default_base_market() }; @@ -10668,11 +10708,11 @@ pub mod get_maker_orders_info { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - terminal_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + terminal_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 100, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -10696,25 +10736,33 @@ pub mod get_maker_orders_info { ..PerpMarket::default() }; market.status = MarketStatus::Active; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); let (new_ask_base_asset_reserve, new_ask_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Long) .unwrap(); let (new_bid_base_asset_reserve, new_bid_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Short) .unwrap(); - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -10858,11 +10906,11 @@ pub mod get_maker_orders_info { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - terminal_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + terminal_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 100, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -10886,25 +10934,33 @@ pub mod get_maker_orders_info { ..PerpMarket::default() }; market.status = MarketStatus::Active; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); let (new_ask_base_asset_reserve, new_ask_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Long) .unwrap(); let (new_bid_base_asset_reserve, new_bid_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Short) .unwrap(); - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -11049,11 +11105,11 @@ pub mod get_maker_orders_info { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - terminal_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + terminal_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 100, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -11077,25 +11133,33 @@ pub mod get_maker_orders_info { ..PerpMarket::default() }; market.status = MarketStatus::Active; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); let (new_ask_base_asset_reserve, new_ask_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Long) .unwrap(); let (new_bid_base_asset_reserve, new_bid_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Short) .unwrap(); - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -11226,11 +11290,11 @@ pub mod get_maker_orders_info { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - terminal_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + terminal_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 100, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -11254,25 +11318,33 @@ pub mod get_maker_orders_info { ..PerpMarket::default() }; market.status = MarketStatus::Active; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); let (new_ask_base_asset_reserve, new_ask_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Long) .unwrap(); let (new_bid_base_asset_reserve, new_bid_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Short) .unwrap(); - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -11477,11 +11549,11 @@ pub mod get_maker_orders_info { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - terminal_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + terminal_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 100, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -11505,25 +11577,33 @@ pub mod get_maker_orders_info { ..PerpMarket::default() }; market.status = MarketStatus::Active; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); let (new_ask_base_asset_reserve, new_ask_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Long) .unwrap(); let (new_bid_base_asset_reserve, new_bid_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Short) .unwrap(); - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -11670,11 +11750,11 @@ pub mod get_maker_orders_info { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - terminal_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + terminal_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 100, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -11698,25 +11778,33 @@ pub mod get_maker_orders_info { ..PerpMarket::default() }; market.status = MarketStatus::Active; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); let (new_ask_base_asset_reserve, new_ask_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Long) .unwrap(); let (new_bid_base_asset_reserve, new_bid_quote_asset_reserve) = crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Short) .unwrap(); - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -11919,7 +12007,7 @@ pub mod get_spot_maker_orders_info { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -11929,7 +12017,7 @@ pub mod get_spot_maker_orders_info { create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info); let mut base_market = SpotMarket { - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), oracle: oracle_price_key, ..SpotMarket::default_base_market() }; @@ -12076,7 +12164,7 @@ pub mod get_spot_maker_orders_info { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -12086,7 +12174,7 @@ pub mod get_spot_maker_orders_info { create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info); let mut base_market = SpotMarket { - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), oracle: oracle_price_key, ..SpotMarket::default_base_market() }; @@ -12235,7 +12323,7 @@ pub mod get_spot_maker_orders_info { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -12245,7 +12333,7 @@ pub mod get_spot_maker_orders_info { create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info); let mut base_market = SpotMarket { - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), oracle: oracle_price_key, ..SpotMarket::default_base_market() }; @@ -12379,7 +12467,7 @@ pub mod get_spot_maker_orders_info { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -12389,7 +12477,7 @@ pub mod get_spot_maker_orders_info { create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info); let mut base_market = SpotMarket { - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), oracle: oracle_price_key, ..SpotMarket::default_base_market() }; @@ -12601,7 +12689,7 @@ pub mod get_spot_maker_orders_info { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -12611,7 +12699,7 @@ pub mod get_spot_maker_orders_info { create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info); let mut base_market = SpotMarket { - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), oracle: oracle_price_key, ..SpotMarket::default_base_market() }; @@ -12762,7 +12850,7 @@ pub mod get_spot_maker_orders_info { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -12772,7 +12860,7 @@ pub mod get_spot_maker_orders_info { create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info); let mut base_market = SpotMarket { - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), oracle: oracle_price_key, ..SpotMarket::default_base_market() }; diff --git a/programs/drift/src/controller/pnl.rs b/programs/drift/src/controller/pnl.rs index 98add9fa67..ba85a8a489 100644 --- a/programs/drift/src/controller/pnl.rs +++ b/programs/drift/src/controller/pnl.rs @@ -68,8 +68,8 @@ pub fn settle_pnl( update_spot_market_cumulative_interest(spot_market, None, now)?; tvl_before = spot_market.get_tvl()?; - deposits_balance_before = spot_market.deposit_balance; - borrows_balance_before = spot_market.borrow_balance; + deposits_balance_before = spot_market.deposit_balance(); + borrows_balance_before = spot_market.borrow_balance(); } let mut market = perp_market_map.get_ref_mut(&market_index)?; @@ -220,13 +220,13 @@ pub fn settle_pnl( } let pnl_pool_token_amount = get_token_amount( - perp_market.pnl_pool.scaled_balance, + perp_market.pnl_pool.scaled_balance(), spot_market, perp_market.pnl_pool.balance_type(), )?; let fraction_of_fee_pool_token_amount = get_token_amount( - perp_market.amm.fee_pool.scaled_balance, + perp_market.amm.fee_pool.scaled_balance(), spot_market, perp_market.amm.fee_pool.balance_type(), )? @@ -256,8 +256,8 @@ pub fn settle_pnl( )?; // if the spot market balance has changed, we have to fail if we are in try settle mode - if (spot_market.deposit_balance != deposits_balance_before - || spot_market.borrow_balance != borrows_balance_before) + if (spot_market.deposit_balance() != deposits_balance_before + || spot_market.borrow_balance() != borrows_balance_before) && mode == SettlePnlMode::TrySettle { msg!("Spot market balance has changed, switch to MUST_SETTLE mode"); @@ -554,10 +554,10 @@ pub fn settle_expired_position( update_settled_pnl(user, position_index, pnl_to_settle_with_user.cast()?)?; - perp_market.amm.base_asset_amount_with_amm = perp_market - .amm - .base_asset_amount_with_amm - .safe_add(position_delta.base_asset_amount.cast()?)?; + let base_asset_amount_with_amm = perp_market.amm.base_asset_amount_with_amm(); + perp_market.amm.set_base_asset_amount_with_amm( + base_asset_amount_with_amm.safe_add(position_delta.base_asset_amount.cast()?)?, + ); let quote_asset_amount_after = user.perp_positions[position_index].quote_asset_amount; diff --git a/programs/drift/src/controller/pnl/delisting.rs b/programs/drift/src/controller/pnl/delisting.rs index db148230f0..b33b7d02d3 100644 --- a/programs/drift/src/controller/pnl/delisting.rs +++ b/programs/drift/src/controller/pnl/delisting.rs @@ -78,16 +78,16 @@ pub mod delisting_test { // net users are short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: (AMM_RESERVE_PRECISION / 2) as i128, - base_asset_amount_long: (AMM_RESERVE_PRECISION / 2) as i128, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + base_asset_amount_with_amm: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + base_asset_amount_long: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -100,8 +100,8 @@ pub mod delisting_test { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -109,7 +109,7 @@ pub mod delisting_test { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, ..SpotMarket::default() @@ -192,16 +192,16 @@ pub mod delisting_test { // net users are long let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: (AMM_RESERVE_PRECISION / 2) as i128, - base_asset_amount_long: (AMM_RESERVE_PRECISION / 2) as i128, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + base_asset_amount_with_amm: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + base_asset_amount_long: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -212,7 +212,7 @@ pub mod delisting_test { last_oracle_price_twap_5min: (99 * PRICE_PRECISION) as i64, ..HistoricalOracleData::default() }, - quote_asset_amount: -(QUOTE_PRECISION_I128 * 50), //longs have $100 cost basis + quote_asset_amount: (-(QUOTE_PRECISION_I128 * 50)).into(), //longs have $100 cost basis ..AMM::default() }, margin_ratio_initial: 1000, @@ -222,8 +222,8 @@ pub mod delisting_test { ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -231,7 +231,7 @@ pub mod delisting_test { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, ..SpotMarket::default() @@ -302,16 +302,16 @@ pub mod delisting_test { // net users are short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: (AMM_RESERVE_PRECISION / 2) as i128, - base_asset_amount_long: (AMM_RESERVE_PRECISION / 2) as i128, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + base_asset_amount_with_amm: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + base_asset_amount_long: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -322,7 +322,7 @@ pub mod delisting_test { last_oracle_price_twap_5min: (99 * PRICE_PRECISION) as i64, ..HistoricalOracleData::default() }, - quote_asset_amount: -(QUOTE_PRECISION_I128 * 10), //longs have $20 cost basis + quote_asset_amount: (-(QUOTE_PRECISION_I128 * 10)).into(), //longs have $20 cost basis ..AMM::default() }, margin_ratio_initial: 1000, @@ -332,8 +332,8 @@ pub mod delisting_test { ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -341,7 +341,7 @@ pub mod delisting_test { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, ..SpotMarket::default() @@ -415,16 +415,16 @@ pub mod delisting_test { // net users are short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: (AMM_RESERVE_PRECISION / 2) as i128, - base_asset_amount_long: (AMM_RESERVE_PRECISION / 2) as i128, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + base_asset_amount_with_amm: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + base_asset_amount_long: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -435,8 +435,8 @@ pub mod delisting_test { last_oracle_price_twap_5min: (99 * PRICE_PRECISION) as i64, ..HistoricalOracleData::default() }, - total_fee_minus_distributions: -(100000 * QUOTE_PRECISION_I128), // down $100k - quote_asset_amount: -(QUOTE_PRECISION_I128 * 10), //longs have $20 cost basis + total_fee_minus_distributions: (-(100000 * QUOTE_PRECISION_I128)).into(), // down $100k + quote_asset_amount: (-(QUOTE_PRECISION_I128 * 10)).into(), //longs have $20 cost basis ..AMM::default() }, margin_ratio_initial: 1000, @@ -446,8 +446,8 @@ pub mod delisting_test { ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -455,7 +455,7 @@ pub mod delisting_test { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, ..SpotMarket::default() @@ -529,16 +529,16 @@ pub mod delisting_test { // net users are short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: -((AMM_RESERVE_PRECISION / 2) as i128), - base_asset_amount_long: (AMM_RESERVE_PRECISION / 2) as i128, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + base_asset_amount_with_amm: (-((AMM_RESERVE_PRECISION / 2) as i128)).into(), + base_asset_amount_long: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -549,8 +549,8 @@ pub mod delisting_test { last_oracle_price_twap_5min: (99 * PRICE_PRECISION) as i64, ..HistoricalOracleData::default() }, - total_fee_minus_distributions: -(100000 * QUOTE_PRECISION_I128), // down $100k - quote_asset_amount: (QUOTE_PRECISION_I128 * 10), //shorts have $20 cost basis + total_fee_minus_distributions: (-(100000 * QUOTE_PRECISION_I128)).into(), // down $100k + quote_asset_amount: (QUOTE_PRECISION_I128 * 10).into(), //shorts have $20 cost basis ..AMM::default() }, margin_ratio_initial: 1000, @@ -560,8 +560,8 @@ pub mod delisting_test { ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -569,7 +569,7 @@ pub mod delisting_test { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, ..SpotMarket::default() @@ -639,16 +639,16 @@ pub mod delisting_test { // net users are short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: (AMM_RESERVE_PRECISION / 2) as i128, - base_asset_amount_long: (AMM_RESERVE_PRECISION / 2) as i128, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + base_asset_amount_with_amm: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + base_asset_amount_long: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -659,8 +659,8 @@ pub mod delisting_test { last_oracle_price_twap_5min: (99 * PRICE_PRECISION) as i64, ..HistoricalOracleData::default() }, - quote_asset_amount: -(QUOTE_PRECISION_I128 * 10), //longs have $20 cost basis - total_fee_minus_distributions: 0, + quote_asset_amount: (-(QUOTE_PRECISION_I128 * 10)).into(), //longs have $20 cost basis + total_fee_minus_distributions: 0.into(), ..AMM::default() }, number_of_users_with_base: 1, @@ -669,7 +669,7 @@ pub mod delisting_test { margin_ratio_maintenance: 500, status: MarketStatus::Initialized, pnl_pool: PoolBalance { - scaled_balance: (1000 * SPOT_BALANCE_PRECISION), + scaled_balance: (1000 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -677,8 +677,8 @@ pub mod delisting_test { ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -686,14 +686,14 @@ pub mod delisting_test { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, initial_liability_weight: SPOT_WEIGHT_PRECISION, maintenance_liability_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, - borrow_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), + borrow_balance: (100 * SPOT_BALANCE_PRECISION).into(), historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64), ..SpotMarket::default() }; @@ -801,7 +801,7 @@ pub mod delisting_test { assert_eq!(margin_requirement, 10000); let market = market_map.get_ref_mut(&0).unwrap(); - assert_eq!(market.pnl_pool.scaled_balance, 1000000000000); + assert_eq!(market.pnl_pool.scaled_balance(), 1000000000000); assert_eq!(taker.spot_positions[0].scaled_balance, 100000000000); assert_eq!(taker.perp_positions[0].quote_asset_amount, -10000000); drop(market); @@ -822,7 +822,7 @@ pub mod delisting_test { assert_eq!(taker.spot_positions[0].scaled_balance, 139450500000); let market = market_map.get_ref_mut(&0).unwrap(); - assert_eq!(market.pnl_pool.scaled_balance, 960549500000); + assert_eq!(market.pnl_pool.scaled_balance(), 960549500000); drop(market); assert_eq!(taker.perp_positions[0].open_orders, 0); @@ -858,16 +858,16 @@ pub mod delisting_test { // net users are short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: (AMM_RESERVE_PRECISION / 2) as i128, - base_asset_amount_long: (AMM_RESERVE_PRECISION / 2) as i128, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + base_asset_amount_with_amm: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + base_asset_amount_long: ((AMM_RESERVE_PRECISION / 2) as i128).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -879,8 +879,8 @@ pub mod delisting_test { ..HistoricalOracleData::default() }, - quote_asset_amount: (QUOTE_PRECISION_I128 * 10), //longs have -$20 cost basis - total_fee_minus_distributions: 0, + quote_asset_amount: (QUOTE_PRECISION_I128 * 10).into(), //longs have -$20 cost basis + total_fee_minus_distributions: 0.into(), ..AMM::default() }, number_of_users_with_base: 1, @@ -889,7 +889,7 @@ pub mod delisting_test { margin_ratio_maintenance: 500, status: MarketStatus::Initialized, pnl_pool: PoolBalance { - scaled_balance: (1000 * SPOT_BALANCE_PRECISION), + scaled_balance: (1000 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -897,8 +897,8 @@ pub mod delisting_test { ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -906,14 +906,14 @@ pub mod delisting_test { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, initial_liability_weight: SPOT_WEIGHT_PRECISION, maintenance_liability_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, - borrow_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), + borrow_balance: (100 * SPOT_BALANCE_PRECISION).into(), historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64), ..SpotMarket::default() }; @@ -1021,7 +1021,7 @@ pub mod delisting_test { assert_eq!(margin_requirement, 10000); // settlement in margin now let market = market_map.get_ref_mut(&0).unwrap(); - assert_eq!(market.pnl_pool.scaled_balance, 1000000000000); + assert_eq!(market.pnl_pool.scaled_balance(), 1000000000000); assert_eq!(taker.spot_positions[0].scaled_balance, 100000000000); assert_eq!(taker.perp_positions[0].quote_asset_amount, 10000000); drop(market); @@ -1042,7 +1042,7 @@ pub mod delisting_test { assert_eq!(taker.spot_positions[0].scaled_balance, 159450500000); let market = market_map.get_ref_mut(&0).unwrap(); - assert_eq!(market.pnl_pool.scaled_balance, 940549500000); + assert_eq!(market.pnl_pool.scaled_balance(), 940549500000); drop(market); assert_eq!(taker.perp_positions[0].open_orders, 0); @@ -1081,16 +1081,16 @@ pub mod delisting_test { // net users are short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: (AMM_RESERVE_PRECISION * 2000) as i128, - base_asset_amount_long: (AMM_RESERVE_PRECISION * 2000) as i128, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + base_asset_amount_with_amm: ((AMM_RESERVE_PRECISION * 2000) as i128).into(), + base_asset_amount_long: ((AMM_RESERVE_PRECISION * 2000) as i128).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -1102,8 +1102,8 @@ pub mod delisting_test { ..HistoricalOracleData::default() }, - quote_asset_amount: (QUOTE_PRECISION_I128 * 20 * 2000), //longs have -$20 cost basis - total_fee_minus_distributions: 0, + quote_asset_amount: (QUOTE_PRECISION_I128 * 20 * 2000).into(), //longs have -$20 cost basis + total_fee_minus_distributions: 0.into(), ..AMM::default() }, number_of_users_with_base: 1, @@ -1112,7 +1112,7 @@ pub mod delisting_test { margin_ratio_maintenance: 500, status: MarketStatus::Initialized, pnl_pool: PoolBalance { - scaled_balance: (1000 * SPOT_BALANCE_PRECISION), + scaled_balance: (1000 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -1120,8 +1120,8 @@ pub mod delisting_test { ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -1129,14 +1129,14 @@ pub mod delisting_test { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, initial_liability_weight: SPOT_WEIGHT_PRECISION, maintenance_liability_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, - borrow_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), + borrow_balance: (100 * SPOT_BALANCE_PRECISION).into(), historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64), ..SpotMarket::default() }; @@ -1228,7 +1228,7 @@ pub mod delisting_test { assert_eq!(margin_requirement, 10000); let market = market_map.get_ref_mut(&0).unwrap(); - assert_eq!(market.pnl_pool.scaled_balance, 1000000000000); + assert_eq!(market.pnl_pool.scaled_balance(), 1000000000000); assert_eq!(taker.spot_positions[0].scaled_balance, 100000000000); assert_eq!(taker.perp_positions[0].quote_asset_amount, 40000000000); drop(market); @@ -1248,8 +1248,8 @@ pub mod delisting_test { assert_eq!(taker.spot_positions[0].scaled_balance > 100000000000, true); let market = market_map.get_ref_mut(&0).unwrap(); - assert_eq!(market.pnl_pool.scaled_balance, 39002002000); // no settle fee since base_asse_value=0 (since price is negative) - assert_eq!(market.amm.fee_pool.scaled_balance, 0); + assert_eq!(market.pnl_pool.scaled_balance(), 39002002000); // no settle fee since base_asse_value=0 (since price is negative) + assert_eq!(market.amm.fee_pool.scaled_balance(), 0); drop(market); assert_eq!(taker.perp_positions[0].open_orders, 0); @@ -1285,17 +1285,17 @@ pub mod delisting_test { // net users are short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: (AMM_RESERVE_PRECISION * 1000) as i128, - base_asset_amount_long: (AMM_RESERVE_PRECISION * 2000) as i128, - base_asset_amount_short: -((AMM_RESERVE_PRECISION * 1000) as i128), - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + base_asset_amount_with_amm: ((AMM_RESERVE_PRECISION * 1000) as i128).into(), + base_asset_amount_long: ((AMM_RESERVE_PRECISION * 2000) as i128).into(), + base_asset_amount_short: (-((AMM_RESERVE_PRECISION * 1000) as i128)).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -1307,8 +1307,9 @@ pub mod delisting_test { ..HistoricalOracleData::default() }, - quote_asset_amount: -(QUOTE_PRECISION_I128 * 20 * 1000 - QUOTE_PRECISION_I128), - total_fee_minus_distributions: 0, + quote_asset_amount: (-(QUOTE_PRECISION_I128 * 20 * 1000 - QUOTE_PRECISION_I128)) + .into(), + total_fee_minus_distributions: 0.into(), ..AMM::default() }, number_of_users_with_base: 2, @@ -1317,7 +1318,7 @@ pub mod delisting_test { margin_ratio_maintenance: 500, status: MarketStatus::Initialized, pnl_pool: PoolBalance { - scaled_balance: (1000 * SPOT_BALANCE_PRECISION), + scaled_balance: (1000 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -1325,8 +1326,8 @@ pub mod delisting_test { ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -1334,14 +1335,14 @@ pub mod delisting_test { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, initial_liability_weight: SPOT_WEIGHT_PRECISION, maintenance_liability_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 300000 * SPOT_BALANCE_PRECISION, - borrow_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (300000 * SPOT_BALANCE_PRECISION).into(), + borrow_balance: (100 * SPOT_BALANCE_PRECISION).into(), historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64), ..SpotMarket::default() }; @@ -1350,15 +1351,15 @@ pub mod delisting_test { market_index: 1, oracle_source: OracleSource::Pyth, oracle: oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: SPOT_BALANCE_PRECISION.into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: (oracle_price.agg.price * 99 / 100), @@ -1532,7 +1533,7 @@ pub mod delisting_test { .unwrap(); let market = market_map.get_ref_mut(&0).unwrap(); - assert_eq!(market.pnl_pool.scaled_balance, 1000000000000); + assert_eq!(market.pnl_pool.scaled_balance(), 1000000000000); let orig_short_balance = shorter.spot_positions[0].scaled_balance; @@ -1582,8 +1583,8 @@ pub mod delisting_test { assert_eq!(shorter_loss, 1019997999000); //$1020 loss let market = market_map.get_ref_mut(&0).unwrap(); - assert_eq!(market.pnl_pool.scaled_balance, 2019997999000); //$2020 - assert_eq!(market.amm.fee_pool.scaled_balance, 0); + assert_eq!(market.pnl_pool.scaled_balance(), 2019997999000); //$2020 + assert_eq!(market.amm.fee_pool.scaled_balance(), 0); drop(market); assert_eq!(shorter.perp_positions[0].open_orders, 0); @@ -1610,7 +1611,7 @@ pub mod delisting_test { assert_eq!(margin_requirement, 10000); let market = market_map.get_ref_mut(&0).unwrap(); - assert_eq!(market.pnl_pool.scaled_balance, 2019997999000); + assert_eq!(market.pnl_pool.scaled_balance(), 2019997999000); assert_eq!(longer.spot_positions[0].scaled_balance, 20000000000000); assert_eq!(longer.perp_positions[0].quote_asset_amount, -40001000000); drop(market); @@ -1631,8 +1632,8 @@ pub mod delisting_test { assert_eq!(longer.spot_positions[0].scaled_balance, 21955000002000); let market = market_map.get_ref_mut(&0).unwrap(); - assert_eq!(market.pnl_pool.scaled_balance, 64997997000); //fee from settling - assert_eq!(market.amm.fee_pool.scaled_balance, 0); + assert_eq!(market.pnl_pool.scaled_balance(), 64997997000); //fee from settling + assert_eq!(market.amm.fee_pool.scaled_balance(), 0); drop(market); assert_eq!(longer.perp_positions[0].open_orders, 0); @@ -1651,7 +1652,7 @@ pub mod delisting_test { let market = market_map.get_ref_mut(&0).unwrap(); assert_eq!(market.number_of_users_with_base, 0); - assert_eq!(market.amm.quote_asset_amount, 2000000); + assert_eq!(market.amm.quote_asset_amount(), 2000000); drop(market); settle_expired_position( 0, @@ -1675,12 +1676,12 @@ pub mod delisting_test { assert_eq!(liq.spot_positions[0].scaled_balance > 0, true); let market = market_map.get_ref_mut(&0).unwrap(); - assert_eq!(market.amm.base_asset_amount_long, 0); - assert_eq!(market.amm.base_asset_amount_short, 0); + assert_eq!(market.amm.base_asset_amount_long(), 0); + assert_eq!(market.amm.base_asset_amount_short(), 0); assert_eq!(market.number_of_users_with_base, 0); - assert_eq!(market.amm.base_asset_amount_with_amm, 0); - assert_eq!(market.amm.quote_asset_amount, 0); - assert_eq!(market.amm.total_social_loss, 0); + assert_eq!(market.amm.base_asset_amount_with_amm(), 0); + assert_eq!(market.amm.quote_asset_amount(), 0); + assert_eq!(market.amm.total_social_loss(), 0); drop(market); } @@ -1710,17 +1711,17 @@ pub mod delisting_test { // net users are short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: -((AMM_RESERVE_PRECISION * 800) as i128), - base_asset_amount_long: (AMM_RESERVE_PRECISION * 200) as i128, - base_asset_amount_short: -((AMM_RESERVE_PRECISION * 1000) as i128), - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + base_asset_amount_with_amm: (-((AMM_RESERVE_PRECISION * 800) as i128)).into(), + base_asset_amount_long: ((AMM_RESERVE_PRECISION * 200) as i128).into(), + base_asset_amount_short: (-((AMM_RESERVE_PRECISION * 1000) as i128)).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -1732,9 +1733,10 @@ pub mod delisting_test { ..HistoricalOracleData::default() }, - quote_asset_amount: (QUOTE_PRECISION_I128 * 200) - + (QUOTE_PRECISION_I128 * 97 * 1000), - total_fee_minus_distributions: 0, + quote_asset_amount: ((QUOTE_PRECISION_I128 * 200) + + (QUOTE_PRECISION_I128 * 97 * 1000)) + .into(), + total_fee_minus_distributions: 0.into(), ..AMM::default() }, number_of_users_with_base: 2, @@ -1742,7 +1744,7 @@ pub mod delisting_test { margin_ratio_maintenance: 500, status: MarketStatus::Initialized, pnl_pool: PoolBalance { - scaled_balance: (1000 * SPOT_BALANCE_PRECISION), + scaled_balance: (1000 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -1750,8 +1752,8 @@ pub mod delisting_test { ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -1759,15 +1761,15 @@ pub mod delisting_test { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, initial_liability_weight: SPOT_WEIGHT_PRECISION, maintenance_liability_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, - borrow_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), + borrow_balance: (100 * SPOT_BALANCE_PRECISION).into(), historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64), ..SpotMarket::default() }; @@ -1776,15 +1778,15 @@ pub mod delisting_test { market_index: 1, oracle_source: OracleSource::Pyth, oracle: oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: SPOT_BALANCE_PRECISION.into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: (oracle_price.agg.price * 99 / 100), @@ -1960,7 +1962,7 @@ pub mod delisting_test { assert_eq!(margin_requirement, 10000); let market = market_map.get_ref_mut(&0).unwrap(); - assert_eq!(market.pnl_pool.scaled_balance, 1000000000000); + assert_eq!(market.pnl_pool.scaled_balance(), 1000000000000); assert_eq!(longer.spot_positions[0].scaled_balance, 20000000000000); assert_eq!(longer.perp_positions[0].quote_asset_amount, 2000000000); let longer_balance_before = longer.spot_positions[0].scaled_balance; @@ -1988,8 +1990,8 @@ pub mod delisting_test { ); let market = market_map.get_ref_mut(&0).unwrap(); - assert_eq!(market.pnl_pool.scaled_balance, 1000000000000); - assert_eq!(market.amm.fee_pool.scaled_balance, 0); + assert_eq!(market.pnl_pool.scaled_balance(), 1000000000000); + assert_eq!(market.amm.fee_pool.scaled_balance(), 0); drop(market); } @@ -2094,17 +2096,17 @@ pub mod delisting_test { // net users are short let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - base_asset_amount_with_amm: -((AMM_RESERVE_PRECISION * 800) as i128), - base_asset_amount_long: (AMM_RESERVE_PRECISION * 200) as i128, - base_asset_amount_short: -((AMM_RESERVE_PRECISION * 1000) as i128), - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + base_asset_amount_with_amm: (-((AMM_RESERVE_PRECISION * 800) as i128)).into(), + base_asset_amount_long: ((AMM_RESERVE_PRECISION * 200) as i128).into(), + base_asset_amount_short: (-((AMM_RESERVE_PRECISION * 1000) as i128)).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -2116,8 +2118,8 @@ pub mod delisting_test { ..HistoricalOracleData::default() }, - quote_asset_amount: QUOTE_PRECISION_I128 * (97 * 1000 + 200), - total_fee_minus_distributions: 0, + quote_asset_amount: (QUOTE_PRECISION_I128 * (97 * 1000 + 200)).into(), + total_fee_minus_distributions: 0.into(), ..AMM::default() }, number_of_users_with_base: 2, @@ -2126,7 +2128,7 @@ pub mod delisting_test { margin_ratio_maintenance: 500, status: MarketStatus::Initialized, pnl_pool: PoolBalance { - scaled_balance: (1000 * SPOT_BALANCE_PRECISION), + scaled_balance: (1000 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -2134,8 +2136,8 @@ pub mod delisting_test { ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -2143,15 +2145,15 @@ pub mod delisting_test { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, initial_liability_weight: SPOT_WEIGHT_PRECISION, maintenance_liability_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 40000 * SPOT_BALANCE_PRECISION, - borrow_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (40000 * SPOT_BALANCE_PRECISION).into(), + borrow_balance: (100 * SPOT_BALANCE_PRECISION).into(), historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64), ..SpotMarket::default() }; @@ -2160,15 +2162,15 @@ pub mod delisting_test { market_index: 1, oracle_source: OracleSource::Pyth, oracle: oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: SPOT_BALANCE_PRECISION.into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: (oracle_price.agg.price * 99 / 100), @@ -2338,7 +2340,7 @@ pub mod delisting_test { .unwrap(); let market = market_map.get_ref_mut(&0).unwrap(); - assert_eq!(market.pnl_pool.scaled_balance, 1000000000000); + assert_eq!(market.pnl_pool.scaled_balance(), 1000000000000); let orig_short_balance = shorter.spot_positions[0].scaled_balance; @@ -2386,10 +2388,10 @@ pub mod delisting_test { assert_eq!(longer.perp_positions[0].quote_asset_amount, 200000000); assert_eq!( - market.amm.base_asset_amount_long + market.amm.base_asset_amount_short, + market.amm.base_asset_amount_long() + market.amm.base_asset_amount_short(), -800000000000 ); - assert_eq!(market.amm.quote_asset_amount, 97200000000); + assert_eq!(market.amm.quote_asset_amount(), 97200000000); drop(market); @@ -2456,17 +2458,17 @@ pub mod delisting_test { assert_eq!(longer.perp_positions[0].quote_asset_amount, 200000000); assert_eq!( - market.amm.base_asset_amount_long + market.amm.base_asset_amount_short, + market.amm.base_asset_amount_long() + market.amm.base_asset_amount_short(), -800000000000 ); - assert_eq!(market.amm.quote_asset_amount, 97200000000); + assert_eq!(market.amm.quote_asset_amount(), 97200000000); assert_eq!(shorter.perp_positions[0].base_asset_amount, 0); assert_eq!(shorter.perp_positions[0].quote_asset_amount, -23250001000); assert_eq!( liquidator.perp_positions[0].base_asset_amount as i128, - market.amm.base_asset_amount_short + market.amm.base_asset_amount_short() ); assert_eq!( liquidator.perp_positions[0].quote_asset_amount, @@ -2476,10 +2478,10 @@ pub mod delisting_test { assert_eq!( longer.perp_positions[0].base_asset_amount as i128, - market.amm.base_asset_amount_long + market.amm.base_asset_amount_long() ); - assert_eq!(market.amm.quote_asset_amount, 97200000000); + assert_eq!(market.amm.quote_asset_amount(), 97200000000); drop(market); } @@ -2511,10 +2513,10 @@ pub mod delisting_test { let mut market = market_map.get_ref_mut(&0).unwrap(); let oracle_price_data = oracle_map.get_price_data(&market.oracle_id()).unwrap(); - assert_eq!(market.amm.quote_asset_amount, 97200000000); + assert_eq!(market.amm.quote_asset_amount(), 97200000000); - assert_eq!(market.amm.cumulative_funding_rate_long, 0); - assert_eq!(market.amm.cumulative_funding_rate_short, 0); + assert_eq!(market.amm.cumulative_funding_rate_long(), 0); + assert_eq!(market.amm.cumulative_funding_rate_short(), 0); let strict_quote_price = StrictOraclePrice::test(QUOTE_PRECISION_I64); let (perp_margin_requirement, weighted_pnl, _, _, _) = @@ -2544,17 +2546,17 @@ pub mod delisting_test { assert_eq!(longer.perp_positions[0].quote_asset_amount, 200000000); assert_eq!( - market.amm.base_asset_amount_long + market.amm.base_asset_amount_short, + market.amm.base_asset_amount_long() + market.amm.base_asset_amount_short(), -800000000000 ); - assert_eq!(market.amm.quote_asset_amount, 97200000000); + assert_eq!(market.amm.quote_asset_amount(), 97200000000); assert_eq!(shorter.perp_positions[0].base_asset_amount, 0); assert_eq!(shorter.perp_positions[0].quote_asset_amount, -23249001000); assert_eq!( liquidator.perp_positions[0].base_asset_amount as i128, - market.amm.base_asset_amount_short + market.amm.base_asset_amount_short() ); assert_eq!( @@ -2564,11 +2566,11 @@ pub mod delisting_test { assert_eq!( longer.perp_positions[0].base_asset_amount as i128, - market.amm.base_asset_amount_long + market.amm.base_asset_amount_long() ); assert_eq!(longer.perp_positions[0].quote_asset_amount, 200000000); - assert_eq!(market.amm.quote_asset_amount, 201000000 + 96999000000); + assert_eq!(market.amm.quote_asset_amount(), 201000000 + 96999000000); // add a liq fee now market.liquidator_fee = 10000; @@ -2603,10 +2605,10 @@ pub mod delisting_test { let market = market_map.get_ref_mut(&0).unwrap(); let oracle_price_data = oracle_map.get_price_data(&market.oracle_id()).unwrap(); - assert_eq!(market.amm.quote_asset_amount, 20000010000 + 77199990000); + assert_eq!(market.amm.quote_asset_amount(), 20000010000 + 77199990000); - assert_eq!(market.amm.cumulative_funding_rate_long, 0); - assert_eq!(market.amm.cumulative_funding_rate_short, 0); + assert_eq!(market.amm.cumulative_funding_rate_long(), 0); + assert_eq!(market.amm.cumulative_funding_rate_short(), 0); let strict_quote_price = StrictOraclePrice::test(QUOTE_PRECISION_I64); let (perp_margin_requirement, weighted_pnl, _, _, _) = @@ -2636,17 +2638,17 @@ pub mod delisting_test { assert_eq!(longer.perp_positions[0].quote_asset_amount, 200000000); assert_eq!( - market.amm.base_asset_amount_long + market.amm.base_asset_amount_short, + market.amm.base_asset_amount_long() + market.amm.base_asset_amount_short(), -800000000000 ); - assert_eq!(market.amm.quote_asset_amount, 97200000000); + assert_eq!(market.amm.quote_asset_amount(), 97200000000); assert_eq!(shorter.perp_positions[0].base_asset_amount, 0); assert_eq!(shorter.perp_positions[0].quote_asset_amount, -3449991000); assert_eq!( liquidator.perp_positions[0].base_asset_amount as i128, - market.amm.base_asset_amount_short + market.amm.base_asset_amount_short() ); assert_eq!( liquidator.perp_positions[0].quote_asset_amount, @@ -2655,12 +2657,12 @@ pub mod delisting_test { assert_eq!( longer.perp_positions[0].base_asset_amount as i128, - market.amm.base_asset_amount_long + market.amm.base_asset_amount_long() ); assert_eq!(longer.perp_positions[0].quote_asset_amount, 200000000,); - assert_eq!(market.amm.quote_asset_amount, 20000010000 + 77199990000); - assert_eq!(market.amm.total_social_loss, 0); + assert_eq!(market.amm.quote_asset_amount(), 20000010000 + 77199990000); + assert_eq!(market.amm.total_social_loss(), 0); drop(market); } @@ -2734,16 +2736,16 @@ pub mod delisting_test { assert_eq!(shorter_loss, 20000000000000); //$16629 loss let market = market_map.get_ref_mut(&0).unwrap(); - assert_eq!(market.amm.total_social_loss, 3449991000); - assert_eq!(market.amm.base_asset_amount_long, 200000000000); - assert_eq!(market.amm.base_asset_amount_short, 0); - assert_eq!(market.amm.base_asset_amount_with_amm, 200000000000); + assert_eq!(market.amm.total_social_loss(), 3449991000); + assert_eq!(market.amm.base_asset_amount_long(), 200000000000); + assert_eq!(market.amm.base_asset_amount_short(), 0); + assert_eq!(market.amm.base_asset_amount_with_amm(), 200000000000); - assert_eq!(market.amm.cumulative_funding_rate_long, 17249955000); - assert_eq!(market.amm.cumulative_funding_rate_short, -17249955000); + assert_eq!(market.amm.cumulative_funding_rate_long(), 17249955000); + assert_eq!(market.amm.cumulative_funding_rate_short(), -17249955000); - assert_eq!(market.pnl_pool.scaled_balance, 20920260001000); //$20920 - assert_eq!(market.amm.fee_pool.scaled_balance, 0); + assert_eq!(market.pnl_pool.scaled_balance(), 20920260001000); //$20920 + assert_eq!(market.amm.fee_pool.scaled_balance(), 0); drop(market); assert_eq!(shorter.perp_positions[0].open_orders, 0); @@ -2794,22 +2796,22 @@ pub mod delisting_test { .unwrap(); let market = market_map.get_ref_mut(&0).unwrap(); - assert_eq!(market.pnl_pool.scaled_balance, 20920260001000); + assert_eq!(market.pnl_pool.scaled_balance(), 20920260001000); assert_eq!(longer.spot_positions[0].scaled_balance, 20000000000000); assert_eq!(longer.perp_positions[0].quote_asset_amount, 200000000); assert_eq!(longer.perp_positions[0].quote_asset_amount, 200000000); assert_eq!(longer.perp_positions[0].last_cumulative_funding_rate, 0); - assert_eq!(market.amm.cumulative_funding_rate_long, 17249955000); + assert_eq!(market.amm.cumulative_funding_rate_long(), 17249955000); let longer_funding_payment = calculate_funding_payment( - market.amm.cumulative_funding_rate_long, + market.amm.cumulative_funding_rate_long(), &longer.perp_positions[0], ) .unwrap(); assert_eq!(longer_funding_payment, -3449991000); - assert_eq!(market.amm.quote_asset_amount, 200000000); - assert_eq!(market.amm.total_social_loss, 3449991000); + assert_eq!(market.amm.quote_asset_amount(), 200000000); + assert_eq!(market.amm.total_social_loss(), 3449991000); drop(market); @@ -2832,25 +2834,25 @@ pub mod delisting_test { assert_eq!(longer.spot_positions[0].scaled_balance, 40775959200000); //$40775 let market = market_map.get_ref_mut(&0).unwrap(); - assert_eq!(market.pnl_pool.scaled_balance, 144300801000); // fees collected - assert_eq!(market.amm.fee_pool.scaled_balance, 0); + assert_eq!(market.pnl_pool.scaled_balance(), 144300801000); // fees collected + assert_eq!(market.amm.fee_pool.scaled_balance(), 0); assert_eq!(market.number_of_users_with_base, 0); - assert_eq!(market.amm.base_asset_amount_long, 0); - assert_eq!(market.amm.base_asset_amount_short, 0); + assert_eq!(market.amm.base_asset_amount_long(), 0); + assert_eq!(market.amm.base_asset_amount_short(), 0); - assert_eq!(market.amm.base_asset_amount_with_amm, 0); + assert_eq!(market.amm.base_asset_amount_with_amm(), 0); - assert_eq!(market.amm.quote_asset_amount, 0); + assert_eq!(market.amm.quote_asset_amount(), 0); - assert_eq!(market.amm.total_social_loss, 3449991000); + assert_eq!(market.amm.total_social_loss(), 3449991000); let oracle_price_data = oracle_map.get_price_data(&market.oracle_id()).unwrap(); assert_eq!(oracle_price_data.price, 100 * PRICE_PRECISION_I64); let net_pnl = calculate_net_user_pnl(&market.amm, oracle_price_data.price).unwrap(); assert_eq!(net_pnl, 3449991000); assert_eq!(market.amm.net_unsettled_funding_pnl, 3449991000); //todo? - assert_eq!(market.amm.quote_asset_amount_per_lp, 0); + assert_eq!(market.amm.quote_asset_amount_per_lp(), 0); drop(market); diff --git a/programs/drift/src/controller/pnl/tests.rs b/programs/drift/src/controller/pnl/tests.rs index 4a35df4e49..3fee1d5440 100644 --- a/programs/drift/src/controller/pnl/tests.rs +++ b/programs/drift/src/controller/pnl/tests.rs @@ -66,20 +66,20 @@ pub fn user_no_position() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, - base_asset_amount_long: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), + base_asset_amount_long: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData { last_oracle_price: oracle_price.agg.price, @@ -95,7 +95,7 @@ pub fn user_no_position() { status: MarketStatus::Active, liquidator_fee: LIQUIDATION_FEE_PRECISION / 100, pnl_pool: PoolBalance { - scaled_balance: (50 * SPOT_BALANCE_PRECISION), + scaled_balance: (50 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -107,10 +107,10 @@ pub fn user_no_position() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default() }; create_anchor_account_info!(spot_market, SpotMarket, spot_market_account_info); @@ -184,20 +184,20 @@ pub fn user_does_not_meet_maintenance_requirement() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, - base_asset_amount_long: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), + base_asset_amount_long: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData { last_oracle_price: oracle_price.agg.price, @@ -213,7 +213,7 @@ pub fn user_does_not_meet_maintenance_requirement() { status: MarketStatus::Active, liquidator_fee: LIQUIDATION_FEE_PRECISION / 100, pnl_pool: PoolBalance { - scaled_balance: (50 * SPOT_BALANCE_PRECISION), + scaled_balance: (50 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -226,11 +226,11 @@ pub fn user_does_not_meet_maintenance_requirement() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64), ..SpotMarket::default() }; @@ -309,20 +309,20 @@ pub fn user_does_not_meet_strict_maintenance_requirement() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, - base_asset_amount_long: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), + base_asset_amount_long: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData { last_oracle_price: oracle_price.agg.price, @@ -338,7 +338,7 @@ pub fn user_does_not_meet_strict_maintenance_requirement() { status: MarketStatus::Active, liquidator_fee: LIQUIDATION_FEE_PRECISION / 100, pnl_pool: PoolBalance { - scaled_balance: (50 * SPOT_BALANCE_PRECISION), + scaled_balance: (50 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -351,11 +351,11 @@ pub fn user_does_not_meet_strict_maintenance_requirement() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), historical_oracle_data: HistoricalOracleData { last_oracle_price_twap_5min: PRICE_PRECISION_I64 / 2, ..HistoricalOracleData::default_price(QUOTE_PRECISION_I64) @@ -451,20 +451,20 @@ pub fn user_unsettled_negative_pnl() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, - base_asset_amount_long: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), + base_asset_amount_long: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData { last_oracle_price: oracle_price.agg.price, @@ -481,7 +481,7 @@ pub fn user_unsettled_negative_pnl() { status: MarketStatus::Active, liquidator_fee: LIQUIDATION_FEE_PRECISION / 100, pnl_pool: PoolBalance { - scaled_balance: (50 * SPOT_BALANCE_PRECISION), + scaled_balance: (50 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -494,11 +494,11 @@ pub fn user_unsettled_negative_pnl() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64), ..SpotMarket::default() }; @@ -530,8 +530,12 @@ pub fn user_unsettled_negative_pnl() { expected_user.spot_positions[0].scaled_balance = 50 * SPOT_BALANCE_PRECISION_U64; let mut expected_market = market; - expected_market.pnl_pool.scaled_balance = 100 * SPOT_BALANCE_PRECISION; - expected_market.amm.quote_asset_amount = -100 * QUOTE_PRECISION_I128; + expected_market + .pnl_pool + .set_scaled_balance(100 * SPOT_BALANCE_PRECISION); + expected_market + .amm + .set_quote_asset_amount(-100 * QUOTE_PRECISION_I128); expected_market.number_of_users = 0; settle_pnl( @@ -588,20 +592,20 @@ pub fn user_unsettled_positive_pnl_more_than_pool() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, - base_asset_amount_long: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), + base_asset_amount_long: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData { last_oracle_price: oracle_price.agg.price, @@ -617,7 +621,7 @@ pub fn user_unsettled_positive_pnl_more_than_pool() { status: MarketStatus::Active, liquidator_fee: LIQUIDATION_FEE_PRECISION / 100, pnl_pool: PoolBalance { - scaled_balance: (50 * SPOT_BALANCE_PRECISION), + scaled_balance: (50 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -630,11 +634,11 @@ pub fn user_unsettled_positive_pnl_more_than_pool() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64), ..SpotMarket::default() }; @@ -666,8 +670,10 @@ pub fn user_unsettled_positive_pnl_more_than_pool() { expected_user.spot_positions[0].scaled_balance = 150 * SPOT_BALANCE_PRECISION_U64; let mut expected_market = market; - expected_market.pnl_pool.scaled_balance = 0; - expected_market.amm.quote_asset_amount = -200 * QUOTE_PRECISION_I128; + expected_market.pnl_pool.set_scaled_balance(0); + expected_market + .amm + .set_quote_asset_amount(-200 * QUOTE_PRECISION_I128); settle_pnl( 0, @@ -723,20 +729,20 @@ pub fn user_unsettled_positive_pnl_less_than_pool() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, - base_asset_amount_long: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), + base_asset_amount_long: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData { last_oracle_price: oracle_price.agg.price, @@ -753,7 +759,7 @@ pub fn user_unsettled_positive_pnl_less_than_pool() { status: MarketStatus::Active, liquidator_fee: LIQUIDATION_FEE_PRECISION / 100, pnl_pool: PoolBalance { - scaled_balance: (50 * SPOT_BALANCE_PRECISION), + scaled_balance: (50 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -766,11 +772,11 @@ pub fn user_unsettled_positive_pnl_less_than_pool() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64), ..SpotMarket::default() }; @@ -802,8 +808,12 @@ pub fn user_unsettled_positive_pnl_less_than_pool() { expected_user.spot_positions[0].scaled_balance = 125 * SPOT_BALANCE_PRECISION_U64; let mut expected_market = market; - expected_market.pnl_pool.scaled_balance = 25 * SPOT_BALANCE_PRECISION; - expected_market.amm.quote_asset_amount = -175 * QUOTE_PRECISION_I128; + expected_market + .pnl_pool + .set_scaled_balance(25 * SPOT_BALANCE_PRECISION); + expected_market + .amm + .set_quote_asset_amount(-175 * QUOTE_PRECISION_I128); expected_market.number_of_users = 0; settle_pnl( @@ -861,22 +871,22 @@ pub fn market_fee_pool_receives_portion() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, - base_asset_amount_long: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), + base_asset_amount_long: BASE_PRECISION_I128.into(), oracle: oracle_price_key, - total_fee_minus_distributions: QUOTE_PRECISION_I128, + total_fee_minus_distributions: QUOTE_PRECISION_I128.into(), historical_oracle_data: HistoricalOracleData { last_oracle_price: oracle_price.agg.price, last_oracle_price_twap_5min: oracle_price.agg.price, @@ -892,7 +902,7 @@ pub fn market_fee_pool_receives_portion() { status: MarketStatus::Active, liquidator_fee: LIQUIDATION_FEE_PRECISION / 100, pnl_pool: PoolBalance { - scaled_balance: (50 * SPOT_BALANCE_PRECISION), + scaled_balance: (50 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -905,11 +915,11 @@ pub fn market_fee_pool_receives_portion() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64), ..SpotMarket::default() }; @@ -941,9 +951,16 @@ pub fn market_fee_pool_receives_portion() { expected_user.spot_positions[0].scaled_balance = 100 * SPOT_BALANCE_PRECISION_U64; let mut expected_market = market; - expected_market.pnl_pool.scaled_balance = 149 * SPOT_BALANCE_PRECISION; - expected_market.amm.fee_pool.scaled_balance = SPOT_BALANCE_PRECISION; - expected_market.amm.quote_asset_amount = -50 * QUOTE_PRECISION_I128; + expected_market + .pnl_pool + .set_scaled_balance(149 * SPOT_BALANCE_PRECISION); + expected_market + .amm + .fee_pool + .set_scaled_balance(SPOT_BALANCE_PRECISION); + expected_market + .amm + .set_quote_asset_amount(-50 * QUOTE_PRECISION_I128); expected_market.number_of_users = 0; settle_pnl( @@ -1000,24 +1017,24 @@ pub fn market_fee_pool_pays_back_to_pnl_pool() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, - base_asset_amount_long: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), + base_asset_amount_long: BASE_PRECISION_I128.into(), oracle: oracle_price_key, - total_fee_minus_distributions: QUOTE_PRECISION_I128, + total_fee_minus_distributions: QUOTE_PRECISION_I128.into(), fee_pool: PoolBalance { - scaled_balance: (2 * SPOT_BALANCE_PRECISION), + scaled_balance: (2 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -1036,7 +1053,7 @@ pub fn market_fee_pool_pays_back_to_pnl_pool() { status: MarketStatus::Active, liquidator_fee: LIQUIDATION_FEE_PRECISION / 100, pnl_pool: PoolBalance { - scaled_balance: (50 * SPOT_BALANCE_PRECISION), + scaled_balance: (50 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -1049,11 +1066,11 @@ pub fn market_fee_pool_pays_back_to_pnl_pool() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64), ..SpotMarket::default() }; @@ -1085,9 +1102,16 @@ pub fn market_fee_pool_pays_back_to_pnl_pool() { expected_user.spot_positions[0].scaled_balance = 100 * SPOT_BALANCE_PRECISION_U64; let mut expected_market = market; - expected_market.pnl_pool.scaled_balance = 151 * SPOT_BALANCE_PRECISION; - expected_market.amm.fee_pool.scaled_balance = SPOT_BALANCE_PRECISION; - expected_market.amm.quote_asset_amount = -50 * QUOTE_PRECISION_I128; + expected_market + .pnl_pool + .set_scaled_balance(151 * SPOT_BALANCE_PRECISION); + expected_market + .amm + .fee_pool + .set_scaled_balance(SPOT_BALANCE_PRECISION); + expected_market + .amm + .set_quote_asset_amount(-50 * QUOTE_PRECISION_I128); expected_market.number_of_users = 0; settle_pnl( @@ -1144,20 +1168,20 @@ pub fn user_long_positive_unrealized_pnl_up_to_max_positive_pnl() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 151 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (151 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, - base_asset_amount_long: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), + base_asset_amount_long: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData { last_oracle_price: oracle_price.agg.price, @@ -1173,7 +1197,7 @@ pub fn user_long_positive_unrealized_pnl_up_to_max_positive_pnl() { status: MarketStatus::Active, liquidator_fee: LIQUIDATION_FEE_PRECISION / 100, pnl_pool: PoolBalance { - scaled_balance: (50 * SPOT_BALANCE_PRECISION), + scaled_balance: (50 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -1186,11 +1210,11 @@ pub fn user_long_positive_unrealized_pnl_up_to_max_positive_pnl() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64), ..SpotMarket::default() }; @@ -1225,8 +1249,10 @@ pub fn user_long_positive_unrealized_pnl_up_to_max_positive_pnl() { expected_user.spot_positions[0].scaled_balance = 150 * SPOT_BALANCE_PRECISION_U64; let mut expected_market = market; - expected_market.pnl_pool.scaled_balance = 0; - expected_market.amm.quote_asset_amount = -200 * QUOTE_PRECISION_I128; + expected_market.pnl_pool.set_scaled_balance(0); + expected_market + .amm + .set_quote_asset_amount(-200 * QUOTE_PRECISION_I128); settle_pnl( 0, @@ -1282,20 +1308,20 @@ pub fn user_long_positive_unrealized_pnl_up_to_max_positive_pnl_price_breached() let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 121 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (121 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, - base_asset_amount_long: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), + base_asset_amount_long: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData { last_oracle_price: oracle_price.agg.price, @@ -1311,7 +1337,7 @@ pub fn user_long_positive_unrealized_pnl_up_to_max_positive_pnl_price_breached() status: MarketStatus::Active, liquidator_fee: LIQUIDATION_FEE_PRECISION / 100, pnl_pool: PoolBalance { - scaled_balance: (50 * SPOT_BALANCE_PRECISION), + scaled_balance: (50 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -1324,11 +1350,11 @@ pub fn user_long_positive_unrealized_pnl_up_to_max_positive_pnl_price_breached() let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64), ..SpotMarket::default() }; @@ -1363,8 +1389,10 @@ pub fn user_long_positive_unrealized_pnl_up_to_max_positive_pnl_price_breached() expected_user.spot_positions[0].cumulative_deposits = 50 * QUOTE_PRECISION_I64; let mut expected_market = market; - expected_market.pnl_pool.scaled_balance = 0; - expected_market.amm.quote_asset_amount = -200 * QUOTE_PRECISION_I128; + expected_market.pnl_pool.set_scaled_balance(0); + expected_market + .amm + .set_quote_asset_amount(-200 * QUOTE_PRECISION_I128); assert!(settle_pnl( 0, @@ -1417,20 +1445,20 @@ pub fn user_long_negative_unrealized_pnl() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 51 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (51 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: -150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, - base_asset_amount_long: BASE_PRECISION_I128, + quote_asset_amount: (-150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), + base_asset_amount_long: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData { last_oracle_price: oracle_price.agg.price, @@ -1446,7 +1474,7 @@ pub fn user_long_negative_unrealized_pnl() { status: MarketStatus::Active, liquidator_fee: LIQUIDATION_FEE_PRECISION / 100, pnl_pool: PoolBalance { - scaled_balance: (50 * SPOT_BALANCE_PRECISION), + scaled_balance: (50 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -1459,11 +1487,11 @@ pub fn user_long_negative_unrealized_pnl() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64), ..SpotMarket::default() }; @@ -1498,8 +1526,12 @@ pub fn user_long_negative_unrealized_pnl() { expected_user.spot_positions[0].scaled_balance = 50 * SPOT_BALANCE_PRECISION_U64; let mut expected_market = market; - expected_market.pnl_pool.scaled_balance = 100 * SPOT_BALANCE_PRECISION; - expected_market.amm.quote_asset_amount = -100 * QUOTE_PRECISION_I128; + expected_market + .pnl_pool + .set_scaled_balance(100 * SPOT_BALANCE_PRECISION); + expected_market + .amm + .set_quote_asset_amount(-100 * QUOTE_PRECISION_I128); settle_pnl( 0, @@ -1555,20 +1587,20 @@ pub fn user_short_positive_unrealized_pnl_up_to_max_positive_pnl() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 51 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (51 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, - base_asset_amount_long: BASE_PRECISION_I128, + quote_asset_amount: (150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), + base_asset_amount_long: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData { last_oracle_price: oracle_price.agg.price, @@ -1584,7 +1616,7 @@ pub fn user_short_positive_unrealized_pnl_up_to_max_positive_pnl() { status: MarketStatus::Active, liquidator_fee: LIQUIDATION_FEE_PRECISION / 100, pnl_pool: PoolBalance { - scaled_balance: (50 * SPOT_BALANCE_PRECISION), + scaled_balance: (50 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -1597,11 +1629,11 @@ pub fn user_short_positive_unrealized_pnl_up_to_max_positive_pnl() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64), ..SpotMarket::default() }; @@ -1636,8 +1668,10 @@ pub fn user_short_positive_unrealized_pnl_up_to_max_positive_pnl() { expected_user.spot_positions[0].scaled_balance = 150 * SPOT_BALANCE_PRECISION_U64; let mut expected_market = market; - expected_market.pnl_pool.scaled_balance = 0; - expected_market.amm.quote_asset_amount = 100 * QUOTE_PRECISION_I128; + expected_market.pnl_pool.set_scaled_balance(0); + expected_market + .amm + .set_quote_asset_amount(100 * QUOTE_PRECISION_I128); settle_pnl( 0, @@ -1693,20 +1727,20 @@ pub fn user_short_negative_unrealized_pnl() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, - base_asset_amount_long: BASE_PRECISION_I128, + quote_asset_amount: (150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), + base_asset_amount_long: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData { last_oracle_price: oracle_price.agg.price, @@ -1722,7 +1756,7 @@ pub fn user_short_negative_unrealized_pnl() { status: MarketStatus::Active, liquidator_fee: LIQUIDATION_FEE_PRECISION / 100, pnl_pool: PoolBalance { - scaled_balance: (50 * SPOT_BALANCE_PRECISION), + scaled_balance: (50 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -1735,11 +1769,11 @@ pub fn user_short_negative_unrealized_pnl() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64), ..SpotMarket::default() }; @@ -1774,8 +1808,12 @@ pub fn user_short_negative_unrealized_pnl() { expected_user.spot_positions[0].scaled_balance = 50 * SPOT_BALANCE_PRECISION_U64; let mut expected_market = market; - expected_market.pnl_pool.scaled_balance = 100 * SPOT_BALANCE_PRECISION; - expected_market.amm.quote_asset_amount = 200 * QUOTE_PRECISION_I128; + expected_market + .pnl_pool + .set_scaled_balance(100 * SPOT_BALANCE_PRECISION); + expected_market + .amm + .set_quote_asset_amount(200 * QUOTE_PRECISION_I128); settle_pnl( 0, @@ -1832,20 +1870,20 @@ pub fn user_invalid_oracle_position() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, - base_asset_amount_long: BASE_PRECISION_I128, + quote_asset_amount: (150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), + base_asset_amount_long: BASE_PRECISION_I128.into(), oracle: oracle_price_key, curve_update_intensity: 100, historical_oracle_data: HistoricalOracleData { @@ -1862,7 +1900,7 @@ pub fn user_invalid_oracle_position() { status: MarketStatus::Active, liquidator_fee: LIQUIDATION_FEE_PRECISION / 100, pnl_pool: PoolBalance { - scaled_balance: (50 * SPOT_BALANCE_PRECISION), + scaled_balance: (50 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -1872,11 +1910,11 @@ pub fn user_invalid_oracle_position() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), historical_oracle_data: HistoricalOracleData::default_price(QUOTE_PRECISION_I64), ..SpotMarket::default() }; @@ -2053,20 +2091,20 @@ pub fn is_price_divergence_ok_on_invalid_oracle() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 150 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, - base_asset_amount_long: BASE_PRECISION_I128, + quote_asset_amount: (150 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), + base_asset_amount_long: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData { last_oracle_price: oracle_price.agg.price, @@ -2082,7 +2120,7 @@ pub fn is_price_divergence_ok_on_invalid_oracle() { status: MarketStatus::Active, liquidator_fee: LIQUIDATION_FEE_PRECISION / 100, pnl_pool: PoolBalance { - scaled_balance: (50 * SPOT_BALANCE_PRECISION), + scaled_balance: (50 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, diff --git a/programs/drift/src/controller/position.rs b/programs/drift/src/controller/position.rs index 6b6ec530d5..b6debc088e 100644 --- a/programs/drift/src/controller/position.rs +++ b/programs/drift/src/controller/position.rs @@ -1,6 +1,8 @@ use crate::msg; -use anchor_lang::prelude::*; -use borsh::{BorshDeserialize, BorshSerialize}; +use anchor_lang::prelude::{ + borsh::{BorshDeserialize, BorshSerialize}, + *, +}; use crate::controller; use crate::controller::amm::SwapDirection; @@ -193,139 +195,183 @@ pub fn update_position_and_market( market.number_of_users_with_base = market.number_of_users_with_base.saturating_sub(1); } - market.amm.quote_asset_amount = market - .amm - .quote_asset_amount - .safe_add(delta.quote_asset_amount.cast()?)?; + market.amm.set_quote_asset_amount( + market + .amm + .quote_asset_amount() + .safe_add(delta.quote_asset_amount.cast()?)?, + ); match update_type { PositionUpdateType::Open | PositionUpdateType::Increase => { if new_base_asset_amount > 0 { - market.amm.base_asset_amount_long = market - .amm - .base_asset_amount_long - .safe_add(delta.base_asset_amount.cast()?)?; - market.amm.quote_entry_amount_long = market - .amm - .quote_entry_amount_long - .safe_add(delta.quote_asset_amount.cast()?)?; - market.amm.quote_break_even_amount_long = + market.amm.set_base_asset_amount_long( + market + .amm + .base_asset_amount_long() + .safe_add(delta.base_asset_amount.cast()?)?, + ); + market.amm.set_quote_entry_amount_long( market .amm - .quote_break_even_amount_long - .safe_add(delta.quote_asset_amount.cast()?)?; + .quote_entry_amount_long() + .safe_add(delta.quote_asset_amount.cast()?)?, + ); + market.amm.set_quote_break_even_amount_long( + market + .amm + .quote_break_even_amount_long() + .safe_add(delta.quote_asset_amount.cast()?)?, + ); } else { - market.amm.base_asset_amount_short = market - .amm - .base_asset_amount_short - .safe_add(delta.base_asset_amount.cast()?)?; - market.amm.quote_entry_amount_short = market - .amm - .quote_entry_amount_short - .safe_add(delta.quote_asset_amount.cast()?)?; - market.amm.quote_break_even_amount_short = market - .amm - .quote_break_even_amount_short - .safe_add(delta.quote_asset_amount.cast()?)?; + market.amm.set_base_asset_amount_short( + market + .amm + .base_asset_amount_short() + .safe_add(delta.base_asset_amount.cast()?)?, + ); + market.amm.set_quote_entry_amount_short( + market + .amm + .quote_entry_amount_short() + .safe_add(delta.quote_asset_amount.cast()?)?, + ); + market.amm.set_quote_break_even_amount_short( + market + .amm + .quote_break_even_amount_short() + .safe_add(delta.quote_asset_amount.cast()?)?, + ); } } PositionUpdateType::Reduce | PositionUpdateType::Close => { if position.base_asset_amount > 0 { - market.amm.base_asset_amount_long = market - .amm - .base_asset_amount_long - .safe_add(delta.base_asset_amount.cast()?)?; - market.amm.quote_entry_amount_long = market.amm.quote_entry_amount_long.safe_sub( - position - .quote_entry_amount - .safe_sub(new_quote_entry_amount)? - .cast()?, - )?; - market.amm.quote_break_even_amount_long = - market.amm.quote_break_even_amount_long.safe_sub( + market.amm.set_base_asset_amount_long( + market + .amm + .base_asset_amount_long() + .safe_add(delta.base_asset_amount.cast()?)?, + ); + market.amm.set_quote_entry_amount_long( + market.amm.quote_entry_amount_long().safe_sub( + position + .quote_entry_amount + .safe_sub(new_quote_entry_amount)? + .cast()?, + )?, + ); + market.amm.set_quote_break_even_amount_long( + market.amm.quote_break_even_amount_long().safe_sub( position .quote_break_even_amount .safe_sub(new_quote_break_even_amount)? .cast()?, - )?; + )?, + ); } else { - market.amm.base_asset_amount_short = market - .amm - .base_asset_amount_short - .safe_add(delta.base_asset_amount.cast()?)?; - market.amm.quote_entry_amount_short = - market.amm.quote_entry_amount_short.safe_sub( + market.amm.set_base_asset_amount_short( + market + .amm + .base_asset_amount_short() + .safe_add(delta.base_asset_amount.cast()?)?, + ); + market.amm.set_quote_entry_amount_short( + market.amm.quote_entry_amount_short().safe_sub( position .quote_entry_amount .safe_sub(new_quote_entry_amount)? .cast()?, - )?; - market.amm.quote_break_even_amount_short = - market.amm.quote_break_even_amount_short.safe_sub( + )?, + ); + market.amm.set_quote_break_even_amount_short( + market.amm.quote_break_even_amount_short().safe_sub( position .quote_break_even_amount .safe_sub(new_quote_break_even_amount)? .cast()?, - )?; + )?, + ); } } PositionUpdateType::Flip => { if new_base_asset_amount > 0 { - market.amm.base_asset_amount_short = market - .amm - .base_asset_amount_short - .safe_sub(position.base_asset_amount.cast()?)?; - market.amm.base_asset_amount_long = market - .amm - .base_asset_amount_long - .safe_add(new_base_asset_amount.cast()?)?; - - market.amm.quote_entry_amount_short = market - .amm - .quote_entry_amount_short - .safe_sub(position.quote_entry_amount.cast()?)?; - market.amm.quote_entry_amount_long = market - .amm - .quote_entry_amount_long - .safe_add(new_quote_entry_amount.cast()?)?; - - market.amm.quote_break_even_amount_short = market - .amm - .quote_break_even_amount_short - .safe_sub(position.quote_break_even_amount.cast()?)?; - market.amm.quote_break_even_amount_long = + market.amm.set_base_asset_amount_short( + market + .amm + .base_asset_amount_short() + .safe_sub(position.base_asset_amount.cast()?)?, + ); + market.amm.set_base_asset_amount_long( + market + .amm + .base_asset_amount_long() + .safe_add(new_base_asset_amount.cast()?)?, + ); + + market.amm.set_quote_entry_amount_short( market .amm - .quote_break_even_amount_long - .safe_add(new_quote_break_even_amount.cast()?)?; + .quote_entry_amount_short() + .safe_sub(position.quote_entry_amount.cast()?)?, + ); + market.amm.set_quote_entry_amount_long( + market + .amm + .quote_entry_amount_long() + .safe_add(new_quote_entry_amount.cast()?)?, + ); + + market.amm.set_quote_break_even_amount_short( + market + .amm + .quote_break_even_amount_short() + .safe_sub(position.quote_break_even_amount.cast()?)?, + ); + market.amm.set_quote_break_even_amount_long( + market + .amm + .quote_break_even_amount_long() + .safe_add(new_quote_break_even_amount.cast()?)?, + ); } else { - market.amm.base_asset_amount_long = market - .amm - .base_asset_amount_long - .safe_sub(position.base_asset_amount.cast()?)?; - market.amm.base_asset_amount_short = market - .amm - .base_asset_amount_short - .safe_add(new_base_asset_amount.cast()?)?; - - market.amm.quote_entry_amount_long = market - .amm - .quote_entry_amount_long - .safe_sub(position.quote_entry_amount.cast()?)?; - market.amm.quote_entry_amount_short = market - .amm - .quote_entry_amount_short - .safe_add(new_quote_entry_amount.cast()?)?; - - market.amm.quote_break_even_amount_long = + market.amm.set_base_asset_amount_long( market .amm - .quote_break_even_amount_long - .safe_sub(position.quote_break_even_amount.cast()?)?; - market.amm.quote_break_even_amount_short = market - .amm - .quote_break_even_amount_short - .safe_add(new_quote_break_even_amount.cast()?)?; + .base_asset_amount_long() + .safe_sub(position.base_asset_amount.cast()?)?, + ); + market.amm.set_base_asset_amount_short( + market + .amm + .base_asset_amount_short() + .safe_add(new_base_asset_amount.cast()?)?, + ); + + market.amm.set_quote_entry_amount_long( + market + .amm + .quote_entry_amount_long() + .safe_sub(position.quote_entry_amount.cast()?)?, + ); + market.amm.set_quote_entry_amount_short( + market + .amm + .quote_entry_amount_short() + .safe_add(new_quote_entry_amount.cast()?)?, + ); + + market.amm.set_quote_break_even_amount_long( + market + .amm + .quote_break_even_amount_long() + .safe_sub(position.quote_break_even_amount.cast()?)?, + ); + market.amm.set_quote_break_even_amount_short( + market + .amm + .quote_break_even_amount_short() + .safe_add(new_quote_break_even_amount.cast()?)?, + ); } } } @@ -334,20 +380,20 @@ pub fn update_position_and_market( match position.get_direction() { PositionDirection::Long if position.base_asset_amount != 0 => { validate!( - position.last_cumulative_funding_rate.cast::()? == market.amm.cumulative_funding_rate_long, + position.last_cumulative_funding_rate.cast::()? == market.amm.cumulative_funding_rate_long(), ErrorCode::InvalidPositionLastFundingRate, "position.last_cumulative_funding_rate {} market.amm.cumulative_funding_rate_long {}", position.last_cumulative_funding_rate.cast::()?, - market.amm.cumulative_funding_rate_long, + market.amm.cumulative_funding_rate_long(), )?; } PositionDirection::Short => { validate!( - position.last_cumulative_funding_rate == market.amm.cumulative_funding_rate_short.cast::()?, + position.last_cumulative_funding_rate == market.amm.cumulative_funding_rate_short().cast::()?, ErrorCode::InvalidPositionLastFundingRate, "position.last_cumulative_funding_rate {} market.amm.cumulative_funding_rate_short {}", position.last_cumulative_funding_rate, - market.amm.cumulative_funding_rate_short, + market.amm.cumulative_funding_rate_short(), )?; } _ => {} @@ -362,10 +408,10 @@ pub fn update_position_and_market( ) { if new_base_asset_amount > 0 { position.last_cumulative_funding_rate = - market.amm.cumulative_funding_rate_long.cast()?; + market.amm.cumulative_funding_rate_long().cast()?; } else { position.last_cumulative_funding_rate = - market.amm.cumulative_funding_rate_short.cast()?; + market.amm.cumulative_funding_rate_short().cast()?; } } @@ -424,16 +470,18 @@ pub fn update_position_with_base_asset_amount( &position_delta, )?; - market.amm.base_asset_amount_with_amm = market - .amm - .base_asset_amount_with_amm - .safe_add(position_delta.base_asset_amount.cast()?)?; + market.amm.set_base_asset_amount_with_amm( + market + .amm + .base_asset_amount_with_amm() + .safe_add(position_delta.base_asset_amount.cast()?)?, + ); validate!( - market.amm.base_asset_amount_with_amm.unsigned_abs() <= MAX_BASE_ASSET_AMOUNT_WITH_AMM, + market.amm.base_asset_amount_with_amm().unsigned_abs() <= MAX_BASE_ASSET_AMOUNT_WITH_AMM, ErrorCode::InvalidAmmDetected, "market.amm.base_asset_amount_with_amm={} cannot exceed MAX_BASE_ASSET_AMOUNT_WITH_AMM", - market.amm.base_asset_amount_with_amm + market.amm.base_asset_amount_with_amm() )?; controller::amm::update_spread_reserves(market)?; @@ -490,7 +538,9 @@ pub fn update_quote_asset_amount( position.quote_asset_amount = position.quote_asset_amount.safe_add(delta)?; - market.amm.quote_asset_amount = market.amm.quote_asset_amount.safe_add(delta.cast()?)?; + market + .amm + .set_quote_asset_amount(market.amm.quote_asset_amount().safe_add(delta.cast()?)?); if position.quote_asset_amount == 0 && position.base_asset_amount == 0 { market.number_of_users = market.number_of_users.saturating_sub(1); @@ -510,18 +560,18 @@ pub fn update_quote_break_even_amount( position.quote_break_even_amount = position.quote_break_even_amount.safe_add(delta)?; match position.get_direction() { - PositionDirection::Long => { - market.amm.quote_break_even_amount_long = market + PositionDirection::Long => market.amm.set_quote_break_even_amount_long( + market .amm - .quote_break_even_amount_long - .safe_add(delta.cast()?)? - } - PositionDirection::Short => { - market.amm.quote_break_even_amount_short = market + .quote_break_even_amount_long() + .safe_add(delta.cast()?)?, + ), + PositionDirection::Short => market.amm.set_quote_break_even_amount_short( + market .amm - .quote_break_even_amount_short - .safe_add(delta.cast()?)? - } + .quote_break_even_amount_short() + .safe_add(delta.cast()?)?, + ), } Ok(()) diff --git a/programs/drift/src/controller/position/tests.rs b/programs/drift/src/controller/position/tests.rs index 390e8502a5..684469c4b5 100644 --- a/programs/drift/src/controller/position/tests.rs +++ b/programs/drift/src/controller/position/tests.rs @@ -71,12 +71,12 @@ fn amm_pool_balance_liq_fees_example() { ); let mut spot_market = SpotMarket { - cumulative_deposit_interest: 11425141382, - cumulative_borrow_interest: 12908327537, + cumulative_deposit_interest: 11425141382.into(), + cumulative_borrow_interest: 12908327537.into(), decimals: 6, ..SpotMarket::default() }; - spot_market.deposit_balance = 10_u128.pow(19_u32); + spot_market.set_deposit_balance(10_u128.pow(19_u32)); spot_market.deposit_token_twap = 10_u64.pow(16_u32); let spot_position = SpotPosition::default(); @@ -85,11 +85,14 @@ fn amm_pool_balance_liq_fees_example() { let mut perp_market = perp_market_loader.load_mut().unwrap(); // assert_eq!(perp_market.amm.oracle, Pubkey::default()); - assert_eq!(perp_market.pnl_pool.scaled_balance, 0); - assert_eq!(perp_market.amm.fee_pool.scaled_balance, 1349764971875250); - let fee_before = perp_market.amm.fee_pool.scaled_balance; + assert_eq!(perp_market.pnl_pool.scaled_balance(), 0); + assert_eq!(perp_market.amm.fee_pool.scaled_balance(), 1349764971875250); + let fee_before = perp_market.amm.fee_pool.scaled_balance(); - assert_eq!(perp_market.amm.total_fee_minus_distributions, 1276488252050); + assert_eq!( + perp_market.amm.total_fee_minus_distributions(), + 1276488252050 + ); let new_total_fee_minus_distributions = crate::controller::amm::calculate_perp_market_amm_summary_stats( @@ -100,11 +103,19 @@ fn amm_pool_balance_liq_fees_example() { ) .unwrap(); let fee_difference = new_total_fee_minus_distributions - .safe_sub(perp_market.amm.total_fee_minus_distributions) + .safe_sub(perp_market.amm.total_fee_minus_distributions()) .unwrap(); - perp_market.amm.total_fee = perp_market.amm.total_fee.saturating_add(fee_difference); - perp_market.amm.total_mm_fee = perp_market.amm.total_mm_fee.saturating_add(fee_difference); - perp_market.amm.total_fee_minus_distributions = new_total_fee_minus_distributions; + let total_fee = perp_market.amm.total_fee(); + perp_market + .amm + .set_total_fee(total_fee.saturating_add(fee_difference)); + let total_mm_fee = perp_market.amm.total_mm_fee(); + perp_market + .amm + .set_total_mm_fee(total_mm_fee.saturating_add(fee_difference)); + perp_market + .amm + .set_total_fee_minus_distributions(new_total_fee_minus_distributions); assert_eq!(new_total_fee_minus_distributions, 640881949608); @@ -138,7 +149,7 @@ fn amm_pool_balance_liq_fees_example() { assert_eq!(fee_pool_token_amount, 1276764026200); // 1.27M // assert_eq!(perp_market.amm.fee_pool.scaled_balance, fee_before + 1000000000); // pre change - assert!(perp_market.amm.fee_pool.scaled_balance < fee_before); // post change + assert!(perp_market.amm.fee_pool.scaled_balance() < fee_before); // post change } } @@ -204,7 +215,7 @@ fn amm_pred_expiry_price_yes_market_example() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -223,7 +234,7 @@ fn amm_pred_expiry_price_yes_market_example() { { let mut perp_market = perp_market_loader.load_mut().unwrap(); perp_market.amm.historical_oracle_data.last_oracle_price = 1_000_000; - perp_market.amm.base_asset_amount_with_amm = 0; + perp_market.amm.set_base_asset_amount_with_amm(0); market_index = perp_market.market_index; assert_eq!(perp_market.expiry_ts, 1725559200); @@ -317,7 +328,7 @@ fn amm_pred_expiry_price_market_example() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -336,7 +347,7 @@ fn amm_pred_expiry_price_market_example() { { let mut perp_market = perp_market_loader.load_mut().unwrap(); market_index = perp_market.market_index; - perp_market.amm.base_asset_amount_with_amm = 0; + perp_market.amm.set_base_asset_amount_with_amm(0); perp_market.amm.historical_oracle_data.last_oracle_price = 1; assert_eq!(perp_market.expiry_ts, 1725559200); @@ -429,7 +440,7 @@ fn amm_pred_settle_market_example() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, @@ -511,15 +522,18 @@ fn amm_pred_market_example() { assert_eq!(max_bids, 3_824_624_394_874); // 3824 shares assert_eq!(max_asks, -5_241_195_799_744); // -5000 shares - assert_eq!(perp_market.amm.sqrt_k, 56_649_660_613_272); + assert_eq!(perp_market.amm.sqrt_k(), 56_649_660_613_272); let (optimal_peg, fee_budget, _check_lower_bound) = repeg::calculate_optimal_peg_and_budget(&perp_market, &mm_oracle_price_data).unwrap(); - assert_eq!(perp_market.amm.terminal_quote_asset_reserve, 56405211622548); - assert_eq!(perp_market.amm.quote_asset_reserve, 56933567973708); assert_eq!( - perp_market.amm.quote_asset_reserve - perp_market.amm.terminal_quote_asset_reserve, + perp_market.amm.terminal_quote_asset_reserve(), + 56405211622548 + ); + assert_eq!(perp_market.amm.quote_asset_reserve(), 56933567973708); + assert_eq!( + perp_market.amm.quote_asset_reserve() - perp_market.amm.terminal_quote_asset_reserve(), 528356351160 ); @@ -589,10 +603,10 @@ fn amm_ref_price_decay_tail_test() { assert_eq!(max_ref_offset, 10000); let liquidity_ratio = crate::math::amm_spread::calculate_inventory_liquidity_ratio( - perp_market.amm.base_asset_amount_with_amm, - perp_market.amm.base_asset_reserve, - perp_market.amm.max_base_asset_reserve, - perp_market.amm.min_base_asset_reserve, + perp_market.amm.base_asset_amount_with_amm(), + perp_market.amm.base_asset_reserve(), + perp_market.amm.max_base_asset_reserve(), + perp_market.amm.min_base_asset_reserve(), ) .unwrap(); @@ -766,10 +780,10 @@ fn amm_ref_price_offset_decay_logic() { let max_ref_offset = perp_market.amm.get_max_reference_price_offset().unwrap(); let liquidity_ratio = crate::math::amm_spread::calculate_inventory_liquidity_ratio( - perp_market.amm.base_asset_amount_with_amm, - perp_market.amm.base_asset_reserve, - perp_market.amm.max_base_asset_reserve, - perp_market.amm.min_base_asset_reserve, + perp_market.amm.base_asset_amount_with_amm(), + perp_market.amm.base_asset_reserve(), + perp_market.amm.max_base_asset_reserve(), + perp_market.amm.min_base_asset_reserve(), ) .unwrap(); @@ -941,10 +955,10 @@ fn amm_negative_ref_price_offset_decay_logic() { let max_ref_offset = perp_market.amm.get_max_reference_price_offset().unwrap(); let liquidity_ratio = crate::math::amm_spread::calculate_inventory_liquidity_ratio( - perp_market.amm.base_asset_amount_with_amm, - perp_market.amm.base_asset_reserve, - perp_market.amm.max_base_asset_reserve, - perp_market.amm.min_base_asset_reserve, + perp_market.amm.base_asset_amount_with_amm(), + perp_market.amm.base_asset_reserve(), + perp_market.amm.max_base_asset_reserve(), + perp_market.amm.min_base_asset_reserve(), ) .unwrap(); @@ -1101,7 +1115,7 @@ fn amm_perp_ref_offset() { AccountLoader::try_from(&perp_market_account_info).unwrap(); let mut perp_market = perp_market_loader.load_mut().unwrap(); - perp_market.amm.base_asset_amount_with_amm = 40000000000; // override old LP related fields + perp_market.amm.set_base_asset_amount_with_amm(40000000000); // override old LP related fields let reserve_price = perp_market.amm.reserve_price().unwrap(); let (b1, a1) = perp_market.amm.bid_ask_price(reserve_price).unwrap(); @@ -1121,16 +1135,16 @@ fn amm_perp_ref_offset() { .last_oracle_price_twap_ts, 1741207620 ); - assert_eq!(perp_market.amm.bid_base_asset_reserve, 4674304094737516); - assert_eq!(perp_market.amm.ask_base_asset_reserve, 4631420570932586); + assert_eq!(perp_market.amm.bid_base_asset_reserve(), 4674304094737516); + assert_eq!(perp_market.amm.ask_base_asset_reserve(), 4631420570932586); let max_ref_offset = perp_market.amm.get_max_reference_price_offset().unwrap(); let liquidity_ratio = crate::math::amm_spread::calculate_inventory_liquidity_ratio( - perp_market.amm.base_asset_amount_with_amm, - perp_market.amm.base_asset_reserve, - perp_market.amm.max_base_asset_reserve, - perp_market.amm.min_base_asset_reserve, + perp_market.amm.base_asset_amount_with_amm(), + perp_market.amm.base_asset_reserve(), + perp_market.amm.max_base_asset_reserve(), + perp_market.amm.min_base_asset_reserve(), ) .unwrap(); @@ -1205,8 +1219,8 @@ fn amm_perp_ref_offset() { assert_eq!(perp_market.amm.max_spread, 90000); assert_eq!(r, 7101599); - assert_eq!(perp_market.amm.bid_base_asset_reserve, 4675159724262455); - assert_eq!(perp_market.amm.ask_base_asset_reserve, 4672813088646692); + assert_eq!(perp_market.amm.bid_base_asset_reserve(), 4675159724262455); + assert_eq!(perp_market.amm.ask_base_asset_reserve(), 4672813088646692); crate::validation::perp_market::validate_perp_market(&perp_market).unwrap(); @@ -1269,8 +1283,8 @@ fn test_position_entry_sim() { }; let mut market = PerpMarket { amm: AMM { - cumulative_funding_rate_long: 1, - sqrt_k: 1, + cumulative_funding_rate_long: 1.into(), + sqrt_k: 1.into(), order_step_size: (BASE_PRECISION_I64 / 10) as u64, ..AMM::default() }, @@ -1325,8 +1339,8 @@ fn increase_long_from_no_position() { }; let mut market = PerpMarket { amm: AMM { - cumulative_funding_rate_long: 1, - sqrt_k: 1, + cumulative_funding_rate_long: 1.into(), + sqrt_k: 1.into(), order_step_size: 1, ..AMM::default() }, @@ -1345,14 +1359,14 @@ fn increase_long_from_no_position() { assert_eq!(existing_position.last_cumulative_funding_rate, 1); assert_eq!(market.number_of_users_with_base, 1); - assert_eq!(market.amm.base_asset_amount_long, 1); - assert_eq!(market.amm.base_asset_amount_short, 0); - assert_eq!(market.amm.base_asset_amount_with_amm, 0); - assert_eq!(market.amm.quote_asset_amount, -1); - assert_eq!(market.amm.quote_entry_amount_long, -1); - assert_eq!(market.amm.quote_entry_amount_short, 0); - assert_eq!(market.amm.quote_break_even_amount_long, -1); - assert_eq!(market.amm.quote_break_even_amount_short, 0); + assert_eq!(market.amm.base_asset_amount_long(), 1); + assert_eq!(market.amm.base_asset_amount_short(), 0); + assert_eq!(market.amm.base_asset_amount_with_amm(), 0); + assert_eq!(market.amm.quote_asset_amount(), -1); + assert_eq!(market.amm.quote_entry_amount_long(), -1); + assert_eq!(market.amm.quote_entry_amount_short(), 0); + assert_eq!(market.amm.quote_break_even_amount_long(), -1); + assert_eq!(market.amm.quote_break_even_amount_short(), 0); } #[test] @@ -1364,7 +1378,7 @@ fn increase_short_from_no_position() { }; let mut market = PerpMarket { amm: AMM { - cumulative_funding_rate_short: 1, + cumulative_funding_rate_short: 1.into(), ..AMM::default_test() }, number_of_users_with_base: 0, @@ -1382,13 +1396,13 @@ fn increase_short_from_no_position() { assert_eq!(existing_position.last_cumulative_funding_rate, 1); assert_eq!(market.number_of_users_with_base, 1); - assert_eq!(market.amm.base_asset_amount_long, 0); - assert_eq!(market.amm.base_asset_amount_short, -1); - assert_eq!(market.amm.quote_asset_amount, 1); - assert_eq!(market.amm.quote_entry_amount_long, 0); - assert_eq!(market.amm.quote_entry_amount_short, 1); - assert_eq!(market.amm.quote_break_even_amount_long, 0); - assert_eq!(market.amm.quote_break_even_amount_short, 1); + assert_eq!(market.amm.base_asset_amount_long(), 0); + assert_eq!(market.amm.base_asset_amount_short(), -1); + assert_eq!(market.amm.quote_asset_amount(), 1); + assert_eq!(market.amm.quote_entry_amount_long(), 0); + assert_eq!(market.amm.quote_entry_amount_short(), 1); + assert_eq!(market.amm.quote_break_even_amount_long(), 0); + assert_eq!(market.amm.quote_break_even_amount_short(), 1); } #[test] @@ -1407,13 +1421,13 @@ fn increase_long() { }; let mut market = PerpMarket { amm: AMM { - base_asset_amount_with_amm: 1, - base_asset_amount_long: 1, - base_asset_amount_short: 0, - quote_asset_amount: -1, - quote_break_even_amount_long: -2, - quote_entry_amount_long: -1, - cumulative_funding_rate_long: 1, + base_asset_amount_with_amm: 1.into(), + base_asset_amount_long: 1.into(), + base_asset_amount_short: 0.into(), + quote_asset_amount: (-1).into(), + quote_break_even_amount_long: (-2).into(), + quote_entry_amount_long: (-1).into(), + cumulative_funding_rate_long: 1.into(), ..AMM::default_test() }, number_of_users_with_base: 1, @@ -1431,15 +1445,15 @@ fn increase_long() { assert_eq!(existing_position.last_cumulative_funding_rate, 1); assert_eq!(market.number_of_users_with_base, 1); - assert_eq!(market.amm.base_asset_amount_long, 2); - assert_eq!(market.amm.base_asset_amount_short, 0); - assert_eq!(market.amm.quote_asset_amount, -2); - assert_eq!(market.amm.quote_entry_amount_long, -2); - assert_eq!(market.amm.quote_entry_amount_short, 0); - assert_eq!(market.amm.quote_break_even_amount_long, -3); - assert_eq!(market.amm.quote_break_even_amount_short, 0); - - assert_eq!(market.amm.base_asset_amount_with_amm, 1); // todo: update_position_and_market doesnt modify this properly? + assert_eq!(market.amm.base_asset_amount_long(), 2); + assert_eq!(market.amm.base_asset_amount_short(), 0); + assert_eq!(market.amm.quote_asset_amount(), -2); + assert_eq!(market.amm.quote_entry_amount_long(), -2); + assert_eq!(market.amm.quote_entry_amount_short(), 0); + assert_eq!(market.amm.quote_break_even_amount_long(), -3); + assert_eq!(market.amm.quote_break_even_amount_short(), 0); + + assert_eq!(market.amm.base_asset_amount_with_amm(), 1); // todo: update_position_and_market doesnt modify this properly? } #[test] @@ -1458,12 +1472,12 @@ fn increase_short() { }; let mut market = PerpMarket { amm: AMM { - base_asset_amount_short: -1, - base_asset_amount_long: 0, - quote_asset_amount: 1, - quote_entry_amount_short: 1, - quote_break_even_amount_short: 2, - cumulative_funding_rate_short: 1, + base_asset_amount_short: (-1).into(), + base_asset_amount_long: 0.into(), + quote_asset_amount: 1.into(), + quote_entry_amount_short: 1.into(), + quote_break_even_amount_short: 2.into(), + cumulative_funding_rate_short: 1.into(), ..AMM::default_test() }, number_of_users_with_base: 1, @@ -1481,13 +1495,13 @@ fn increase_short() { assert_eq!(existing_position.last_cumulative_funding_rate, 1); assert_eq!(market.number_of_users_with_base, 1); - assert_eq!(market.amm.base_asset_amount_long, 0); - assert_eq!(market.amm.base_asset_amount_short, -2); - assert_eq!(market.amm.quote_asset_amount, 2); - assert_eq!(market.amm.quote_entry_amount_long, 0); - assert_eq!(market.amm.quote_entry_amount_short, 2); - assert_eq!(market.amm.quote_break_even_amount_long, 0); - assert_eq!(market.amm.quote_break_even_amount_short, 3); + assert_eq!(market.amm.base_asset_amount_long(), 0); + assert_eq!(market.amm.base_asset_amount_short(), -2); + assert_eq!(market.amm.quote_asset_amount(), 2); + assert_eq!(market.amm.quote_entry_amount_long(), 0); + assert_eq!(market.amm.quote_entry_amount_short(), 2); + assert_eq!(market.amm.quote_break_even_amount_long(), 0); + assert_eq!(market.amm.quote_break_even_amount_short(), 3); } #[test] @@ -1506,13 +1520,13 @@ fn reduce_long_profitable() { }; let mut market = PerpMarket { amm: AMM { - base_asset_amount_with_amm: 10, - base_asset_amount_long: 10, - base_asset_amount_short: 0, - quote_asset_amount: -10, - quote_entry_amount_long: -10, - quote_break_even_amount_long: -12, - cumulative_funding_rate_long: 1, + base_asset_amount_with_amm: 10.into(), + base_asset_amount_long: 10.into(), + base_asset_amount_short: 0.into(), + quote_asset_amount: (-10).into(), + quote_entry_amount_long: (-10).into(), + quote_break_even_amount_long: (-12).into(), + cumulative_funding_rate_long: 1.into(), ..AMM::default_test() }, number_of_users_with_base: 1, @@ -1530,14 +1544,14 @@ fn reduce_long_profitable() { assert_eq!(existing_position.last_cumulative_funding_rate, 1); assert_eq!(market.number_of_users_with_base, 1); - assert_eq!(market.amm.base_asset_amount_long, 9); - assert_eq!(market.amm.base_asset_amount_short, 0); + assert_eq!(market.amm.base_asset_amount_long(), 9); + assert_eq!(market.amm.base_asset_amount_short(), 0); // assert_eq!(market.amm.base_asset_amount_with_amm, 9); - assert_eq!(market.amm.quote_asset_amount, -5); - assert_eq!(market.amm.quote_entry_amount_long, -9); - assert_eq!(market.amm.quote_entry_amount_short, 0); - assert_eq!(market.amm.quote_break_even_amount_long, -11); - assert_eq!(market.amm.quote_break_even_amount_short, 0); + assert_eq!(market.amm.quote_asset_amount(), -5); + assert_eq!(market.amm.quote_entry_amount_long(), -9); + assert_eq!(market.amm.quote_entry_amount_short(), 0); + assert_eq!(market.amm.quote_break_even_amount_long(), -11); + assert_eq!(market.amm.quote_break_even_amount_short(), 0); } #[test] @@ -1556,13 +1570,13 @@ fn reduce_long_unprofitable() { }; let mut market = PerpMarket { amm: AMM { - base_asset_amount_with_amm: 10, - base_asset_amount_long: 10, - base_asset_amount_short: 0, - quote_asset_amount: -100, - quote_entry_amount_long: -100, - quote_break_even_amount_long: -200, - cumulative_funding_rate_long: 1, + base_asset_amount_with_amm: 10.into(), + base_asset_amount_long: 10.into(), + base_asset_amount_short: 0.into(), + quote_asset_amount: (-100).into(), + quote_entry_amount_long: (-100).into(), + quote_break_even_amount_long: (-200).into(), + cumulative_funding_rate_long: 1.into(), ..AMM::default_test() }, number_of_users_with_base: 1, @@ -1580,14 +1594,14 @@ fn reduce_long_unprofitable() { assert_eq!(existing_position.last_cumulative_funding_rate, 1); assert_eq!(market.number_of_users_with_base, 1); - assert_eq!(market.amm.base_asset_amount_long, 9); - assert_eq!(market.amm.base_asset_amount_short, 0); + assert_eq!(market.amm.base_asset_amount_long(), 9); + assert_eq!(market.amm.base_asset_amount_short(), 0); // assert_eq!(market.amm.base_asset_amount_with_amm, 9); - assert_eq!(market.amm.quote_asset_amount, -95); - assert_eq!(market.amm.quote_entry_amount_long, -90); - assert_eq!(market.amm.quote_entry_amount_short, 0); - assert_eq!(market.amm.quote_break_even_amount_long, -180); - assert_eq!(market.amm.quote_break_even_amount_short, 0); + assert_eq!(market.amm.quote_asset_amount(), -95); + assert_eq!(market.amm.quote_entry_amount_long(), -90); + assert_eq!(market.amm.quote_entry_amount_short(), 0); + assert_eq!(market.amm.quote_break_even_amount_long(), -180); + assert_eq!(market.amm.quote_break_even_amount_short(), 0); } #[test] @@ -1606,14 +1620,14 @@ fn flip_long_to_short_profitable() { }; let mut market = PerpMarket { amm: AMM { - base_asset_amount_with_amm: 10, - base_asset_amount_long: 10, - base_asset_amount_short: 0, - quote_asset_amount: -10, - quote_break_even_amount_long: -12, - quote_entry_amount_long: -10, - cumulative_funding_rate_short: 2, - cumulative_funding_rate_long: 1, + base_asset_amount_with_amm: 10.into(), + base_asset_amount_long: 10.into(), + base_asset_amount_short: 0.into(), + quote_asset_amount: (-10).into(), + quote_break_even_amount_long: (-12).into(), + quote_entry_amount_long: (-10).into(), + cumulative_funding_rate_short: 2.into(), + cumulative_funding_rate_long: 1.into(), ..AMM::default_test() }, number_of_users_with_base: 1, @@ -1631,14 +1645,14 @@ fn flip_long_to_short_profitable() { assert_eq!(existing_position.last_cumulative_funding_rate, 2); assert_eq!(market.number_of_users_with_base, 1); - assert_eq!(market.amm.base_asset_amount_long, 0); - assert_eq!(market.amm.base_asset_amount_short, -1); + assert_eq!(market.amm.base_asset_amount_long(), 0); + assert_eq!(market.amm.base_asset_amount_short(), -1); // assert_eq!(market.amm.base_asset_amount_with_amm, -1); - assert_eq!(market.amm.quote_asset_amount, 12); - assert_eq!(market.amm.quote_break_even_amount_long, 0); - assert_eq!(market.amm.quote_break_even_amount_short, 2); - assert_eq!(market.amm.quote_entry_amount_long, 0); - assert_eq!(market.amm.quote_entry_amount_short, 2); + assert_eq!(market.amm.quote_asset_amount(), 12); + assert_eq!(market.amm.quote_break_even_amount_long(), 0); + assert_eq!(market.amm.quote_break_even_amount_short(), 2); + assert_eq!(market.amm.quote_entry_amount_long(), 0); + assert_eq!(market.amm.quote_entry_amount_short(), 2); } #[test] @@ -1657,14 +1671,14 @@ fn flip_long_to_short_unprofitable() { }; let mut market = PerpMarket { amm: AMM { - base_asset_amount_with_amm: 10, - base_asset_amount_long: 10, - base_asset_amount_short: 0, - quote_asset_amount: -10, - quote_break_even_amount_long: -12, - quote_entry_amount_long: -10, - cumulative_funding_rate_short: 2, - cumulative_funding_rate_long: 1, + base_asset_amount_with_amm: 10.into(), + base_asset_amount_long: 10.into(), + base_asset_amount_short: 0.into(), + quote_asset_amount: (-10).into(), + quote_break_even_amount_long: (-12).into(), + quote_entry_amount_long: (-10).into(), + cumulative_funding_rate_short: 2.into(), + cumulative_funding_rate_long: 1.into(), order_step_size: 1, ..AMM::default() }, @@ -1683,14 +1697,14 @@ fn flip_long_to_short_unprofitable() { assert_eq!(existing_position.last_cumulative_funding_rate, 2); assert_eq!(market.number_of_users_with_base, 1); - assert_eq!(market.amm.base_asset_amount_long, 0); - assert_eq!(market.amm.base_asset_amount_short, -1); + assert_eq!(market.amm.base_asset_amount_long(), 0); + assert_eq!(market.amm.base_asset_amount_short(), -1); // assert_eq!(market.amm.base_asset_amount_with_amm, -1); - assert_eq!(market.amm.quote_asset_amount, 0); - assert_eq!(market.amm.quote_break_even_amount_long, 0); - assert_eq!(market.amm.quote_break_even_amount_short, 1); - assert_eq!(market.amm.quote_entry_amount_long, 0); - assert_eq!(market.amm.quote_entry_amount_short, 1); + assert_eq!(market.amm.quote_asset_amount(), 0); + assert_eq!(market.amm.quote_break_even_amount_long(), 0); + assert_eq!(market.amm.quote_break_even_amount_short(), 1); + assert_eq!(market.amm.quote_entry_amount_long(), 0); + assert_eq!(market.amm.quote_entry_amount_short(), 1); } #[test] @@ -1709,12 +1723,12 @@ fn reduce_short_profitable() { }; let mut market = PerpMarket { amm: AMM { - base_asset_amount_long: 0, - base_asset_amount_short: -10, - quote_asset_amount: 100, - quote_entry_amount_short: 100, - quote_break_even_amount_short: 200, - cumulative_funding_rate_short: 1, + base_asset_amount_long: 0.into(), + base_asset_amount_short: (-10).into(), + quote_asset_amount: 100.into(), + quote_entry_amount_short: 100.into(), + quote_break_even_amount_short: 200.into(), + cumulative_funding_rate_short: 1.into(), ..AMM::default_test() }, number_of_users_with_base: 1, @@ -1732,13 +1746,13 @@ fn reduce_short_profitable() { assert_eq!(existing_position.last_cumulative_funding_rate, 1); assert_eq!(market.number_of_users_with_base, 1); - assert_eq!(market.amm.base_asset_amount_long, 0); - assert_eq!(market.amm.base_asset_amount_short, -9); - assert_eq!(market.amm.quote_asset_amount, 95); - assert_eq!(market.amm.quote_entry_amount_long, 0); - assert_eq!(market.amm.quote_entry_amount_short, 90); - assert_eq!(market.amm.quote_break_even_amount_long, 0); - assert_eq!(market.amm.quote_break_even_amount_short, 180); + assert_eq!(market.amm.base_asset_amount_long(), 0); + assert_eq!(market.amm.base_asset_amount_short(), -9); + assert_eq!(market.amm.quote_asset_amount(), 95); + assert_eq!(market.amm.quote_entry_amount_long(), 0); + assert_eq!(market.amm.quote_entry_amount_short(), 90); + assert_eq!(market.amm.quote_break_even_amount_long(), 0); + assert_eq!(market.amm.quote_break_even_amount_short(), 180); } #[test] @@ -1757,12 +1771,12 @@ fn decrease_short_unprofitable() { }; let mut market = PerpMarket { amm: AMM { - base_asset_amount_long: 0, - base_asset_amount_short: -10, - quote_asset_amount: 100, - quote_entry_amount_short: 100, - quote_break_even_amount_short: 200, - cumulative_funding_rate_short: 1, + base_asset_amount_long: 0.into(), + base_asset_amount_short: (-10).into(), + quote_asset_amount: 100.into(), + quote_entry_amount_short: 100.into(), + quote_break_even_amount_short: 200.into(), + cumulative_funding_rate_short: 1.into(), ..AMM::default_test() }, number_of_users_with_base: 1, @@ -1780,13 +1794,13 @@ fn decrease_short_unprofitable() { assert_eq!(existing_position.last_cumulative_funding_rate, 1); assert_eq!(market.number_of_users_with_base, 1); - assert_eq!(market.amm.base_asset_amount_long, 0); - assert_eq!(market.amm.base_asset_amount_short, -9); - assert_eq!(market.amm.quote_asset_amount, 85); - assert_eq!(market.amm.quote_entry_amount_long, 0); - assert_eq!(market.amm.quote_entry_amount_short, 90); - assert_eq!(market.amm.quote_break_even_amount_long, 0); - assert_eq!(market.amm.quote_break_even_amount_short, 180); + assert_eq!(market.amm.base_asset_amount_long(), 0); + assert_eq!(market.amm.base_asset_amount_short(), -9); + assert_eq!(market.amm.quote_asset_amount(), 85); + assert_eq!(market.amm.quote_entry_amount_long(), 0); + assert_eq!(market.amm.quote_entry_amount_short(), 90); + assert_eq!(market.amm.quote_break_even_amount_long(), 0); + assert_eq!(market.amm.quote_break_even_amount_short(), 180); } #[test] @@ -1805,14 +1819,14 @@ fn flip_short_to_long_profitable() { }; let mut market = PerpMarket { amm: AMM { - base_asset_amount_with_amm: -10, - base_asset_amount_long: 0, - base_asset_amount_short: -10, - quote_asset_amount: 100, - quote_entry_amount_short: 100, - quote_break_even_amount_short: 200, - cumulative_funding_rate_long: 2, - cumulative_funding_rate_short: 1, + base_asset_amount_with_amm: (-10).into(), + base_asset_amount_long: 0.into(), + base_asset_amount_short: (-10).into(), + quote_asset_amount: 100.into(), + quote_entry_amount_short: 100.into(), + quote_break_even_amount_short: 200.into(), + cumulative_funding_rate_long: 2.into(), + cumulative_funding_rate_short: 1.into(), ..AMM::default_test() }, number_of_users_with_base: 1, @@ -1830,14 +1844,14 @@ fn flip_short_to_long_profitable() { assert_eq!(existing_position.last_cumulative_funding_rate, 2); assert_eq!(market.number_of_users_with_base, 1); - assert_eq!(market.amm.base_asset_amount_long, 1); - assert_eq!(market.amm.base_asset_amount_short, 0); + assert_eq!(market.amm.base_asset_amount_long(), 1); + assert_eq!(market.amm.base_asset_amount_short(), 0); // assert_eq!(market.amm.base_asset_amount_with_amm, 1); - assert_eq!(market.amm.quote_asset_amount, 40); - assert_eq!(market.amm.quote_entry_amount_long, -6); - assert_eq!(market.amm.quote_entry_amount_short, 0); - assert_eq!(market.amm.quote_break_even_amount_long, -6); - assert_eq!(market.amm.quote_break_even_amount_short, 0); + assert_eq!(market.amm.quote_asset_amount(), 40); + assert_eq!(market.amm.quote_entry_amount_long(), -6); + assert_eq!(market.amm.quote_entry_amount_short(), 0); + assert_eq!(market.amm.quote_break_even_amount_long(), -6); + assert_eq!(market.amm.quote_break_even_amount_short(), 0); } #[test] @@ -1856,14 +1870,14 @@ fn flip_short_to_long_unprofitable() { }; let mut market = PerpMarket { amm: AMM { - base_asset_amount_with_amm: -10, - base_asset_amount_long: 0, - base_asset_amount_short: -10, - quote_asset_amount: 100, - quote_entry_amount_short: 100, - quote_break_even_amount_short: 200, - cumulative_funding_rate_long: 2, - cumulative_funding_rate_short: 1, + base_asset_amount_with_amm: (-10).into(), + base_asset_amount_long: 0.into(), + base_asset_amount_short: (-10).into(), + quote_asset_amount: 100.into(), + quote_entry_amount_short: 100.into(), + quote_break_even_amount_short: 200.into(), + cumulative_funding_rate_long: 2.into(), + cumulative_funding_rate_short: 1.into(), ..AMM::default_test() }, number_of_users_with_base: 1, @@ -1881,14 +1895,14 @@ fn flip_short_to_long_unprofitable() { assert_eq!(existing_position.last_cumulative_funding_rate, 2); assert_eq!(market.number_of_users_with_base, 1); - assert_eq!(market.amm.base_asset_amount_long, 1); - assert_eq!(market.amm.base_asset_amount_short, 0); + assert_eq!(market.amm.base_asset_amount_long(), 1); + assert_eq!(market.amm.base_asset_amount_short(), 0); // assert_eq!(market.amm.base_asset_amount_with_amm, 1); - assert_eq!(market.amm.quote_asset_amount, -20); - assert_eq!(market.amm.quote_entry_amount_long, -11); - assert_eq!(market.amm.quote_entry_amount_short, 0); - assert_eq!(market.amm.quote_break_even_amount_long, -11); - assert_eq!(market.amm.quote_break_even_amount_short, 0); + assert_eq!(market.amm.quote_asset_amount(), -20); + assert_eq!(market.amm.quote_entry_amount_long(), -11); + assert_eq!(market.amm.quote_entry_amount_short(), 0); + assert_eq!(market.amm.quote_break_even_amount_long(), -11); + assert_eq!(market.amm.quote_break_even_amount_short(), 0); } #[test] @@ -1907,12 +1921,12 @@ fn close_long_profitable() { }; let mut market = PerpMarket { amm: AMM { - base_asset_amount_with_amm: 11, - base_asset_amount_long: 11, - quote_asset_amount: -11, - quote_entry_amount_long: -11, - quote_break_even_amount_long: -13, - cumulative_funding_rate_long: 1, + base_asset_amount_with_amm: 11.into(), + base_asset_amount_long: 11.into(), + quote_asset_amount: (-11).into(), + quote_entry_amount_long: (-11).into(), + quote_break_even_amount_long: (-13).into(), + cumulative_funding_rate_long: 1.into(), ..AMM::default_test() }, number_of_users_with_base: 2, @@ -1930,15 +1944,15 @@ fn close_long_profitable() { assert_eq!(existing_position.last_cumulative_funding_rate, 0); assert_eq!(market.number_of_users_with_base, 1); - assert_eq!(market.amm.base_asset_amount_long, 1); - assert_eq!(market.amm.base_asset_amount_short, 0); + assert_eq!(market.amm.base_asset_amount_long(), 1); + assert_eq!(market.amm.base_asset_amount_short(), 0); // assert_eq!(market.amm.base_asset_amount_with_amm, 1); // not 5 because quote asset amount long was -11 not -10 before - assert_eq!(market.amm.quote_asset_amount, 4); - assert_eq!(market.amm.quote_entry_amount_long, -1); - assert_eq!(market.amm.quote_entry_amount_short, 0); - assert_eq!(market.amm.quote_break_even_amount_long, -1); - assert_eq!(market.amm.quote_break_even_amount_short, 0); + assert_eq!(market.amm.quote_asset_amount(), 4); + assert_eq!(market.amm.quote_entry_amount_long(), -1); + assert_eq!(market.amm.quote_entry_amount_short(), 0); + assert_eq!(market.amm.quote_break_even_amount_long(), -1); + assert_eq!(market.amm.quote_break_even_amount_short(), 0); } #[test] @@ -1957,12 +1971,12 @@ fn close_long_unprofitable() { }; let mut market = PerpMarket { amm: AMM { - base_asset_amount_with_amm: 11, - base_asset_amount_long: 11, - quote_asset_amount: -11, - quote_entry_amount_long: -11, - quote_break_even_amount_long: -13, - cumulative_funding_rate_long: 1, + base_asset_amount_with_amm: 11.into(), + base_asset_amount_long: 11.into(), + quote_asset_amount: (-11).into(), + quote_entry_amount_long: (-11).into(), + quote_break_even_amount_long: (-13).into(), + cumulative_funding_rate_long: 1.into(), ..AMM::default_test() }, number_of_users_with_base: 2, @@ -1980,14 +1994,14 @@ fn close_long_unprofitable() { assert_eq!(existing_position.last_cumulative_funding_rate, 0); assert_eq!(market.number_of_users_with_base, 1); - assert_eq!(market.amm.base_asset_amount_long, 1); - assert_eq!(market.amm.base_asset_amount_short, 0); + assert_eq!(market.amm.base_asset_amount_long(), 1); + assert_eq!(market.amm.base_asset_amount_short(), 0); // assert_eq!(market.amm.base_asset_amount_with_amm, 1); - assert_eq!(market.amm.quote_asset_amount, -6); - assert_eq!(market.amm.quote_entry_amount_long, -1); - assert_eq!(market.amm.quote_entry_amount_short, 0); - assert_eq!(market.amm.quote_break_even_amount_long, -1); - assert_eq!(market.amm.quote_break_even_amount_short, 0); + assert_eq!(market.amm.quote_asset_amount(), -6); + assert_eq!(market.amm.quote_entry_amount_long(), -1); + assert_eq!(market.amm.quote_entry_amount_short(), 0); + assert_eq!(market.amm.quote_break_even_amount_long(), -1); + assert_eq!(market.amm.quote_break_even_amount_short(), 0); } #[test] @@ -2006,11 +2020,11 @@ fn close_short_profitable() { }; let mut market = PerpMarket { amm: AMM { - base_asset_amount_short: -11, - quote_asset_amount: 11, - quote_entry_amount_short: 11, - quote_break_even_amount_short: 13, - cumulative_funding_rate_short: 1, + base_asset_amount_short: (-11).into(), + quote_asset_amount: 11.into(), + quote_entry_amount_short: 11.into(), + quote_break_even_amount_short: 13.into(), + cumulative_funding_rate_short: 1.into(), ..AMM::default_test() }, number_of_users_with_base: 2, @@ -2028,13 +2042,13 @@ fn close_short_profitable() { assert_eq!(existing_position.last_cumulative_funding_rate, 0); assert_eq!(market.number_of_users_with_base, 1); - assert_eq!(market.amm.base_asset_amount_long, 0); - assert_eq!(market.amm.base_asset_amount_short, -1); - assert_eq!(market.amm.quote_asset_amount, 6); - assert_eq!(market.amm.quote_entry_amount_long, 0); - assert_eq!(market.amm.quote_entry_amount_short, 1); - assert_eq!(market.amm.quote_break_even_amount_long, 0); - assert_eq!(market.amm.quote_break_even_amount_short, 1); + assert_eq!(market.amm.base_asset_amount_long(), 0); + assert_eq!(market.amm.base_asset_amount_short(), -1); + assert_eq!(market.amm.quote_asset_amount(), 6); + assert_eq!(market.amm.quote_entry_amount_long(), 0); + assert_eq!(market.amm.quote_entry_amount_short(), 1); + assert_eq!(market.amm.quote_break_even_amount_long(), 0); + assert_eq!(market.amm.quote_break_even_amount_short(), 1); } #[test] @@ -2053,11 +2067,11 @@ fn close_short_unprofitable() { }; let mut market = PerpMarket { amm: AMM { - base_asset_amount_short: -11, - quote_asset_amount: 11, - quote_entry_amount_short: 11, - quote_break_even_amount_short: 13, - cumulative_funding_rate_short: 1, + base_asset_amount_short: (-11).into(), + quote_asset_amount: 11.into(), + quote_entry_amount_short: 11.into(), + quote_break_even_amount_short: 13.into(), + cumulative_funding_rate_short: 1.into(), ..AMM::default_test() }, number_of_users_with_base: 2, @@ -2075,13 +2089,13 @@ fn close_short_unprofitable() { assert_eq!(existing_position.last_cumulative_funding_rate, 0); assert_eq!(market.number_of_users_with_base, 1); - assert_eq!(market.amm.base_asset_amount_long, 0); - assert_eq!(market.amm.base_asset_amount_short, -1); - assert_eq!(market.amm.quote_asset_amount, -4); - assert_eq!(market.amm.quote_entry_amount_long, 0); - assert_eq!(market.amm.quote_entry_amount_short, 1); - assert_eq!(market.amm.quote_break_even_amount_long, 0); - assert_eq!(market.amm.quote_break_even_amount_short, 1); + assert_eq!(market.amm.base_asset_amount_long(), 0); + assert_eq!(market.amm.base_asset_amount_short(), -1); + assert_eq!(market.amm.quote_asset_amount(), -4); + assert_eq!(market.amm.quote_entry_amount_long(), 0); + assert_eq!(market.amm.quote_entry_amount_short(), 1); + assert_eq!(market.amm.quote_break_even_amount_long(), 0); + assert_eq!(market.amm.quote_break_even_amount_short(), 1); } #[test] @@ -2100,12 +2114,12 @@ fn close_long_with_quote_break_even_amount_less_than_quote_asset_amount() { }; let mut market = PerpMarket { amm: AMM { - base_asset_amount_with_amm: 11, - base_asset_amount_long: 11, - quote_asset_amount: -11, - quote_entry_amount_long: -8, - quote_break_even_amount_long: -9, - cumulative_funding_rate_long: 1, + base_asset_amount_with_amm: 11.into(), + base_asset_amount_long: 11.into(), + quote_asset_amount: (-11).into(), + quote_entry_amount_long: (-8).into(), + quote_break_even_amount_long: (-9).into(), + cumulative_funding_rate_long: 1.into(), order_step_size: 1, ..AMM::default() }, @@ -2124,14 +2138,14 @@ fn close_long_with_quote_break_even_amount_less_than_quote_asset_amount() { assert_eq!(existing_position.last_cumulative_funding_rate, 0); assert_eq!(market.number_of_users_with_base, 1); - assert_eq!(market.amm.base_asset_amount_long, 1); - assert_eq!(market.amm.base_asset_amount_short, 0); + assert_eq!(market.amm.base_asset_amount_long(), 1); + assert_eq!(market.amm.base_asset_amount_short(), 0); // assert_eq!(market.amm.base_asset_amount_with_amm, 1); - assert_eq!(market.amm.quote_asset_amount, -6); - assert_eq!(market.amm.quote_entry_amount_long, 0); - assert_eq!(market.amm.quote_entry_amount_short, 0); - assert_eq!(market.amm.quote_break_even_amount_long, 0); - assert_eq!(market.amm.quote_break_even_amount_short, 0); + assert_eq!(market.amm.quote_asset_amount(), -6); + assert_eq!(market.amm.quote_entry_amount_long(), 0); + assert_eq!(market.amm.quote_entry_amount_short(), 0); + assert_eq!(market.amm.quote_break_even_amount_long(), 0); + assert_eq!(market.amm.quote_break_even_amount_short(), 0); } #[test] @@ -2150,11 +2164,11 @@ fn close_short_with_quote_break_even_amount_more_than_quote_asset_amount() { }; let mut market = PerpMarket { amm: AMM { - base_asset_amount_short: -11, - quote_asset_amount: 11, - quote_entry_amount_short: 15, - quote_break_even_amount_short: 17, - cumulative_funding_rate_short: 1, + base_asset_amount_short: (-11).into(), + quote_asset_amount: 11.into(), + quote_entry_amount_short: 15.into(), + quote_break_even_amount_short: 17.into(), + cumulative_funding_rate_short: 1.into(), order_step_size: 1, ..AMM::default() }, @@ -2173,13 +2187,13 @@ fn close_short_with_quote_break_even_amount_more_than_quote_asset_amount() { assert_eq!(existing_position.last_cumulative_funding_rate, 0); assert_eq!(market.number_of_users_with_base, 1); - assert_eq!(market.amm.base_asset_amount_long, 0); - assert_eq!(market.amm.base_asset_amount_short, -1); - assert_eq!(market.amm.quote_asset_amount, -4); - assert_eq!(market.amm.quote_entry_amount_long, 0); - assert_eq!(market.amm.quote_entry_amount_short, 0); - assert_eq!(market.amm.quote_break_even_amount_long, 0); - assert_eq!(market.amm.quote_break_even_amount_short, 0); + assert_eq!(market.amm.base_asset_amount_long(), 0); + assert_eq!(market.amm.base_asset_amount_short(), -1); + assert_eq!(market.amm.quote_asset_amount(), -4); + assert_eq!(market.amm.quote_entry_amount_long(), 0); + assert_eq!(market.amm.quote_entry_amount_short(), 0); + assert_eq!(market.amm.quote_break_even_amount_long(), 0); + assert_eq!(market.amm.quote_break_even_amount_short(), 0); } #[test] @@ -2215,15 +2229,21 @@ fn update_amm_near_boundary() { OracleMap::load_one(&jto_market_account_info, slot, None).unwrap(); let mut perp_market = perp_market_loader.load_mut().unwrap(); - assert_eq!(perp_market.amm.base_asset_amount_with_amm, 23831444927173); + assert_eq!(perp_market.amm.base_asset_amount_with_amm(), 23831444927173); assert_eq!( - perp_market.amm.base_asset_amount_with_unsettled_lp, + perp_market.amm.base_asset_amount_with_unsettled_lp(), 562555072827 ); - perp_market.amm.base_asset_amount_with_amm += - perp_market.amm.base_asset_amount_with_unsettled_lp; - perp_market.amm.base_asset_amount_with_unsettled_lp = 0; + let base_asset_amount_with_amm = perp_market.amm.base_asset_amount_with_amm; + let base_asset_amount_with_unsettled_lp = perp_market.amm.base_asset_amount_with_unsettled_lp(); + perp_market.amm.set_base_asset_amount_with_amm( + base_asset_amount_with_amm + .as_i128() + .safe_add(base_asset_amount_with_unsettled_lp) + .unwrap(), + ); + perp_market.amm.set_base_asset_amount_with_unsettled_lp(0); println!("perp_market: {:?}", perp_market.amm.last_update_slot); let oracle_price_data = oracle_map.get_price_data(&perp_market.oracle_id()).unwrap(); @@ -2334,7 +2354,7 @@ fn recenter_amm_1() { assert_eq!(cost, 2538958); - let inv = perp_market.amm.base_asset_amount_with_amm; + let inv = perp_market.amm.base_asset_amount_with_amm(); assert_eq!(inv, 24521505718700); let (_, _, r1_orig, r2_orig) = calculate_base_swap_output_with_spread( @@ -2347,15 +2367,15 @@ fn recenter_amm_1() { assert_eq!(r1_orig, 334835274409); assert_eq!(r2_orig, 704841208); - let current_k = perp_market.amm.sqrt_k; - let _current_peg = perp_market.amm.peg_multiplier; + let current_k = perp_market.amm.sqrt_k(); + let _current_peg = perp_market.amm.peg_multiplier(); let new_k = (current_k * 900000) / 100; recenter_perp_market_amm(&mut perp_market, oracle_price_data.price as u128, new_k).unwrap(); - assert_eq!(perp_market.amm.sqrt_k, new_k); + assert_eq!(perp_market.amm.sqrt_k(), new_k); assert_eq!( - perp_market.amm.peg_multiplier, + perp_market.amm.peg_multiplier(), oracle_price_data.price as u128 ); @@ -2420,9 +2440,9 @@ fn recenter_amm_2() { ); // previous values - assert_eq!(perp_market.amm.peg_multiplier, 5); - assert_eq!(perp_market.amm.quote_asset_reserve, 64381518181749930705); - assert_eq!(perp_market.amm.base_asset_reserve, 307161425106214); + assert_eq!(perp_market.amm.peg_multiplier(), 5); + assert_eq!(perp_market.amm.quote_asset_reserve(), 64381518181749930705); + assert_eq!(perp_market.amm.base_asset_reserve(), 307161425106214); let oracle_price_data = oracle_map .get_price_data(&(oracle_price_key, OracleSource::Pyth)) @@ -2442,7 +2462,7 @@ fn recenter_amm_2() { assert_eq!(cost, 0); - let inv = perp_market.amm.base_asset_amount_with_amm; + let inv = perp_market.amm.base_asset_amount_with_amm(); assert_eq!(inv, -291516212); let (_, _, r1_orig, r2_orig) = calculate_base_swap_output_with_spread( @@ -2455,34 +2475,40 @@ fn recenter_amm_2() { assert_eq!(r1_orig, 326219); assert_eq!(r2_orig, 20707); - let current_k = perp_market.amm.sqrt_k; - let _current_peg = perp_market.amm.peg_multiplier; + let current_k = perp_market.amm.sqrt_k(); + let _current_peg = perp_market.amm.peg_multiplier(); let new_k = current_k * 2; // refusal to decrease further assert_eq!(current_k, current_k); - assert_eq!(perp_market.amm.user_lp_shares, current_k - 1); + assert_eq!(perp_market.amm.user_lp_shares(), current_k - 1); assert_eq!( perp_market.amm.get_lower_bound_sqrt_k().unwrap(), perp_market.amm.min_order_size as u128 ); - perp_market.amm.base_asset_amount_with_amm += - perp_market.amm.base_asset_amount_with_unsettled_lp; - perp_market.amm.base_asset_amount_with_unsettled_lp = 0; + let base_asset_amount_with_amm = perp_market.amm.base_asset_amount_with_amm; + let base_asset_amount_with_unsettled_lp = perp_market.amm.base_asset_amount_with_unsettled_lp; + perp_market.amm.set_base_asset_amount_with_amm( + base_asset_amount_with_amm + .as_i128() + .safe_add(base_asset_amount_with_unsettled_lp.as_i128()) + .unwrap(), + ); + perp_market.amm.set_base_asset_amount_with_unsettled_lp(0); recenter_perp_market_amm(&mut perp_market, oracle_price_data.price as u128, new_k).unwrap(); - assert_eq!(perp_market.amm.sqrt_k, new_k); + assert_eq!(perp_market.amm.sqrt_k(), new_k); assert_eq!( - perp_market.amm.peg_multiplier, + perp_market.amm.peg_multiplier(), oracle_price_data.price as u128 ); - assert_eq!(perp_market.amm.peg_multiplier, 1_120_000); + assert_eq!(perp_market.amm.peg_multiplier(), 1_120_000); // assert_eq!(perp_market.amm.quote_asset_reserve, 140625455708483789 * 2); // assert_eq!(perp_market.amm.base_asset_reserve, 140625456291516213 * 2); - assert_eq!(perp_market.amm.base_asset_reserve, 281254004000000002); - assert_eq!(perp_market.amm.quote_asset_reserve, 281247820033992278); + assert_eq!(perp_market.amm.base_asset_reserve(), 281254004000000002); + assert_eq!(perp_market.amm.quote_asset_reserve(), 281247820033992278); crate::validation::perp_market::validate_perp_market(&perp_market).unwrap(); @@ -2498,15 +2524,15 @@ fn recenter_amm_2() { assert_eq!(r2, 22129); let new_scale = 2; - let new_sqrt_k = perp_market.amm.sqrt_k * new_scale; + let new_sqrt_k = perp_market.amm.sqrt_k() * new_scale; let update_k_result = get_update_k_result(&perp_market, U192::from(new_sqrt_k), false).unwrap(); let adjustment_cost = adjust_k_cost(&mut perp_market, &update_k_result).unwrap(); assert_eq!(adjustment_cost, 19035); update_k(&mut perp_market, &update_k_result).unwrap(); - assert_eq!(perp_market.amm.sqrt_k, new_sqrt_k); - assert_eq!(perp_market.amm.user_lp_shares, current_k - 1); + assert_eq!(perp_market.amm.sqrt_k(), new_sqrt_k); + assert_eq!(perp_market.amm.user_lp_shares(), current_k - 1); assert_eq!( perp_market.amm.get_lower_bound_sqrt_k().unwrap(), 3092000000000 @@ -2555,16 +2581,18 @@ fn test_move_amm() { // let perp_market_old = market_map.get_ref(&4).unwrap(); let mut perp_market = market_map.get_ref_mut(&9).unwrap(); - perp_market.amm.base_asset_amount_with_amm = -3092 * BASE_PRECISION as i128; + perp_market + .amm + .set_base_asset_amount_with_amm(-3092 * BASE_PRECISION as i128); println!( "perp_market latest slot: {:?}", perp_market.amm.last_update_slot ); // previous values - assert_eq!(perp_market.amm.peg_multiplier, 5); - assert_eq!(perp_market.amm.quote_asset_reserve, 64381518181749930705); - assert_eq!(perp_market.amm.base_asset_reserve, 307161425106214); + assert_eq!(perp_market.amm.peg_multiplier(), 5); + assert_eq!(perp_market.amm.quote_asset_reserve(), 64381518181749930705); + assert_eq!(perp_market.amm.base_asset_reserve(), 307161425106214); let oracle_price_data = oracle_map .get_price_data(&(oracle_price_key, OracleSource::Pyth)) @@ -2584,7 +2612,7 @@ fn test_move_amm() { assert_eq!(cost, 0); - let inv = perp_market.amm.base_asset_amount_with_amm; + let inv = perp_market.amm.base_asset_amount_with_amm(); assert_eq!(inv, -3092000000000); let (_, _, r1_orig, r2_orig) = calculate_base_swap_output_with_spread( @@ -2596,9 +2624,9 @@ fn test_move_amm() { assert_eq!(r1_orig, 3489128798); assert_eq!(r2_orig, 215737299); - let current_bar = perp_market.amm.base_asset_reserve; - let _current_qar = perp_market.amm.quote_asset_reserve; - let current_k = perp_market.amm.sqrt_k; + let current_bar = perp_market.amm.base_asset_reserve(); + let _current_qar = perp_market.amm.quote_asset_reserve(); + let current_k = perp_market.amm.sqrt_k(); let inc_numerator = BASE_PRECISION + BASE_PRECISION / 100; let new_k = current_k * inc_numerator / BASE_PRECISION; @@ -2612,6 +2640,6 @@ fn test_move_amm() { ) .unwrap(); crate::validation::perp_market::validate_perp_market(&perp_market).unwrap(); - assert_eq!(perp_market.amm.sqrt_k, new_k); - assert_eq!(perp_market.amm.peg_multiplier, 5); // still same + assert_eq!(perp_market.amm.sqrt_k(), new_k); + assert_eq!(perp_market.amm.peg_multiplier(), 5); // still same } diff --git a/programs/drift/src/controller/repeg.rs b/programs/drift/src/controller/repeg.rs index f41af93fdc..7685b9ff6d 100644 --- a/programs/drift/src/controller/repeg.rs +++ b/programs/drift/src/controller/repeg.rs @@ -49,7 +49,7 @@ pub fn repeg( ) -> DriftResult { // for adhoc admin only repeg - if new_peg_candidate == market.amm.peg_multiplier { + if new_peg_candidate == market.amm.peg_multiplier() { return Err(ErrorCode::InvalidRepegRedundant); } let (terminal_price_before, _terminal_quote_reserves, _terminal_base_reserves) = @@ -91,7 +91,7 @@ pub fn repeg( // modify market's total fee change and peg change let cost_applied = apply_cost_to_market(market, adjustment_cost, true)?; if cost_applied { - market.amm.peg_multiplier = new_peg_candidate; + market.amm.set_peg_multiplier(new_peg_candidate); } else { return Err(ErrorCode::InvalidRepegProfitability); } @@ -201,12 +201,14 @@ pub fn _update_amm( cp_curve::update_k( market, &UpdateKResult { - sqrt_k: repegged_market.amm.sqrt_k, - base_asset_reserve: repegged_market.amm.base_asset_reserve, - quote_asset_reserve: repegged_market.amm.quote_asset_reserve, + sqrt_k: repegged_market.amm.sqrt_k(), + base_asset_reserve: repegged_market.amm.base_asset_reserve(), + quote_asset_reserve: repegged_market.amm.quote_asset_reserve(), }, )?; - market.amm.peg_multiplier = repegged_market.amm.peg_multiplier; + market + .amm + .set_peg_multiplier(repegged_market.amm.peg_multiplier()); amm_update_cost = repegged_cost; } else { msg!("amm_not_successfully_updated = true (repeg cost not applied for check_lower_bound={})", check_lower_bound); @@ -291,28 +293,34 @@ pub fn apply_cost_to_market( // Reduce pnl to quote asset precision and take the absolute value if cost > 0 { let new_total_fee_minus_distributions = - market.amm.total_fee_minus_distributions.safe_sub(cost)?; + market.amm.total_fee_minus_distributions().safe_sub(cost)?; let fee_reserved_for_protocol = repeg::get_total_fee_lower_bound(market)? - .safe_add(market.amm.total_liquidation_fee)? - .safe_sub(market.amm.total_fee_withdrawn)? + .safe_add(market.amm.total_liquidation_fee())? + .safe_sub(market.amm.total_fee_withdrawn())? .cast::()?; // Only a portion of the protocol fees are allocated to repegging // This checks that the total_fee_minus_distributions does not decrease too much after repeg if check_lower_bound { if new_total_fee_minus_distributions >= fee_reserved_for_protocol { - market.amm.total_fee_minus_distributions = new_total_fee_minus_distributions; + market + .amm + .set_total_fee_minus_distributions(new_total_fee_minus_distributions); } else { return Ok(false); } } else { - market.amm.total_fee_minus_distributions = new_total_fee_minus_distributions; + market + .amm + .set_total_fee_minus_distributions(new_total_fee_minus_distributions); } } else { - market.amm.total_fee_minus_distributions = market - .amm - .total_fee_minus_distributions - .safe_add(cost.abs())?; + market.amm.set_total_fee_minus_distributions( + market + .amm + .total_fee_minus_distributions() + .safe_add(cost.abs())?, + ); } market.amm.net_revenue_since_last_funding = market @@ -349,24 +357,24 @@ pub fn settle_expired_market( )?; validate!( - market.amm.base_asset_amount_with_unsettled_lp == 0, + market.amm.base_asset_amount_with_unsettled_lp() == 0, ErrorCode::MarketSettlementRequiresSettledLP, "Outstanding LP in market" )?; let spot_market = &mut spot_market_map.get_ref_mut("E_SPOT_MARKET_INDEX)?; let fee_reserved_for_protocol = repeg::get_total_fee_lower_bound(market)? - .safe_add(market.amm.total_liquidation_fee)? - .safe_sub(market.amm.total_fee_withdrawn)? + .safe_add(market.amm.total_liquidation_fee())? + .safe_sub(market.amm.total_fee_withdrawn())? .cast::()?; let budget = market .amm - .total_fee_minus_distributions + .total_fee_minus_distributions() .safe_sub(fee_reserved_for_protocol)? .max(0); let available_fee_pool = get_token_amount( - market.amm.fee_pool.scaled_balance, + market.amm.fee_pool.scaled_balance(), spot_market, &SpotBalanceType::Deposit, )? @@ -400,7 +408,7 @@ pub fn settle_expired_market( K_BPS_UPDATE_SCALE, )?; - let new_sqrt_k = bn::U192::from(market.amm.sqrt_k) + let new_sqrt_k = bn::U192::from(market.amm.sqrt_k()) .safe_mul(bn::U192::from(k_scale_numerator))? .safe_div(bn::U192::from(k_scale_denominator))? .min(bn::U192::from(MAX_SQRT_K)); @@ -447,13 +455,13 @@ pub fn settle_expired_market( )?; let pnl_pool_token_amount = get_token_amount( - market.pnl_pool.scaled_balance, + market.pnl_pool.scaled_balance(), spot_market, market.pnl_pool.balance_type(), )?; let fee_pool_token_amount = get_token_amount( - market.amm.fee_pool.scaled_balance, + market.amm.fee_pool.scaled_balance(), spot_market, market.amm.fee_pool.balance_type(), )?; diff --git a/programs/drift/src/controller/repeg/tests.rs b/programs/drift/src/controller/repeg/tests.rs index cc369c25e1..534bb23a4d 100644 --- a/programs/drift/src/controller/repeg/tests.rs +++ b/programs/drift/src/controller/repeg/tests.rs @@ -18,12 +18,12 @@ use crate::state::user::MarketType; pub fn update_amm_test() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 65 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 63015384615, - terminal_quote_asset_reserve: 64 * AMM_RESERVE_PRECISION, - sqrt_k: 64 * AMM_RESERVE_PRECISION, - peg_multiplier: 19_400 * PEG_PRECISION, - base_asset_amount_with_amm: -(AMM_RESERVE_PRECISION as i128), + base_asset_reserve: (65 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: 63015384615.into(), + terminal_quote_asset_reserve: (64 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (64 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (19_400 * PEG_PRECISION).into(), + base_asset_amount_with_amm: (-(AMM_RESERVE_PRECISION as i128)).into(), mark_std: PRICE_PRECISION as u64, last_mark_price_twap_ts: 0, historical_oracle_data: HistoricalOracleData { @@ -35,7 +35,7 @@ pub fn update_amm_test() { base_spread: 250, curve_update_intensity: 100, max_spread: 55500, - concentration_coef: 31020710, //unrealistic but for poc + concentration_coef: 31020710.into(), //unrealistic but for poc ..AMM::default() }, status: MarketStatus::Initialized, @@ -48,10 +48,14 @@ pub fn update_amm_test() { // market.amm.terminal_quote_asset_reserve = new_terminal_quote_reserve; assert_eq!(new_terminal_quote_reserve, 64000000000); let (min_base_asset_reserve, max_base_asset_reserve) = - amm::calculate_bid_ask_bounds(market.amm.concentration_coef, new_terminal_base_reserve) + amm::calculate_bid_ask_bounds(market.amm.concentration_coef(), new_terminal_base_reserve) .unwrap(); - market.amm.min_base_asset_reserve = min_base_asset_reserve; - market.amm.max_base_asset_reserve = max_base_asset_reserve; + market + .amm + .set_min_base_asset_reserve(min_base_asset_reserve); + market + .amm + .set_max_base_asset_reserve(max_base_asset_reserve); let state = State { oracle_guard_rails: OracleGuardRails { @@ -104,7 +108,7 @@ pub fn update_amm_test() { let cost_of_update = _update_amm(&mut market, &mm_oracle_price_data, &state, now, slot).unwrap(); - assert_eq!(market.amm.sqrt_k, 63936000000); + assert_eq!(market.amm.sqrt_k(), 63936000000); let is_oracle_valid = oracle::oracle_validity( MarketType::Perp, market.market_index, @@ -150,8 +154,8 @@ pub fn update_amm_test() { .unwrap(); assert!(too_diverge); - let profit = market.amm.total_fee_minus_distributions; - let peg = market.amm.peg_multiplier; + let profit = market.amm.total_fee_minus_distributions(); + let peg = market.amm.peg_multiplier(); assert_eq!(-cost_of_update, profit); assert!(is_oracle_valid); assert!(profit < 0); @@ -184,15 +188,15 @@ pub fn update_amm_test() { pub fn update_amm_test_bad_oracle() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 65 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 630153846154000, - terminal_quote_asset_reserve: 64 * AMM_RESERVE_PRECISION, - sqrt_k: 64 * AMM_RESERVE_PRECISION, - peg_multiplier: 19_400_000, - base_asset_amount_with_amm: -(AMM_RESERVE_PRECISION as i128), + base_asset_reserve: (65 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: 630153846154000.into(), + terminal_quote_asset_reserve: (64 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (64 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 19_400_000.into(), + base_asset_amount_with_amm: (-(AMM_RESERVE_PRECISION as i128)).into(), mark_std: PRICE_PRECISION as u64, last_mark_price_twap_ts: 0, - concentration_coef: 1020710, + concentration_coef: 1020710.into(), historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: 19_400 * PRICE_PRECISION_I64, ..HistoricalOracleData::default() @@ -263,7 +267,7 @@ pub fn update_amm_larg_conf_test() { let slot = 81680085; let mut market = PerpMarket::default_btc_test(); - assert_eq!(market.amm.base_asset_amount_with_amm, -1000000000); + assert_eq!(market.amm.base_asset_amount_with_amm(), -1000000000); let state = State { oracle_guard_rails: OracleGuardRails { @@ -332,7 +336,7 @@ pub fn update_amm_larg_conf_test() { assert_eq!(mrk, 18849999999); assert_eq!(market.amm.long_spread, 2237); - assert_eq!(market.amm.peg_multiplier, 19443664550); + assert_eq!(market.amm.peg_multiplier(), 19443664550); assert_eq!(market.amm.short_spread, 26454); // add move lower @@ -345,16 +349,16 @@ pub fn update_amm_larg_conf_test() { }; let fee_budget = calculate_fee_pool(&market).unwrap(); - assert_eq!(market.amm.total_fee_minus_distributions, 42992787); + assert_eq!(market.amm.total_fee_minus_distributions(), 42992787); assert_eq!(fee_budget, 42992787); let optimal_peg = calculate_peg_from_target_price( - market.amm.quote_asset_reserve, - market.amm.base_asset_reserve, + market.amm.quote_asset_reserve(), + market.amm.base_asset_reserve(), oracle_price_data.price as u64, ) .unwrap(); - assert_eq!(market.amm.peg_multiplier, 19443664550); + assert_eq!(market.amm.peg_multiplier(), 19443664550); assert_eq!(optimal_peg, 19412719726); let optimal_peg_cost = calculate_repeg_cost(&market.amm, optimal_peg).unwrap(); @@ -412,9 +416,11 @@ pub fn update_amm_larg_conf_w_neg_tfmd_test() { let slot = 81680085; let mut market = PerpMarket::default_btc_test(); - market.amm.concentration_coef = 1414213; - market.amm.total_fee_minus_distributions = -(10000 * QUOTE_PRECISION as i128); - assert_eq!(market.amm.base_asset_amount_with_amm, -1000000000); + market.amm.set_concentration_coef(1414213); + market + .amm + .set_total_fee_minus_distributions(-(10000 * QUOTE_PRECISION as i128)); + assert_eq!(market.amm.base_asset_amount_with_amm(), -1000000000); let state = State { oracle_guard_rails: OracleGuardRails { @@ -448,25 +454,25 @@ pub fn update_amm_larg_conf_w_neg_tfmd_test() { assert_eq!(market.amm.long_spread, 0); assert_eq!(market.amm.short_spread, 0); assert_eq!(market.amm.last_update_slot, 0); - assert_eq!(market.amm.sqrt_k, 64000000000); - let prev_peg_multiplier = market.amm.peg_multiplier; - let prev_total_fee_minus_distributions = market.amm.total_fee_minus_distributions; + assert_eq!(market.amm.sqrt_k(), 64000000000); + let prev_peg_multiplier = market.amm.peg_multiplier(); + let prev_total_fee_minus_distributions = market.amm.total_fee_minus_distributions(); let cost_of_update = _update_amm(&mut market, &mm_oracle_price_data, &state, now, slot).unwrap(); assert!(market.amm.is_recent_oracle_valid(slot).unwrap()); assert_eq!(cost_of_update, -42992787); // amm wins when price increases - assert_eq!(market.amm.sqrt_k, 64000000000); - assert_eq!(market.amm.base_asset_reserve, 65000000000); - assert_eq!(market.amm.quote_asset_reserve, 63015384615); - assert_eq!(market.amm.terminal_quote_asset_reserve, 64000000000); - assert_eq!(market.amm.min_base_asset_reserve, 45254851991); - assert_eq!(market.amm.max_base_asset_reserve, 90509632000); - assert_eq!(market.amm.peg_multiplier, 19443664550); - assert_eq!(market.amm.peg_multiplier > prev_peg_multiplier, true); - assert_eq!(market.amm.total_fee_minus_distributions, -9957007213); + assert_eq!(market.amm.sqrt_k(), 64000000000); + assert_eq!(market.amm.base_asset_reserve(), 65000000000); + assert_eq!(market.amm.quote_asset_reserve(), 63015384615); + assert_eq!(market.amm.terminal_quote_asset_reserve(), 64000000000); + assert_eq!(market.amm.min_base_asset_reserve(), 45254851991); + assert_eq!(market.amm.max_base_asset_reserve(), 90509632000); + assert_eq!(market.amm.peg_multiplier(), 19443664550); + assert_eq!(market.amm.peg_multiplier() > prev_peg_multiplier, true); + assert_eq!(market.amm.total_fee_minus_distributions(), -9957007213); assert_eq!( - market.amm.total_fee_minus_distributions > prev_total_fee_minus_distributions, + market.amm.total_fee_minus_distributions() > prev_total_fee_minus_distributions, true ); @@ -529,37 +535,37 @@ pub fn update_amm_larg_conf_w_neg_tfmd_test() { .unwrap(); let fee_budget = calculate_fee_pool(&market).unwrap(); - assert_eq!(market.amm.total_fee_minus_distributions, -9957007213); + assert_eq!(market.amm.total_fee_minus_distributions(), -9957007213); assert_eq!(fee_budget, 0); let optimal_peg = calculate_peg_from_target_price( - market.amm.quote_asset_reserve, - market.amm.base_asset_reserve, + market.amm.quote_asset_reserve(), + market.amm.base_asset_reserve(), oracle_price_data.price as u64, ) .unwrap(); - assert_eq!(market.amm.peg_multiplier, 19443664550); + assert_eq!(market.amm.peg_multiplier(), 19443664550); assert_eq!(optimal_peg, 19412719726); let optimal_peg_cost = calculate_repeg_cost(&market.amm, optimal_peg).unwrap(); assert_eq!(optimal_peg_cost, 30468749); - let prev_peg_multiplier = market.amm.peg_multiplier; - let prev_total_fee_minus_distributions = market.amm.total_fee_minus_distributions; + let prev_peg_multiplier = market.amm.peg_multiplier(); + let prev_total_fee_minus_distributions = market.amm.total_fee_minus_distributions(); let cost_of_update = _update_amm(&mut market, &mm_oracle_price_data, &state, now, slot).unwrap(); assert_eq!(cost_of_update, 21459587); // amm loses when price decreases (given users are net short) - assert_eq!(market.amm.sqrt_k, 63936000000); // k lowered since cost_of_update is positive and total_fee_minus_distributions negative - assert_eq!(market.amm.base_asset_reserve, 64935000065); - assert_eq!(market.amm.quote_asset_reserve, 62952369167); - assert_eq!(market.amm.terminal_quote_asset_reserve, 63936999950); - assert_eq!(market.amm.min_base_asset_reserve, 45208890078); - assert_eq!(market.amm.max_base_asset_reserve, 90417708246); - assert_eq!(market.amm.peg_multiplier, 19421869997); - assert_eq!(market.amm.peg_multiplier < prev_peg_multiplier, true); + assert_eq!(market.amm.sqrt_k(), 63936000000); // k lowered since cost_of_update is positive and total_fee_minus_distributions negative + assert_eq!(market.amm.base_asset_reserve(), 64935000065); + assert_eq!(market.amm.quote_asset_reserve(), 62952369167); + assert_eq!(market.amm.terminal_quote_asset_reserve(), 63936999950); + assert_eq!(market.amm.min_base_asset_reserve(), 45208890078); + assert_eq!(market.amm.max_base_asset_reserve(), 90417708246); + assert_eq!(market.amm.peg_multiplier(), 19421869997); + assert_eq!(market.amm.peg_multiplier() < prev_peg_multiplier, true); // assert_eq!(market.amm.total_fee_minus_distributions, -9978167413); assert_eq!( - market.amm.total_fee_minus_distributions < prev_total_fee_minus_distributions, + market.amm.total_fee_minus_distributions() < prev_total_fee_minus_distributions, true ); diff --git a/programs/drift/src/controller/revenue_share.rs b/programs/drift/src/controller/revenue_share.rs index 61681cd7a4..8adedabd26 100644 --- a/programs/drift/src/controller/revenue_share.rs +++ b/programs/drift/src/controller/revenue_share.rs @@ -70,7 +70,7 @@ pub fn sweep_completed_revenue_share_for_market<'a>( } let pnl_pool_token_amount = get_token_amount( - perp_market.pnl_pool.scaled_balance, + perp_market.pnl_pool.scaled_balance(), quote_spot_market, perp_market.pnl_pool.balance_type(), )?; diff --git a/programs/drift/src/controller/spot_balance.rs b/programs/drift/src/controller/spot_balance.rs index f8e1b625e9..95f06c19e1 100644 --- a/programs/drift/src/controller/spot_balance.rs +++ b/programs/drift/src/controller/spot_balance.rs @@ -39,13 +39,13 @@ pub fn update_spot_market_twap_stats( let from_start = max(1_i64, SPOT_MARKET_TOKEN_TWAP_WINDOW.safe_sub(since_last)?); let deposit_token_amount = get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), spot_market, &SpotBalanceType::Deposit, )?; let borrow_token_amount = get_token_amount( - spot_market.borrow_balance, + spot_market.borrow_balance(), spot_market, &SpotBalanceType::Borrow, )?; @@ -151,18 +151,22 @@ pub fn update_spot_market_cumulative_interest( deposit_interest.safe_sub(deposit_interest_for_stakers)?; if deposit_interest_for_lenders > 0 { - spot_market.cumulative_deposit_interest = spot_market - .cumulative_deposit_interest - .safe_add(deposit_interest_for_lenders)?; - - spot_market.cumulative_borrow_interest = spot_market - .cumulative_borrow_interest - .safe_add(borrow_interest)?; + spot_market.set_cumulative_deposit_interest( + spot_market + .cumulative_deposit_interest() + .safe_add(deposit_interest_for_lenders)?, + ); + + spot_market.set_cumulative_borrow_interest( + spot_market + .cumulative_borrow_interest() + .safe_add(borrow_interest)?, + ); spot_market.last_interest_ts = now.cast()?; // add deposit_interest_for_stakers as balance for revenue_pool let token_amount = get_interest_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), spot_market, deposit_interest_for_stakers, )?; @@ -172,10 +176,10 @@ pub fn update_spot_market_cumulative_interest( emit!(SpotInterestRecord { ts: now, market_index: spot_market.market_index, - deposit_balance: spot_market.deposit_balance, - cumulative_deposit_interest: spot_market.cumulative_deposit_interest, - borrow_balance: spot_market.borrow_balance, - cumulative_borrow_interest: spot_market.cumulative_borrow_interest, + deposit_balance: spot_market.deposit_balance(), + cumulative_deposit_interest: spot_market.cumulative_deposit_interest(), + borrow_balance: spot_market.borrow_balance(), + cumulative_borrow_interest: spot_market.cumulative_borrow_interest(), optimal_utilization: spot_market.optimal_utilization, optimal_borrow_rate: spot_market.optimal_borrow_rate, max_borrow_rate: spot_market.max_borrow_rate, @@ -261,13 +265,13 @@ pub fn update_spot_balances( if is_leaving_drift && update_direction == &SpotBalanceType::Borrow { let deposit_token_amount = get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), spot_market, &SpotBalanceType::Deposit, )?; let borrow_token_amount = get_token_amount( - spot_market.borrow_balance, + spot_market.borrow_balance(), spot_market, &SpotBalanceType::Borrow, )?; @@ -302,10 +306,10 @@ pub fn transfer_spot_balances( if from_spot_balance.balance_type() == &SpotBalanceType::Deposit { validate!( - spot_market.deposit_balance >= from_spot_balance.balance(), + spot_market.deposit_balance() >= from_spot_balance.balance(), ErrorCode::InvalidSpotMarketState, "spot_market.deposit_balance={} lower than individual spot balance={}", - spot_market.deposit_balance, + spot_market.deposit_balance(), from_spot_balance.balance() )?; } @@ -435,10 +439,10 @@ fn increase_spot_balance( ) -> DriftResult { match balance_type { SpotBalanceType::Deposit => { - spot_market.deposit_balance = spot_market.deposit_balance.safe_add(delta)? + spot_market.set_deposit_balance(spot_market.deposit_balance().safe_add(delta)?) } SpotBalanceType::Borrow => { - spot_market.borrow_balance = spot_market.borrow_balance.safe_add(delta)? + spot_market.set_borrow_balance(spot_market.borrow_balance().safe_add(delta)?) } } @@ -452,10 +456,10 @@ fn decrease_spot_balance( ) -> DriftResult { match balance_type { SpotBalanceType::Deposit => { - spot_market.deposit_balance = spot_market.deposit_balance.safe_sub(delta)? + spot_market.set_deposit_balance(spot_market.deposit_balance().safe_sub(delta)?) } SpotBalanceType::Borrow => { - spot_market.borrow_balance = spot_market.borrow_balance.safe_sub(delta)? + spot_market.set_borrow_balance(spot_market.borrow_balance().safe_sub(delta)?) } } diff --git a/programs/drift/src/controller/spot_balance/tests.rs b/programs/drift/src/controller/spot_balance/tests.rs index b1c19153c1..844b90925a 100644 --- a/programs/drift/src/controller/spot_balance/tests.rs +++ b/programs/drift/src/controller/spot_balance/tests.rs @@ -72,19 +72,19 @@ fn test_daily_withdraw_limits() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 50 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (50 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), ..AMM::default() @@ -102,14 +102,14 @@ fn test_daily_withdraw_limits() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: 0, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: 0.into(), deposit_token_twap: QUOTE_PRECISION_U64 / 2, status: MarketStatus::Active, @@ -120,15 +120,15 @@ fn test_daily_withdraw_limits() { market_index: 1, oracle_source: OracleSource::Pyth, oracle: oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 10, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: SPOT_BALANCE_PRECISION.into(), deposit_token_twap: (SPOT_BALANCE_PRECISION * 10) as u64, borrow_token_twap: 0, @@ -159,11 +159,11 @@ fn test_daily_withdraw_limits() { let amount: u64 = QUOTE_PRECISION as u64; assert_eq!( - spot_market.cumulative_deposit_interest, + spot_market.cumulative_deposit_interest(), SPOT_CUMULATIVE_INTEREST_PRECISION ); assert_eq!( - spot_market.cumulative_borrow_interest, + spot_market.cumulative_borrow_interest(), SPOT_CUMULATIVE_INTEREST_PRECISION ); @@ -181,13 +181,13 @@ fn test_daily_withdraw_limits() { .is_err()); spot_market = spot_market_backup; user = user_backup; - assert_eq!(spot_market.deposit_balance, SPOT_BALANCE_PRECISION); + assert_eq!(spot_market.deposit_balance(), SPOT_BALANCE_PRECISION); // .50 * .2 = .1 assert_eq!(spot_market.deposit_token_twap, 500000); assert_eq!(user.spot_positions[0].scaled_balance, 1000000000); - assert_eq!(spot_market.deposit_balance, 1000000000); - assert_eq!(spot_market.borrow_balance, 0); + assert_eq!(spot_market.deposit_balance(), 1000000000); + assert_eq!(spot_market.borrow_balance(), 0); assert_eq!((amount / 2), 500000); update_spot_balances_and_cumulative_deposits_with_limits( (amount / 2) as u128, @@ -198,8 +198,8 @@ fn test_daily_withdraw_limits() { .unwrap(); assert_eq!(user.spot_positions[0].scaled_balance, 499999999); assert_eq!(spot_market.deposit_token_twap, 500000); - assert_eq!(spot_market.deposit_balance, 499999999); - assert_eq!(spot_market.borrow_balance, 0); + assert_eq!(spot_market.deposit_balance(), 499999999); + assert_eq!(spot_market.borrow_balance(), 0); // .50 * .25 = .125 update_spot_balances_and_cumulative_deposits_with_limits( @@ -222,7 +222,7 @@ fn test_daily_withdraw_limits() { .is_err()); spot_market = spot_market_backup; user = user_backup; - assert_eq!(spot_market.deposit_balance, 375001998); + assert_eq!(spot_market.deposit_balance(), 375001998); assert_eq!(user.spot_positions[0].scaled_balance, 375001998); assert_eq!(user.spot_positions[0].market_index, 0); @@ -245,7 +245,7 @@ fn test_daily_withdraw_limits() { &mut user, ) .unwrap(); - assert_eq!(spot_market.deposit_balance, 100000375001998); + assert_eq!(spot_market.deposit_balance(), 100000375001998); assert_eq!(user.spot_positions[0].scaled_balance, 100000375001998); assert_eq!(user.spot_positions[1].scaled_balance, 0); @@ -268,7 +268,7 @@ fn test_daily_withdraw_limits() { }), ..User::default() }; - sol_spot_market.deposit_balance = 50 * SPOT_BALANCE_PRECISION; + sol_spot_market.set_deposit_balance(50 * SPOT_BALANCE_PRECISION); sol_spot_market.deposit_token_twap = (500 * SPOT_BALANCE_PRECISION) as u64; sol_spot_market.optimal_utilization = 1_000_000 as u32; //20% APR @@ -344,8 +344,8 @@ fn test_daily_withdraw_limits() { ) .unwrap(); - assert_eq!(sol_spot_market.deposit_balance, 50000000000); - assert_eq!(sol_spot_market.borrow_balance, 8000000002); + assert_eq!(sol_spot_market.deposit_balance(), 50000000000); + assert_eq!(sol_spot_market.borrow_balance(), 8000000002); assert_eq!(sol_spot_market.borrow_token_twap, 0); update_spot_market_cumulative_interest(&mut sol_spot_market, None, now + 3655 * 24).unwrap(); @@ -419,14 +419,14 @@ fn test_check_withdraw_limits() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: 0, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: 0.into(), deposit_token_twap: QUOTE_PRECISION_U64 / 2, status: MarketStatus::Active, @@ -437,15 +437,15 @@ fn test_check_withdraw_limits() { market_index: 1, oracle_source: OracleSource::Pyth, oracle: oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 10, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: 2 * SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION, + deposit_balance: (2 * SPOT_BALANCE_PRECISION).into(), + borrow_balance: SPOT_BALANCE_PRECISION.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, deposit_token_twap: 28_000_000_000_u64, status: MarketStatus::Active, @@ -512,8 +512,8 @@ fn test_check_withdraw_limits_below_optimal_utilization() { market_index: 1, oracle_source: OracleSource::Pyth, oracle: oracle_price_key, - cumulative_deposit_interest: 1020 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000, - cumulative_borrow_interest: 1222 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000, + cumulative_deposit_interest: (1020 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000).into(), + cumulative_borrow_interest: (1222 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000).into(), optimal_utilization: 700000, optimal_borrow_rate: 60000, @@ -524,8 +524,8 @@ fn test_check_withdraw_limits_below_optimal_utilization() { maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: 200_000 * SPOT_BALANCE_PRECISION, // 200k sol - borrow_balance: 100_000 * SPOT_BALANCE_PRECISION, + deposit_balance: (200_000 * SPOT_BALANCE_PRECISION).into(), // 200k sol + borrow_balance: (100_000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, deposit_token_twap: 204000000000000_u64, borrow_token_twap: 122200000000000_u64, @@ -541,13 +541,13 @@ fn test_check_withdraw_limits_below_optimal_utilization() { ); // below optimal util let deposit_tokens_1 = get_token_amount( - sol_spot_market.deposit_balance, + sol_spot_market.deposit_balance(), &sol_spot_market, &SpotBalanceType::Deposit, ) .unwrap(); let borrow_tokens_1 = get_token_amount( - sol_spot_market.borrow_balance, + sol_spot_market.borrow_balance(), &sol_spot_market, &SpotBalanceType::Borrow, ) @@ -584,11 +584,11 @@ fn test_check_withdraw_limits_below_optimal_utilization() { assert_eq!(valid_withdraw, true); // ensure it fails due to higher min_dep above - sol_spot_market.deposit_balance = 174571428571428 / 1020 * 1000; + sol_spot_market.set_deposit_balance(174571428571428 / 1020 * 1000); sol_spot_market.utilization_twap = 100000; let deposit_tokens_1 = get_token_amount( - sol_spot_market.deposit_balance, + sol_spot_market.deposit_balance(), &sol_spot_market, &SpotBalanceType::Deposit, ) @@ -613,8 +613,8 @@ fn test_check_withdraw_limits_above_optimal_utilization() { market_index: 1, oracle_source: OracleSource::Pyth, oracle: oracle_price_key, - cumulative_deposit_interest: 1020 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000, - cumulative_borrow_interest: 1222 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000, + cumulative_deposit_interest: (1020 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000).into(), + cumulative_borrow_interest: (1222 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000).into(), optimal_utilization: 700000, // 70% optimal_borrow_rate: 60000, // 6% @@ -625,8 +625,8 @@ fn test_check_withdraw_limits_above_optimal_utilization() { maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: 200_000 * SPOT_BALANCE_PRECISION, // 200k sol - borrow_balance: 160_000 * SPOT_BALANCE_PRECISION, + deposit_balance: (200_000 * SPOT_BALANCE_PRECISION).into(), // 200k sol + borrow_balance: (160_000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, deposit_token_twap: 204000000000000_u64, borrow_token_twap: 199200000000000_u64, @@ -642,13 +642,13 @@ fn test_check_withdraw_limits_above_optimal_utilization() { ); // below optimal util let deposit_tokens_1 = get_token_amount( - sol_spot_market.deposit_balance, + sol_spot_market.deposit_balance(), &sol_spot_market, &SpotBalanceType::Deposit, ) .unwrap(); let borrow_tokens_1 = get_token_amount( - sol_spot_market.borrow_balance, + sol_spot_market.borrow_balance(), &sol_spot_market, &SpotBalanceType::Borrow, ) @@ -711,7 +711,7 @@ fn test_check_withdraw_limits_above_optimal_utilization() { assert_eq!(valid_withdraw, true); // now ensure it fails due to higher min_dep above - sol_spot_market.deposit_balance = min_dep / 1020 * 1000; + sol_spot_market.set_deposit_balance(min_dep / 1020 * 1000); let valid_withdraw = check_withdraw_limits(&sol_spot_market, None, None).unwrap(); assert_eq!(valid_withdraw, false); } @@ -735,19 +735,19 @@ fn check_fee_collection() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 50 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (50 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), ..AMM::default() @@ -765,13 +765,13 @@ fn check_fee_collection() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: 0, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: 0.into(), deposit_token_twap: QUOTE_PRECISION_U64, optimal_utilization: SPOT_UTILIZATION_PRECISION_U32 / 2, @@ -787,15 +787,15 @@ fn check_fee_collection() { market_index: 1, oracle_source: OracleSource::Pyth, oracle: oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 10, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: SPOT_BALANCE_PRECISION.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, insurance_fund: InsuranceFund { revenue_settle_period: 1, @@ -827,8 +827,8 @@ fn check_fee_collection() { spot_market.insurance_fund.total_factor = 1000; //1_000_000 assert_eq!(spot_market.utilization_twap, 0); - assert_eq!(spot_market.deposit_balance, 1000000000); - assert_eq!(spot_market.borrow_balance, 0); + assert_eq!(spot_market.deposit_balance(), 1000000000); + assert_eq!(spot_market.borrow_balance(), 0); let amount = QUOTE_PRECISION / 4; update_spot_balances_and_cumulative_deposits_with_limits( @@ -842,33 +842,33 @@ fn check_fee_collection() { assert_eq!(user.total_deposits, 0); assert_eq!(user.total_withdraws, 0); - assert_eq!(spot_market.deposit_balance, 1000000000); - assert_eq!(spot_market.borrow_balance, 125000001); + assert_eq!(spot_market.deposit_balance(), 1000000000); + assert_eq!(spot_market.borrow_balance(), 125000001); assert_eq!(spot_market.utilization_twap, 0); update_spot_market_cumulative_interest(&mut spot_market, None, now + 100).unwrap(); - assert_eq!(spot_market.revenue_pool.scaled_balance, 0); - assert_eq!(spot_market.cumulative_deposit_interest, 10000019799); - assert_eq!(spot_market.cumulative_borrow_interest, 10000158551); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 0); + assert_eq!(spot_market.cumulative_deposit_interest(), 10000019799); + assert_eq!(spot_market.cumulative_borrow_interest(), 10000158551); assert_eq!(spot_market.last_interest_ts, 100); assert_eq!(spot_market.last_twap_ts, 100); assert_eq!(spot_market.utilization_twap, 143); let deposit_tokens_1 = get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), &spot_market, &SpotBalanceType::Deposit, ) .unwrap(); let borrow_tokens_1 = get_token_amount( - spot_market.borrow_balance, + spot_market.borrow_balance(), &spot_market, &SpotBalanceType::Borrow, ) .unwrap(); let if_tokens_1 = get_token_amount( - spot_market.revenue_pool.scaled_balance, + spot_market.revenue_pool.scaled_balance(), &spot_market, &SpotBalanceType::Borrow, ) @@ -884,24 +884,24 @@ fn check_fee_collection() { assert_eq!(spot_market.last_twap_ts, 7500); assert_eq!(spot_market.utilization_twap, 10846); - assert_eq!(spot_market.cumulative_deposit_interest, 10001484937); - assert_eq!(spot_market.cumulative_borrow_interest, 10011891454); - assert_eq!(spot_market.revenue_pool.scaled_balance, 0); + assert_eq!(spot_market.cumulative_deposit_interest(), 10001484937); + assert_eq!(spot_market.cumulative_borrow_interest(), 10011891454); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 0); let deposit_tokens_2 = get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), &spot_market, &SpotBalanceType::Deposit, ) .unwrap(); let borrow_tokens_2 = get_token_amount( - spot_market.borrow_balance, + spot_market.borrow_balance(), &spot_market, &SpotBalanceType::Borrow, ) .unwrap(); let if_tokens_2 = get_token_amount( - spot_market.revenue_pool.scaled_balance, + spot_market.revenue_pool.scaled_balance(), &spot_market, &SpotBalanceType::Borrow, ) @@ -926,24 +926,24 @@ fn check_fee_collection() { now = now + 750 + (60 * 60 * 24 * 365); - assert_eq!(spot_market.cumulative_deposit_interest, 16257818378); - assert_eq!(spot_market.cumulative_borrow_interest, 60112684636); - assert_eq!(spot_market.revenue_pool.scaled_balance, 385045); + assert_eq!(spot_market.cumulative_deposit_interest(), 16257818378); + assert_eq!(spot_market.cumulative_borrow_interest(), 60112684636); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 385045); let deposit_tokens_3 = get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), &spot_market, &SpotBalanceType::Deposit, ) .unwrap(); let borrow_tokens_3 = get_token_amount( - spot_market.borrow_balance, + spot_market.borrow_balance(), &spot_market, &SpotBalanceType::Borrow, ) .unwrap(); let if_tokens_3 = get_token_amount( - spot_market.revenue_pool.scaled_balance, + spot_market.revenue_pool.scaled_balance(), &spot_market, &SpotBalanceType::Borrow, ) @@ -964,7 +964,7 @@ fn check_fee_collection() { ); // settle IF pool to 100% utilization boundary - assert_eq!(spot_market.revenue_pool.scaled_balance, 385045); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 385045); assert_eq!(spot_market.utilization_twap, 462004); spot_market.insurance_fund.revenue_settle_period = 1; @@ -978,26 +978,26 @@ fn check_fee_collection() { .unwrap(); assert_eq!(settle_amount, 626); - assert_eq!(spot_market.insurance_fund.user_shares, 0); - assert_eq!(spot_market.insurance_fund.total_shares, 0); + assert_eq!(spot_market.insurance_fund.user_shares(), 0); + assert_eq!(spot_market.insurance_fund.total_shares(), 0); assert_eq!(if_tokens_3 - (settle_amount as u128), 1689); - assert_eq!(spot_market.revenue_pool.scaled_balance, 0); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 0); assert_eq!(spot_market.utilization_twap, 462005); let deposit_tokens_4 = get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), &spot_market, &SpotBalanceType::Deposit, ) .unwrap(); let borrow_tokens_4 = get_token_amount( - spot_market.borrow_balance, + spot_market.borrow_balance(), &spot_market, &SpotBalanceType::Borrow, ) .unwrap(); let if_tokens_4 = get_token_amount( - spot_market.revenue_pool.scaled_balance, + spot_market.revenue_pool.scaled_balance(), &spot_market, &SpotBalanceType::Deposit, ) @@ -1026,19 +1026,19 @@ fn check_fee_collection() { .unwrap(); let deposit_tokens_5 = get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), &spot_market, &SpotBalanceType::Deposit, ) .unwrap(); let borrow_tokens_5 = get_token_amount( - spot_market.borrow_balance, + spot_market.borrow_balance(), &spot_market, &SpotBalanceType::Borrow, ) .unwrap(); let _if_tokens_5 = get_token_amount( - spot_market.revenue_pool.scaled_balance, + spot_market.revenue_pool.scaled_balance(), &spot_market, &SpotBalanceType::Deposit, ) @@ -1070,13 +1070,13 @@ fn check_fee_collection() { .unwrap(); let deposit_tokens_6 = get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), &spot_market, &SpotBalanceType::Deposit, ) .unwrap(); let borrow_tokens_6 = get_token_amount( - spot_market.borrow_balance, + spot_market.borrow_balance(), &spot_market, &SpotBalanceType::Borrow, ) @@ -1108,19 +1108,19 @@ fn check_fee_collection_larger_nums() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 50 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (50 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), ..AMM::default() @@ -1138,13 +1138,13 @@ fn check_fee_collection_larger_nums() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 1000000 * SPOT_BALANCE_PRECISION, - borrow_balance: 0, + deposit_balance: (1000000 * SPOT_BALANCE_PRECISION).into(), + borrow_balance: 0.into(), deposit_token_twap: QUOTE_PRECISION_U64 / 2, optimal_utilization: SPOT_UTILIZATION_PRECISION_U32 / 2, @@ -1158,15 +1158,15 @@ fn check_fee_collection_larger_nums() { market_index: 1, oracle_source: OracleSource::Pyth, oracle: oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 10, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION, + deposit_balance: SPOT_BALANCE_PRECISION.into(), + borrow_balance: SPOT_BALANCE_PRECISION.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, ..SpotMarket::default() }; @@ -1194,10 +1194,10 @@ fn check_fee_collection_larger_nums() { assert_eq!(spot_market.utilization_twap, 0); assert_eq!( - spot_market.deposit_balance, + spot_market.deposit_balance(), 1000000 * SPOT_BALANCE_PRECISION ); - assert_eq!(spot_market.borrow_balance, 0); + assert_eq!(spot_market.borrow_balance(), 0); let amount = 540510 * QUOTE_PRECISION; update_spot_balances( @@ -1210,37 +1210,37 @@ fn check_fee_collection_larger_nums() { .unwrap(); assert_eq!( - spot_market.deposit_balance, + spot_market.deposit_balance(), 1000000 * SPOT_BALANCE_PRECISION ); - assert_eq!(spot_market.borrow_balance, 540510000000001); + assert_eq!(spot_market.borrow_balance(), 540510000000001); assert_eq!(spot_market.utilization_twap, 0); update_spot_market_cumulative_interest(&mut spot_market, None, now + 100).unwrap(); let br = calculate_borrow_rate(&spot_market, spot_market.get_utilization().unwrap()).unwrap(); assert_eq!(br, 20173678); - assert_eq!(spot_market.revenue_pool.scaled_balance, 3457492406); - assert_eq!(spot_market.cumulative_deposit_interest, 10000311188); - assert_eq!(spot_market.cumulative_borrow_interest, 10000639702); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 3457492406); + assert_eq!(spot_market.cumulative_deposit_interest(), 10000311188); + assert_eq!(spot_market.cumulative_borrow_interest(), 10000639702); assert_eq!(spot_market.last_interest_ts, 100); assert_eq!(spot_market.last_twap_ts, 100); assert_eq!(spot_market.utilization_twap, 624); let deposit_tokens_1 = get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), &spot_market, &SpotBalanceType::Deposit, ) .unwrap(); let borrow_tokens_1 = get_token_amount( - spot_market.borrow_balance, + spot_market.borrow_balance(), &spot_market, &SpotBalanceType::Borrow, ) .unwrap(); let if_tokens_1 = get_token_amount( - spot_market.revenue_pool.scaled_balance, + spot_market.revenue_pool.scaled_balance(), &spot_market, &SpotBalanceType::Deposit, ) @@ -1256,24 +1256,24 @@ fn check_fee_collection_larger_nums() { assert_eq!(spot_market.last_twap_ts, 7500); assert_eq!(spot_market.utilization_twap, 46964); - assert_eq!(spot_market.cumulative_deposit_interest, 10023340555); - assert_eq!(spot_market.cumulative_borrow_interest, 10047980763); - assert_eq!(spot_market.revenue_pool.scaled_balance, 258744322775); + assert_eq!(spot_market.cumulative_deposit_interest(), 10023340555); + assert_eq!(spot_market.cumulative_borrow_interest(), 10047980763); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 258744322775); let deposit_tokens_2 = get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), &spot_market, &SpotBalanceType::Deposit, ) .unwrap(); let borrow_tokens_2 = get_token_amount( - spot_market.borrow_balance, + spot_market.borrow_balance(), &spot_market, &SpotBalanceType::Borrow, ) .unwrap(); let if_tokens_2 = get_token_amount( - spot_market.revenue_pool.scaled_balance, + spot_market.revenue_pool.scaled_balance(), &spot_market, &SpotBalanceType::Deposit, ) @@ -1300,24 +1300,24 @@ fn check_fee_collection_larger_nums() { assert_eq!(spot_market.get_utilization().unwrap(), 961580); - assert_eq!(spot_market.cumulative_deposit_interest, 108608729074); - assert_eq!(spot_market.cumulative_borrow_interest, 212759822472); - assert_eq!(spot_market.revenue_pool.scaled_balance, 101141669831135); + assert_eq!(spot_market.cumulative_deposit_interest(), 108608729074); + assert_eq!(spot_market.cumulative_borrow_interest(), 212759822472); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 101141669831135); let deposit_tokens_3 = get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), &spot_market, &SpotBalanceType::Deposit, ) .unwrap(); let borrow_tokens_3 = get_token_amount( - spot_market.borrow_balance, + spot_market.borrow_balance(), &spot_market, &SpotBalanceType::Borrow, ) .unwrap(); let if_tokens_3 = get_token_amount( - spot_market.revenue_pool.scaled_balance, + spot_market.revenue_pool.scaled_balance(), &spot_market, &SpotBalanceType::Deposit, ) @@ -1340,7 +1340,7 @@ fn check_fee_collection_larger_nums() { // settle IF pool to 100% utilization boundary // only half of depositors available claim was settled (to protect vault) - assert_eq!(spot_market.revenue_pool.scaled_balance, 101141669831135); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 101141669831135); spot_market.insurance_fund.revenue_settle_period = 1; let settle_amount = settle_revenue_to_insurance_fund( deposit_tokens_3 as u64, @@ -1351,29 +1351,29 @@ fn check_fee_collection_larger_nums() { ) .unwrap(); assert_eq!(settle_amount, 229739282275); - assert_eq!(spot_market.insurance_fund.user_shares, 0); - assert_eq!(spot_market.insurance_fund.total_shares, 0); + assert_eq!(spot_market.insurance_fund.user_shares(), 0); + assert_eq!(spot_market.insurance_fund.total_shares(), 0); if_balance_2 += settle_amount; assert_eq!(if_balance_2, 229739282275); assert_eq!(if_tokens_3 - (settle_amount as u128), 868747539403); // w/ update interest for settle_spot_market_to_if - assert_eq!(spot_market.revenue_pool.scaled_balance, 79996002243946); + assert_eq!(spot_market.revenue_pool.scaled_balance(), 79996002243946); assert_eq!(spot_market.utilization_twap, 961580); let deposit_tokens_4 = get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), &spot_market, &SpotBalanceType::Deposit, ) .unwrap(); let borrow_tokens_4 = get_token_amount( - spot_market.borrow_balance, + spot_market.borrow_balance(), &spot_market, &SpotBalanceType::Borrow, ) .unwrap(); let if_tokens_4 = get_token_amount( - spot_market.revenue_pool.scaled_balance, + spot_market.revenue_pool.scaled_balance(), &spot_market, &SpotBalanceType::Deposit, ) @@ -1388,13 +1388,13 @@ fn test_multi_stage_borrow_rate_curve() { let spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 1_000_000 * SPOT_BALANCE_PRECISION, - borrow_balance: 0, + deposit_balance: (1_000_000 * SPOT_BALANCE_PRECISION).into(), + borrow_balance: 0.into(), deposit_token_twap: QUOTE_PRECISION_U64 / 2, optimal_utilization: SPOT_UTILIZATION_PRECISION_U32 * 70 / 100, // 70% @@ -1525,15 +1525,15 @@ fn attempt_borrow_with_massive_upnl() { // sol coin let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, - quote_asset_amount: 50 * QUOTE_PRECISION_I128, - base_asset_amount_with_amm: BASE_PRECISION_I128, + quote_asset_amount: (50 * QUOTE_PRECISION_I128).into(), + base_asset_amount_with_amm: BASE_PRECISION_I128.into(), oracle: oracle_price_key, historical_oracle_data: HistoricalOracleData::default_price(oracle_price.agg.price), ..AMM::default() @@ -1553,14 +1553,14 @@ fn attempt_borrow_with_massive_upnl() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 100_000_000 * SPOT_BALANCE_PRECISION, //$100M usdc - borrow_balance: 0, + deposit_balance: (100_000_000 * SPOT_BALANCE_PRECISION).into(), //$100M usdc + borrow_balance: 0.into(), deposit_token_twap: QUOTE_PRECISION_U64 / 2, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), status: MarketStatus::Active, @@ -1572,15 +1572,15 @@ fn attempt_borrow_with_massive_upnl() { market_index: 1, oracle_source: OracleSource::Pyth, oracle: oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 10, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, - deposit_balance: 100 * SPOT_BALANCE_PRECISION, - borrow_balance: SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), + borrow_balance: SPOT_BALANCE_PRECISION.into(), liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, status: MarketStatus::Active, @@ -1682,13 +1682,13 @@ fn check_usdc_spot_market_twap() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 100_000_000 * SPOT_BALANCE_PRECISION, //$100M usdc - borrow_balance: 0, + deposit_balance: (100_000_000 * SPOT_BALANCE_PRECISION).into(), //$100M usdc + borrow_balance: 0.into(), deposit_token_twap: QUOTE_PRECISION_U64 / 2, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), status: MarketStatus::Active, @@ -1827,13 +1827,13 @@ fn check_spot_market_max_borrow_fraction() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 100_000_000 * SPOT_BALANCE_PRECISION, //$100M usdc - borrow_balance: 0, + deposit_balance: (100_000_000 * SPOT_BALANCE_PRECISION).into(), //$100M usdc + borrow_balance: 0.into(), deposit_token_twap: QUOTE_PRECISION_U64 / 2, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), status: MarketStatus::Active, @@ -1851,7 +1851,7 @@ fn check_spot_market_max_borrow_fraction() { .validate_max_token_deposits_and_borrows(true) .is_ok()); - spot_market.borrow_balance = spot_market.deposit_balance; + spot_market.set_borrow_balance(spot_market.deposit_balance()); assert!(spot_market .validate_max_token_deposits_and_borrows(true) .is_ok()); @@ -1861,27 +1861,27 @@ fn check_spot_market_max_borrow_fraction() { assert!(spot_market .validate_max_token_deposits_and_borrows(true) .is_err()); - spot_market.borrow_balance = spot_market.deposit_balance / 100; + spot_market.set_borrow_balance(spot_market.deposit_balance() / 100); assert!(spot_market .validate_max_token_deposits_and_borrows(true) .is_err()); - spot_market.borrow_balance = spot_market.deposit_balance / (10000 - 2); // just above 10000th + spot_market.set_borrow_balance(spot_market.deposit_balance() / (10000 - 2)); // just above 10000th assert!(spot_market .validate_max_token_deposits_and_borrows(true) .is_err()); - spot_market.borrow_balance = spot_market.deposit_balance / (10000); // exactly 10000th of deposit + spot_market.set_borrow_balance(spot_market.deposit_balance() / (10000)); // exactly 10000th of deposit assert!(spot_market .validate_max_token_deposits_and_borrows(true) .is_ok()); - spot_market.borrow_balance = spot_market.deposit_balance / (10000 + 1); // < 10000th of deposit + spot_market.set_borrow_balance(spot_market.deposit_balance() / (10000 + 1)); // < 10000th of deposit assert!(spot_market .validate_max_token_deposits_and_borrows(true) .is_ok()); - spot_market.borrow_balance = spot_market.deposit_balance / 100000; // 1/10th of 10000 + spot_market.set_borrow_balance(spot_market.deposit_balance() / 100000); // 1/10th of 10000 assert!(spot_market .validate_max_token_deposits_and_borrows(true) .is_ok()); @@ -1896,13 +1896,13 @@ fn check_spot_market_min_borrow_rate() { let mut spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 100_000_000 * SPOT_BALANCE_PRECISION, //$100M usdc - borrow_balance: 0, + deposit_balance: (100_000_000 * SPOT_BALANCE_PRECISION).into(), //$100M usdc + borrow_balance: 0.into(), deposit_token_twap: QUOTE_PRECISION_U64 / 2, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), status: MarketStatus::Active, @@ -1928,28 +1928,28 @@ fn check_spot_market_min_borrow_rate() { assert_eq!(accum_interest.deposit_interest, 0); spot_market.min_borrow_rate = 1; // .5% - spot_market.borrow_balance = spot_market.deposit_balance / 100; + spot_market.set_borrow_balance(spot_market.deposit_balance() / 100); let accum_interest = calculate_accumulated_interest(&spot_market, now + 10000).unwrap(); assert_eq!(accum_interest.borrow_interest, 15903); assert_eq!(accum_interest.deposit_interest, 159); spot_market.min_borrow_rate = 10; // 5% - spot_market.borrow_balance = spot_market.deposit_balance / 100; + spot_market.set_borrow_balance(spot_market.deposit_balance() / 100); let accum_interest = calculate_accumulated_interest(&spot_market, now + 10000).unwrap(); assert_eq!(accum_interest.borrow_interest, 159025); assert_eq!(accum_interest.deposit_interest, 1590); spot_market.min_borrow_rate = 10; // 5% - spot_market.borrow_balance = spot_market.deposit_balance / 100; + spot_market.set_borrow_balance(spot_market.deposit_balance() / 100); let accum_interest = calculate_accumulated_interest(&spot_market, now + 1000000).unwrap(); assert_eq!(accum_interest.borrow_interest, 15855372); assert_eq!(accum_interest.deposit_interest, 158553); spot_market.min_borrow_rate = 200; // 100% - spot_market.borrow_balance = spot_market.deposit_balance / 100; + spot_market.set_borrow_balance(spot_market.deposit_balance() / 100); let accum_interest = calculate_accumulated_interest(&spot_market, now + 1000000).unwrap(); assert_eq!(accum_interest.borrow_interest, 317107433); diff --git a/programs/drift/src/controller/spot_position/tests.rs b/programs/drift/src/controller/spot_position/tests.rs index 9618d09969..fc2d3002a7 100644 --- a/programs/drift/src/controller/spot_position/tests.rs +++ b/programs/drift/src/controller/spot_position/tests.rs @@ -31,7 +31,7 @@ mod update_spot_position_balance { fn borrow() { let mut user = User::default(); let mut spot_market = SpotMarket { - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_quote_market() }; @@ -55,7 +55,7 @@ mod update_spot_position_balance { let mut user2 = User::default(); let mut spot_market = SpotMarket { - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_quote_market() }; @@ -111,12 +111,12 @@ mod update_spot_position_balance { }; let mut spot_market = SpotMarket { - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_quote_market() }; let mut sol_market = SpotMarket { - deposit_balance: 101 * SPOT_BALANCE_PRECISION, + deposit_balance: (101 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default_base_market() }; diff --git a/programs/drift/src/instructions/admin.rs b/programs/drift/src/instructions/admin.rs index 14de7e9216..1d67df8e8c 100644 --- a/programs/drift/src/instructions/admin.rs +++ b/programs/drift/src/instructions/admin.rs @@ -1,5 +1,5 @@ use crate::{msg, FeatureBitFlags}; -use anchor_lang::prelude::*; +use anchor_lang::prelude::{borsh::BorshDeserialize, *}; use anchor_spl::token_2022::Token2022; use anchor_spl::token_interface::{Mint, TokenAccount, TokenInterface}; use phoenix::quantities::WrapperU64; @@ -313,7 +313,7 @@ pub fn handle_initialize_spot_market( mint: ctx.accounts.spot_market_mint.key(), vault: ctx.accounts.spot_market_vault.key(), revenue_pool: PoolBalance { - scaled_balance: 0, + scaled_balance: 0.into(), market_index: spot_market_index, ..PoolBalance::default() }, // in base asset @@ -321,16 +321,16 @@ pub fn handle_initialize_spot_market( optimal_utilization, optimal_borrow_rate, max_borrow_rate, - deposit_balance: 0, - borrow_balance: 0, + deposit_balance: 0.into(), + borrow_balance: 0.into(), max_token_deposits: 0, deposit_token_twap: 0, borrow_token_twap: 0, utilization_twap: 0, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - total_social_loss: 0, - total_quote_social_loss: 0, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + total_social_loss: 0.into(), + total_quote_social_loss: 0.into(), last_interest_ts: now, last_twap_ts: now, initial_asset_weight, @@ -348,7 +348,7 @@ pub fn handle_initialize_spot_market( next_fill_record_id: 1, next_deposit_record_id: 1, spot_fee_pool: PoolBalance::default(), // in quote asset - total_spot_fee: 0, + total_spot_fee: 0.into(), orders_enabled: spot_market_index != 0, paused_operations: 0, if_paused_operations: 0, @@ -1007,16 +1007,16 @@ pub fn handle_initialize_perp_market( amm: AMM { oracle: *ctx.accounts.oracle.key, oracle_source, - base_asset_reserve: amm_base_asset_reserve, - quote_asset_reserve: amm_quote_asset_reserve, - terminal_quote_asset_reserve: amm_quote_asset_reserve, - ask_base_asset_reserve: amm_base_asset_reserve, - ask_quote_asset_reserve: amm_quote_asset_reserve, - bid_base_asset_reserve: amm_base_asset_reserve, - bid_quote_asset_reserve: amm_quote_asset_reserve, - cumulative_funding_rate_long: 0, - cumulative_funding_rate_short: 0, - total_social_loss: 0, + base_asset_reserve: amm_base_asset_reserve.into(), + quote_asset_reserve: amm_quote_asset_reserve.into(), + terminal_quote_asset_reserve: amm_quote_asset_reserve.into(), + ask_base_asset_reserve: amm_base_asset_reserve.into(), + ask_quote_asset_reserve: amm_quote_asset_reserve.into(), + bid_base_asset_reserve: amm_base_asset_reserve.into(), + bid_quote_asset_reserve: amm_quote_asset_reserve.into(), + cumulative_funding_rate_long: 0.into(), + cumulative_funding_rate_short: 0.into(), + total_social_loss: 0.into(), last_funding_rate: 0, last_funding_rate_long: 0, last_funding_rate_short: 0, @@ -1026,17 +1026,17 @@ pub fn handle_initialize_perp_market( last_mark_price_twap: init_reserve_price, last_mark_price_twap_5min: init_reserve_price, last_mark_price_twap_ts: now, - sqrt_k: amm_base_asset_reserve, - concentration_coef, - min_base_asset_reserve, - max_base_asset_reserve, - peg_multiplier: amm_peg_multiplier, - total_fee: 0, - total_fee_withdrawn: 0, - total_fee_minus_distributions: 0, - total_mm_fee: 0, - total_exchange_fee: 0, - total_liquidation_fee: 0, + sqrt_k: amm_base_asset_reserve.into(), + concentration_coef: concentration_coef.into(), + min_base_asset_reserve: min_base_asset_reserve.into(), + max_base_asset_reserve: max_base_asset_reserve.into(), + peg_multiplier: amm_peg_multiplier.into(), + total_fee: 0.into(), + total_fee_withdrawn: 0.into(), + total_fee_minus_distributions: 0.into(), + total_mm_fee: 0.into(), + total_exchange_fee: 0.into(), + total_liquidation_fee: 0.into(), net_revenue_since_last_funding: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price: oracle_price, @@ -1061,15 +1061,15 @@ pub fn handle_initialize_perp_market( max_spread, last_bid_price_twap: init_reserve_price, last_ask_price_twap: init_reserve_price, - base_asset_amount_with_amm: 0, - base_asset_amount_long: 0, - base_asset_amount_short: 0, - quote_asset_amount: 0, - quote_entry_amount_long: 0, - quote_entry_amount_short: 0, - quote_break_even_amount_long: 0, - quote_break_even_amount_short: 0, - max_open_interest, + base_asset_amount_with_amm: 0.into(), + base_asset_amount_long: 0.into(), + base_asset_amount_short: 0.into(), + quote_asset_amount: 0.into(), + quote_entry_amount_long: 0.into(), + quote_entry_amount_short: 0.into(), + quote_break_even_amount_long: 0.into(), + quote_break_even_amount_short: 0.into(), + max_open_interest: max_open_interest.into(), mark_std: 0, oracle_std: 0, volume_24h: 0, @@ -1079,13 +1079,13 @@ pub fn handle_initialize_perp_market( last_trade_ts: now, curve_update_intensity, fee_pool: PoolBalance::default(), - base_asset_amount_per_lp: 0, - quote_asset_amount_per_lp: 0, + base_asset_amount_per_lp: 0.into(), + quote_asset_amount_per_lp: 0.into(), last_update_slot: clock_slot, // lp stuff - base_asset_amount_with_unsettled_lp: 0, - user_lp_shares: 0, + base_asset_amount_with_unsettled_lp: 0.into(), + user_lp_shares: 0.into(), amm_jit_intensity, last_oracle_valid: false, @@ -1250,16 +1250,16 @@ pub fn handle_delete_initialized_spot_market( "spot_market.status != Initialized", )?; validate!( - spot_market.deposit_balance == 0, + spot_market.deposit_balance() == 0, ErrorCode::InvalidMarketAccountforDeletion, "spot_market.number_of_users={} != 0", - spot_market.deposit_balance, + spot_market.deposit_balance(), )?; validate!( - spot_market.borrow_balance == 0, + spot_market.borrow_balance() == 0, ErrorCode::InvalidMarketAccountforDeletion, "spot_market.borrow_balance={} != 0", - spot_market.borrow_balance, + spot_market.borrow_balance(), )?; validate!( spot_market.market_index == market_index, @@ -1557,20 +1557,20 @@ pub fn handle_move_amm_price( perp_market.market_index ); - let base_asset_reserve_before = perp_market.amm.base_asset_reserve; - let quote_asset_reserve_before = perp_market.amm.quote_asset_reserve; - let sqrt_k_before = perp_market.amm.sqrt_k; - let max_base_asset_reserve_before = perp_market.amm.max_base_asset_reserve; - let min_base_asset_reserve_before = perp_market.amm.min_base_asset_reserve; + let base_asset_reserve_before = perp_market.amm.base_asset_reserve(); + let quote_asset_reserve_before = perp_market.amm.quote_asset_reserve(); + let sqrt_k_before = perp_market.amm.sqrt_k(); + let max_base_asset_reserve_before = perp_market.amm.max_base_asset_reserve(); + let min_base_asset_reserve_before = perp_market.amm.min_base_asset_reserve(); controller::amm::move_price(perp_market, base_asset_reserve, quote_asset_reserve, sqrt_k)?; validate_perp_market(perp_market)?; - let base_asset_reserve_after = perp_market.amm.base_asset_reserve; - let quote_asset_reserve_after = perp_market.amm.quote_asset_reserve; - let sqrt_k_after = perp_market.amm.sqrt_k; - let max_base_asset_reserve_after = perp_market.amm.max_base_asset_reserve; - let min_base_asset_reserve_after = perp_market.amm.min_base_asset_reserve; + let base_asset_reserve_after = perp_market.amm.base_asset_reserve(); + let quote_asset_reserve_after = perp_market.amm.quote_asset_reserve(); + let sqrt_k_after = perp_market.amm.sqrt_k(); + let max_base_asset_reserve_after = perp_market.amm.max_base_asset_reserve(); + let min_base_asset_reserve_after = perp_market.amm.min_base_asset_reserve(); msg!( "base_asset_reserve {} -> {}", @@ -1616,22 +1616,22 @@ pub fn handle_recenter_perp_market_amm( perp_market.market_index ); - let base_asset_reserve_before = perp_market.amm.base_asset_reserve; - let quote_asset_reserve_before = perp_market.amm.quote_asset_reserve; - let sqrt_k_before = perp_market.amm.sqrt_k; - let peg_multiplier_before = perp_market.amm.peg_multiplier; - let max_base_asset_reserve_before = perp_market.amm.max_base_asset_reserve; - let min_base_asset_reserve_before = perp_market.amm.min_base_asset_reserve; + let base_asset_reserve_before = perp_market.amm.base_asset_reserve(); + let quote_asset_reserve_before = perp_market.amm.quote_asset_reserve(); + let sqrt_k_before = perp_market.amm.sqrt_k(); + let peg_multiplier_before = perp_market.amm.peg_multiplier(); + let max_base_asset_reserve_before = perp_market.amm.max_base_asset_reserve(); + let min_base_asset_reserve_before = perp_market.amm.min_base_asset_reserve(); controller::amm::recenter_perp_market_amm(perp_market, peg_multiplier, sqrt_k)?; validate_perp_market(perp_market)?; - let base_asset_reserve_after = perp_market.amm.base_asset_reserve; - let quote_asset_reserve_after = perp_market.amm.quote_asset_reserve; - let sqrt_k_after = perp_market.amm.sqrt_k; - let peg_multiplier_after = perp_market.amm.peg_multiplier; - let max_base_asset_reserve_after = perp_market.amm.max_base_asset_reserve; - let min_base_asset_reserve_after = perp_market.amm.min_base_asset_reserve; + let base_asset_reserve_after = perp_market.amm.base_asset_reserve(); + let quote_asset_reserve_after = perp_market.amm.quote_asset_reserve(); + let sqrt_k_after = perp_market.amm.sqrt_k(); + let peg_multiplier_after = perp_market.amm.peg_multiplier(); + let max_base_asset_reserve_after = perp_market.amm.max_base_asset_reserve(); + let min_base_asset_reserve_after = perp_market.amm.min_base_asset_reserve(); msg!( "base_asset_reserve {} -> {}", @@ -1690,12 +1690,12 @@ pub fn handle_recenter_perp_market_amm_crank( perp_market.market_index ); - let base_asset_reserve_before = perp_market.amm.base_asset_reserve; - let quote_asset_reserve_before = perp_market.amm.quote_asset_reserve; - let sqrt_k_before = perp_market.amm.sqrt_k; - let peg_multiplier_before = perp_market.amm.peg_multiplier; - let max_base_asset_reserve_before = perp_market.amm.max_base_asset_reserve; - let min_base_asset_reserve_before = perp_market.amm.min_base_asset_reserve; + let base_asset_reserve_before = perp_market.amm.base_asset_reserve(); + let quote_asset_reserve_before = perp_market.amm.quote_asset_reserve(); + let sqrt_k_before = perp_market.amm.sqrt_k(); + let peg_multiplier_before = perp_market.amm.peg_multiplier(); + let max_base_asset_reserve_before = perp_market.amm.max_base_asset_reserve(); + let min_base_asset_reserve_before = perp_market.amm.min_base_asset_reserve(); let mut sqrt_k = sqrt_k_before; let peg_multiplier: u128 = oracle_price.cast()?; @@ -1716,12 +1716,12 @@ pub fn handle_recenter_perp_market_amm_crank( controller::amm::recenter_perp_market_amm(perp_market, peg_multiplier, sqrt_k)?; validate_perp_market(perp_market)?; - let base_asset_reserve_after = perp_market.amm.base_asset_reserve; - let quote_asset_reserve_after = perp_market.amm.quote_asset_reserve; - let sqrt_k_after = perp_market.amm.sqrt_k; - let peg_multiplier_after = perp_market.amm.peg_multiplier; - let max_base_asset_reserve_after = perp_market.amm.max_base_asset_reserve; - let min_base_asset_reserve_after = perp_market.amm.min_base_asset_reserve; + let base_asset_reserve_after = perp_market.amm.base_asset_reserve(); + let quote_asset_reserve_after = perp_market.amm.quote_asset_reserve(); + let sqrt_k_after = perp_market.amm.sqrt_k(); + let peg_multiplier_after = perp_market.amm.peg_multiplier(); + let max_base_asset_reserve_after = perp_market.amm.max_base_asset_reserve(); + let min_base_asset_reserve_after = perp_market.amm.min_base_asset_reserve(); msg!( "base_asset_reserve {} -> {}", @@ -1836,28 +1836,39 @@ pub fn handle_update_perp_market_amm_summary_stats( msg!( "total_fee_minus_distributions: {:?} -> {:?}", - perp_market.amm.total_fee_minus_distributions, + perp_market.amm.total_fee_minus_distributions(), new_total_fee_minus_distributions, ); let fee_difference = new_total_fee_minus_distributions - .safe_sub(perp_market.amm.total_fee_minus_distributions)?; + .safe_sub(perp_market.amm.total_fee_minus_distributions())?; msg!( "perp_market.amm.total_fee: {} -> {}", - perp_market.amm.total_fee, - perp_market.amm.total_fee.saturating_add(fee_difference) + perp_market.amm.total_fee(), + perp_market.amm.total_fee().saturating_add(fee_difference) ); msg!( "perp_market.amm.total_mm_fee: {} -> {}", - perp_market.amm.total_mm_fee, - perp_market.amm.total_mm_fee.saturating_add(fee_difference) + perp_market.amm.total_mm_fee(), + perp_market + .amm + .total_mm_fee() + .saturating_add(fee_difference) ); - perp_market.amm.total_fee = perp_market.amm.total_fee.saturating_add(fee_difference); - perp_market.amm.total_mm_fee = perp_market.amm.total_mm_fee.saturating_add(fee_difference); - perp_market.amm.total_fee_minus_distributions = new_total_fee_minus_distributions; + let total_fee = perp_market.amm.total_fee(); + perp_market + .amm + .set_total_fee(total_fee.saturating_add(fee_difference)); + let total_mm_fee = perp_market.amm.total_mm_fee(); + perp_market + .amm + .set_total_mm_fee(total_mm_fee.saturating_add(fee_difference)); + perp_market + .amm + .set_total_fee_minus_distributions(new_total_fee_minus_distributions); } validate_perp_market(perp_market)?; @@ -1903,13 +1914,13 @@ pub fn handle_settle_expired_market_pools_to_revenue_pool( )?; validate!( - perp_market.amm.base_asset_amount_long == 0 - && perp_market.amm.base_asset_amount_short == 0 + perp_market.amm.base_asset_amount_long() == 0 + && perp_market.amm.base_asset_amount_short() == 0 && perp_market.number_of_users_with_base == 0, ErrorCode::DefaultError, "outstanding base_asset_amounts must be balanced {} {} {}", - perp_market.amm.base_asset_amount_long, - perp_market.amm.base_asset_amount_short, + perp_market.amm.base_asset_amount_long(), + perp_market.amm.base_asset_amount_short(), perp_market.number_of_users_with_base )?; @@ -1946,12 +1957,12 @@ pub fn handle_settle_expired_market_pools_to_revenue_pool( )?; let fee_pool_token_amount = get_token_amount( - perp_market.amm.fee_pool.scaled_balance, + perp_market.amm.fee_pool.scaled_balance(), spot_market, &SpotBalanceType::Deposit, )?; let pnl_pool_token_amount = get_token_amount( - perp_market.pnl_pool.scaled_balance, + perp_market.pnl_pool.scaled_balance(), spot_market, &SpotBalanceType::Deposit, )?; @@ -2006,17 +2017,17 @@ pub fn handle_deposit_into_perp_market_fee_pool<'c: 'info, 'info>( msg!( "perp_market.amm.total_fee_minus_distributions: {:?} -> {:?}", - perp_market.amm.total_fee_minus_distributions, + perp_market.amm.total_fee_minus_distributions(), perp_market .amm - .total_fee_minus_distributions + .total_fee_minus_distributions() .safe_add(amount.cast()?)?, ); - perp_market.amm.total_fee_minus_distributions = perp_market + let total_fee_minus_distributions = perp_market.amm.total_fee_minus_distributions(); + perp_market .amm - .total_fee_minus_distributions - .safe_add(amount.cast()?)?; + .set_total_fee_minus_distributions(total_fee_minus_distributions.safe_add(amount.cast()?)?); let quote_spot_market = &mut load_mut!(ctx.accounts.quote_spot_market)?; @@ -2121,11 +2132,11 @@ pub fn handle_deposit_into_spot_market_vault<'c: 'info, 'info>( let token_precision = spot_market.get_precision(); - let cumulative_deposit_interest_before = spot_market.cumulative_deposit_interest; + let cumulative_deposit_interest_before = spot_market.cumulative_deposit_interest(); let cumulative_deposit_interest_after = deposit_token_amount_after .safe_mul(SPOT_CUMULATIVE_INTEREST_PRECISION)? - .safe_div(spot_market.deposit_balance)? + .safe_div(spot_market.deposit_balance())? .safe_mul(SPOT_BALANCE_PRECISION)? .safe_div(token_precision.cast()?)?; @@ -2137,7 +2148,7 @@ pub fn handle_deposit_into_spot_market_vault<'c: 'info, 'info>( cumulative_deposit_interest_before )?; - spot_market.cumulative_deposit_interest = cumulative_deposit_interest_after; + spot_market.set_cumulative_deposit_interest(cumulative_deposit_interest_after); controller::token::receive( &ctx.accounts.token_program, @@ -2161,7 +2172,7 @@ pub fn handle_deposit_into_spot_market_vault<'c: 'info, 'info>( emit!(SpotMarketVaultDepositRecord { ts: Clock::get()?.unix_timestamp, market_index: spot_market.market_index, - deposit_balance: spot_market.deposit_balance, + deposit_balance: spot_market.deposit_balance(), cumulative_deposit_interest_before, cumulative_deposit_interest_after, deposit_token_amount_before: deposit_token_amount_before.cast()?, @@ -2192,10 +2203,10 @@ pub fn handle_repeg_amm_curve(ctx: Context, new_peg_candidate: u128) .. } = get_oracle_price(&perp_market.amm.oracle_source, price_oracle, clock.slot)?; - let peg_multiplier_before = perp_market.amm.peg_multiplier; - let base_asset_reserve_before = perp_market.amm.base_asset_reserve; - let quote_asset_reserve_before = perp_market.amm.quote_asset_reserve; - let sqrt_k_before = perp_market.amm.sqrt_k; + let peg_multiplier_before = perp_market.amm.peg_multiplier(); + let base_asset_reserve_before = perp_market.amm.base_asset_reserve(); + let quote_asset_reserve_before = perp_market.amm.quote_asset_reserve(); + let sqrt_k_before = perp_market.amm.sqrt_k(); let oracle_validity_rails = &ctx.accounts.state.oracle_guard_rails; @@ -2207,10 +2218,10 @@ pub fn handle_repeg_amm_curve(ctx: Context, new_peg_candidate: u128) oracle_validity_rails, )?; - let peg_multiplier_after = perp_market.amm.peg_multiplier; - let base_asset_reserve_after = perp_market.amm.base_asset_reserve; - let quote_asset_reserve_after = perp_market.amm.quote_asset_reserve; - let sqrt_k_after = perp_market.amm.sqrt_k; + let peg_multiplier_after = perp_market.amm.peg_multiplier(); + let base_asset_reserve_after = perp_market.amm.base_asset_reserve(); + let quote_asset_reserve_after = perp_market.amm.quote_asset_reserve(); + let sqrt_k_after = perp_market.amm.sqrt_k(); msg!( "perp_market.amm.peg_multiplier {} -> {}", @@ -2248,12 +2259,12 @@ pub fn handle_repeg_amm_curve(ctx: Context, new_peg_candidate: u128) base_asset_reserve_after, quote_asset_reserve_after, sqrt_k_after, - base_asset_amount_long: perp_market.amm.base_asset_amount_long.unsigned_abs(), - base_asset_amount_short: perp_market.amm.base_asset_amount_short.unsigned_abs(), - base_asset_amount_with_amm: perp_market.amm.base_asset_amount_with_amm, + base_asset_amount_long: perp_market.amm.base_asset_amount_long().unsigned_abs(), + base_asset_amount_short: perp_market.amm.base_asset_amount_short().unsigned_abs(), + base_asset_amount_with_amm: perp_market.amm.base_asset_amount_with_amm(), number_of_users: perp_market.number_of_users, - total_fee: perp_market.amm.total_fee, - total_fee_minus_distributions: perp_market.amm.total_fee_minus_distributions, + total_fee: perp_market.amm.total_fee(), + total_fee_minus_distributions: perp_market.amm.total_fee_minus_distributions(), adjustment_cost, oracle_price, fill_record: 0, @@ -2372,23 +2383,23 @@ pub fn handle_update_k(ctx: Context, sqrt_k: u128) -> Result<()> { let perp_market = &mut load_mut!(ctx.accounts.perp_market)?; msg!("updating k for perp market {}", perp_market.market_index); - let base_asset_amount_long = perp_market.amm.base_asset_amount_long.unsigned_abs(); - let base_asset_amount_short = perp_market.amm.base_asset_amount_short.unsigned_abs(); - let base_asset_amount_with_amm = perp_market.amm.base_asset_amount_with_amm; + let base_asset_amount_long = perp_market.amm.base_asset_amount_long().unsigned_abs(); + let base_asset_amount_short = perp_market.amm.base_asset_amount_short().unsigned_abs(); + let base_asset_amount_with_amm = perp_market.amm.base_asset_amount_with_amm(); let number_of_users = perp_market.number_of_users_with_base; let price_before = math::amm::calculate_price( - perp_market.amm.quote_asset_reserve, - perp_market.amm.base_asset_reserve, - perp_market.amm.peg_multiplier, + perp_market.amm.quote_asset_reserve(), + perp_market.amm.base_asset_reserve(), + perp_market.amm.peg_multiplier(), )?; - let peg_multiplier_before = perp_market.amm.peg_multiplier; - let base_asset_reserve_before = perp_market.amm.base_asset_reserve; - let quote_asset_reserve_before = perp_market.amm.quote_asset_reserve; - let sqrt_k_before = perp_market.amm.sqrt_k; + let peg_multiplier_before = perp_market.amm.peg_multiplier(); + let base_asset_reserve_before = perp_market.amm.base_asset_reserve(); + let quote_asset_reserve_before = perp_market.amm.quote_asset_reserve(); + let sqrt_k_before = perp_market.amm.sqrt_k(); - let k_increasing = sqrt_k > perp_market.amm.sqrt_k; + let k_increasing = sqrt_k > perp_market.amm.sqrt_k(); let new_sqrt_k_u192 = bn::U192::from(sqrt_k); @@ -2415,9 +2426,9 @@ pub fn handle_update_k(ctx: Context, sqrt_k: u128) -> Result<()> { if adjustment_cost > 0 { let max_cost = perp_market .amm - .total_fee_minus_distributions + .total_fee_minus_distributions() .safe_sub(get_total_fee_lower_bound(perp_market)?.cast()?)? - .safe_sub(perp_market.amm.total_fee_withdrawn.cast()?)?; + .safe_sub(perp_market.amm.total_fee_withdrawn().cast()?)?; validate!( adjustment_cost <= max_cost, @@ -2429,16 +2440,16 @@ pub fn handle_update_k(ctx: Context, sqrt_k: u128) -> Result<()> { } validate!( - !k_increasing || perp_market.amm.sqrt_k < MAX_SQRT_K, + !k_increasing || perp_market.amm.sqrt_k() < MAX_SQRT_K, ErrorCode::InvalidUpdateK, "cannot increase sqrt_k={} past MAX_SQRT_K", - perp_market.amm.sqrt_k + perp_market.amm.sqrt_k() )?; - perp_market.amm.total_fee_minus_distributions = perp_market - .amm - .total_fee_minus_distributions - .safe_sub(adjustment_cost)?; + let total_fee_minus_distributions = perp_market.amm.total_fee_minus_distributions(); + perp_market.amm.set_total_fee_minus_distributions( + total_fee_minus_distributions.safe_sub(adjustment_cost)?, + ); perp_market.amm.net_revenue_since_last_funding = perp_market .amm @@ -2448,9 +2459,9 @@ pub fn handle_update_k(ctx: Context, sqrt_k: u128) -> Result<()> { let amm = &perp_market.amm; let price_after = math::amm::calculate_price( - amm.quote_asset_reserve, - amm.base_asset_reserve, - amm.peg_multiplier, + amm.quote_asset_reserve(), + amm.base_asset_reserve(), + amm.peg_multiplier(), )?; let price_change_too_large = price_before @@ -2469,24 +2480,29 @@ pub fn handle_update_k(ctx: Context, sqrt_k: u128) -> Result<()> { return Err(ErrorCode::InvalidUpdateK.into()); } - let k_sqrt_check = bn::U192::from(amm.base_asset_reserve) - .safe_mul(bn::U192::from(amm.quote_asset_reserve))? + let k_sqrt_check = bn::U192::from(amm.base_asset_reserve()) + .safe_mul(bn::U192::from(amm.quote_asset_reserve()))? .integer_sqrt() .try_to_u128()?; let k_err = k_sqrt_check .cast::()? - .safe_sub(amm.sqrt_k.cast::()?)?; + .safe_sub(amm.sqrt_k().cast::()?)?; if k_err.unsigned_abs() > 100 { - msg!("k_err={:?}, {:?} != {:?}", k_err, k_sqrt_check, amm.sqrt_k); + msg!( + "k_err={:?}, {:?} != {:?}", + k_err, + k_sqrt_check, + amm.sqrt_k() + ); return Err(ErrorCode::InvalidUpdateK.into()); } - let peg_multiplier_after = amm.peg_multiplier; - let base_asset_reserve_after = amm.base_asset_reserve; - let quote_asset_reserve_after = amm.quote_asset_reserve; - let sqrt_k_after = amm.sqrt_k; + let peg_multiplier_after = amm.peg_multiplier(); + let base_asset_reserve_after = amm.base_asset_reserve(); + let quote_asset_reserve_after = amm.quote_asset_reserve(); + let sqrt_k_after = amm.sqrt_k(); msg!( "perp_market.amm.peg_multiplier {} -> {}", @@ -2512,8 +2528,8 @@ pub fn handle_update_k(ctx: Context, sqrt_k: u128) -> Result<()> { sqrt_k_after ); - let total_fee = amm.total_fee; - let total_fee_minus_distributions = amm.total_fee_minus_distributions; + let total_fee = amm.total_fee(); + let total_fee_minus_distributions = amm.total_fee_minus_distributions(); let OraclePriceData { price: oracle_price, @@ -3497,9 +3513,9 @@ pub fn handle_update_perp_market_concentration_coef( let perp_market = &mut load_mut!(ctx.accounts.perp_market)?; msg!("perp market {}", perp_market.market_index); - let prev_concentration_coef = perp_market.amm.concentration_coef; + let prev_concentration_coef = perp_market.amm.concentration_coef(); controller::amm::update_concentration_coef(perp_market, concentration_scale)?; - let new_concentration_coef = perp_market.amm.concentration_coef; + let new_concentration_coef = perp_market.amm.concentration_coef(); msg!( "perp_market.amm.concentration_coef: {} -> {}", @@ -3560,10 +3576,10 @@ pub fn handle_update_perp_market_reference_price_offset_deadband_pct( let liquidity_ratio = crate::math::amm_spread::calculate_inventory_liquidity_ratio_for_reference_price_offset( - perp_market.amm.base_asset_amount_with_amm, - perp_market.amm.base_asset_reserve, - perp_market.amm.min_base_asset_reserve, - perp_market.amm.max_base_asset_reserve, + perp_market.amm.base_asset_amount_with_amm(), + perp_market.amm.base_asset_reserve(), + perp_market.amm.min_base_asset_reserve(), + perp_market.amm.max_base_asset_reserve(), )?; let signed_liquidity_ratio = liquidity_ratio.safe_mul( @@ -4113,11 +4129,11 @@ pub fn handle_update_perp_market_max_open_interest( msg!( "perp_market.amm.max_open_interest: {:?} -> {:?}", - perp_market.amm.max_open_interest, + perp_market.amm.max_open_interest(), max_open_interest ); - perp_market.amm.max_open_interest = max_open_interest; + perp_market.amm.set_max_open_interest(max_open_interest); Ok(()) } @@ -4584,10 +4600,10 @@ pub fn handle_update_protocol_if_shares_transfer_config( if let Some(max_transfer_per_epoch) = max_transfer_per_epoch { msg!( "max_transfer_per_epoch: {:?} -> {:?}", - config.max_transfer_per_epoch, + config.max_transfer_per_epoch(), max_transfer_per_epoch ); - config.max_transfer_per_epoch = max_transfer_per_epoch; + config.set_max_transfer_per_epoch(max_transfer_per_epoch); } else { msg!("max_transfer_per_epoch: unchanged"); } @@ -4951,10 +4967,10 @@ pub fn handle_admin_deposit<'c: 'info, 'info>( direction: DepositDirection::Deposit, amount, oracle_price, - market_deposit_balance: spot_market.deposit_balance, - market_withdraw_balance: spot_market.borrow_balance, - market_cumulative_deposit_interest: spot_market.cumulative_deposit_interest, - market_cumulative_borrow_interest: spot_market.cumulative_borrow_interest, + market_deposit_balance: spot_market.deposit_balance(), + market_withdraw_balance: spot_market.borrow_balance(), + market_cumulative_deposit_interest: spot_market.cumulative_deposit_interest(), + market_cumulative_borrow_interest: spot_market.cumulative_borrow_interest(), total_deposits_after, total_withdraws_after, market_index, diff --git a/programs/drift/src/instructions/if_staker.rs b/programs/drift/src/instructions/if_staker.rs index bdc506abe8..2b706de701 100644 --- a/programs/drift/src/instructions/if_staker.rs +++ b/programs/drift/src/instructions/if_staker.rs @@ -95,7 +95,7 @@ pub fn handle_add_insurance_fund_stake<'c: 'info, 'info>( )?; validate!( - insurance_fund_stake.last_withdraw_request_shares == 0 + insurance_fund_stake.last_withdraw_request_shares() == 0 && insurance_fund_stake.last_withdraw_request_value == 0, ErrorCode::IFWithdrawRequestInProgress, "withdraw request in progress" @@ -187,14 +187,14 @@ pub fn handle_request_remove_insurance_fund_stake( )?; validate!( - insurance_fund_stake.last_withdraw_request_shares == 0, + insurance_fund_stake.last_withdraw_request_shares() == 0, ErrorCode::IFWithdrawRequestInProgress, "Withdraw request is already in progress" )?; let n_shares = math::insurance::vault_amount_to_if_shares( amount, - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.total_shares(), ctx.accounts.insurance_fund_vault.amount, )?; @@ -236,7 +236,7 @@ pub fn handle_cancel_request_remove_insurance_fund_stake( )?; validate!( - insurance_fund_stake.last_withdraw_request_shares != 0, + insurance_fund_stake.last_withdraw_request_shares() != 0, ErrorCode::NoIFWithdrawRequestInProgress, "No withdraw request in progress" )?; @@ -343,7 +343,8 @@ pub fn handle_transfer_protocol_if_shares( transfer_config.update_epoch(now)?; transfer_config.validate_transfer(shares)?; - transfer_config.current_epoch_transfer += shares; + let current_epoch_transfer = transfer_config.current_epoch_transfer(); + transfer_config.set_current_epoch_transfer(current_epoch_transfer.safe_add(shares)?); let mut if_stake = ctx.accounts.insurance_fund_stake.load_mut()?; let mut user_stats = ctx.accounts.user_stats.load_mut()?; @@ -862,7 +863,7 @@ pub fn handle_deposit_into_insurance_fund_stake<'c: 'info, 'info>( )?; validate!( - insurance_fund_stake.last_withdraw_request_shares == 0 + insurance_fund_stake.last_withdraw_request_shares() == 0 && insurance_fund_stake.last_withdraw_request_value == 0, ErrorCode::IFWithdrawRequestInProgress, "withdraw request in progress" diff --git a/programs/drift/src/instructions/keeper.rs b/programs/drift/src/instructions/keeper.rs index 21d3e871e3..fb11cf1c43 100644 --- a/programs/drift/src/instructions/keeper.rs +++ b/programs/drift/src/instructions/keeper.rs @@ -3411,12 +3411,12 @@ pub fn handle_settle_perp_to_lp_pool<'c: 'info, 'info>( quote_owed_from_lp: cached_info.quote_owed_from_lp_pool, quote_constituent_token_balance: quote_constituent.vault_token_balance, fee_pool_balance: get_token_amount( - perp_market.amm.fee_pool.scaled_balance, + perp_market.amm.fee_pool.scaled_balance(), quote_market, &SpotBalanceType::Deposit, )?, pnl_pool_balance: get_token_amount( - perp_market.pnl_pool.scaled_balance, + perp_market.pnl_pool.scaled_balance(), quote_market, &SpotBalanceType::Deposit, )?, @@ -3503,7 +3503,7 @@ pub fn handle_settle_perp_to_lp_pool<'c: 'info, 'info>( .last_exchange_fees .safe_sub(cached_info.last_settle_amm_ex_fees)? .cast::()?, - lp_aum: lp_pool.last_aum, + lp_aum: lp_pool.last_aum(), lp_price: lp_pool.get_price(lp_pool.token_supply)?, lp_pool: lp_pool_key, }); @@ -3525,14 +3525,19 @@ pub fn handle_settle_perp_to_lp_pool<'c: 'info, 'info>( // Update LP pool stats match settlement_result.direction { SettlementDirection::FromLpPool => { - lp_pool.cumulative_quote_sent_to_perp_markets = lp_pool - .cumulative_quote_sent_to_perp_markets - .saturating_add(settlement_result.amount_transferred as u128); + let cumulative_quote_sent = lp_pool.cumulative_quote_sent_to_perp_markets(); + lp_pool.set_cumulative_quote_sent_to_perp_markets( + cumulative_quote_sent + .saturating_add(settlement_result.amount_transferred as u128), + ); } SettlementDirection::ToLpPool => { - lp_pool.cumulative_quote_received_from_perp_markets = lp_pool - .cumulative_quote_received_from_perp_markets - .saturating_add(settlement_result.amount_transferred as u128); + let cumulative_quote_received = + lp_pool.cumulative_quote_received_from_perp_markets(); + lp_pool.set_cumulative_quote_received_from_perp_markets( + cumulative_quote_received + .saturating_add(settlement_result.amount_transferred as u128), + ); } SettlementDirection::None => {} } diff --git a/programs/drift/src/instructions/lp_admin.rs b/programs/drift/src/instructions/lp_admin.rs index 0e5225d832..428661c9a4 100644 --- a/programs/drift/src/instructions/lp_admin.rs +++ b/programs/drift/src/instructions/lp_admin.rs @@ -17,7 +17,7 @@ use crate::state::spot_market::SpotMarket; use crate::state::state::State; use crate::validate; use crate::{controller, load_mut}; -use anchor_lang::prelude::*; +use anchor_lang::prelude::{borsh::BorshDeserialize, *}; use anchor_lang::Discriminator; use anchor_spl::associated_token::AssociatedToken; use anchor_spl::token::Token; @@ -61,20 +61,20 @@ pub fn handle_initialize_lp_pool( constituent_target_base: ctx.accounts.constituent_target_base.key(), constituent_correlations: ctx.accounts.constituent_correlations.key(), constituents: 0, - max_aum, - last_aum: 0, + max_aum: max_aum.into(), + last_aum: 0.into(), last_aum_slot: 0, max_settle_quote_amount: max_settle_quote_amount_per_market, _padding: 0, - total_mint_redeem_fees_paid: 0, + total_mint_redeem_fees_paid: 0.into(), bump: ctx.bumps.lp_pool, min_mint_fee, token_supply: 0, mint_redeem_id: 1, settle_id: 1, quote_consituent_index: 0, - cumulative_quote_sent_to_perp_markets: 0, - cumulative_quote_received_from_perp_markets: 0, + cumulative_quote_sent_to_perp_markets: 0.into(), + cumulative_quote_received_from_perp_markets: 0.into(), gamma_execution: 2, volatility: 4, xi: 2, @@ -431,12 +431,12 @@ pub fn handle_update_lp_pool_params<'info>( if let Some(max_aum) = lp_pool_params.max_aum { validate!( - max_aum >= lp_pool.max_aum, + max_aum >= lp_pool.max_aum(), ErrorCode::DefaultError, "new max_aum must be greater than or equal to current max_aum" )?; - msg!("max_aum: {:?} -> {:?}", lp_pool.max_aum, max_aum); - lp_pool.max_aum = max_aum; + msg!("max_aum: {:?} -> {:?}", lp_pool.max_aum(), max_aum); + lp_pool.set_max_aum(max_aum); } Ok(()) diff --git a/programs/drift/src/instructions/lp_pool.rs b/programs/drift/src/instructions/lp_pool.rs index 3108af7ca9..287946ee0e 100644 --- a/programs/drift/src/instructions/lp_pool.rs +++ b/programs/drift/src/instructions/lp_pool.rs @@ -376,13 +376,13 @@ pub fn handle_lp_pool_swap<'c: 'info, 'info>( in_constituent.constituent_index, &in_spot_market, in_oracle.price, - lp_pool.last_aum, + lp_pool.last_aum(), )?; let out_target_weight = constituent_target_base.get_target_weight( out_constituent.constituent_index, &out_spot_market, out_oracle.price, - lp_pool.last_aum, + lp_pool.last_aum(), )?; let in_target_datum = constituent_target_base.get(in_constituent.constituent_index as u32); let in_target_position_slot_delay = slot.saturating_sub(in_target_datum.last_position_slot); @@ -463,20 +463,20 @@ pub fn handle_lp_pool_swap<'c: 'info, 'info>( in_constituent_index: in_constituent.constituent_index, out_oracle_price: out_oracle.price, in_oracle_price: in_oracle.price, - last_aum: lp_pool.last_aum, + last_aum: lp_pool.last_aum(), last_aum_slot: lp_pool.last_aum_slot, in_market_current_weight: in_constituent.get_weight( in_oracle.price, &in_spot_market, 0, - lp_pool.last_aum, + lp_pool.last_aum(), )?, in_market_target_weight: in_target_weight, out_market_current_weight: out_constituent.get_weight( out_oracle.price, &out_spot_market, 0, - lp_pool.last_aum, + lp_pool.last_aum(), )?, out_market_target_weight: out_target_weight, in_swap_id, @@ -722,15 +722,15 @@ pub fn handle_lp_pool_add_liquidity<'c: 'info, 'info>( update_spot_market_cumulative_interest(&mut in_spot_market, Some(&in_oracle), now)?; - msg!("aum: {}", lp_pool.last_aum); - let in_target_weight = if lp_pool.last_aum == 0 { + msg!("aum: {}", lp_pool.last_aum()); + let in_target_weight = if lp_pool.last_aum() == 0 { PERCENTAGE_PRECISION_I64 // 100% weight if no aum } else { constituent_target_base.get_target_weight( in_constituent.constituent_index, &in_spot_market, in_oracle.price, - lp_pool.last_aum, // TODO: add in_amount * in_oracle to est post add_liquidity aum + lp_pool.last_aum(), // TODO: add in_amount * in_oracle to est post add_liquidity aum )? }; @@ -817,14 +817,17 @@ pub fn handle_lp_pool_add_liquidity<'c: 'info, 'info>( let mut lp_pool = ctx.accounts.lp_pool.load_mut()?; - lp_pool.last_aum = lp_pool.last_aum.safe_add( - in_amount - .cast::()? - .safe_mul(in_oracle.price.cast::()?)? - .safe_div(10_u128.pow(in_spot_market.decimals))?, - )?; + let last_aum = lp_pool.last_aum(); + lp_pool.set_last_aum( + last_aum.safe_add( + in_amount + .cast::()? + .safe_mul(in_oracle.price.cast::()?)? + .safe_div(10_u128.pow(in_spot_market.decimals))?, + )?, + ); - if lp_pool.last_aum > lp_pool.max_aum { + if lp_pool.last_aum() > lp_pool.max_aum() { return Err(ErrorCode::MaxDlpAumBreached.into()); } @@ -864,13 +867,13 @@ pub fn handle_lp_pool_add_liquidity<'c: 'info, 'info>( lp_fee: lp_fee_amount, lp_price: lp_price_after, mint_redeem_id, - last_aum: lp_pool.last_aum, + last_aum: lp_pool.last_aum(), last_aum_slot: lp_pool.last_aum_slot, in_market_current_weight: in_constituent.get_weight( in_oracle.price, &in_spot_market, 0, - lp_pool.last_aum, + lp_pool.last_aum(), )?, in_market_target_weight: in_target_weight, lp_pool: lp_pool_key, @@ -938,15 +941,15 @@ pub fn handle_view_lp_pool_add_liquidity_fees<'c: 'info, 'info>( return Err(ErrorCode::InvalidOracle.into()); } - msg!("aum: {}", lp_pool.last_aum); - let in_target_weight = if lp_pool.last_aum == 0 { + msg!("aum: {}", lp_pool.last_aum()); + let in_target_weight = if lp_pool.last_aum() == 0 { PERCENTAGE_PRECISION_I64 // 100% weight if no aum } else { constituent_target_base.get_target_weight( in_constituent.constituent_index, &in_spot_market, in_oracle.price, - lp_pool.last_aum, // TODO: add in_amount * in_oracle to est post add_liquidity aum + lp_pool.last_aum(), // TODO: add in_amount * in_oracle to est post add_liquidity aum )? }; @@ -1094,7 +1097,7 @@ pub fn handle_lp_pool_remove_liquidity<'c: 'info, 'info>( out_constituent.constituent_index, &out_spot_market, out_oracle.price, - lp_pool.last_aum, // TODO: remove out_amount * out_oracle to est post remove_liquidity aum + lp_pool.last_aum(), // TODO: remove out_amount * out_oracle to est post remove_liquidity aum )?; let dlp_total_supply = ctx.accounts.lp_mint.supply; @@ -1219,12 +1222,15 @@ pub fn handle_lp_pool_remove_liquidity<'c: 'info, 'info>( let mut lp_pool = ctx.accounts.lp_pool.load_mut()?; - lp_pool.last_aum = lp_pool.last_aum.safe_sub( - out_amount_net_fees - .cast::()? - .safe_mul(out_oracle.price.cast::()?)? - .safe_div(10_u128.pow(out_spot_market.decimals))?, - )?; + let last_aum = lp_pool.last_aum(); + lp_pool.set_last_aum( + last_aum.safe_sub( + out_amount_net_fees + .cast::()? + .safe_mul(out_oracle.price.cast::()?)? + .safe_div(10_u128.pow(out_spot_market.decimals))?, + )?, + ); ctx.accounts.constituent_out_token_account.reload()?; ctx.accounts.lp_mint.reload()?; @@ -1262,13 +1268,13 @@ pub fn handle_lp_pool_remove_liquidity<'c: 'info, 'info>( lp_fee: lp_fee_amount, lp_price: lp_price_after, mint_redeem_id, - last_aum: lp_pool.last_aum, + last_aum: lp_pool.last_aum(), last_aum_slot: lp_pool.last_aum_slot, in_market_current_weight: out_constituent.get_weight( out_oracle.price, &out_spot_market, 0, - lp_pool.last_aum, + lp_pool.last_aum(), )?, in_market_target_weight: out_target_weight, lp_pool: lp_pool_key, @@ -1346,7 +1352,7 @@ pub fn handle_view_lp_pool_remove_liquidity_fees<'c: 'info, 'info>( out_constituent.constituent_index, &out_spot_market, out_oracle.price, - lp_pool.last_aum, + lp_pool.last_aum(), )?; let dlp_total_supply = ctx.accounts.lp_mint.supply; diff --git a/programs/drift/src/instructions/user.rs b/programs/drift/src/instructions/user.rs index e75f02ca1c..336fc076a9 100644 --- a/programs/drift/src/instructions/user.rs +++ b/programs/drift/src/instructions/user.rs @@ -1,8 +1,14 @@ use std::convert::TryFrom; use std::ops::DerefMut; -use anchor_lang::prelude::*; -use anchor_lang::Discriminator; +use anchor_lang::{ + prelude::{ + borsh::{BorshDeserialize, BorshSerialize}, + *, + }, + solana_program::sysvar::instructions, + Discriminator, +}; use anchor_spl::{ token::Token, token_2022::Token2022, @@ -115,8 +121,6 @@ use crate::validation::user::validate_user_deletion; use crate::validation::whitelist::validate_whitelist_token; use crate::{controller, math}; use crate::{load_mut, ExchangeStatus}; -use anchor_lang::solana_program::sysvar::instructions; -use borsh::{BorshDeserialize, BorshSerialize}; use solana_program::sysvar::instructions::ID as IX_ID; use super::optional_accounts::get_high_leverage_mode_config; @@ -393,10 +397,8 @@ pub fn handle_initialize_fuel_overflow<'c: 'info, 'info>( .load_init() .or(Err(ErrorCode::UnableToLoadAccountLoader))?; - *fuel_overflow = FuelOverflow { - authority: ctx.accounts.authority.key(), - ..FuelOverflow::default() - }; + *fuel_overflow = FuelOverflow::default(); + fuel_overflow.authority = ctx.accounts.authority.key(); user_stats.update_fuel_overflow_status(true); Ok(()) @@ -424,12 +426,12 @@ pub fn handle_sweep_fuel<'c: 'info, 'info>( user_stats_fuel_positions: user_stats.fuel_positions, user_stats_fuel_taker: user_stats.fuel_taker, user_stats_fuel_maker: user_stats.fuel_maker, - fuel_overflow_fuel_insurance: fuel_overflow.fuel_insurance, - fuel_overflow_fuel_deposits: fuel_overflow.fuel_deposits, - fuel_overflow_fuel_borrows: fuel_overflow.fuel_borrows, - fuel_overflow_fuel_positions: fuel_overflow.fuel_positions, - fuel_overflow_fuel_taker: fuel_overflow.fuel_taker, - fuel_overflow_fuel_maker: fuel_overflow.fuel_maker, + fuel_overflow_fuel_insurance: fuel_overflow.fuel_insurance(), + fuel_overflow_fuel_deposits: fuel_overflow.fuel_deposits(), + fuel_overflow_fuel_borrows: fuel_overflow.fuel_borrows(), + fuel_overflow_fuel_positions: fuel_overflow.fuel_positions(), + fuel_overflow_fuel_taker: fuel_overflow.fuel_taker(), + fuel_overflow_fuel_maker: fuel_overflow.fuel_maker(), }); fuel_overflow.update_from_user_stats(&user_stats, clock.unix_timestamp.cast()?)?; @@ -459,24 +461,24 @@ pub fn handle_reset_fuel_season<'c: 'info, 'info>( user_stats_fuel_positions: user_stats.fuel_positions, user_stats_fuel_taker: user_stats.fuel_taker, user_stats_fuel_maker: user_stats.fuel_maker, - fuel_overflow_fuel_insurance: fuel_overflow.fuel_insurance, - fuel_overflow_fuel_deposits: fuel_overflow.fuel_deposits, - fuel_overflow_fuel_borrows: fuel_overflow.fuel_borrows, - fuel_overflow_fuel_positions: fuel_overflow.fuel_positions, - fuel_overflow_fuel_taker: fuel_overflow.fuel_taker, - fuel_overflow_fuel_maker: fuel_overflow.fuel_maker, + fuel_overflow_fuel_insurance: fuel_overflow.fuel_insurance(), + fuel_overflow_fuel_deposits: fuel_overflow.fuel_deposits(), + fuel_overflow_fuel_borrows: fuel_overflow.fuel_borrows(), + fuel_overflow_fuel_positions: fuel_overflow.fuel_positions(), + fuel_overflow_fuel_taker: fuel_overflow.fuel_taker(), + fuel_overflow_fuel_maker: fuel_overflow.fuel_maker(), }); fuel_overflow.update_from_user_stats(&user_stats, clock.unix_timestamp.cast()?)?; emit!(FuelSeasonRecord { ts: clock.unix_timestamp.cast()?, authority: ctx.accounts.authority.key(), - fuel_insurance: fuel_overflow.fuel_insurance, - fuel_deposits: fuel_overflow.fuel_deposits, - fuel_borrows: fuel_overflow.fuel_borrows, - fuel_positions: fuel_overflow.fuel_positions, - fuel_taker: fuel_overflow.fuel_taker, - fuel_maker: fuel_overflow.fuel_maker, + fuel_insurance: fuel_overflow.fuel_insurance(), + fuel_deposits: fuel_overflow.fuel_deposits(), + fuel_borrows: fuel_overflow.fuel_borrows(), + fuel_positions: fuel_overflow.fuel_positions(), + fuel_taker: fuel_overflow.fuel_taker(), + fuel_maker: fuel_overflow.fuel_maker(), fuel_total: fuel_overflow.total_fuel()?, }); fuel_overflow.reset_fuel(clock.unix_timestamp.cast()?); @@ -806,10 +808,10 @@ pub fn handle_deposit<'c: 'info, 'info>( direction: DepositDirection::Deposit, amount, oracle_price, - market_deposit_balance: spot_market.deposit_balance, - market_withdraw_balance: spot_market.borrow_balance, - market_cumulative_deposit_interest: spot_market.cumulative_deposit_interest, - market_cumulative_borrow_interest: spot_market.cumulative_borrow_interest, + market_deposit_balance: spot_market.deposit_balance(), + market_withdraw_balance: spot_market.borrow_balance(), + market_cumulative_deposit_interest: spot_market.cumulative_deposit_interest(), + market_cumulative_borrow_interest: spot_market.cumulative_borrow_interest(), total_deposits_after, total_withdraws_after, market_index, @@ -962,10 +964,10 @@ pub fn handle_withdraw<'c: 'info, 'info>( oracle_price, amount, market_index, - market_deposit_balance: spot_market.deposit_balance, - market_withdraw_balance: spot_market.borrow_balance, - market_cumulative_deposit_interest: spot_market.cumulative_deposit_interest, - market_cumulative_borrow_interest: spot_market.cumulative_borrow_interest, + market_deposit_balance: spot_market.deposit_balance(), + market_withdraw_balance: spot_market.borrow_balance(), + market_cumulative_deposit_interest: spot_market.cumulative_deposit_interest(), + market_cumulative_borrow_interest: spot_market.cumulative_borrow_interest(), total_deposits_after: user.total_deposits, total_withdraws_after: user.total_withdraws, explanation: deposit_explanation, @@ -1132,10 +1134,10 @@ pub fn handle_transfer_deposit<'c: 'info, 'info>( amount, oracle_price, market_index, - market_deposit_balance: spot_market.deposit_balance, - market_withdraw_balance: spot_market.borrow_balance, - market_cumulative_deposit_interest: spot_market.cumulative_deposit_interest, - market_cumulative_borrow_interest: spot_market.cumulative_borrow_interest, + market_deposit_balance: spot_market.deposit_balance(), + market_withdraw_balance: spot_market.borrow_balance(), + market_cumulative_deposit_interest: spot_market.cumulative_deposit_interest(), + market_cumulative_borrow_interest: spot_market.cumulative_borrow_interest(), total_deposits_after: from_user.total_deposits, total_withdraws_after: from_user.total_withdraws, explanation: DepositExplanation::Transfer, @@ -1196,10 +1198,10 @@ pub fn handle_transfer_deposit<'c: 'info, 'info>( amount, oracle_price, market_index, - market_deposit_balance: spot_market.deposit_balance, - market_withdraw_balance: spot_market.borrow_balance, - market_cumulative_deposit_interest: spot_market.cumulative_deposit_interest, - market_cumulative_borrow_interest: spot_market.cumulative_borrow_interest, + market_deposit_balance: spot_market.deposit_balance(), + market_withdraw_balance: spot_market.borrow_balance(), + market_cumulative_deposit_interest: spot_market.cumulative_deposit_interest(), + market_cumulative_borrow_interest: spot_market.cumulative_borrow_interest(), total_deposits_after, total_withdraws_after, explanation: DepositExplanation::Transfer, @@ -1407,11 +1409,12 @@ pub fn handle_transfer_pools<'c: 'info, 'info>( amount: deposit_transfer, oracle_price: deposit_from_oracle_price_data.price, market_index: deposit_from_market_index, - market_deposit_balance: deposit_from_spot_market.deposit_balance, - market_withdraw_balance: deposit_from_spot_market.borrow_balance, + market_deposit_balance: deposit_from_spot_market.deposit_balance(), + market_withdraw_balance: deposit_from_spot_market.borrow_balance(), market_cumulative_deposit_interest: deposit_from_spot_market - .cumulative_deposit_interest, - market_cumulative_borrow_interest: deposit_from_spot_market.cumulative_borrow_interest, + .cumulative_deposit_interest(), + market_cumulative_borrow_interest: deposit_from_spot_market + .cumulative_borrow_interest(), total_deposits_after: from_user.total_deposits, total_withdraws_after: from_user.total_withdraws, explanation: DepositExplanation::Transfer, @@ -1442,10 +1445,11 @@ pub fn handle_transfer_pools<'c: 'info, 'info>( amount: deposit_transfer, oracle_price: deposit_to_oracle_price_data.price, market_index: deposit_to_market_index, - market_deposit_balance: deposit_to_spot_market.deposit_balance, - market_withdraw_balance: deposit_to_spot_market.borrow_balance, - market_cumulative_deposit_interest: deposit_to_spot_market.cumulative_deposit_interest, - market_cumulative_borrow_interest: deposit_to_spot_market.cumulative_borrow_interest, + market_deposit_balance: deposit_to_spot_market.deposit_balance(), + market_withdraw_balance: deposit_to_spot_market.borrow_balance(), + market_cumulative_deposit_interest: deposit_to_spot_market + .cumulative_deposit_interest(), + market_cumulative_borrow_interest: deposit_to_spot_market.cumulative_borrow_interest(), total_deposits_after: to_user.total_deposits, total_withdraws_after: to_user.total_withdraws, explanation: DepositExplanation::Transfer, @@ -1508,10 +1512,11 @@ pub fn handle_transfer_pools<'c: 'info, 'info>( amount: borrow_transfer, oracle_price: borrow_from_oracle_price_data.price, market_index: borrow_from_market_index, - market_deposit_balance: borrow_from_spot_market.deposit_balance, - market_withdraw_balance: borrow_from_spot_market.borrow_balance, - market_cumulative_deposit_interest: borrow_from_spot_market.cumulative_deposit_interest, - market_cumulative_borrow_interest: borrow_from_spot_market.cumulative_borrow_interest, + market_deposit_balance: borrow_from_spot_market.deposit_balance(), + market_withdraw_balance: borrow_from_spot_market.borrow_balance(), + market_cumulative_deposit_interest: borrow_from_spot_market + .cumulative_deposit_interest(), + market_cumulative_borrow_interest: borrow_from_spot_market.cumulative_borrow_interest(), total_deposits_after: from_user.total_deposits, total_withdraws_after: from_user.total_withdraws, explanation: DepositExplanation::Transfer, @@ -1542,10 +1547,10 @@ pub fn handle_transfer_pools<'c: 'info, 'info>( amount: borrow_transfer, oracle_price: borrow_to_oracle_price_data.price, market_index: borrow_to_market_index, - market_deposit_balance: borrow_to_spot_market.deposit_balance, - market_withdraw_balance: borrow_to_spot_market.borrow_balance, - market_cumulative_deposit_interest: borrow_to_spot_market.cumulative_deposit_interest, - market_cumulative_borrow_interest: borrow_to_spot_market.cumulative_borrow_interest, + market_deposit_balance: borrow_to_spot_market.deposit_balance(), + market_withdraw_balance: borrow_to_spot_market.borrow_balance(), + market_cumulative_deposit_interest: borrow_to_spot_market.cumulative_deposit_interest(), + market_cumulative_borrow_interest: borrow_to_spot_market.cumulative_borrow_interest(), total_deposits_after: to_user.total_deposits, total_withdraws_after: to_user.total_withdraws, explanation: DepositExplanation::Transfer, diff --git a/programs/drift/src/lib.rs b/programs/drift/src/lib.rs index 2dfdf04f7e..cf1a726f27 100644 --- a/programs/drift/src/lib.rs +++ b/programs/drift/src/lib.rs @@ -1,8 +1,9 @@ +#![allow(unexpected_cfgs)] #![allow(clippy::too_many_arguments)] #![allow(clippy::bool_assert_comparison)] #![allow(clippy::comparison_chain)] -use anchor_lang::prelude::*; +use anchor_lang::prelude::{borsh::BorshDeserialize, *}; use instructions::*; #[cfg(test)] diff --git a/programs/drift/src/math/amm.rs b/programs/drift/src/math/amm.rs index 914dffe621..82573fe83f 100644 --- a/programs/drift/src/math/amm.rs +++ b/programs/drift/src/math/amm.rs @@ -63,9 +63,9 @@ pub fn calculate_bid_ask_bounds( } pub fn calculate_market_open_bids_asks(amm: &AMM) -> DriftResult<(i128, i128)> { - let base_asset_reserve = amm.base_asset_reserve; - let min_base_asset_reserve = amm.min_base_asset_reserve; - let max_base_asset_reserve = amm.max_base_asset_reserve; + let base_asset_reserve = amm.base_asset_reserve(); + let min_base_asset_reserve = amm.min_base_asset_reserve(); + let max_base_asset_reserve = amm.max_base_asset_reserve(); let (max_bids, max_asks) = _calculate_market_open_bids_asks( base_asset_reserve, @@ -712,16 +712,16 @@ pub fn calculate_quote_asset_amount_swapped( } pub fn calculate_terminal_reserves(amm: &AMM) -> DriftResult<(u128, u128)> { - let swap_direction = if amm.base_asset_amount_with_amm > 0 { + let swap_direction = if amm.base_asset_amount_with_amm() > 0 { SwapDirection::Add } else { SwapDirection::Remove }; let (new_quote_asset_amount, new_base_asset_amount) = calculate_swap_output( - amm.base_asset_amount_with_amm.unsigned_abs(), - amm.base_asset_reserve, + amm.base_asset_amount_with_amm().unsigned_abs(), + amm.base_asset_reserve(), swap_direction, - amm.sqrt_k, + amm.sqrt_k(), )?; Ok((new_quote_asset_amount, new_base_asset_amount)) @@ -733,7 +733,7 @@ pub fn calculate_terminal_price_and_reserves(amm: &AMM) -> DriftResult<(u64, u12 let terminal_price = calculate_price( new_quote_asset_amount, new_base_asset_amount, - amm.peg_multiplier, + amm.peg_multiplier(), )?; Ok(( @@ -844,20 +844,20 @@ pub fn calculate_amm_available_liquidity( amm: &AMM, order_direction: &PositionDirection, ) -> DriftResult { - let max_fill_size: u64 = (amm.base_asset_reserve / amm.max_fill_reserve_fraction as u128) + let max_fill_size: u64 = (amm.base_asset_reserve() / amm.max_fill_reserve_fraction as u128) .min(u64::MAX as u128) .cast()?; // one fill can only take up to half of side's liquidity let max_base_asset_amount_on_side = match order_direction { PositionDirection::Long => { - amm.base_asset_reserve - .saturating_sub(amm.min_base_asset_reserve) + amm.base_asset_reserve() + .saturating_sub(amm.min_base_asset_reserve()) / 2 } PositionDirection::Short => { - amm.max_base_asset_reserve - .saturating_sub(amm.base_asset_reserve) + amm.max_base_asset_reserve() + .saturating_sub(amm.base_asset_reserve()) / 2 } } @@ -870,7 +870,7 @@ pub fn calculate_amm_available_liquidity( } pub fn calculate_net_user_cost_basis(amm: &AMM) -> DriftResult { - amm.quote_asset_amount + amm.quote_asset_amount() .safe_add(amm.quote_asset_amount_with_unsettled_lp.cast()?)? .safe_add(amm.net_unsettled_funding_pnl.cast()?) } @@ -883,8 +883,8 @@ pub fn calculate_net_user_pnl(amm: &AMM, oracle_price: i64) -> DriftResult )?; let net_user_base_asset_value = amm - .base_asset_amount_with_amm - .safe_add(amm.base_asset_amount_with_unsettled_lp)? + .base_asset_amount_with_amm() + .safe_add(amm.base_asset_amount_with_unsettled_lp())? .safe_mul(oracle_price.cast()?)? .safe_div(PRICE_TIMES_AMM_TO_QUOTE_PRECISION_RATIO.cast()?)?; @@ -896,7 +896,7 @@ pub fn calculate_expiry_price( target_price: i64, total_excess_balance: i128, ) -> DriftResult { - if amm.base_asset_amount_with_amm.abs() < amm.order_step_size.cast::()? { + if amm.base_asset_amount_with_amm().abs() < amm.order_step_size.cast::()? { return Ok(target_price); } // net_baa * price + net_quote <= 0 @@ -905,13 +905,13 @@ pub fn calculate_expiry_price( // net_user_unrealized_pnl negative = surplus in market // net_user_unrealized_pnl positive = expiry price needs to differ from oracle let best_expiry_price = -(amm - .quote_asset_amount - .safe_sub(total_excess_balance.cast::()?)? + .quote_asset_amount() + .safe_sub(total_excess_balance)? .safe_mul(PRICE_TIMES_AMM_TO_QUOTE_PRECISION_RATIO_I128)? - .safe_div(amm.base_asset_amount_with_amm)?) + .safe_div(amm.base_asset_amount_with_amm())?) .cast::()?; - let expiry_price = if amm.base_asset_amount_with_amm >= 0 { + let expiry_price = if amm.base_asset_amount_with_amm() >= 0 { // net longs only get as high as oracle_price best_expiry_price.min(target_price).saturating_sub(1) } else { diff --git a/programs/drift/src/math/amm/tests.rs b/programs/drift/src/math/amm/tests.rs index 938dc1e0c0..e458b0fdfa 100644 --- a/programs/drift/src/math/amm/tests.rs +++ b/programs/drift/src/math/amm/tests.rs @@ -26,47 +26,55 @@ fn calculate_amm_available_guards() { }; assert_eq!(market.amm.net_revenue_since_last_funding, 0); - assert_eq!(market.amm.total_fee_minus_distributions, 0); + assert_eq!(market.amm.total_fee_minus_distributions(), 0); assert_eq!(!market.has_too_much_drawdown().unwrap(), true); market.amm.net_revenue_since_last_funding = -100_000_000_000; - market.amm.total_fee_minus_distributions = 100_000_000_000; + market + .amm + .set_total_fee_minus_distributions(100_000_000_000); assert_eq!(!market.has_too_much_drawdown().unwrap(), false); market.amm.net_revenue_since_last_funding = -10_000_000_000; - market.amm.total_fee_minus_distributions = 100_000_000_000; + market + .amm + .set_total_fee_minus_distributions(100_000_000_000); assert_eq!(!market.has_too_much_drawdown().unwrap(), false); market.amm.net_revenue_since_last_funding = -5_000_000_000; - market.amm.total_fee_minus_distributions = 100_000_000_000; + market + .amm + .set_total_fee_minus_distributions(100_000_000_000); assert_eq!(!market.has_too_much_drawdown().unwrap(), false); market.amm.net_revenue_since_last_funding = -1_000_000_000; - market.amm.total_fee_minus_distributions = 100_000_000_000; + market + .amm + .set_total_fee_minus_distributions(100_000_000_000); assert_eq!(!market.has_too_much_drawdown().unwrap(), true); market.amm.net_revenue_since_last_funding = -1_000_000_000; - market.amm.total_fee_minus_distributions = 1_000_000; + market.amm.set_total_fee_minus_distributions(1_000_000); assert_eq!(!market.has_too_much_drawdown().unwrap(), true); market.amm.net_revenue_since_last_funding = -6_000_000_000; - market.amm.total_fee_minus_distributions = 1_000_000; + market.amm.set_total_fee_minus_distributions(1_000_000); assert_eq!(!market.has_too_much_drawdown().unwrap(), false); market.amm.net_revenue_since_last_funding = -5_000; - market.amm.total_fee_minus_distributions = -9279797219; + market.amm.set_total_fee_minus_distributions(-9279797219); assert_eq!(!market.has_too_much_drawdown().unwrap(), true); // too small net_revenue_since_last_funding drawdown market.amm.net_revenue_since_last_funding = -88_000_000_000; - market.amm.total_fee_minus_distributions = -9279797219; + market.amm.set_total_fee_minus_distributions(-9279797219); assert_eq!(!market.has_too_much_drawdown().unwrap(), false); // too small net_revenue_since_last_funding drawdown } @@ -79,9 +87,9 @@ fn calculate_net_user_pnl_test() { let px = 32 * PRICE_PRECISION; let amm = AMM { - base_asset_reserve: 2 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 2 * AMM_RESERVE_PRECISION, - peg_multiplier: PEG_PRECISION, + base_asset_reserve: (2 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (2 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: PEG_PRECISION.into(), historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: px as i64, last_oracle_price_twap_ts: prev, @@ -142,13 +150,13 @@ fn calculate_expiry_price_long_imbalance_with_loss_test() { let market = PerpMarket { market_index: 0, amm: AMM { - base_asset_reserve: 512295081967, - quote_asset_reserve: 488 * AMM_RESERVE_PRECISION, - sqrt_k: 500 * AMM_RESERVE_PRECISION, - peg_multiplier: 22_100_000_000, - base_asset_amount_with_amm: (12295081967_i128), + base_asset_reserve: 512295081967.into(), + quote_asset_reserve: (488 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (500 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 22_100_000_000.into(), + base_asset_amount_with_amm: (12295081967_i128).into(), max_spread: 1000, - quote_asset_amount: market_position.quote_asset_amount as i128 * 2, + quote_asset_amount: (market_position.quote_asset_amount as i128 * 2).into(), // assume someone else has other half same entry, ..AMM::default() }, @@ -225,13 +233,13 @@ fn calculate_expiry_price_long_imbalance_test() { let market = PerpMarket { market_index: 0, amm: AMM { - base_asset_reserve: 512295081967, - quote_asset_reserve: 488 * AMM_RESERVE_PRECISION, - sqrt_k: 500 * AMM_RESERVE_PRECISION, - peg_multiplier: 22_100_000_000, - base_asset_amount_with_amm: (12295081967_i128), + base_asset_reserve: 512295081967.into(), + quote_asset_reserve: (488 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (500 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 22_100_000_000.into(), + base_asset_amount_with_amm: (12295081967_i128).into(), max_spread: 1000, - quote_asset_amount: market_position.quote_asset_amount as i128 * 2, + quote_asset_amount: (market_position.quote_asset_amount as i128 * 2).into(), // assume someone else has other half same entry, ..AMM::default() }, @@ -291,9 +299,9 @@ fn calculate_expiry_price_test() { let px = 32 * PRICE_PRECISION; let amm = AMM { - base_asset_reserve: 2 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 2 * AMM_RESERVE_PRECISION, - peg_multiplier: PEG_PRECISION, + base_asset_reserve: (2 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (2 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: PEG_PRECISION.into(), historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: px as i64, last_oracle_price_twap_ts: prev, @@ -342,13 +350,13 @@ fn calculate_expiry_price_test() { let market = PerpMarket { market_index: 0, amm: AMM { - base_asset_reserve: 512295081967, - quote_asset_reserve: 488 * AMM_RESERVE_PRECISION, - sqrt_k: 500 * AMM_RESERVE_PRECISION, - peg_multiplier: 22_100_000_000, - base_asset_amount_with_amm: -(12295081967_i128), + base_asset_reserve: 512295081967.into(), + quote_asset_reserve: (488 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (500 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 22_100_000_000.into(), + base_asset_amount_with_amm: (-(12295081967_i128)).into(), max_spread: 1000, - quote_asset_amount: market_position.quote_asset_amount as i128 * 2, + quote_asset_amount: (market_position.quote_asset_amount as i128 * 2).into(), // assume someone else has other half same entry, ..AMM::default() }, @@ -408,9 +416,9 @@ fn calc_delayed_mark_twap_tests() { let prev = 1656682258; let now = prev + 60; let mut amm = AMM { - base_asset_reserve: 2 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 2 * AMM_RESERVE_PRECISION, - peg_multiplier: PRICE_PRECISION, + base_asset_reserve: (2 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (2 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: PRICE_PRECISION.into(), base_spread: 655, //base spread is .065%, max_spread: 65535, //base spread is 6.5% mark_std: PRICE_PRECISION as u64, @@ -428,7 +436,7 @@ fn calc_delayed_mark_twap_tests() { ..AMM::default() }; let px = 22850 * PRICE_PRECISION as i64; - amm.peg_multiplier = px as u128; + amm.set_peg_multiplier(px as u128); let trade_direction = PositionDirection::Long; update_mark_twap_from_estimates(&mut amm, now, Some(px as u64), Some(trade_direction), None) .unwrap(); @@ -448,9 +456,9 @@ fn calc_mark_std_tests() { let prev = 1656682258; let mut now = prev + 60; let mut amm = AMM { - base_asset_reserve: 2 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 2 * AMM_RESERVE_PRECISION, - peg_multiplier: PRICE_PRECISION, + base_asset_reserve: (2 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (2 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: PRICE_PRECISION.into(), base_spread: 65535, //max base spread is 6.5% mark_std: PRICE_PRECISION as u64, historical_oracle_data: HistoricalOracleData { @@ -502,7 +510,7 @@ fn calc_mark_std_tests() { update_oracle_price_twap(&mut amm, now, &mm_oracle_price_data, None, None).unwrap(); - amm.peg_multiplier = px as u128; + amm.set_peg_multiplier(px as u128); let trade_direction = PositionDirection::Long; update_mark_twap_from_estimates(&mut amm, now, Some(px), Some(trade_direction), None) .unwrap(); @@ -527,7 +535,7 @@ fn calc_mark_std_tests() { let mut px: u64 = 31_936_658; let stop_time = now + 3600 * 2; assert_eq!(amm.reserve_price().unwrap(), 39397); - amm.peg_multiplier = 31_986_658; + amm.set_peg_multiplier(31_986_658); assert_eq!(amm.reserve_price().unwrap(), 31986658); amm.historical_oracle_data.last_oracle_price = 31986658; @@ -536,7 +544,7 @@ fn calc_mark_std_tests() { if now % 15 == 0 { px = 31_986_658; //31.98 amm.historical_oracle_data.last_oracle_price = (px - 1000000) as i64; - amm.peg_multiplier = px as u128; + amm.set_peg_multiplier(px as u128); let amm_reserve_price = amm.reserve_price().unwrap(); let (amm_bid_price, amm_ask_price) = amm.bid_ask_price(amm_reserve_price).unwrap(); msg!("bid={:?} ask={:?}", amm_bid_price, amm_ask_price); @@ -550,7 +558,7 @@ fn calc_mark_std_tests() { } if now % 189 == 0 { px = 31_883_651; //31.88 - amm.peg_multiplier = px as u128; + amm.set_peg_multiplier(px as u128); amm.historical_oracle_data.last_oracle_price = (px + 1000000) as i64; let amm_reserve_price = amm.reserve_price().unwrap(); @@ -575,7 +583,7 @@ fn calc_mark_std_tests() { now += 1; if now % 2 == 1 { px = 31_986_658; //31.98 - amm.peg_multiplier = px as u128; + amm.set_peg_multiplier(px as u128); amm.historical_oracle_data.last_oracle_price = (px - 1000000) as i64; let trade_direction = PositionDirection::Long; @@ -584,7 +592,7 @@ fn calc_mark_std_tests() { } if now % 2 == 0 { px = 31_883_651; //31.88 - amm.peg_multiplier = px as u128; + amm.set_peg_multiplier(px as u128); amm.historical_oracle_data.last_oracle_price = (px + 1000000) as i64; let trade_direction = PositionDirection::Short; @@ -626,9 +634,9 @@ fn update_mark_twap_tests() { // $40 everything init let mut amm = AMM { - quote_asset_reserve: 2 * AMM_RESERVE_PRECISION, - base_asset_reserve: 2 * AMM_RESERVE_PRECISION, - peg_multiplier: 40 * PEG_PRECISION, + quote_asset_reserve: (2 * AMM_RESERVE_PRECISION).into(), + base_asset_reserve: (2 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (40 * PEG_PRECISION).into(), base_spread: 800, max_spread: 10000, long_spread: 0, @@ -767,9 +775,9 @@ fn calc_oracle_twap_tests() { let px = 32 * PRICE_PRECISION; let mut amm = AMM { - base_asset_reserve: 2 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 2 * AMM_RESERVE_PRECISION, - peg_multiplier: PEG_PRECISION, + base_asset_reserve: (2 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (2 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: PEG_PRECISION.into(), historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: px as i64, last_oracle_price_twap_ts: prev, @@ -889,9 +897,9 @@ fn calc_oracle_twap_clamp_update_tests() { // $13 everything init let mut amm = AMM { - quote_asset_reserve: 200 * AMM_RESERVE_PRECISION, - base_asset_reserve: 200 * AMM_RESERVE_PRECISION, - peg_multiplier: 13 * PEG_PRECISION, + quote_asset_reserve: (200 * AMM_RESERVE_PRECISION).into(), + base_asset_reserve: (200 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (13 * PEG_PRECISION).into(), base_spread: 0, long_spread: 0, short_spread: 0, @@ -978,9 +986,9 @@ fn test_last_oracle_conf_update() { let now = prev + 1; let mut amm = AMM { - quote_asset_reserve: 200 * AMM_RESERVE_PRECISION, - base_asset_reserve: 200 * AMM_RESERVE_PRECISION, - peg_multiplier: 13 * PEG_PRECISION, + quote_asset_reserve: (200 * AMM_RESERVE_PRECISION).into(), + base_asset_reserve: (200 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (13 * PEG_PRECISION).into(), base_spread: 0, long_spread: 0, short_spread: 0, diff --git a/programs/drift/src/math/amm_jit.rs b/programs/drift/src/math/amm_jit.rs index 2d2c22c0fd..1f62b7c209 100644 --- a/programs/drift/src/math/amm_jit.rs +++ b/programs/drift/src/math/amm_jit.rs @@ -132,7 +132,7 @@ pub fn calculate_clamped_jit_base_asset_amount( // bound it; dont flip the net_baa let max_amm_base_asset_amount = market .amm - .base_asset_amount_with_amm + .base_asset_amount_with_amm() .unsigned_abs() .cast::()?; diff --git a/programs/drift/src/math/amm_jit/tests.rs b/programs/drift/src/math/amm_jit/tests.rs index 5e3488741d..ddd34aea22 100644 --- a/programs/drift/src/math/amm_jit/tests.rs +++ b/programs/drift/src/math/amm_jit/tests.rs @@ -5,7 +5,7 @@ use crate::state::perp_market::AMM; fn balanced_market_zero_jit() { let market = PerpMarket { amm: AMM { - base_asset_amount_with_amm: 0, + base_asset_amount_with_amm: 0.into(), amm_jit_intensity: 100, ..AMM::default_test() }, @@ -22,7 +22,7 @@ fn balanced_market_zero_jit() { fn balanced_market_zero_intensity() { let market = PerpMarket { amm: AMM { - base_asset_amount_with_amm: 100, + base_asset_amount_with_amm: 100.into(), amm_jit_intensity: 0, ..AMM::default_test() }, @@ -39,7 +39,7 @@ fn balanced_market_zero_intensity() { fn balanced_market_full_intensity() { let market = PerpMarket { amm: AMM { - base_asset_amount_with_amm: 100, + base_asset_amount_with_amm: 100.into(), amm_jit_intensity: 100, ..AMM::default_test() }, @@ -56,7 +56,7 @@ fn balanced_market_full_intensity() { fn balanced_market_half_intensity() { let market = PerpMarket { amm: AMM { - base_asset_amount_with_amm: 100, + base_asset_amount_with_amm: 100.into(), amm_jit_intensity: 50, ..AMM::default_test() }, diff --git a/programs/drift/src/math/amm_spread.rs b/programs/drift/src/math/amm_spread.rs index 7007722932..161df0688a 100644 --- a/programs/drift/src/math/amm_spread.rs +++ b/programs/drift/src/math/amm_spread.rs @@ -27,7 +27,7 @@ pub fn calculate_base_asset_amount_to_trade_to_price( limit_price: u64, direction: PositionDirection, ) -> DriftResult<(u64, PositionDirection)> { - let invariant_sqrt_u192 = U192::from(amm.sqrt_k); + let invariant_sqrt_u192 = U192::from(amm.sqrt_k()); let invariant = invariant_sqrt_u192.safe_mul(invariant_sqrt_u192)?; validate!( @@ -39,7 +39,7 @@ pub fn calculate_base_asset_amount_to_trade_to_price( let new_base_asset_reserve_squared = invariant .safe_mul(U192::from(PRICE_PRECISION))? .safe_div(U192::from(limit_price))? - .safe_mul(U192::from(amm.peg_multiplier))? + .safe_mul(U192::from(amm.peg_multiplier()))? .safe_div(U192::from(PEG_PRECISION))?; let new_base_asset_reserve = new_base_asset_reserve_squared @@ -50,7 +50,7 @@ pub fn calculate_base_asset_amount_to_trade_to_price( let (spread_base_asset_reserve, _) = get_spread_reserves(amm, direction)?; spread_base_asset_reserve } else { - amm.base_asset_reserve + amm.base_asset_reserve() }; if new_base_asset_reserve > base_asset_reserve_before { @@ -519,8 +519,8 @@ pub fn calculate_spread( pub fn get_spread_reserves(amm: &AMM, direction: PositionDirection) -> DriftResult<(u128, u128)> { let (base_asset_reserve, quote_asset_reserve) = match direction { - PositionDirection::Long => (amm.ask_base_asset_reserve, amm.ask_quote_asset_reserve), - PositionDirection::Short => (amm.bid_base_asset_reserve, amm.bid_quote_asset_reserve), + PositionDirection::Long => (amm.ask_base_asset_reserve(), amm.ask_quote_asset_reserve()), + PositionDirection::Short => (amm.bid_base_asset_reserve(), amm.bid_quote_asset_reserve()), }; Ok((base_asset_reserve, quote_asset_reserve)) @@ -548,7 +548,7 @@ pub fn calculate_spread_reserves( BID_ASK_SPREAD_PRECISION_I128 / (spread_with_offset / 2).cast::()?; market .amm - .quote_asset_reserve + .quote_asset_reserve() .cast::()? .safe_div(quote_reserve_divisor)? } else { @@ -558,12 +558,12 @@ pub fn calculate_spread_reserves( let mut quote_asset_reserve = if quote_asset_reserve_delta > 0 { market .amm - .quote_asset_reserve + .quote_asset_reserve() .safe_add(quote_asset_reserve_delta.unsigned_abs())? } else { market .amm - .quote_asset_reserve + .quote_asset_reserve() .safe_sub(quote_asset_reserve_delta.unsigned_abs())? }; @@ -577,7 +577,7 @@ pub fn calculate_spread_reserves( ); } - let invariant_sqrt_u192 = U192::from(market.amm.sqrt_k); + let invariant_sqrt_u192 = U192::from(market.amm.sqrt_k()); let invariant = invariant_sqrt_u192.safe_mul(invariant_sqrt_u192)?; let base_asset_reserve = invariant diff --git a/programs/drift/src/math/amm_spread/tests.rs b/programs/drift/src/math/amm_spread/tests.rs index ac57fb45df..fa861c8428 100644 --- a/programs/drift/src/math/amm_spread/tests.rs +++ b/programs/drift/src/math/amm_spread/tests.rs @@ -194,12 +194,12 @@ mod test { fn calculate_reference_price_offset_deadband_tests() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: AMM_RESERVE_PRECISION * 11, - quote_asset_reserve: AMM_RESERVE_PRECISION * 10, - sqrt_k: AMM_RESERVE_PRECISION * 10, - peg_multiplier: 34_000_000, - min_base_asset_reserve: AMM_RESERVE_PRECISION * 7, - max_base_asset_reserve: AMM_RESERVE_PRECISION * 14, + base_asset_reserve: (AMM_RESERVE_PRECISION * 11).into(), + quote_asset_reserve: (AMM_RESERVE_PRECISION * 10).into(), + sqrt_k: (AMM_RESERVE_PRECISION * 10).into(), + peg_multiplier: 34_000_000.into(), + min_base_asset_reserve: (AMM_RESERVE_PRECISION * 7).into(), + max_base_asset_reserve: (AMM_RESERVE_PRECISION * 14).into(), base_spread: 1000, max_spread: 20_000, curve_update_intensity: 110, @@ -219,12 +219,14 @@ mod test { let reserve_price = 4216 * 10000; - market.amm.base_asset_amount_with_amm = (AMM_RESERVE_PRECISION * 7 / 20) as i128; + market + .amm + .set_base_asset_amount_with_amm((AMM_RESERVE_PRECISION * 7 / 20) as i128); let inventory_ratio = calculate_inventory_liquidity_ratio_for_reference_price_offset( - market.amm.base_asset_amount_with_amm, - market.amm.base_asset_reserve, - market.amm.min_base_asset_reserve, - market.amm.max_base_asset_reserve, + market.amm.base_asset_amount_with_amm(), + market.amm.base_asset_reserve(), + market.amm.min_base_asset_reserve(), + market.amm.max_base_asset_reserve(), ) .unwrap(); assert_eq!(inventory_ratio, 100000); // 10% @@ -232,12 +234,16 @@ mod test { market.amm.reference_price_offset_deadband_pct = 10; // 10% // If inventory exceeds threshold positive ref price offset - market.amm.base_asset_amount_with_amm = (AMM_RESERVE_PRECISION * 8 / 20) as i128; + market + .amm + .set_base_asset_amount_with_amm((AMM_RESERVE_PRECISION * 8 / 20) as i128); let (_l, _s) = update_spreads(&mut market, reserve_price as u64, None).unwrap(); assert!(market.amm.reference_price_offset > 0); // If inventory is small, goes to 0 - market.amm.base_asset_amount_with_amm = (AMM_RESERVE_PRECISION * 6 / 20) as i128; + market + .amm + .set_base_asset_amount_with_amm((AMM_RESERVE_PRECISION * 6 / 20) as i128); let (_l, _s) = update_spreads(&mut market, reserve_price as u64, None).unwrap(); assert_eq!(market.amm.reference_price_offset, 0); @@ -246,13 +252,17 @@ mod test { market.amm.last_24h_avg_funding_rate = -1; market.amm.last_mark_price_twap_5min = 4216 * 10000 - 2 * 10000; market.amm.last_mark_price_twap = 4216 * 10000 - 2 * 10000; - market.amm.base_asset_amount_with_amm = (AMM_RESERVE_PRECISION * 8 / 20) as i128 * -1; + market + .amm + .set_base_asset_amount_with_amm((AMM_RESERVE_PRECISION * 8 / 20) as i128 * -1); let (_l, _s) = update_spreads(&mut market, reserve_price as u64, None).unwrap(); println!("ref offset: {}", market.amm.reference_price_offset); assert!(market.amm.reference_price_offset < 0); // Same for short pos - market.amm.base_asset_amount_with_amm = (AMM_RESERVE_PRECISION * 6 / 20) as i128 * -1; + market + .amm + .set_base_asset_amount_with_amm((AMM_RESERVE_PRECISION * 6 / 20) as i128 * -1); let (_l, _s) = update_spreads(&mut market, reserve_price as u64, None).unwrap(); assert_eq!(market.amm.reference_price_offset, 0); } @@ -440,10 +450,10 @@ mod test { assert_eq!(short_spread5, 500); let amm = AMM { - base_asset_reserve: 2 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 2 * AMM_RESERVE_PRECISION, - sqrt_k: 2 * AMM_RESERVE_PRECISION, - peg_multiplier: PEG_PRECISION, + base_asset_reserve: (2 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (2 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (2 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: PEG_PRECISION.into(), long_spread: long_spread5, short_spread: short_spread5, max_spread: 1000, @@ -472,9 +482,9 @@ mod test { assert_eq!(max_ref_offset, 25000); // 250 bps (5% of max spread) let orig_price = calculate_price( - amm.quote_asset_reserve, - amm.base_asset_reserve, - amm.peg_multiplier, + amm.quote_asset_reserve(), + amm.base_asset_reserve(), + amm.peg_multiplier(), ) .unwrap(); assert_eq!(orig_price, 1000000); @@ -487,13 +497,13 @@ mod test { assert_eq!(qar_l, 2027397260); assert_eq!(qar_s, 1999500000); - assert!(qar_l > amm.quote_asset_reserve); - assert!(bar_l < amm.base_asset_reserve); - assert!(qar_s < amm.quote_asset_reserve); - assert!(bar_s > amm.base_asset_reserve); + assert!(qar_l > amm.quote_asset_reserve()); + assert!(bar_l < amm.base_asset_reserve()); + assert!(qar_s < amm.quote_asset_reserve()); + assert!(bar_s > amm.base_asset_reserve()); - let l_price = calculate_price(qar_l, bar_l, amm.peg_multiplier).unwrap(); - let s_price = calculate_price(qar_s, bar_s, amm.peg_multiplier).unwrap(); + let l_price = calculate_price(qar_l, bar_l, amm.peg_multiplier()).unwrap(); + let s_price = calculate_price(qar_s, bar_s, amm.peg_multiplier()).unwrap(); assert_eq!(l_price, 1027584); assert_eq!(s_price, 999500); assert!(l_price > s_price); @@ -503,19 +513,19 @@ mod test { let (bar_l, qar_l) = calculate_spread_reserves(&market, PositionDirection::Long).unwrap(); let (bar_s, qar_s) = calculate_spread_reserves(&market, PositionDirection::Short).unwrap(); - assert_eq!(amm.quote_asset_reserve, 2000000000); + assert_eq!(amm.quote_asset_reserve(), 2000000000); assert_eq!(qar_s, 2000500000); // down - assert!(qar_l > amm.quote_asset_reserve); - assert!(bar_l < amm.base_asset_reserve); - assert!(qar_s > amm.quote_asset_reserve); - assert!(bar_s < amm.base_asset_reserve); + assert!(qar_l > amm.quote_asset_reserve()); + assert!(bar_l < amm.base_asset_reserve()); + assert!(qar_s > amm.quote_asset_reserve()); + assert!(bar_s < amm.base_asset_reserve()); assert_eq!(bar_s, 1999500124); // up assert_eq!(bar_l, 1971830986); // down assert_eq!(qar_l, 2028571428); // up - let l_price = calculate_price(qar_l, bar_l, amm.peg_multiplier).unwrap(); - let s_price = calculate_price(qar_s, bar_s, amm.peg_multiplier).unwrap(); + let l_price = calculate_price(qar_l, bar_l, amm.peg_multiplier()).unwrap(); + let s_price = calculate_price(qar_s, bar_s, amm.peg_multiplier()).unwrap(); assert_eq!(l_price, 1028775); assert_eq!(s_price, 1000500); assert!(l_price > s_price); @@ -524,17 +534,17 @@ mod test { let (bar_l, qar_l) = calculate_spread_reserves(&market, PositionDirection::Long).unwrap(); let (bar_s, qar_s) = calculate_spread_reserves(&market, PositionDirection::Short).unwrap(); - assert!(qar_l > amm.quote_asset_reserve); - assert!(bar_l < amm.base_asset_reserve); - assert!(qar_s < amm.quote_asset_reserve); - assert!(bar_s > amm.base_asset_reserve); + assert!(qar_l > amm.quote_asset_reserve()); + assert!(bar_l < amm.base_asset_reserve()); + assert!(qar_s < amm.quote_asset_reserve()); + assert!(bar_s > amm.base_asset_reserve()); assert_eq!(bar_s, 2001501501); // up assert_eq!(bar_l, 1974025974); // up assert_eq!(qar_l, 2026315789); // down assert_eq!(qar_s, 1998499625); // down - let l_price = calculate_price(qar_l, bar_l, amm.peg_multiplier).unwrap(); - let s_price = calculate_price(qar_s, bar_s, amm.peg_multiplier).unwrap(); + let l_price = calculate_price(qar_l, bar_l, amm.peg_multiplier()).unwrap(); + let s_price = calculate_price(qar_s, bar_s, amm.peg_multiplier()).unwrap(); assert_eq!(l_price, 1026488); assert_eq!(s_price, 998500); assert!(l_price > s_price); @@ -1668,10 +1678,10 @@ mod test { #[test] fn calculate_prediction_market_spread_tests_low_price() { let amm = AMM { - base_asset_reserve: 2 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 2 * AMM_RESERVE_PRECISION, - sqrt_k: 2 * AMM_RESERVE_PRECISION, - peg_multiplier: PEG_PRECISION / 30, // .02 + base_asset_reserve: (2 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (2 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (2 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (PEG_PRECISION / 30).into(), // .02 long_spread: 10000, short_spread: 10000, base_spread: 10000, @@ -1697,7 +1707,7 @@ mod test { crate::math::amm::calculate_price( new_ask_quote_asset_reserve, new_ask_base_asset_reserve, - market.amm.peg_multiplier, + market.amm.peg_multiplier(), ) .unwrap(), 50001 // over .05 @@ -1707,7 +1717,7 @@ mod test { crate::math::amm::calculate_price( new_bid_quote_asset_reserve, new_bid_base_asset_reserve, - market.amm.peg_multiplier, + market.amm.peg_multiplier(), ) .unwrap(), 33000 @@ -1717,10 +1727,10 @@ mod test { #[test] fn calculate_prediction_market_spread_tests_high_price() { let amm = AMM { - base_asset_reserve: 2 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 2 * AMM_RESERVE_PRECISION, - sqrt_k: 2 * AMM_RESERVE_PRECISION, - peg_multiplier: PEG_PRECISION - PEG_PRECISION / 1000, // .999 + base_asset_reserve: (2 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (2 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (2 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (PEG_PRECISION - PEG_PRECISION / 1000).into(), // .999 long_spread: 10000, short_spread: 10000, base_spread: 10000, @@ -1746,7 +1756,7 @@ mod test { crate::math::amm::calculate_price( new_ask_quote_asset_reserve, new_ask_base_asset_reserve, - market.amm.peg_multiplier, + market.amm.peg_multiplier(), ) .unwrap(), 1000000 // exactly $1 @@ -1756,7 +1766,7 @@ mod test { crate::math::amm::calculate_price( new_bid_quote_asset_reserve, new_bid_base_asset_reserve, - market.amm.peg_multiplier, + market.amm.peg_multiplier(), ) .unwrap(), 949981 // under .95 diff --git a/programs/drift/src/math/bn.rs b/programs/drift/src/math/bn.rs index 0218b2a5a3..09ab7b117f 100644 --- a/programs/drift/src/math/bn.rs +++ b/programs/drift/src/math/bn.rs @@ -12,6 +12,282 @@ use uint::construct_uint; use crate::error::DriftResult; +pub mod compat { + #![allow(non_camel_case_types)] + use anchor_lang::prelude::borsh::{BorshDeserialize, BorshSerialize}; + use bytemuck::{Pod, Zeroable}; + use std::{ + cmp::Ordering, + convert::TryFrom, + ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign}, + }; + + use crate::{error::DriftResult, math::casting::Cast}; + + /// `u128` with legacy bit layout + #[derive(Copy, Clone, PartialEq, Eq, Debug, Default, BorshSerialize, BorshDeserialize)] + pub struct u128([u8; 16]); + + impl std::fmt::Display for self::u128 { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(&self.as_u128(), f) + } + } + + impl PartialOrd for self::u128 { + fn partial_cmp(&self, other: &Self) -> Option { + self.as_u128().partial_cmp(&other.as_u128()) + } + } + + impl PartialOrd for self::i128 { + fn partial_cmp(&self, other: &Self) -> Option { + self.as_i128().partial_cmp(&other.as_i128()) + } + } + + // Safety: u128 is a transparent wrapper around [u8; 16], which is Pod + Zeroable + unsafe impl Pod for self::u128 {} + unsafe impl Zeroable for self::u128 {} + impl u128 { + pub const ONE: Self = u128(1_u128.to_le_bytes()); + pub const ZERO: Self = u128(0_u128.to_le_bytes()); + /// convert to std u128 + #[inline] + pub fn as_u128(self) -> std::primitive::u128 { + std::primitive::u128::from_le_bytes(self.0) + } + pub const fn one() -> Self { + Self::ONE + } + pub const fn zero() -> Self { + Self::ZERO + } + pub fn cast>(&self) -> DriftResult { + self.as_u128().cast() + } + } + impl From for u128 { + fn from(value: std::primitive::u128) -> Self { + u128(value.to_le_bytes()) + } + } + impl From for std::primitive::u128 { + fn from(value: u128) -> Self { + value.as_u128() + } + } + + impl num_traits::Zero for self::u128 { + fn zero() -> Self { + Self::ZERO + } + fn is_zero(&self) -> bool { + self.0 == Self::ZERO.0 + } + } + + // Arithmetic operations for u128 - using From/Into conversions + impl Add for u128 { + type Output = Self; + #[inline(always)] + fn add(self, other: Self) -> Self { + let a: std::primitive::u128 = self.into(); + let b: std::primitive::u128 = other.into(); + Self::from(a + b) + } + } + impl AddAssign for u128 { + #[inline(always)] + fn add_assign(&mut self, other: Self) { + *self = *self + other; + } + } + impl Sub for u128 { + type Output = Self; + #[inline(always)] + fn sub(self, other: Self) -> Self { + let a: std::primitive::u128 = self.into(); + let b: std::primitive::u128 = other.into(); + Self::from(a - b) + } + } + impl SubAssign for u128 { + #[inline(always)] + fn sub_assign(&mut self, other: Self) { + *self = *self - other; + } + } + impl Mul for u128 { + type Output = Self; + #[inline(always)] + fn mul(self, other: Self) -> Self { + let a: std::primitive::u128 = self.into(); + + let b: std::primitive::u128 = other.into(); + Self::from(a * b) + } + } + impl MulAssign for u128 { + #[inline(always)] + fn mul_assign(&mut self, other: Self) { + *self = *self * other; + } + } + impl Div for u128 { + type Output = Self; + #[inline(always)] + fn div(self, other: Self) -> Self { + let a: std::primitive::u128 = self.into(); + + let b: std::primitive::u128 = other.into(); + Self::from(a / b) + } + } + impl DivAssign for u128 { + #[inline(always)] + fn div_assign(&mut self, other: Self) { + *self = *self / other; + } + } + + /// `i128` with legacy bit layout + #[derive(Copy, Clone, PartialEq, Eq, Debug, Default, BorshSerialize, BorshDeserialize)] + pub struct i128([u8; 16]); + + impl std::fmt::Display for self::i128 { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(&self.as_i128(), f) + } + } + + // Safety: i128 is a transparent wrapper around [u8; 16], which is Pod + Zeroable + unsafe impl Pod for self::i128 {} + unsafe impl Zeroable for self::i128 {} + impl i128 { + pub const ONE: Self = i128(1_i128.to_le_bytes()); + pub const ZERO: Self = i128(0_i128.to_le_bytes()); + + pub const fn one() -> Self { + Self::ONE + } + pub const fn zero() -> Self { + Self::ZERO + } + + pub fn abs(&self) -> std::primitive::i128 { + self.as_i128().abs() + } + + pub fn unsigned_abs(&self) -> std::primitive::u128 { + self.as_i128().unsigned_abs() + } + + /// convert to std i128 + #[inline] + pub fn as_i128(self) -> std::primitive::i128 { + std::primitive::i128::from_le_bytes(self.0) + } + + pub fn cast>(&self) -> DriftResult { + self.as_i128().cast() + } + } + + impl num_traits::Zero for self::i128 { + fn zero() -> Self { + Self::ZERO + } + fn is_zero(&self) -> bool { + self.0 == Self::ZERO.0 + } + } + + impl std::ops::Neg for self::i128 { + type Output = std::primitive::i128; + fn neg(self) -> Self::Output { + self.as_i128().neg() + } + } + + impl From for i128 { + fn from(value: std::primitive::i128) -> Self { + i128(value.to_le_bytes()) + } + } + impl From for std::primitive::i128 { + fn from(value: i128) -> Self { + value.as_i128() + } + } + + // Arithmetic operations for i128 - using From/Into conversions + impl Add for i128 { + type Output = Self; + #[inline(always)] + fn add(self, other: Self) -> Self { + let a: std::primitive::i128 = self.into(); + + let b: std::primitive::i128 = other.into(); + Self::from(a + b) + } + } + impl AddAssign for i128 { + #[inline(always)] + fn add_assign(&mut self, other: Self) { + *self = *self + other; + } + } + impl Sub for i128 { + type Output = Self; + #[inline(always)] + fn sub(self, other: Self) -> Self { + let a: std::primitive::i128 = self.into(); + + let b: std::primitive::i128 = other.into(); + Self::from(a - b) + } + } + impl SubAssign for i128 { + #[inline(always)] + fn sub_assign(&mut self, other: Self) { + *self = *self - other; + } + } + impl Mul for i128 { + type Output = Self; + #[inline(always)] + fn mul(self, other: Self) -> Self { + let a: std::primitive::i128 = self.into(); + + let b: std::primitive::i128 = other.into(); + Self::from(a * b) + } + } + impl MulAssign for i128 { + #[inline(always)] + fn mul_assign(&mut self, other: Self) { + *self = *self * other; + } + } + impl Div for i128 { + type Output = Self; + #[inline(always)] + fn div(self, other: Self) -> Self { + let a: std::primitive::i128 = self.into(); + + let b: std::primitive::i128 = other.into(); + Self::from(a / b) + } + } + impl DivAssign for i128 { + #[inline(always)] + fn div_assign(&mut self, other: Self) { + *self = *self / other; + } + } +} + construct_uint! { /// 256-bit unsigned integer. pub struct U256(4); diff --git a/programs/drift/src/math/cp_curve.rs b/programs/drift/src/math/cp_curve.rs index 2ac13376f9..2a76fc6086 100644 --- a/programs/drift/src/math/cp_curve.rs +++ b/programs/drift/src/math/cp_curve.rs @@ -41,11 +41,11 @@ pub fn calculate_budgeted_k_scale( )?; let (numerator, denominator) = _calculate_budgeted_k_scale( - market.amm.base_asset_reserve, - market.amm.quote_asset_reserve, + market.amm.base_asset_reserve(), + market.amm.quote_asset_reserve(), budget, - market.amm.peg_multiplier, - market.amm.base_asset_amount_with_amm, + market.amm.peg_multiplier(), + market.amm.base_asset_amount_with_amm(), k_pct_upper_bound, k_pct_lower_bound, )?; @@ -176,7 +176,7 @@ pub fn adjust_k_cost( // Find the net market value before adjusting k let (current_net_market_value, _) = calculate_base_asset_value_and_pnl( - market_clone.amm.base_asset_amount_with_amm, + market_clone.amm.base_asset_amount_with_amm(), 0, &market_clone.amm, )?; @@ -184,7 +184,7 @@ pub fn adjust_k_cost( update_k(&mut market_clone, update_k_result)?; let (_new_net_market_value, cost) = calculate_base_asset_value_and_pnl( - market_clone.amm.base_asset_amount_with_amm, + market_clone.amm.base_asset_amount_with_amm(), current_net_market_value, &market_clone.amm, )?; @@ -201,12 +201,12 @@ pub fn adjust_k_cost_and_update( ) -> DriftResult { // Find the net market value before adjusting k let current_net_market_value = - calculate_base_asset_value(market.amm.base_asset_amount_with_amm, &market.amm)?; + calculate_base_asset_value(market.amm.base_asset_amount_with_amm(), &market.amm)?; update_k(market, update_k_result)?; let (_new_net_market_value, cost) = calculate_base_asset_value_and_pnl( - market.amm.base_asset_amount_with_amm, + market.amm.base_asset_amount_with_amm(), current_net_market_value, &market.amm, )?; @@ -227,7 +227,7 @@ pub fn get_update_k_result( ) -> DriftResult { let sqrt_k_ratio_precision = bn::U192::from(AMM_RESERVE_PRECISION); - let old_sqrt_k = bn::U192::from(market.amm.sqrt_k); + let old_sqrt_k = bn::U192::from(market.amm.sqrt_k()); let mut sqrt_k_ratio = new_sqrt_k .safe_mul(sqrt_k_ratio_precision)? .safe_div(old_sqrt_k)?; @@ -253,14 +253,14 @@ pub fn get_update_k_result( // only allow too small when market is in reduce only mode if market.status != MarketStatus::ReduceOnly && new_sqrt_k < old_sqrt_k - && market.amm.base_asset_amount_with_amm.unsigned_abs() > sqrt_k + && market.amm.base_asset_amount_with_amm().unsigned_abs() > sqrt_k { // todo, check less lp_tokens as well msg!("new_sqrt_k too small relative to market imbalance"); return Err(ErrorCode::InvalidUpdateK); } - let base_asset_reserve = bn::U192::from(market.amm.base_asset_reserve) + let base_asset_reserve = bn::U192::from(market.amm.base_asset_reserve()) .safe_mul(sqrt_k_ratio)? .safe_div(sqrt_k_ratio_precision)? .try_to_u128()?; @@ -280,18 +280,28 @@ pub fn get_update_k_result( } pub fn update_k(market: &mut PerpMarket, update_k_result: &UpdateKResult) -> DriftResult { - market.amm.base_asset_reserve = update_k_result.base_asset_reserve; - market.amm.quote_asset_reserve = update_k_result.quote_asset_reserve; - market.amm.sqrt_k = update_k_result.sqrt_k; + market + .amm + .set_base_asset_reserve(update_k_result.base_asset_reserve); + market + .amm + .set_quote_asset_reserve(update_k_result.quote_asset_reserve); + market.amm.set_sqrt_k(update_k_result.sqrt_k); let (new_terminal_quote_reserve, new_terminal_base_reserve) = amm::calculate_terminal_reserves(&market.amm)?; - market.amm.terminal_quote_asset_reserve = new_terminal_quote_reserve; + market + .amm + .set_terminal_quote_asset_reserve(new_terminal_quote_reserve); let (min_base_asset_reserve, max_base_asset_reserve) = - amm::calculate_bid_ask_bounds(market.amm.concentration_coef, new_terminal_base_reserve)?; - market.amm.min_base_asset_reserve = min_base_asset_reserve; - market.amm.max_base_asset_reserve = max_base_asset_reserve; + amm::calculate_bid_ask_bounds(market.amm.concentration_coef(), new_terminal_base_reserve)?; + market + .amm + .set_min_base_asset_reserve(min_base_asset_reserve); + market + .amm + .set_max_base_asset_reserve(max_base_asset_reserve); let reserve_price_after = market.amm.reserve_price()?; crate::controller::amm::update_spreads(market, reserve_price_after, None)?; diff --git a/programs/drift/src/math/cp_curve/tests.rs b/programs/drift/src/math/cp_curve/tests.rs index b6c8b667b7..6b4379b3bf 100644 --- a/programs/drift/src/math/cp_curve/tests.rs +++ b/programs/drift/src/math/cp_curve/tests.rs @@ -10,9 +10,9 @@ use crate::state::perp_market::AMM; fn k_update_results_bound_flag() { let init_reserves = 100 * AMM_RESERVE_PRECISION; let amm = AMM { - sqrt_k: init_reserves, - base_asset_reserve: init_reserves, - quote_asset_reserve: init_reserves, + sqrt_k: init_reserves.into(), + base_asset_reserve: init_reserves.into(), + quote_asset_reserve: init_reserves.into(), ..AMM::default() }; let market = PerpMarket { @@ -32,18 +32,18 @@ fn k_update_results_bound_flag() { fn calculate_k_tests_with_spread() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 512295081967, - quote_asset_reserve: 488 * AMM_RESERVE_PRECISION, - concentration_coef: MAX_CONCENTRATION_COEFFICIENT, - sqrt_k: 500 * AMM_RESERVE_PRECISION, - peg_multiplier: 50000000, - base_asset_amount_with_amm: -12295081967, + base_asset_reserve: 512295081967.into(), + quote_asset_reserve: (488 * AMM_RESERVE_PRECISION).into(), + concentration_coef: MAX_CONCENTRATION_COEFFICIENT.into(), + sqrt_k: (500 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 50000000.into(), + base_asset_amount_with_amm: (-12295081967).into(), ..AMM::default() }, ..PerpMarket::default() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); market.amm.base_spread = 10; market.amm.long_spread = 5; market.amm.short_spread = 5; @@ -55,21 +55,29 @@ fn calculate_k_tests_with_spread() { crate::math::amm_spread::calculate_spread_reserves(&market, PositionDirection::Short) .unwrap(); - market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve; - market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve; - market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve; - market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve; + market + .amm + .set_ask_base_asset_reserve(new_ask_base_asset_reserve); + market + .amm + .set_bid_base_asset_reserve(new_bid_base_asset_reserve); + market + .amm + .set_ask_quote_asset_reserve(new_ask_quote_asset_reserve); + market + .amm + .set_bid_quote_asset_reserve(new_bid_quote_asset_reserve); validate!( - market.amm.bid_base_asset_reserve >= market.amm.base_asset_reserve - && market.amm.bid_quote_asset_reserve <= market.amm.quote_asset_reserve, + market.amm.bid_base_asset_reserve() >= market.amm.base_asset_reserve() + && market.amm.bid_quote_asset_reserve() <= market.amm.quote_asset_reserve(), ErrorCode::InvalidAmmDetected, "market index {} amm bid reserves invalid: {} -> {}, quote: {} -> {}", market.market_index, - market.amm.bid_base_asset_reserve, - market.amm.base_asset_reserve, - market.amm.bid_quote_asset_reserve, - market.amm.quote_asset_reserve + market.amm.bid_base_asset_reserve(), + market.amm.base_asset_reserve(), + market.amm.bid_quote_asset_reserve(), + market.amm.quote_asset_reserve() ) .unwrap(); @@ -79,14 +87,14 @@ fn calculate_k_tests_with_spread() { update_k(&mut market, &update_k_result).unwrap(); validate!( - market.amm.bid_base_asset_reserve >= market.amm.base_asset_reserve - && market.amm.bid_quote_asset_reserve <= market.amm.quote_asset_reserve, + market.amm.bid_base_asset_reserve() >= market.amm.base_asset_reserve() + && market.amm.bid_quote_asset_reserve() <= market.amm.quote_asset_reserve(), ErrorCode::InvalidAmmDetected, "bid reserves out of wack: {} -> {}, quote: {} -> {}", - market.amm.bid_base_asset_reserve, - market.amm.base_asset_reserve, - market.amm.bid_quote_asset_reserve, - market.amm.quote_asset_reserve + market.amm.bid_base_asset_reserve(), + market.amm.base_asset_reserve(), + market.amm.bid_quote_asset_reserve(), + market.amm.quote_asset_reserve() ) .unwrap(); } @@ -103,12 +111,12 @@ fn calculate_k_with_rounding() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve, - quote_asset_reserve, - concentration_coef: MAX_CONCENTRATION_COEFFICIENT, - sqrt_k: 10000000000000000000, - peg_multiplier, - base_asset_amount_with_amm, + base_asset_reserve: base_asset_reserve.into(), + quote_asset_reserve: quote_asset_reserve.into(), + concentration_coef: MAX_CONCENTRATION_COEFFICIENT.into(), + sqrt_k: 10000000000000000000.into(), + peg_multiplier: peg_multiplier.into(), + base_asset_amount_with_amm: base_asset_amount_with_amm.into(), ..AMM::default() }, ..PerpMarket::default() @@ -132,7 +140,7 @@ fn calculate_k_with_rounding() { let k_scale_numerator: u128 = 373175; let k_scale_denominator: u128 = 340980; - let new_sqrt_k = bn::U192::from(market.amm.sqrt_k) + let new_sqrt_k = bn::U192::from(market.amm.sqrt_k()) .safe_mul(bn::U192::from(k_scale_numerator)) .unwrap() .safe_div(bn::U192::from(k_scale_denominator)) @@ -149,12 +157,12 @@ fn calculate_k_with_rounding() { fn calculate_k_tests() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 512295081967, - quote_asset_reserve: 488 * AMM_RESERVE_PRECISION, - concentration_coef: MAX_CONCENTRATION_COEFFICIENT, - sqrt_k: 500 * AMM_RESERVE_PRECISION, - peg_multiplier: 50000000, - base_asset_amount_with_amm: -12295081967, + base_asset_reserve: 512295081967.into(), + quote_asset_reserve: (488 * AMM_RESERVE_PRECISION).into(), + concentration_coef: MAX_CONCENTRATION_COEFFICIENT.into(), + sqrt_k: (500 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 50000000.into(), + base_asset_amount_with_amm: (-12295081967).into(), ..AMM::default() }, ..PerpMarket::default() @@ -167,7 +175,7 @@ fn calculate_k_tests() { // new terminal reserves are balanced, terminal price = peg) assert_eq!(t_qar, 500 * AMM_RESERVE_PRECISION); assert_eq!(t_bar, 500 * AMM_RESERVE_PRECISION); - assert_eq!(t_price as u128, market.amm.peg_multiplier); + assert_eq!(t_price as u128, market.amm.peg_multiplier()); assert_eq!(update_k_up.sqrt_k, 501 * AMM_RESERVE_PRECISION); assert_eq!(update_k_up.base_asset_reserve, 513319672130); @@ -175,7 +183,7 @@ fn calculate_k_tests() { // cost to increase k is always positive when imbalanced let cost = adjust_k_cost_and_update(&mut market, &update_k_up).unwrap(); - assert_eq!(market.amm.terminal_quote_asset_reserve, 500975411043); + assert_eq!(market.amm.terminal_quote_asset_reserve(), 500975411043); assert!(cost > 0); assert_eq!(cost, 29448); @@ -279,11 +287,11 @@ fn calculate_k_tests() { fn calculate_k_tests_wrapper_fcn() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: AMM_RESERVE_PRECISION * 55414, - quote_asset_reserve: AMM_RESERVE_PRECISION * 55530, - sqrt_k: 500 * AMM_RESERVE_PRECISION, - peg_multiplier: 36365000, - base_asset_amount_with_amm: (AMM_RESERVE_PRECISION * 66) as i128, + base_asset_reserve: (AMM_RESERVE_PRECISION * 55414).into(), + quote_asset_reserve: (AMM_RESERVE_PRECISION * 55530).into(), + sqrt_k: (500 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 36365000.into(), + base_asset_amount_with_amm: ((AMM_RESERVE_PRECISION * 66) as i128).into(), ..AMM::default() }, ..PerpMarket::default() @@ -309,13 +317,13 @@ fn calculate_k_tests_wrapper_fcn() { fn amm_spread_adj_logic() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - terminal_quote_asset_reserve: 999900009999000 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 50_000_000_000, - base_asset_amount_with_amm: (AMM_RESERVE_PRECISION / 10) as i128, - base_asset_amount_long: (AMM_RESERVE_PRECISION / 10) as i128, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + terminal_quote_asset_reserve: (999900009999000 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 50_000_000_000.into(), + base_asset_amount_with_amm: ((AMM_RESERVE_PRECISION / 10) as i128).into(), + base_asset_amount_long: ((AMM_RESERVE_PRECISION / 10) as i128).into(), order_step_size: 5, base_spread: 100, max_spread: 10000, @@ -329,8 +337,10 @@ fn amm_spread_adj_logic() { // todo fix this - market.amm.base_asset_amount_per_lp = 1; - market.amm.quote_asset_amount_per_lp = -QUOTE_PRECISION_I64 as i128; + market.amm.set_base_asset_amount_per_lp(1); + market + .amm + .set_quote_asset_amount_per_lp(-QUOTE_PRECISION_I64 as i128); let reserve_price = market.amm.reserve_price().unwrap(); update_spreads(&mut market, reserve_price, None).unwrap(); diff --git a/programs/drift/src/math/fees.rs b/programs/drift/src/math/fees.rs index 8431be24bf..d227f1cf18 100644 --- a/programs/drift/src/math/fees.rs +++ b/programs/drift/src/math/fees.rs @@ -265,6 +265,7 @@ fn calculate_filler_reward( let min_time_filler_reward = filler_reward_structure .time_based_reward_lower_bound + .as_u128() .safe_mul( multiplier .cast::()? diff --git a/programs/drift/src/math/fees/tests.rs b/programs/drift/src/math/fees/tests.rs index 296f3bfce5..d24470c161 100644 --- a/programs/drift/src/math/fees/tests.rs +++ b/programs/drift/src/math/fees/tests.rs @@ -53,7 +53,7 @@ mod calculate_fee_for_taker_and_maker { let mut fee_structure = FeeStructure::test_default(); fee_structure .filler_reward_structure - .time_based_reward_lower_bound = 10000000000000000; // big number + .time_based_reward_lower_bound = 10000000000000000.into(); // big number let FillFees { user_fee: taker_fee, @@ -1020,7 +1020,7 @@ mod calcuate_fee_tiers { filler_reward_structure: OrderFillerRewardStructure { reward_numerator: 10, reward_denominator: FEE_PERCENTAGE_DENOMINATOR, - time_based_reward_lower_bound: 10_000, // 1 cent + time_based_reward_lower_bound: 10_000.into(), // 1 cent }, flat_filler_fee: 10_000, referrer_reward_epoch_upper_bound: MAX_REFERRER_REWARD_EPOCH_UPPER_BOUND, diff --git a/programs/drift/src/math/floor_div.rs b/programs/drift/src/math/floor_div.rs index fb1fa0ed2a..9e07ed8c68 100644 --- a/programs/drift/src/math/floor_div.rs +++ b/programs/drift/src/math/floor_div.rs @@ -12,7 +12,6 @@ macro_rules! checked_impl { #[inline] fn checked_floor_div(&self, rhs: $t) -> Option<$t> { let quotient = self.checked_div(rhs)?; - let remainder = self.checked_rem(rhs)?; if remainder != <$t>::zero() { diff --git a/programs/drift/src/math/fulfillment/tests.rs b/programs/drift/src/math/fulfillment/tests.rs index 005bbefa63..c99da241d8 100644 --- a/programs/drift/src/math/fulfillment/tests.rs +++ b/programs/drift/src/math/fulfillment/tests.rs @@ -14,14 +14,14 @@ mod determine_perp_fulfillment_methods { fn amm_available_and_taker_doesnt_cross_maker() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -41,8 +41,8 @@ mod determine_perp_fulfillment_methods { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); let taker_order = Order { direction: PositionDirection::Long, @@ -69,14 +69,14 @@ mod determine_perp_fulfillment_methods { fn amm_available_and_maker_better_than_amm() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -96,8 +96,8 @@ mod determine_perp_fulfillment_methods { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); let taker_order = Order { direction: PositionDirection::Long, @@ -130,14 +130,14 @@ mod determine_perp_fulfillment_methods { fn amm_available_and_amm_better_than_maker() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -157,8 +157,8 @@ mod determine_perp_fulfillment_methods { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); let taker_order = Order { direction: PositionDirection::Long, @@ -198,14 +198,14 @@ mod determine_perp_fulfillment_methods { fn maker_amm_maker_amm_maker_ask() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -225,8 +225,8 @@ mod determine_perp_fulfillment_methods { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); let taker_order = Order { direction: PositionDirection::Long, @@ -264,14 +264,14 @@ mod determine_perp_fulfillment_methods { fn maker_maker_amm_ask() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -291,8 +291,8 @@ mod determine_perp_fulfillment_methods { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); let taker_order = Order { direction: PositionDirection::Long, @@ -337,14 +337,14 @@ mod determine_perp_fulfillment_methods { fn amm_maker_amm_maker_amm_ask() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -364,8 +364,8 @@ mod determine_perp_fulfillment_methods { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); let taker_order = Order { direction: PositionDirection::Long, @@ -404,14 +404,14 @@ mod determine_perp_fulfillment_methods { fn maker_amm_maker_amm_maker_bid() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -431,8 +431,8 @@ mod determine_perp_fulfillment_methods { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); let taker_order = Order { direction: PositionDirection::Short, @@ -470,14 +470,14 @@ mod determine_perp_fulfillment_methods { fn maker_maker_amm_bid() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -497,8 +497,8 @@ mod determine_perp_fulfillment_methods { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); let taker_order = Order { direction: PositionDirection::Short, @@ -535,14 +535,14 @@ mod determine_perp_fulfillment_methods { fn amm_maker_amm_maker_amm_bid() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -562,8 +562,8 @@ mod determine_perp_fulfillment_methods { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); let taker_order = Order { direction: PositionDirection::Short, @@ -602,14 +602,14 @@ mod determine_perp_fulfillment_methods { fn no_asks() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -629,8 +629,8 @@ mod determine_perp_fulfillment_methods { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); let taker_order = Order { direction: PositionDirection::Long, @@ -660,14 +660,14 @@ mod determine_perp_fulfillment_methods { fn no_bids() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -687,8 +687,8 @@ mod determine_perp_fulfillment_methods { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); let taker_order = Order { direction: PositionDirection::Short, @@ -718,14 +718,14 @@ mod determine_perp_fulfillment_methods { fn amm_available_and_maker_crosses() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -745,8 +745,8 @@ mod determine_perp_fulfillment_methods { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); let taker_order = Order { post_only: true, @@ -774,14 +774,14 @@ mod determine_perp_fulfillment_methods { fn amm_available_and_maker_doesnt_cross() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 10000000, @@ -801,8 +801,8 @@ mod determine_perp_fulfillment_methods { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); let taker_order = Order { post_only: true, diff --git a/programs/drift/src/math/funding.rs b/programs/drift/src/math/funding.rs index c723cb37bb..09c915027d 100644 --- a/programs/drift/src/math/funding.rs +++ b/programs/drift/src/math/funding.rs @@ -27,7 +27,7 @@ pub fn calculate_funding_rate_long_short( ) -> DriftResult<(i128, i128, i128)> { // Calculate the funding payment owed by the net_market_position if funding is not capped // If the net market position owes funding payment, the protocol receives payment - let settled_net_market_position = market.amm.base_asset_amount_with_amm; + let settled_net_market_position = market.amm.base_asset_amount_with_amm(); let net_market_position_funding_payment = calculate_funding_payment_in_quote_precision(funding_rate, settled_net_market_position)?; @@ -35,10 +35,12 @@ pub fn calculate_funding_rate_long_short( // If the uncapped_funding_pnl is positive, the protocol receives money. if uncapped_funding_pnl >= 0 { - market.amm.total_fee_minus_distributions = market - .amm - .total_fee_minus_distributions - .safe_add(uncapped_funding_pnl)?; + market.amm.set_total_fee_minus_distributions( + market + .amm + .total_fee_minus_distributions() + .safe_add(uncapped_funding_pnl)?, + ); market.amm.net_revenue_since_last_funding = market .amm @@ -53,7 +55,7 @@ pub fn calculate_funding_rate_long_short( let new_total_fee_minus_distributions = market .amm - .total_fee_minus_distributions + .total_fee_minus_distributions() .safe_add(capped_funding_pnl)?; // protocol is paying part of funding imbalance @@ -67,7 +69,9 @@ pub fn calculate_funding_rate_long_short( return Err(ErrorCode::InvalidFundingProfitability); } } - market.amm.total_fee_minus_distributions = new_total_fee_minus_distributions; + market + .amm + .set_total_fee_minus_distributions(new_total_fee_minus_distributions); market.amm.net_revenue_since_last_funding = market .amm .net_revenue_since_last_funding @@ -107,9 +111,9 @@ fn calculate_capped_funding_rate( let funding_payment_from_users = calculate_funding_payment_in_quote_precision( funding_rate, if funding_rate > 0 { - market.amm.base_asset_amount_long + market.amm.base_asset_amount_long() } else { - market.amm.base_asset_amount_short + market.amm.base_asset_amount_short() }, )?; @@ -122,13 +126,13 @@ fn calculate_capped_funding_rate( // longs receive calculate_funding_rate_from_pnl_limit( funding_rate_pnl_limit, - market.amm.base_asset_amount_long, + market.amm.base_asset_amount_long(), )? } else { // shorts receive calculate_funding_rate_from_pnl_limit( funding_rate_pnl_limit, - market.amm.base_asset_amount_short, + market.amm.base_asset_amount_short(), )? } } else { diff --git a/programs/drift/src/math/funding/tests.rs b/programs/drift/src/math/funding/tests.rs index fb46a5cbce..b8771fb492 100644 --- a/programs/drift/src/math/funding/tests.rs +++ b/programs/drift/src/math/funding/tests.rs @@ -63,15 +63,15 @@ fn balanced_funding_test() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: sqrt_k, - quote_asset_reserve: sqrt_k, - sqrt_k, - peg_multiplier: px, - base_asset_amount_with_amm: 0, - base_asset_amount_long: 12295081967, - base_asset_amount_short: -12295081967, - total_exchange_fee: (count * 1000000783) / 2888, - total_fee_minus_distributions: (count * 1000000783) as i128, + base_asset_reserve: sqrt_k.into(), + quote_asset_reserve: sqrt_k.into(), + sqrt_k: sqrt_k.into(), + peg_multiplier: px.into(), + base_asset_amount_with_amm: 0.into(), + base_asset_amount_long: 12295081967.into(), + base_asset_amount_short: (-12295081967).into(), + total_exchange_fee: ((count * 1000000783) / 2888).into(), + total_fee_minus_distributions: ((count * 1000000783) as i128).into(), last_mark_price_twap: (px * 999 / 1000) as u64, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: (px * 1001 / 1000) as i64, @@ -115,15 +115,15 @@ fn balanced_funding_test() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: sqrt_k, - quote_asset_reserve: sqrt_k, - sqrt_k, - peg_multiplier: px, - base_asset_amount_with_amm: 0, - base_asset_amount_long: 7845926098328, - base_asset_amount_short: -7845926098328, - total_exchange_fee: (count * 1000000783) / 2888, - total_fee_minus_distributions: (count * 1000000783) as i128, + base_asset_reserve: sqrt_k.into(), + quote_asset_reserve: sqrt_k.into(), + sqrt_k: sqrt_k.into(), + peg_multiplier: px.into(), + base_asset_amount_with_amm: 0.into(), + base_asset_amount_long: 7845926098328.into(), + base_asset_amount_short: (-7845926098328).into(), + total_exchange_fee: ((count * 1000000783) / 2888).into(), + total_fee_minus_distributions: ((count * 1000000783) as i128).into(), last_mark_price_twap: (px * 999 / 1000) as u64, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: (px * 888 / 1000) as i64, @@ -172,15 +172,15 @@ fn capped_sym_funding_test() { // more shorts than longs, positive funding, 1/3 of fee pool too small let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 512295081967, - quote_asset_reserve: 488 * AMM_RESERVE_PRECISION, - sqrt_k: 500 * AMM_RESERVE_PRECISION, - peg_multiplier: 50000000, - base_asset_amount_with_amm: -12295081967, - base_asset_amount_long: 12295081967, - base_asset_amount_short: -12295081967 * 2, - total_exchange_fee: QUOTE_PRECISION / 2, - total_fee_minus_distributions: (QUOTE_PRECISION as i128) / 2, + base_asset_reserve: 512295081967.into(), + quote_asset_reserve: (488 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (500 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 50000000.into(), + base_asset_amount_with_amm: (-12295081967).into(), + base_asset_amount_long: 12295081967.into(), + base_asset_amount_short: (-12295081967 * 2).into(), + total_exchange_fee: (QUOTE_PRECISION / 2).into(), + total_fee_minus_distributions: ((QUOTE_PRECISION as i128) / 2).into(), last_mark_price_twap: 50 * PRICE_PRECISION_U64, historical_oracle_data: HistoricalOracleData { @@ -212,20 +212,20 @@ fn capped_sym_funding_test() { assert_eq!(short_funding, 24222164); // only spend 1/3 of fee pool, ((.5-.416667)) * 3 < .25 - assert_eq!(market.amm.total_fee_minus_distributions, 416667); + assert_eq!(market.amm.total_fee_minus_distributions(), 416667); // more longs than shorts, positive funding, amm earns funding market = PerpMarket { amm: AMM { - base_asset_reserve: 512295081967, - quote_asset_reserve: 488 * AMM_RESERVE_PRECISION, - sqrt_k: 500 * AMM_RESERVE_PRECISION, - peg_multiplier: 50000000, - base_asset_amount_with_amm: 12295081967, - base_asset_amount_long: 12295081967 * 2, - base_asset_amount_short: -12295081967, - total_exchange_fee: QUOTE_PRECISION / 2, - total_fee_minus_distributions: (QUOTE_PRECISION as i128) / 2, + base_asset_reserve: 512295081967.into(), + quote_asset_reserve: (488 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (500 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 50000000.into(), + base_asset_amount_with_amm: 12295081967.into(), + base_asset_amount_long: (12295081967 * 2).into(), + base_asset_amount_short: (-12295081967).into(), + total_exchange_fee: (QUOTE_PRECISION / 2).into(), + total_fee_minus_distributions: ((QUOTE_PRECISION as i128) / 2).into(), last_mark_price_twap: 50 * PRICE_PRECISION_U64, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: (49 * PRICE_PRECISION) as i64, @@ -246,7 +246,7 @@ fn capped_sym_funding_test() { assert_eq!(long_funding, balanced_funding); assert_eq!(long_funding, short_funding); - let new_fees = market.amm.total_fee_minus_distributions; + let new_fees = market.amm.total_fee_minus_distributions(); assert!(new_fees > QUOTE_PRECISION as i128 / 2); assert_eq!(new_fees, 1012295); // made over $.50 } @@ -285,16 +285,16 @@ fn max_funding_rates() { amm: AMM { oracle: oracle_price_key, - base_asset_reserve: 512295081967, - quote_asset_reserve: 488 * AMM_RESERVE_PRECISION, - sqrt_k: 500 * AMM_RESERVE_PRECISION, - peg_multiplier: 50000000, - base_asset_amount_with_amm: -12295081967, //~12 - base_asset_amount_long: 12295081967, - base_asset_amount_short: -12295081967 * 2, - base_asset_amount_with_unsettled_lp: -((AMM_RESERVE_PRECISION * 500) as i128), //wowsers - total_exchange_fee: QUOTE_PRECISION / 2, - total_fee_minus_distributions: ((QUOTE_PRECISION * 99999) as i128), + base_asset_reserve: 512295081967.into(), + quote_asset_reserve: (488 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (500 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 50000000.into(), + base_asset_amount_with_amm: (-12295081967).into(), //~12 + base_asset_amount_long: 12295081967.into(), + base_asset_amount_short: (-12295081967 * 2).into(), + base_asset_amount_with_unsettled_lp: (-((AMM_RESERVE_PRECISION * 500) as i128)).into(), //wowsers + total_exchange_fee: (QUOTE_PRECISION / 2).into(), + total_fee_minus_distributions: ((QUOTE_PRECISION * 99999) as i128).into(), last_mark_price_twap: 50 * PRICE_PRECISION_U64, historical_oracle_data: HistoricalOracleData { @@ -372,15 +372,16 @@ fn unsettled_funding_pnl() { amm: AMM { oracle: oracle_price_key, - base_asset_reserve: 512295081967, - quote_asset_reserve: 488 * AMM_RESERVE_PRECISION, - sqrt_k: 500 * AMM_RESERVE_PRECISION, - peg_multiplier: 50000000, - base_asset_amount_with_amm: -12295081967 + -((AMM_RESERVE_PRECISION * 500) as i128), //~ 12 - 500 - base_asset_amount_long: 12295081967, - base_asset_amount_short: -12295081967 * 2, - total_exchange_fee: QUOTE_PRECISION / 2, - total_fee_minus_distributions: ((QUOTE_PRECISION * 99999) as i128), + base_asset_reserve: 512295081967.into(), + quote_asset_reserve: (488 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (500 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 50000000.into(), + base_asset_amount_with_amm: (-12295081967 + -((AMM_RESERVE_PRECISION * 500) as i128)) + .into(), //~ 12 - 500 + base_asset_amount_long: 12295081967.into(), + base_asset_amount_short: (-12295081967 * 2).into(), + total_exchange_fee: (QUOTE_PRECISION / 2).into(), + total_fee_minus_distributions: ((QUOTE_PRECISION * 99999) as i128).into(), last_mark_price_twap: 50 * PRICE_PRECISION_U64, historical_oracle_data: HistoricalOracleData { @@ -455,7 +456,7 @@ fn unsettled_funding_pnl() { ) .unwrap(); assert_eq!(block_funding_rate_update, false); - assert_eq!(market.amm.total_fee_minus_distributions, 99999000000); + assert_eq!(market.amm.total_fee_minus_distributions(), 99999000000); let did_succeed = update_funding_rate( 0, @@ -477,14 +478,14 @@ fn unsettled_funding_pnl() { 51000000 ); - assert_eq!(market.amm.cumulative_funding_rate_long, -140002666); // negative funding - assert_eq!(market.amm.cumulative_funding_rate_short, -140002666); + assert_eq!(market.amm.cumulative_funding_rate_long(), -140002666); // negative funding + assert_eq!(market.amm.cumulative_funding_rate_short(), -140002666); assert_eq!(market.amm.last_funding_rate, -140002666); assert_eq!(market.amm.last_24h_avg_funding_rate, -140002666 / 24 + 1); assert_eq!(market.amm.last_funding_rate_ts, now); assert_eq!(market.amm.net_revenue_since_last_funding, 0); // back to 0 - assert_eq!(market.amm.total_fee_minus_distributions, 100070722677); //71.722677 gain - assert_eq!(market.amm.total_fee, 0); + assert_eq!(market.amm.total_fee_minus_distributions(), 100070722677); //71.722677 gain + assert_eq!(market.amm.total_fee(), 0); assert_ne!(market.amm.net_unsettled_funding_pnl, 0); // important: imbalanced market adds funding rev assert_eq!(market.amm.net_unsettled_funding_pnl, -71722677); // users up diff --git a/programs/drift/src/math/insurance.rs b/programs/drift/src/math/insurance.rs index 4658f97db6..10329814c6 100644 --- a/programs/drift/src/math/insurance.rs +++ b/programs/drift/src/math/insurance.rs @@ -87,18 +87,21 @@ pub fn calculate_if_shares_lost( spot_market: &SpotMarket, insurance_fund_vault_balance: u64, ) -> DriftResult { - let n_shares = insurance_fund_stake.last_withdraw_request_shares; + let n_shares = insurance_fund_stake.last_withdraw_request_shares(); let amount = if_shares_to_vault_amount( n_shares, - spot_market.insurance_fund.total_shares, + spot_market.insurance_fund.total_shares(), insurance_fund_vault_balance, )?; let if_shares_lost = if amount > insurance_fund_stake.last_withdraw_request_value { let new_n_shares = vault_amount_to_if_shares( insurance_fund_stake.last_withdraw_request_value, - spot_market.insurance_fund.total_shares.safe_sub(n_shares)?, + spot_market + .insurance_fund + .total_shares() + .safe_sub(n_shares)?, insurance_fund_vault_balance .safe_sub(insurance_fund_stake.last_withdraw_request_value)?, )?; diff --git a/programs/drift/src/math/insurance/tests.rs b/programs/drift/src/math/insurance/tests.rs index 46287cedb3..69cb54095f 100644 --- a/programs/drift/src/math/insurance/tests.rs +++ b/programs/drift/src/math/insurance/tests.rs @@ -101,12 +101,12 @@ pub fn basic_stake_if_test() { pub fn if_shares_lost_test() { let _amount = QUOTE_PRECISION as u64; // $1 let mut spot_market = SpotMarket { - deposit_balance: 0, - cumulative_deposit_interest: 1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000, + deposit_balance: 0.into(), + cumulative_deposit_interest: (1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000).into(), insurance_fund: InsuranceFund { unstaking_period: 0, - total_shares: 1000 * QUOTE_PRECISION, - user_shares: 1000 * QUOTE_PRECISION, + total_shares: (1000 * QUOTE_PRECISION).into(), + user_shares: (1000 * QUOTE_PRECISION).into(), ..InsuranceFund::default() }, ..SpotMarket::default() @@ -116,7 +116,7 @@ pub fn if_shares_lost_test() { if_stake .update_if_shares(100 * QUOTE_PRECISION, &spot_market) .unwrap(); - if_stake.last_withdraw_request_shares = 100 * QUOTE_PRECISION; + if_stake.set_last_withdraw_request_shares(100 * QUOTE_PRECISION); if_stake.last_withdraw_request_value = ((100 * QUOTE_PRECISION) - 1) as u64; let if_balance = (1000 * QUOTE_PRECISION) as u64; @@ -126,14 +126,38 @@ pub fn if_shares_lost_test() { assert_eq!(lost_shares, 2); let if_balance = if_balance + (100 * QUOTE_PRECISION) as u64; - spot_market.insurance_fund.total_shares += 100 * QUOTE_PRECISION; - spot_market.insurance_fund.user_shares += 100 * QUOTE_PRECISION; + spot_market.insurance_fund.set_total_shares( + spot_market + .insurance_fund + .total_shares() + .safe_add(100 * QUOTE_PRECISION) + .unwrap(), + ); + spot_market.insurance_fund.set_user_shares( + spot_market + .insurance_fund + .user_shares() + .safe_add(100 * QUOTE_PRECISION) + .unwrap(), + ); let lost_shares = calculate_if_shares_lost(&if_stake, &spot_market, if_balance).unwrap(); assert_eq!(lost_shares, 2); // giving up $5 of gains let if_balance = if_balance - (100 * QUOTE_PRECISION) as u64; - spot_market.insurance_fund.total_shares -= 100 * QUOTE_PRECISION; - spot_market.insurance_fund.user_shares -= 100 * QUOTE_PRECISION; + spot_market.insurance_fund.set_total_shares( + spot_market + .insurance_fund + .total_shares() + .safe_sub(100 * QUOTE_PRECISION) + .unwrap(), + ); + spot_market.insurance_fund.set_user_shares( + spot_market + .insurance_fund + .user_shares() + .safe_sub(100 * QUOTE_PRECISION) + .unwrap(), + ); let lost_shares = calculate_if_shares_lost(&if_stake, &spot_market, if_balance).unwrap(); assert_eq!(lost_shares, 2); // giving up $5 of gains @@ -150,19 +174,13 @@ pub fn if_shares_lost_test() { // take back gain and total_if_shares alter w/o user alter let if_balance = (2100 * QUOTE_PRECISION) as u64; - spot_market.insurance_fund.total_shares *= 2; + spot_market.insurance_fund.set_total_shares( + spot_market + .insurance_fund + .total_shares() + .safe_mul(2) + .unwrap(), + ); let lost_shares = calculate_if_shares_lost(&if_stake, &spot_market, if_balance).unwrap(); assert_eq!(lost_shares, 5_000_001); // giving up $5 of gains - - let if_balance = (2100 * QUOTE_PRECISION * 10) as u64; - - let expected_gain_if_no_loss = if_balance * 100 / 2000; - assert_eq!(expected_gain_if_no_loss, 1_050_000_000); - let lost_shares = calculate_if_shares_lost(&if_stake, &spot_market, if_balance).unwrap(); - assert_eq!(lost_shares, 90_909_092); // giving up $5 of gains - assert_eq!( - (9090908 * if_balance / ((spot_market.insurance_fund.total_shares - lost_shares) as u64)) - < if_stake.last_withdraw_request_value, - true - ); } diff --git a/programs/drift/src/math/liquidation.rs b/programs/drift/src/math/liquidation.rs index dbe608ceaa..229575f10f 100644 --- a/programs/drift/src/math/liquidation.rs +++ b/programs/drift/src/math/liquidation.rs @@ -265,9 +265,9 @@ pub fn calculate_funding_rate_deltas_to_resolve_bankruptcy( ) -> DriftResult { let total_base_asset_amount = market .amm - .base_asset_amount_long + .base_asset_amount_long() .abs() - .safe_add(market.amm.base_asset_amount_short.abs())?; + .safe_add(market.amm.base_asset_amount_short().abs())?; validate!( total_base_asset_amount != 0, @@ -286,13 +286,13 @@ pub fn calculate_cumulative_deposit_interest_delta_to_resolve_bankruptcy( spot_market: &SpotMarket, ) -> DriftResult { let total_deposits = get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), spot_market, &SpotBalanceType::Deposit, )?; spot_market - .cumulative_deposit_interest + .cumulative_deposit_interest() .safe_mul(borrow)? .safe_div_ceil(total_deposits) .or(Ok(0)) diff --git a/programs/drift/src/math/liquidation/tests.rs b/programs/drift/src/math/liquidation/tests.rs index 04c066b76e..4566d017a6 100644 --- a/programs/drift/src/math/liquidation/tests.rs +++ b/programs/drift/src/math/liquidation/tests.rs @@ -503,8 +503,8 @@ mod calculate_funding_rate_deltas_to_resolve_bankruptcy { let loss = -QUOTE_PRECISION_I128; let market = PerpMarket { amm: AMM { - base_asset_amount_long: 0, - base_asset_amount_short: 0, + base_asset_amount_long: 0.into(), + base_asset_amount_short: 0.into(), ..AMM::default() }, ..PerpMarket::default() @@ -518,8 +518,8 @@ mod calculate_funding_rate_deltas_to_resolve_bankruptcy { let loss = -100 * QUOTE_PRECISION_I128; let market = PerpMarket { amm: AMM { - base_asset_amount_long: 7 * BASE_PRECISION_I128, - base_asset_amount_short: -4 * BASE_PRECISION_I128, + base_asset_amount_long: (7 * BASE_PRECISION_I128).into(), + base_asset_amount_short: (-4 * BASE_PRECISION_I128).into(), ..AMM::default() }, ..PerpMarket::default() @@ -543,8 +543,8 @@ mod calculate_cumulative_deposit_interest_delta_to_resolve_bankruptcy { fn zero_total_deposits() { let loss = 100 * QUOTE_PRECISION; let spot_market = SpotMarket { - deposit_balance: 0, - cumulative_deposit_interest: 1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000, + deposit_balance: 0.into(), + cumulative_deposit_interest: (1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000).into(), ..SpotMarket::default() }; @@ -559,8 +559,8 @@ mod calculate_cumulative_deposit_interest_delta_to_resolve_bankruptcy { fn non_zero_total_deposits() { let loss = 11 * QUOTE_PRECISION; let spot_market = SpotMarket { - deposit_balance: 120 * SPOT_BALANCE_PRECISION, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + deposit_balance: (120 * SPOT_BALANCE_PRECISION).into(), + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, ..SpotMarket::default() }; diff --git a/programs/drift/src/math/margin.rs b/programs/drift/src/math/margin.rs index fc31624842..ebeb029dca 100644 --- a/programs/drift/src/math/margin.rs +++ b/programs/drift/src/math/margin.rs @@ -154,9 +154,9 @@ pub fn calculate_perp_position_value_and_pnl( // the funding must be calculated before calculated the unrealized pnl w simulated lp position let unrealized_funding = calculate_funding_payment( if market_position.base_asset_amount > 0 { - market.amm.cumulative_funding_rate_long + market.amm.cumulative_funding_rate_long() } else { - market.amm.cumulative_funding_rate_short + market.amm.cumulative_funding_rate_short() }, market_position, )?; @@ -1060,9 +1060,9 @@ pub fn calculate_user_equity( let unrealized_funding = calculate_funding_payment( if market_position.base_asset_amount > 0 { - market.amm.cumulative_funding_rate_long + market.amm.cumulative_funding_rate_long() } else { - market.amm.cumulative_funding_rate_short + market.amm.cumulative_funding_rate_short() }, market_position, )?; diff --git a/programs/drift/src/math/margin/tests.rs b/programs/drift/src/math/margin/tests.rs index d4b1eefd2e..155068212f 100644 --- a/programs/drift/src/math/margin/tests.rs +++ b/programs/drift/src/math/margin/tests.rs @@ -195,34 +195,34 @@ mod test { decimals: 9, imf_factor: 0, scale_initial_asset_weight_start: 500_000 * QUOTE_PRECISION_U64, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), ..SpotMarket::default() }; let oracle_price = 25 * PRICE_PRECISION_I64; - sol_spot_market.deposit_balance = SPOT_BALANCE_PRECISION; + sol_spot_market.set_deposit_balance(SPOT_BALANCE_PRECISION); let asset_weight = sol_spot_market .get_scaled_initial_asset_weight(oracle_price) .unwrap(); assert_eq!(asset_weight, 9000); - sol_spot_market.deposit_balance = 20000 * SPOT_BALANCE_PRECISION; + sol_spot_market.set_deposit_balance(20000 * SPOT_BALANCE_PRECISION); let asset_weight = sol_spot_market .get_scaled_initial_asset_weight(oracle_price) .unwrap(); assert_eq!(asset_weight, 9000); - sol_spot_market.deposit_balance = 40000 * SPOT_BALANCE_PRECISION; + sol_spot_market.set_deposit_balance(40000 * SPOT_BALANCE_PRECISION); let asset_weight = sol_spot_market .get_scaled_initial_asset_weight(oracle_price) .unwrap(); assert_eq!(asset_weight, 4500); - sol_spot_market.deposit_balance = 60000 * SPOT_BALANCE_PRECISION; + sol_spot_market.set_deposit_balance(60000 * SPOT_BALANCE_PRECISION); let asset_weight = sol_spot_market .get_scaled_initial_asset_weight(oracle_price) .unwrap(); @@ -235,11 +235,11 @@ mod test { let mut market = PerpMarket { market_index: 0, amm: AMM { - base_asset_reserve: 512295081967, - quote_asset_reserve: 488 * AMM_RESERVE_PRECISION, - sqrt_k: 500 * AMM_RESERVE_PRECISION, - peg_multiplier: 22_100_000_000, - base_asset_amount_with_amm: -(12295081967_i128), + base_asset_reserve: 512295081967.into(), + quote_asset_reserve: (488 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (500 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 22_100_000_000.into(), + base_asset_amount_with_amm: (-(12295081967_i128)).into(), max_spread: 1000, ..AMM::default() }, @@ -443,11 +443,11 @@ mod calculate_margin_requirement_and_total_collateral { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -457,8 +457,8 @@ mod calculate_margin_requirement_and_total_collateral { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -534,11 +534,11 @@ mod calculate_margin_requirement_and_total_collateral { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -548,8 +548,8 @@ mod calculate_margin_requirement_and_total_collateral { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -625,13 +625,13 @@ mod calculate_margin_requirement_and_total_collateral { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, initial_liability_weight: SPOT_WEIGHT_PRECISION, maintenance_liability_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -641,15 +641,15 @@ mod calculate_margin_requirement_and_total_collateral { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, initial_liability_weight: 12 * SPOT_WEIGHT_PRECISION / 10, maintenance_liability_weight: 11 * SPOT_WEIGHT_PRECISION / 10, liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), ..SpotMarket::default() }; create_anchor_account_info!(sol_spot_market, SpotMarket, sol_spot_market_account_info); @@ -716,14 +716,14 @@ mod calculate_margin_requirement_and_total_collateral { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), order_step_size: 10000000, oracle: sol_oracle_price_key, ..AMM::default() @@ -739,11 +739,11 @@ mod calculate_margin_requirement_and_total_collateral { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -753,8 +753,8 @@ mod calculate_margin_requirement_and_total_collateral { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -913,14 +913,14 @@ mod calculate_margin_requirement_and_total_collateral { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), order_step_size: 10000000, oracle: sol_oracle_price_key, ..AMM::default() @@ -936,11 +936,11 @@ mod calculate_margin_requirement_and_total_collateral { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -950,8 +950,8 @@ mod calculate_margin_requirement_and_total_collateral { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -1098,14 +1098,14 @@ mod calculate_margin_requirement_and_total_collateral { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), order_step_size: 10000000, oracle: sol_oracle_price_key, ..AMM::default() @@ -1121,11 +1121,11 @@ mod calculate_margin_requirement_and_total_collateral { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -1135,8 +1135,8 @@ mod calculate_margin_requirement_and_total_collateral { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -1224,14 +1224,14 @@ mod calculate_margin_requirement_and_total_collateral { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), order_step_size: 10000000, oracle: sol_oracle_price_key, ..AMM::default() @@ -1247,11 +1247,11 @@ mod calculate_margin_requirement_and_total_collateral { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -1261,8 +1261,8 @@ mod calculate_margin_requirement_and_total_collateral { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION / 99, // big loss - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: (SPOT_CUMULATIVE_INTEREST_PRECISION / 99).into(), // big loss + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -1347,14 +1347,14 @@ mod calculate_margin_requirement_and_total_collateral { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), order_step_size: 10000000, oracle: sol_oracle_price_key, ..AMM::default() @@ -1370,11 +1370,11 @@ mod calculate_margin_requirement_and_total_collateral { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -1384,8 +1384,8 @@ mod calculate_margin_requirement_and_total_collateral { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -1520,11 +1520,11 @@ mod calculate_margin_requirement_and_total_collateral { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap_5min: PRICE_PRECISION_I64 * 9 / 10, @@ -1537,8 +1537,8 @@ mod calculate_margin_requirement_and_total_collateral { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -1614,14 +1614,14 @@ mod calculate_margin_requirement_and_total_collateral { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), order_step_size: 10000000, oracle: sol_oracle_price_key, ..AMM::default() @@ -1637,12 +1637,12 @@ mod calculate_margin_requirement_and_total_collateral { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -1652,8 +1652,8 @@ mod calculate_margin_requirement_and_total_collateral { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -1731,14 +1731,14 @@ mod calculate_margin_requirement_and_total_collateral { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), order_step_size: 10000000, oracle: sol_oracle_price_key, ..AMM::default() @@ -1754,12 +1754,12 @@ mod calculate_margin_requirement_and_total_collateral { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -1769,8 +1769,8 @@ mod calculate_margin_requirement_and_total_collateral { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -1843,14 +1843,14 @@ mod calculate_margin_requirement_and_total_collateral { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), order_step_size: 10000000, oracle: sol_oracle_price_key, ..AMM::default() @@ -1866,12 +1866,12 @@ mod calculate_margin_requirement_and_total_collateral { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -1881,8 +1881,8 @@ mod calculate_margin_requirement_and_total_collateral { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -1985,14 +1985,14 @@ mod calculate_margin_requirement_and_total_collateral_and_liability_info { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), order_step_size: 10000000, oracle: sol_oracle_price_key, ..AMM::default() @@ -2008,11 +2008,11 @@ mod calculate_margin_requirement_and_total_collateral_and_liability_info { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -2022,8 +2022,8 @@ mod calculate_margin_requirement_and_total_collateral_and_liability_info { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -2096,14 +2096,14 @@ mod calculate_margin_requirement_and_total_collateral_and_liability_info { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), order_step_size: 10000000, oracle: sol_oracle_price_key, ..AMM::default() @@ -2120,11 +2120,11 @@ mod calculate_margin_requirement_and_total_collateral_and_liability_info { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -2134,8 +2134,8 @@ mod calculate_margin_requirement_and_total_collateral_and_liability_info { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -2253,14 +2253,14 @@ mod calculate_margin_requirement_and_total_collateral_and_liability_info { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), order_step_size: 10000000, oracle: sol_oracle_price_key, ..AMM::default() @@ -2276,11 +2276,11 @@ mod calculate_margin_requirement_and_total_collateral_and_liability_info { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -2290,8 +2290,8 @@ mod calculate_margin_requirement_and_total_collateral_and_liability_info { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -2379,11 +2379,11 @@ mod calculate_margin_requirement_and_total_collateral_and_liability_info { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::PythStableCoin, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), oracle: usdc_oracle_price_key, @@ -2394,8 +2394,8 @@ mod calculate_margin_requirement_and_total_collateral_and_liability_info { market_index: 1, oracle_source: OracleSource::Pyth, oracle: usdc_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -2530,13 +2530,13 @@ mod calculate_margin_requirement_and_total_collateral_and_liability_info { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::PythStableCoin, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, - borrow_balance: 1000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), + borrow_balance: (1000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), oracle: usdc_oracle_price_key, @@ -2547,8 +2547,8 @@ mod calculate_margin_requirement_and_total_collateral_and_liability_info { market_index: 1, oracle_source: OracleSource::Pyth, oracle: usdc_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -2676,11 +2676,11 @@ mod calculate_margin_requirement_and_total_collateral_and_liability_info { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::PythStableCoin, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_price(usdc_price), oracle: usdc_oracle_price_key, @@ -2691,8 +2691,8 @@ mod calculate_margin_requirement_and_total_collateral_and_liability_info { market_index: 1, oracle_source: OracleSource::Pyth, oracle: usdc_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -2711,14 +2711,14 @@ mod calculate_margin_requirement_and_total_collateral_and_liability_info { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), order_step_size: 10000000, oracle: sol_oracle_price_key, ..AMM::default() @@ -2830,14 +2830,14 @@ mod calculate_margin_requirement_and_total_collateral_and_liability_info { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), order_step_size: 10000000, oracle: sol_oracle_price_key, ..AMM::default() @@ -2853,11 +2853,11 @@ mod calculate_margin_requirement_and_total_collateral_and_liability_info { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -2867,8 +2867,8 @@ mod calculate_margin_requirement_and_total_collateral_and_liability_info { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -2970,11 +2970,11 @@ mod calculate_max_withdrawable_amount { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -2984,8 +2984,8 @@ mod calculate_max_withdrawable_amount { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -3056,12 +3056,12 @@ mod calculate_max_withdrawable_amount { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), initial_liability_weight: SPOT_WEIGHT_PRECISION, maintenance_liability_weight: SPOT_WEIGHT_PRECISION, liquidator_fee: 0, @@ -3073,8 +3073,8 @@ mod calculate_max_withdrawable_amount { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -3144,12 +3144,12 @@ mod calculate_max_withdrawable_amount { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), initial_liability_weight: SPOT_WEIGHT_PRECISION, maintenance_liability_weight: SPOT_WEIGHT_PRECISION, liquidator_fee: 0, @@ -3161,8 +3161,8 @@ mod calculate_max_withdrawable_amount { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -3260,11 +3260,11 @@ mod validate_spot_margin_trading { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, ..SpotMarket::default() }; @@ -3273,8 +3273,8 @@ mod validate_spot_margin_trading { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -3343,11 +3343,11 @@ mod validate_spot_margin_trading { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, ..SpotMarket::default() }; @@ -3356,8 +3356,8 @@ mod validate_spot_margin_trading { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -3426,11 +3426,11 @@ mod validate_spot_margin_trading { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, ..SpotMarket::default() }; @@ -3439,8 +3439,8 @@ mod validate_spot_margin_trading { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -3509,11 +3509,11 @@ mod validate_spot_margin_trading { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, ..SpotMarket::default() }; @@ -3522,8 +3522,8 @@ mod validate_spot_margin_trading { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -3592,11 +3592,11 @@ mod validate_spot_margin_trading { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, ..SpotMarket::default() }; @@ -3605,8 +3605,8 @@ mod validate_spot_margin_trading { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -3675,11 +3675,11 @@ mod validate_spot_margin_trading { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, ..SpotMarket::default() }; @@ -3688,8 +3688,8 @@ mod validate_spot_margin_trading { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -3755,14 +3755,14 @@ mod validate_spot_margin_trading { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), order_step_size: 10000000, oracle: sol_oracle_price_key, ..AMM::default() @@ -3780,11 +3780,11 @@ mod validate_spot_margin_trading { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, ..SpotMarket::default() }; @@ -3793,8 +3793,8 @@ mod validate_spot_margin_trading { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -3902,14 +3902,14 @@ mod calculate_user_equity { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -3930,8 +3930,8 @@ mod calculate_user_equity { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -3939,11 +3939,11 @@ mod calculate_user_equity { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: PRICE_PRECISION_I64, @@ -4000,14 +4000,14 @@ mod calculate_user_equity { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -4028,8 +4028,8 @@ mod calculate_user_equity { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -4037,11 +4037,11 @@ mod calculate_user_equity { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: PRICE_PRECISION_I64, @@ -4101,11 +4101,11 @@ mod calculate_user_equity { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -4115,8 +4115,8 @@ mod calculate_user_equity { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -4354,11 +4354,11 @@ mod pools { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), pool_id: 1, @@ -4487,14 +4487,14 @@ mod get_margin_calculation_for_disable_high_leverage_mode { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 101 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), order_step_size: 10000000, oracle: sol_oracle_price_key, ..AMM::default() @@ -4510,11 +4510,11 @@ mod get_margin_calculation_for_disable_high_leverage_mode { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -4524,8 +4524,8 @@ mod get_margin_calculation_for_disable_high_leverage_mode { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, diff --git a/programs/drift/src/math/oracle.rs b/programs/drift/src/math/oracle.rs index 92bf2a47e8..8ec3555a02 100644 --- a/programs/drift/src/math/oracle.rs +++ b/programs/drift/src/math/oracle.rs @@ -1,4 +1,4 @@ -use borsh::{BorshDeserialize, BorshSerialize}; +use anchor_lang::prelude::borsh::{BorshDeserialize, BorshSerialize}; use crate::error::{DriftResult, ErrorCode}; use crate::math::amm; diff --git a/programs/drift/src/math/oracle/tests.rs b/programs/drift/src/math/oracle/tests.rs index adf34a28b4..29ea89984b 100644 --- a/programs/drift/src/math/oracle/tests.rs +++ b/programs/drift/src/math/oracle/tests.rs @@ -15,9 +15,9 @@ fn calculate_oracle_valid() { let px = 32 * PRICE_PRECISION; let amm = AMM { - base_asset_reserve: 2 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 2 * AMM_RESERVE_PRECISION, - peg_multiplier: 33 * PEG_PRECISION, + base_asset_reserve: (2 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (2 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (33 * PEG_PRECISION).into(), historical_oracle_data: HistoricalOracleData { last_oracle_price_twap_5min: px as i64, last_oracle_price_twap: (px as i64) - 1000, diff --git a/programs/drift/src/math/orders/tests.rs b/programs/drift/src/math/orders/tests.rs index ffb3d11380..26a82f87d9 100644 --- a/programs/drift/src/math/orders/tests.rs +++ b/programs/drift/src/math/orders/tests.rs @@ -466,7 +466,7 @@ mod get_max_fill_amounts { #[test] fn fully_collateralized_selling_base() { let base_market = SpotMarket { - deposit_balance: 4 * 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (4 * 100 * SPOT_BALANCE_PRECISION).into(), deposit_token_twap: 4 * 100 * LAMPORTS_PER_SOL_U64, ..SpotMarket::default_base_market() }; @@ -546,7 +546,7 @@ mod get_max_fill_amounts { #[test] fn selling_base_with_borrow_liquidity_greater_than_order() { let base_market = SpotMarket { - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), deposit_token_twap: 100 * SPOT_BALANCE_PRECISION as u64, ..SpotMarket::default_base_market() }; @@ -588,7 +588,7 @@ mod get_max_fill_amounts { fn fully_collateralized_selling_quote() { let base_market = SpotMarket::default_base_market(); let quote_market = SpotMarket { - deposit_balance: 4 * 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (4 * 100 * SPOT_BALANCE_PRECISION).into(), deposit_token_twap: 4 * 100 * QUOTE_PRECISION_U64, ..SpotMarket::default_quote_market() }; @@ -668,7 +668,7 @@ mod get_max_fill_amounts { fn selling_quote_with_borrow_liquidity_greater_than_order() { let base_market = SpotMarket::default_base_market(); let quote_market = SpotMarket { - deposit_balance: 100 * SPOT_BALANCE_PRECISION, + deposit_balance: (100 * SPOT_BALANCE_PRECISION).into(), deposit_token_twap: 100 * QUOTE_PRECISION_U64, ..SpotMarket::default_quote_market() @@ -1108,11 +1108,11 @@ mod calculate_max_spot_order_size { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -1122,8 +1122,8 @@ mod calculate_max_spot_order_size { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -1215,11 +1215,11 @@ mod calculate_max_spot_order_size { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -1229,8 +1229,8 @@ mod calculate_max_spot_order_size { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -1323,11 +1323,11 @@ mod calculate_max_spot_order_size { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -1337,8 +1337,8 @@ mod calculate_max_spot_order_size { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -1430,11 +1430,11 @@ mod calculate_max_spot_order_size { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -1444,8 +1444,8 @@ mod calculate_max_spot_order_size { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -1520,11 +1520,11 @@ mod calculate_max_spot_order_size { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -1534,8 +1534,8 @@ mod calculate_max_spot_order_size { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -1628,11 +1628,11 @@ mod calculate_max_spot_order_size { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -1642,8 +1642,8 @@ mod calculate_max_spot_order_size { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -1736,11 +1736,11 @@ mod calculate_max_spot_order_size { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -1750,8 +1750,8 @@ mod calculate_max_spot_order_size { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -1844,11 +1844,11 @@ mod calculate_max_spot_order_size { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -1858,8 +1858,8 @@ mod calculate_max_spot_order_size { market_index: 1, oracle_source: OracleSource::Pyth, oracle: sol_oracle_price_key, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, initial_asset_weight: 8 * SPOT_WEIGHT_PRECISION / 10, maintenance_asset_weight: 9 * SPOT_WEIGHT_PRECISION / 10, @@ -1985,14 +1985,14 @@ mod calculate_max_perp_order_size { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -2013,8 +2013,8 @@ mod calculate_max_perp_order_size { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -2022,11 +2022,11 @@ mod calculate_max_perp_order_size { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: PRICE_PRECISION_I64, @@ -2107,14 +2107,14 @@ mod calculate_max_perp_order_size { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -2135,8 +2135,8 @@ mod calculate_max_perp_order_size { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -2144,11 +2144,11 @@ mod calculate_max_perp_order_size { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: PRICE_PRECISION_I64, @@ -2212,14 +2212,14 @@ mod calculate_max_perp_order_size { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -2240,8 +2240,8 @@ mod calculate_max_perp_order_size { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -2249,11 +2249,11 @@ mod calculate_max_perp_order_size { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: PRICE_PRECISION_I64, @@ -2334,14 +2334,14 @@ mod calculate_max_perp_order_size { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -2362,8 +2362,8 @@ mod calculate_max_perp_order_size { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -2371,11 +2371,11 @@ mod calculate_max_perp_order_size { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: PRICE_PRECISION_I64, @@ -2439,14 +2439,14 @@ mod calculate_max_perp_order_size { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -2467,8 +2467,8 @@ mod calculate_max_perp_order_size { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -2476,11 +2476,11 @@ mod calculate_max_perp_order_size { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: PRICE_PRECISION_I64, @@ -2562,14 +2562,14 @@ mod calculate_max_perp_order_size { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -2590,8 +2590,8 @@ mod calculate_max_perp_order_size { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -2599,11 +2599,11 @@ mod calculate_max_perp_order_size { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: PRICE_PRECISION_I64, @@ -2685,14 +2685,14 @@ mod calculate_max_perp_order_size { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -2714,8 +2714,8 @@ mod calculate_max_perp_order_size { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -2723,11 +2723,11 @@ mod calculate_max_perp_order_size { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: PRICE_PRECISION_I64, @@ -2808,14 +2808,14 @@ mod calculate_max_perp_order_size { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -2836,8 +2836,8 @@ mod calculate_max_perp_order_size { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -2845,11 +2845,11 @@ mod calculate_max_perp_order_size { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: PRICE_PRECISION_I64, @@ -2931,14 +2931,14 @@ mod calculate_max_perp_order_size { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -2959,8 +2959,8 @@ mod calculate_max_perp_order_size { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -2968,11 +2968,11 @@ mod calculate_max_perp_order_size { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: PRICE_PRECISION_I64, @@ -3054,14 +3054,14 @@ mod calculate_max_perp_order_size { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -3083,8 +3083,8 @@ mod calculate_max_perp_order_size { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -3092,11 +3092,11 @@ mod calculate_max_perp_order_size { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: PRICE_PRECISION_I64, @@ -3177,14 +3177,14 @@ mod calculate_max_perp_order_size { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -3208,8 +3208,8 @@ mod calculate_max_perp_order_size { status: MarketStatus::Active, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); create_anchor_account_info!(market, PerpMarket, market_account_info); let market_map = PerpMarketMap::load_one(&market_account_info, true).unwrap(); @@ -3217,11 +3217,11 @@ mod calculate_max_perp_order_size { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 10000 * SPOT_BALANCE_PRECISION, + deposit_balance: (10000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: PRICE_PRECISION_I64, @@ -3974,9 +3974,9 @@ pub mod get_price_for_perp_order { #[test] fn bid_crosses_vamm_ask() { let amm = AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), order_tick_size: 100000, short_spread: BID_ASK_SPREAD_PRECISION_U128 as u32 / 100, ..AMM::default() @@ -4005,9 +4005,9 @@ pub mod get_price_for_perp_order { #[test] fn bid_doesnt_cross_vamm_ask() { let amm = AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), order_tick_size: 100000, short_spread: BID_ASK_SPREAD_PRECISION_U128 as u32 / 100, ..AMM::default() @@ -4030,9 +4030,9 @@ pub mod get_price_for_perp_order { #[test] fn ask_crosses_vamm_ask() { let amm = AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), order_tick_size: 100000, long_spread: BID_ASK_SPREAD_PRECISION_U128 as u32 / 100, ..AMM::default() @@ -4061,9 +4061,9 @@ pub mod get_price_for_perp_order { #[test] fn ask_doesnt_cross_vamm_ask() { let amm = AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), order_tick_size: 100000, long_spread: BID_ASK_SPREAD_PRECISION_U128 as u32 / 100, ..AMM::default() @@ -4498,14 +4498,14 @@ mod fallback_price_logic { fn test() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - bid_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - ask_quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + bid_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + ask_quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), max_slippage_ratio: 50, max_fill_reserve_fraction: 100, order_step_size: 1000, @@ -4527,8 +4527,8 @@ mod fallback_price_logic { status: MarketStatus::Initialized, ..PerpMarket::default_test() }; - market.amm.max_base_asset_reserve = u128::MAX; - market.amm.min_base_asset_reserve = 0; + market.amm.set_max_base_asset_reserve(u128::MAX); + market.amm.set_min_base_asset_reserve(0); // fallback are wide from oracle cause twaps arent set on amm let result = market diff --git a/programs/drift/src/math/position.rs b/programs/drift/src/math/position.rs index e40c75205d..288e1ed1ce 100644 --- a/programs/drift/src/math/position.rs +++ b/programs/drift/src/math/position.rs @@ -37,9 +37,9 @@ pub fn calculate_base_asset_value(base_asset_amount: i128, amm: &AMM) -> DriftRe let swap_direction = swap_direction_to_close_position(base_asset_amount); let (base_asset_reserve, quote_asset_reserve) = - (amm.base_asset_reserve, amm.quote_asset_reserve); + (amm.base_asset_reserve(), amm.quote_asset_reserve()); - let amm_lp_shares = amm.sqrt_k; + let amm_lp_shares = amm.sqrt_k(); let (new_quote_asset_reserve, _new_base_asset_reserve) = amm::calculate_swap_output( base_asset_amount.unsigned_abs(), @@ -52,7 +52,7 @@ pub fn calculate_base_asset_value(base_asset_amount: i128, amm: &AMM) -> DriftRe quote_asset_reserve, new_quote_asset_reserve, swap_direction, - amm.peg_multiplier, + amm.peg_multiplier(), )?; Ok(base_asset_value) diff --git a/programs/drift/src/math/repeg.rs b/programs/drift/src/math/repeg.rs index 995e425f64..ac6e9af8f5 100644 --- a/programs/drift/src/math/repeg.rs +++ b/programs/drift/src/math/repeg.rs @@ -92,9 +92,9 @@ pub fn calculate_repeg_validity( // if oracle is valid: check on size/direction of repeg if oracle_is_valid { let reserve_price_after = amm::calculate_price( - market.amm.quote_asset_reserve, - market.amm.base_asset_reserve, - market.amm.peg_multiplier, + market.amm.quote_asset_reserve(), + market.amm.base_asset_reserve(), + market.amm.peg_multiplier(), )?; let oracle_conf_band_top = oracle_price_u128.safe_add(oracle_conf)?; @@ -179,18 +179,18 @@ pub fn adjust_peg_cost( ) -> DriftResult<(PerpMarket, i128)> { let mut market_clone = *market; - let cost = if new_peg_candidate != market_clone.amm.peg_multiplier { + let cost = if new_peg_candidate != market_clone.amm.peg_multiplier() { // Find the net market value before adjusting peg let (current_net_market_value, _) = calculate_base_asset_value_and_pnl( - market_clone.amm.base_asset_amount_with_amm, + market_clone.amm.base_asset_amount_with_amm(), 0, &market_clone.amm, )?; - market_clone.amm.peg_multiplier = new_peg_candidate; + market_clone.amm.set_peg_multiplier(new_peg_candidate); let (_new_net_market_value, cost) = calculate_base_asset_value_and_pnl( - market_clone.amm.base_asset_amount_with_amm, + market_clone.amm.base_asset_amount_with_amm(), current_net_market_value, &market_clone.amm, )?; @@ -203,13 +203,13 @@ pub fn adjust_peg_cost( } pub fn calculate_repeg_cost(amm: &AMM, new_peg: u128) -> DriftResult { - amm.quote_asset_reserve + amm.quote_asset_reserve() .cast::()? - .safe_sub(amm.terminal_quote_asset_reserve.cast()?)? + .safe_sub(amm.terminal_quote_asset_reserve().cast()?)? .safe_mul( new_peg .cast::()? - .safe_sub(amm.peg_multiplier.cast()?)?, + .safe_sub(amm.peg_multiplier().cast()?)?, )? .safe_div(AMM_RESERVE_PRECISION_I128) } @@ -249,17 +249,17 @@ pub fn adjust_amm( let curve_update_intensity = min(market.amm.curve_update_intensity, 100_u8).cast::()?; // return early - if optimal_peg == market.amm.peg_multiplier || curve_update_intensity == 0 { + if optimal_peg == market.amm.peg_multiplier() || curve_update_intensity == 0 { return Ok((Box::new(*market), 0)); } let delta_peg = optimal_peg .cast::()? - .safe_sub(market.amm.peg_multiplier.cast()?)?; // PEG_PRECISION + .safe_sub(market.amm.peg_multiplier().cast()?)?; // PEG_PRECISION let mut per_peg_cost = calculate_per_peg_cost( - market.amm.quote_asset_reserve, - market.amm.terminal_quote_asset_reserve, + market.amm.quote_asset_reserve(), + market.amm.terminal_quote_asset_reserve(), )?; // PEG_PRECISION let budget_i128 = budget.cast::()?; @@ -299,8 +299,8 @@ pub fn adjust_amm( let new_sqrt_k = market .amm - .sqrt_k - .safe_sub(market.amm.sqrt_k.safe_div(1000)?)? + .sqrt_k() + .safe_sub(market.amm.sqrt_k().safe_div(1000)?)? .max(new_sqrt_k_lower_bound); let update_k_result = @@ -309,8 +309,8 @@ pub fn adjust_amm( let adjustment_cost = cp_curve::adjust_k_cost_and_update(&mut market_clone, &update_k_result)?; per_peg_cost = calculate_per_peg_cost( - market_clone.amm.quote_asset_reserve, - market_clone.amm.terminal_quote_asset_reserve, + market_clone.amm.quote_asset_reserve(), + market_clone.amm.terminal_quote_asset_reserve(), )?; adjustment_cost @@ -327,13 +327,13 @@ pub fn adjust_amm( new_peg = if budget_delta_peg > 0 { market .amm - .peg_multiplier + .peg_multiplier() .safe_add(budget_delta_peg_magnitude) .unwrap_or(u128::MAX) - } else if market.amm.peg_multiplier > budget_delta_peg_magnitude { + } else if market.amm.peg_multiplier() > budget_delta_peg_magnitude { market .amm - .peg_multiplier + .peg_multiplier() .safe_sub(budget_delta_peg_magnitude)? } else { 1 @@ -341,7 +341,7 @@ pub fn adjust_amm( cost = calculate_repeg_cost(&market_clone.amm, new_peg)?; } - market_clone.amm.peg_multiplier = new_peg; + market_clone.amm.set_peg_multiplier(new_peg); Ok((market_clone, cost)) } @@ -357,8 +357,8 @@ pub fn calculate_optimal_peg_and_budget( let target_price_i64 = mm_oracle_price_data.get_price(); let target_price = target_price_i64.cast()?; let mut optimal_peg = calculate_peg_from_target_price( - market.amm.quote_asset_reserve, - market.amm.base_asset_reserve, + market.amm.quote_asset_reserve(), + market.amm.base_asset_reserve(), target_price, )?; @@ -389,8 +389,8 @@ pub fn calculate_optimal_peg_and_budget( reserve_price_before.safe_sub(mark_adj)? }; optimal_peg = calculate_peg_from_target_price( - market.amm.quote_asset_reserve, - market.amm.base_asset_reserve, + market.amm.quote_asset_reserve(), + market.amm.base_asset_reserve(), target_price.cast()?, )?; @@ -407,16 +407,16 @@ pub fn calculate_optimal_peg_and_budget( pub fn calculate_fee_pool(market: &PerpMarket) -> DriftResult { let total_fee_minus_distributions_lower_bound = get_total_fee_lower_bound(market)? - .safe_add(market.amm.total_liquidation_fee)? - .safe_sub(market.amm.total_fee_withdrawn)? + .safe_add(market.amm.total_liquidation_fee())? + .safe_sub(market.amm.total_fee_withdrawn())? .cast::() .unwrap_or(0); let fee_pool = - if market.amm.total_fee_minus_distributions > total_fee_minus_distributions_lower_bound { + if market.amm.total_fee_minus_distributions() > total_fee_minus_distributions_lower_bound { market .amm - .total_fee_minus_distributions + .total_fee_minus_distributions() .safe_sub(total_fee_minus_distributions_lower_bound)? .cast()? } else { @@ -430,7 +430,7 @@ pub fn get_total_fee_lower_bound(market: &PerpMarket) -> DriftResult { // market to retain half of exchange fees let total_fee_lower_bound = market .amm - .total_exchange_fee + .total_exchange_fee() .safe_mul(SHARE_OF_FEES_ALLOCATED_TO_DRIFT_NUMERATOR)? .safe_div(SHARE_OF_FEES_ALLOCATED_TO_DRIFT_DENOMINATOR)?; diff --git a/programs/drift/src/math/repeg/tests.rs b/programs/drift/src/math/repeg/tests.rs index 0619d0b35f..5ff027965d 100644 --- a/programs/drift/src/math/repeg/tests.rs +++ b/programs/drift/src/math/repeg/tests.rs @@ -33,19 +33,19 @@ fn calc_peg_tests() { fn calculate_optimal_peg_and_budget_test() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 65 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 63015384615, - terminal_quote_asset_reserve: 64 * AMM_RESERVE_PRECISION, - sqrt_k: 64 * AMM_RESERVE_PRECISION, - peg_multiplier: 19_400_000_000, - base_asset_amount_with_amm: -(AMM_RESERVE_PRECISION as i128), + base_asset_reserve: (65 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: 63015384615.into(), + terminal_quote_asset_reserve: (64 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (64 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 19_400_000_000.into(), + base_asset_amount_with_amm: (-(AMM_RESERVE_PRECISION as i128)).into(), mark_std: PRICE_PRECISION as u64, last_mark_price_twap_ts: 0, base_spread: 250, curve_update_intensity: 100, max_spread: 500 * 100, - total_exchange_fee: QUOTE_PRECISION, - total_fee_minus_distributions: (40 * QUOTE_PRECISION) as i128, + total_exchange_fee: QUOTE_PRECISION.into(), + total_fee_minus_distributions: ((40 * QUOTE_PRECISION) as i128).into(), ..AMM::default() }, margin_ratio_initial: 500, @@ -173,22 +173,26 @@ fn calculate_optimal_peg_and_budget_test() { assert_eq!(budget, 39500000); assert!(check_lb); - market.amm.base_asset_amount_with_amm = AMM_RESERVE_PRECISION as i128; + market + .amm + .set_base_asset_amount_with_amm(AMM_RESERVE_PRECISION as i128); - let swap_direction = if market.amm.base_asset_amount_with_amm > 0 { + let swap_direction = if market.amm.base_asset_amount_with_amm() > 0 { SwapDirection::Add } else { SwapDirection::Remove }; let (new_terminal_quote_reserve, _new_terminal_base_reserve) = amm::calculate_swap_output( - market.amm.base_asset_amount_with_amm.unsigned_abs(), - market.amm.base_asset_reserve, + market.amm.base_asset_amount_with_amm().unsigned_abs(), + market.amm.base_asset_reserve(), swap_direction, - market.amm.sqrt_k, + market.amm.sqrt_k(), ) .unwrap(); - market.amm.terminal_quote_asset_reserve = new_terminal_quote_reserve; + market + .amm + .set_terminal_quote_asset_reserve(new_terminal_quote_reserve); // negative target_price_gap exceeding max_spread (not in favor of vAMM) let oracle_price_data = OraclePriceData { @@ -218,21 +222,21 @@ fn calculate_optimal_peg_and_budget_test() { fn calculate_optimal_peg_and_budget_2_test() { let mut market = PerpMarket { amm: AMM { - base_asset_reserve: 2270516211133, - quote_asset_reserve: 2270925669621, - terminal_quote_asset_reserve: 2270688451627, - sqrt_k: 2270720931148, - peg_multiplier: 17723081263, - base_asset_amount_with_amm: 237200000, + base_asset_reserve: 2270516211133.into(), + quote_asset_reserve: 2270925669621.into(), + terminal_quote_asset_reserve: 2270688451627.into(), + sqrt_k: 2270720931148.into(), + peg_multiplier: 17723081263.into(), + base_asset_amount_with_amm: 237200000.into(), mark_std: 43112524, last_mark_price_twap_ts: 0, base_spread: 250, curve_update_intensity: 100, max_spread: 500 * 100, - total_exchange_fee: 298628987, - total_fee_minus_distributions: -242668966, - total_fee_withdrawn: 124247717, - concentration_coef: 1020710, + total_exchange_fee: 298628987.into(), + total_fee_minus_distributions: (-242668966).into(), + total_fee_withdrawn: 124247717.into(), + concentration_coef: 1020710.into(), historical_oracle_data: HistoricalOracleData { last_oracle_price_twap: 17765940050, last_oracle_price_twap_5min: 17763317077, @@ -246,12 +250,18 @@ fn calculate_optimal_peg_and_budget_2_test() { }; let (new_terminal_quote_reserve, new_terminal_base_reserve) = amm::calculate_terminal_reserves(&market.amm).unwrap(); - market.amm.terminal_quote_asset_reserve = new_terminal_quote_reserve; + market + .amm + .set_terminal_quote_asset_reserve(new_terminal_quote_reserve); let (min_base_asset_reserve, max_base_asset_reserve) = - amm::calculate_bid_ask_bounds(market.amm.concentration_coef, new_terminal_base_reserve) + amm::calculate_bid_ask_bounds(market.amm.concentration_coef(), new_terminal_base_reserve) .unwrap(); - market.amm.min_base_asset_reserve = min_base_asset_reserve; - market.amm.max_base_asset_reserve = max_base_asset_reserve; + market + .amm + .set_min_base_asset_reserve(min_base_asset_reserve); + market + .amm + .set_max_base_asset_reserve(max_base_asset_reserve); let oracle_price_data = OraclePriceData { price: (17_800 * PRICE_PRECISION) as i64, @@ -310,12 +320,12 @@ fn calc_adjust_amm_tests_repeg_in_favour() { // btc-esque market let market = PerpMarket { amm: AMM { - base_asset_reserve: 65 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 63015384615, - terminal_quote_asset_reserve: 64 * AMM_RESERVE_PRECISION, - sqrt_k: 64 * AMM_RESERVE_PRECISION, - peg_multiplier: 19_400_000_000, - base_asset_amount_with_amm: AMM_RESERVE_PRECISION as i128, + base_asset_reserve: (65 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: 63015384615.into(), + terminal_quote_asset_reserve: (64 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (64 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 19_400_000_000.into(), + base_asset_amount_with_amm: (AMM_RESERVE_PRECISION as i128).into(), mark_std: PRICE_PRECISION as u64, last_mark_price_twap_ts: 0, curve_update_intensity: 100, @@ -328,16 +338,16 @@ fn calc_adjust_amm_tests_repeg_in_favour() { let px = 20_401_125_456; let optimal_peg = calculate_peg_from_target_price( - market.amm.quote_asset_reserve, - market.amm.base_asset_reserve, + market.amm.quote_asset_reserve(), + market.amm.base_asset_reserve(), px, ) .unwrap(); - assert!(optimal_peg > market.amm.peg_multiplier); + assert!(optimal_peg > market.amm.peg_multiplier()); let (repegged_market, _amm_update_cost) = adjust_amm(&market, optimal_peg, 0, true).unwrap(); assert_eq!(_amm_update_cost, -1618354580); - assert_eq!(repegged_market.amm.peg_multiplier, optimal_peg); + assert_eq!(repegged_market.amm.peg_multiplier(), optimal_peg); let post_price = repegged_market.amm.reserve_price().unwrap(); assert_eq!(post_price - prev_price, 1593456817); // todo: (15934564582252/1e4 - 1615699103 is the slippage cost?) @@ -349,21 +359,21 @@ fn calc_adjust_amm_tests_sufficent_fee_for_repeg() { let mut market = PerpMarket { amm: AMM { order_step_size: 1000, - base_asset_reserve: 60437939720095, - quote_asset_reserve: 60440212459368, - terminal_quote_asset_reserve: 60439072663003, - sqrt_k: 60439076079049, - peg_multiplier: 34353000, - base_asset_amount_with_amm: AMM_RESERVE_PRECISION as i128, + base_asset_reserve: 60437939720095.into(), + quote_asset_reserve: 60440212459368.into(), + terminal_quote_asset_reserve: 60439072663003.into(), + sqrt_k: 60439076079049.into(), + peg_multiplier: 34353000.into(), + base_asset_amount_with_amm: (AMM_RESERVE_PRECISION as i128).into(), last_mark_price_twap: 34128370, last_mark_price_twap_ts: 165705, curve_update_intensity: 100, base_spread: 1000, - total_fee_minus_distributions: 304289, - total_fee: 607476, - total_exchange_fee: 0, // new fee pool lowerbound + total_fee_minus_distributions: 304289.into(), + total_fee: 607476.into(), + total_exchange_fee: 0.into(), // new fee pool lowerbound funding_period: 3600, - concentration_coef: MAX_CONCENTRATION_COEFFICIENT, + concentration_coef: MAX_CONCENTRATION_COEFFICIENT.into(), ..AMM::default() }, @@ -376,29 +386,35 @@ fn calc_adjust_amm_tests_sufficent_fee_for_repeg() { }; let (new_terminal_quote_reserve, new_terminal_base_reserve) = amm::calculate_terminal_reserves(&market.amm).unwrap(); - market.amm.terminal_quote_asset_reserve = new_terminal_quote_reserve; + market + .amm + .set_terminal_quote_asset_reserve(new_terminal_quote_reserve); let (min_base_asset_reserve, max_base_asset_reserve) = - amm::calculate_bid_ask_bounds(market.amm.concentration_coef, new_terminal_base_reserve) + amm::calculate_bid_ask_bounds(market.amm.concentration_coef(), new_terminal_base_reserve) .unwrap(); - market.amm.min_base_asset_reserve = min_base_asset_reserve; - market.amm.max_base_asset_reserve = max_base_asset_reserve; + market + .amm + .set_min_base_asset_reserve(min_base_asset_reserve); + market + .amm + .set_max_base_asset_reserve(max_base_asset_reserve); let px = 35768 * PRICE_PRECISION_U64 / 1000; let optimal_peg = calculate_peg_from_target_price( - market.amm.quote_asset_reserve, - market.amm.base_asset_reserve, + market.amm.quote_asset_reserve(), + market.amm.base_asset_reserve(), px, ) .unwrap(); - assert!(optimal_peg > market.amm.peg_multiplier); + assert!(optimal_peg > market.amm.peg_multiplier()); let fee_budget = calculate_fee_pool(&market).unwrap(); assert!(fee_budget > 0); let (repegged_market, _amm_update_cost) = adjust_amm(&market, optimal_peg, fee_budget, true).unwrap(); // insufficient fee to repeg - let new_peg = repegged_market.amm.peg_multiplier; - let old_peg = market.amm.peg_multiplier; + let new_peg = repegged_market.amm.peg_multiplier(); + let old_peg = market.amm.peg_multiplier(); assert!(new_peg > old_peg); assert_eq!(new_peg, 34657283); assert_eq!(_amm_update_cost, 304289); diff --git a/programs/drift/src/math/spot_balance.rs b/programs/drift/src/math/spot_balance.rs index da5261a487..c89378ebce 100644 --- a/programs/drift/src/math/spot_balance.rs +++ b/programs/drift/src/math/spot_balance.rs @@ -22,8 +22,8 @@ pub fn get_spot_balance( let precision_increase = 10_u128.pow(19_u32.safe_sub(spot_market.decimals)?); let cumulative_interest = match balance_type { - SpotBalanceType::Deposit => spot_market.cumulative_deposit_interest, - SpotBalanceType::Borrow => spot_market.cumulative_borrow_interest, + SpotBalanceType::Deposit => spot_market.cumulative_deposit_interest(), + SpotBalanceType::Borrow => spot_market.cumulative_borrow_interest(), }; let mut balance = token_amount @@ -45,8 +45,8 @@ pub fn get_token_amount( let precision_decrease = 10_u128.pow(19_u32.safe_sub(spot_market.decimals)?); let cumulative_interest = match balance_type { - SpotBalanceType::Deposit => spot_market.cumulative_deposit_interest, - SpotBalanceType::Borrow => spot_market.cumulative_borrow_interest, + SpotBalanceType::Deposit => spot_market.cumulative_deposit_interest(), + SpotBalanceType::Borrow => spot_market.cumulative_borrow_interest(), }; let token_amount = match balance_type { @@ -111,12 +111,12 @@ pub fn calculate_utilization( pub fn calculate_spot_market_utilization(spot_market: &SpotMarket) -> DriftResult { let deposit_token_amount = get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), spot_market, &SpotBalanceType::Deposit, )?; let borrow_token_amount = get_token_amount( - spot_market.borrow_balance, + spot_market.borrow_balance(), spot_market, &SpotBalanceType::Borrow, )?; @@ -161,14 +161,14 @@ pub fn calculate_accumulated_interest( .safe_div(SPOT_UTILIZATION_PRECISION)?; let borrow_interest = spot_market - .cumulative_borrow_interest + .cumulative_borrow_interest() .safe_mul(modified_borrow_rate)? .safe_div(ONE_YEAR)? .safe_div(SPOT_RATE_PRECISION)? .safe_add(1)?; let deposit_interest = spot_market - .cumulative_deposit_interest + .cumulative_deposit_interest() .safe_mul(modified_deposit_rate)? .safe_div(ONE_YEAR)? .safe_div(SPOT_RATE_PRECISION)?; @@ -206,7 +206,7 @@ pub fn calculate_borrow_rate(spot_market: &SpotMarket, utilization: u128) -> Dri let segment_end = bp; let segment_range = segment_end.safe_sub(segment_start)?; let segment_rate_total = total_extra_rate - .safe_mul(weight as u128)? + .safe_mul(weight)? .safe_div(weights_divisor)?; if utilization <= segment_end { diff --git a/programs/drift/src/math/spot_balance/tests.rs b/programs/drift/src/math/spot_balance/tests.rs index 5fbe7427a4..d9dffd9ff1 100644 --- a/programs/drift/src/math/spot_balance/tests.rs +++ b/programs/drift/src/math/spot_balance/tests.rs @@ -7,7 +7,7 @@ mod test { #[test] fn bonk() { let spot_market = SpotMarket { - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 5, ..SpotMarket::default_quote_market() }; diff --git a/programs/drift/src/math/spot_withdraw.rs b/programs/drift/src/math/spot_withdraw.rs index 9f0b3e0b5a..dc64c73696 100644 --- a/programs/drift/src/math/spot_withdraw.rs +++ b/programs/drift/src/math/spot_withdraw.rs @@ -153,12 +153,12 @@ pub fn check_withdraw_limits( // calculates min/max deposit/borrow amounts permitted for immediate withdraw // takes the stricter of absolute caps on level changes and utilization changes vs 24hr moving averrages let deposit_token_amount = get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), spot_market, &SpotBalanceType::Deposit, )?; let borrow_token_amount = get_token_amount( - spot_market.borrow_balance, + spot_market.borrow_balance(), spot_market, &SpotBalanceType::Borrow, )?; @@ -231,13 +231,13 @@ pub fn get_max_withdraw_for_market_with_token_amount( is_leaving_drift: bool, ) -> DriftResult { let deposit_token_amount = get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), spot_market, &SpotBalanceType::Deposit, )?; let borrow_token_amount = get_token_amount( - spot_market.borrow_balance, + spot_market.borrow_balance(), spot_market, &SpotBalanceType::Borrow, )?; @@ -308,20 +308,20 @@ pub fn get_max_withdraw_for_market_with_token_amount( pub fn validate_spot_balances(spot_market: &SpotMarket) -> DriftResult { let depositors_amount: u64 = get_token_amount( - spot_market.deposit_balance, + spot_market.deposit_balance(), spot_market, &SpotBalanceType::Deposit, )? .cast()?; let borrowers_amount: u64 = get_token_amount( - spot_market.borrow_balance, + spot_market.borrow_balance(), spot_market, &SpotBalanceType::Borrow, )? .cast()?; let revenue_amount: u64 = get_token_amount( - spot_market.revenue_pool.scaled_balance, + spot_market.revenue_pool.scaled_balance(), spot_market, &SpotBalanceType::Deposit, )? @@ -338,7 +338,7 @@ pub fn validate_spot_balances(spot_market: &SpotMarket) -> DriftResult { revenue_amount, depositors_amount, depositors_claim, - spot_market.deposit_balance + spot_market.deposit_balance() )?; Ok(depositors_claim) diff --git a/programs/drift/src/state/amm_cache.rs b/programs/drift/src/state/amm_cache.rs index 1e485b2289..06003edff1 100644 --- a/programs/drift/src/state/amm_cache.rs +++ b/programs/drift/src/state/amm_cache.rs @@ -313,13 +313,13 @@ impl<'a> AccountZeroCopyMut<'a, CacheInfo, AmmCacheFixed> { let cached_info = self.get_mut(perp_market.market_index as u32); let fee_pool_token_amount = get_token_amount( - perp_market.amm.fee_pool.scaled_balance, + perp_market.amm.fee_pool.scaled_balance(), "e_market, perp_market.amm.fee_pool.balance_type(), )?; let net_pnl_pool_token_amount = get_token_amount( - perp_market.pnl_pool.scaled_balance, + perp_market.pnl_pool.scaled_balance(), "e_market, perp_market.pnl_pool.balance_type(), )? @@ -338,15 +338,15 @@ impl<'a> AccountZeroCopyMut<'a, CacheInfo, AmmCacheFixed> { { cached_info.last_fee_pool_token_amount = fee_pool_token_amount; cached_info.last_net_pnl_pool_token_amount = net_pnl_pool_token_amount; - cached_info.last_exchange_fees = perp_market.amm.total_exchange_fee; - cached_info.last_settle_amm_ex_fees = perp_market.amm.total_exchange_fee; + cached_info.last_exchange_fees = perp_market.amm.total_exchange_fee(); + cached_info.last_settle_amm_ex_fees = perp_market.amm.total_exchange_fee(); cached_info.last_settle_amm_pnl = net_pnl_pool_token_amount; return Ok(()); } let exchange_fee_delta = perp_market .amm - .total_exchange_fee + .total_exchange_fee() .saturating_sub(cached_info.last_exchange_fees); let amount_to_send_to_lp_pool = amm_amount_available @@ -366,7 +366,7 @@ impl<'a> AccountZeroCopyMut<'a, CacheInfo, AmmCacheFixed> { cached_info.last_fee_pool_token_amount = fee_pool_token_amount; cached_info.last_net_pnl_pool_token_amount = net_pnl_pool_token_amount; - cached_info.last_exchange_fees = perp_market.amm.total_exchange_fee; + cached_info.last_exchange_fees = perp_market.amm.total_exchange_fee(); Ok(()) } diff --git a/programs/drift/src/state/constituent_map.rs b/programs/drift/src/state/constituent_map.rs index b3e9bd5aa8..8ea2e5b63b 100644 --- a/programs/drift/src/state/constituent_map.rs +++ b/programs/drift/src/state/constituent_map.rs @@ -23,7 +23,7 @@ pub struct ConstituentMap<'a>(pub BTreeMap>) impl<'a> ConstituentMap<'a> { #[track_caller] #[inline(always)] - pub fn get_ref(&self, constituent_index: &u16) -> DriftResult> { + pub fn get_ref(&self, constituent_index: &u16) -> DriftResult> { let loader = match self.0.get(constituent_index) { Some(loader) => loader, None => { @@ -56,7 +56,7 @@ impl<'a> ConstituentMap<'a> { #[track_caller] #[inline(always)] - pub fn get_ref_mut(&self, market_index: &u16) -> DriftResult> { + pub fn get_ref_mut(&self, market_index: &u16) -> DriftResult> { let loader = match self.0.get(market_index) { Some(loader) => loader, None => { diff --git a/programs/drift/src/state/events.rs b/programs/drift/src/state/events.rs index 7c538b74fc..1c5438362c 100644 --- a/programs/drift/src/state/events.rs +++ b/programs/drift/src/state/events.rs @@ -1,5 +1,7 @@ -use anchor_lang::prelude::*; -use borsh::{BorshDeserialize, BorshSerialize}; +use anchor_lang::prelude::{ + borsh::{BorshDeserialize, BorshSerialize}, + *, +}; use crate::controller::position::PositionDirection; use crate::error::{DriftResult, ErrorCode::InvalidOrder}; diff --git a/programs/drift/src/state/fulfillment_params/serum.rs b/programs/drift/src/state/fulfillment_params/serum.rs index fa30f65eac..5c9eae93a5 100644 --- a/programs/drift/src/state/fulfillment_params/serum.rs +++ b/programs/drift/src/state/fulfillment_params/serum.rs @@ -65,7 +65,7 @@ pub struct SerumContext<'a, 'b> { } impl<'a, 'b> SerumContext<'a, 'b> { - pub fn load_serum_market(&self) -> DriftResult { + pub fn load_serum_market(&self) -> DriftResult> { Market::load(self.serum_market, self.serum_program.key, false).map_err(|e| { msg!("{:?}", e); ErrorCode::InvalidSerumMarket diff --git a/programs/drift/src/state/insurance_fund_stake.rs b/programs/drift/src/state/insurance_fund_stake.rs index 25d81f4b0d..35a2d47cc4 100644 --- a/programs/drift/src/state/insurance_fund_stake.rs +++ b/programs/drift/src/state/insurance_fund_stake.rs @@ -3,16 +3,16 @@ use crate::error::ErrorCode; use crate::math::constants::EPOCH_DURATION; use crate::math::safe_math::SafeMath; use crate::math_error; -use crate::safe_decrement; -use crate::safe_increment; use crate::state::spot_market::SpotMarket; use crate::state::traits::Size; use crate::validate; use anchor_lang::prelude::*; +use drift_macros::legacy_layout; #[cfg(test)] mod tests; +#[legacy_layout] #[account(zero_copy(unsafe))] #[derive(Default, Eq, PartialEq, Debug)] #[repr(C)] @@ -39,24 +39,24 @@ impl InsuranceFundStake { InsuranceFundStake { authority, market_index, - last_withdraw_request_shares: 0, + last_withdraw_request_shares: 0.into(), last_withdraw_request_value: 0, last_withdraw_request_ts: 0, cost_basis: 0, - if_base: 0, + if_base: 0.into(), last_valid_ts: now, - if_shares: 0, + if_shares: 0.into(), padding: [0; 14], } } fn validate_base(&self, spot_market: &SpotMarket) -> DriftResult { validate!( - self.if_base == spot_market.insurance_fund.shares_base, + self.if_base == spot_market.insurance_fund.shares_base().into(), ErrorCode::InvalidIFRebase, "if stake bases mismatch. user base: {} market base {}", self.if_base, - spot_market.insurance_fund.shares_base + spot_market.insurance_fund.shares_base() )?; Ok(()) @@ -64,33 +64,35 @@ impl InsuranceFundStake { pub fn checked_if_shares(&self, spot_market: &SpotMarket) -> DriftResult { self.validate_base(spot_market)?; - Ok(self.if_shares) + Ok(self.if_shares()) } pub fn unchecked_if_shares(&self) -> u128 { - self.if_shares + self.if_shares() } pub fn increase_if_shares(&mut self, delta: u128, spot_market: &SpotMarket) -> DriftResult { self.validate_base(spot_market)?; - safe_increment!(self.if_shares, delta); + let if_shares = self.if_shares.as_u128(); + self.set_if_shares(if_shares.checked_add(delta).ok_or_else(math_error!())?); Ok(()) } pub fn decrease_if_shares(&mut self, delta: u128, spot_market: &SpotMarket) -> DriftResult { self.validate_base(spot_market)?; - safe_decrement!(self.if_shares, delta); + let if_shares = self.if_shares.as_u128(); + self.set_if_shares(if_shares.checked_sub(delta).ok_or_else(math_error!())?); Ok(()) } pub fn update_if_shares(&mut self, new_shares: u128, spot_market: &SpotMarket) -> DriftResult { self.validate_base(spot_market)?; - self.if_shares = new_shares; - + self.set_if_shares(new_shares); Ok(()) } } +#[legacy_layout] #[account(zero_copy(unsafe))] #[derive(Default, Eq, PartialEq, Debug)] #[repr(C)] @@ -130,7 +132,7 @@ impl ProtocolIfSharesTransferConfig { .next_epoch_ts .safe_add(EPOCH_DURATION.safe_mul(n_epoch_durations)?)?; - self.current_epoch_transfer = 0; + self.set_current_epoch_transfer(0); } Ok(()) @@ -139,7 +141,8 @@ impl ProtocolIfSharesTransferConfig { pub fn validate_transfer(&self, requested_transfer: u128) -> DriftResult { let max_transfer = self .max_transfer_per_epoch - .saturating_sub(self.current_epoch_transfer); + .as_u128() + .saturating_sub(self.current_epoch_transfer.as_u128()); validate!( requested_transfer < max_transfer, diff --git a/programs/drift/src/state/load_ref.rs b/programs/drift/src/state/load_ref.rs index 6983a24e72..4cac5d4c63 100644 --- a/programs/drift/src/state/load_ref.rs +++ b/programs/drift/src/state/load_ref.rs @@ -1,6 +1,5 @@ use anchor_lang::prelude::*; use anchor_lang::ZeroCopy; -use arrayref::array_ref; use std::cell::{Ref, RefMut}; use std::mem; diff --git a/programs/drift/src/state/lp_pool.rs b/programs/drift/src/state/lp_pool.rs index f975ce9e81..cdc03fe29c 100644 --- a/programs/drift/src/state/lp_pool.rs +++ b/programs/drift/src/state/lp_pool.rs @@ -16,8 +16,11 @@ use crate::state::oracle_map::OracleMap; use crate::state::paused_operations::ConstituentLpOperation; use crate::state::spot_market_map::SpotMarketMap; use crate::state::user::MarketType; -use anchor_lang::prelude::*; -use borsh::{BorshDeserialize, BorshSerialize}; +use anchor_lang::prelude::{ + borsh::{BorshDeserialize, BorshSerialize}, + *, +}; +use drift_macros::legacy_layout; use enumflags2::BitFlags; use super::oracle::OraclePriceData; @@ -59,6 +62,7 @@ pub const MAX_ORACLE_STALENESS_FOR_TARGET_CALC: u64 = 10u64; #[cfg(test)] mod tests; +#[legacy_layout] #[account(zero_copy(unsafe))] #[derive(Debug)] #[repr(C)] @@ -138,11 +142,11 @@ impl Default for LPPool { whitelist_mint: Pubkey::default(), constituent_target_base: Pubkey::default(), constituent_correlations: Pubkey::default(), - max_aum: 0, - last_aum: 0, - cumulative_quote_sent_to_perp_markets: 0, - cumulative_quote_received_from_perp_markets: 0, - total_mint_redeem_fees_paid: 0, + max_aum: 0.into(), + last_aum: 0.into(), + cumulative_quote_sent_to_perp_markets: 0.into(), + cumulative_quote_received_from_perp_markets: 0.into(), + total_mint_redeem_fees_paid: 0.into(), last_aum_slot: 0, max_settle_quote_amount: 0, _padding: 0, @@ -180,6 +184,7 @@ impl LPPool { // TODO: assuming mint decimals = quote decimals = 6 Ok(self .last_aum + .as_u128() .safe_mul(PRICE_PRECISION)? .safe_div(supply as u128)?) } @@ -297,7 +302,7 @@ impl LPPool { in_target_weight: i64, dlp_total_supply: u64, ) -> DriftResult<(u64, u128, i64, i128)> { - let (mut in_fee_pct, out_fee_pct) = if self.last_aum == 0 { + let (mut in_fee_pct, out_fee_pct) = if self.last_aum == 0.into() { (0, 0) } else { self.get_swap_fees( @@ -341,7 +346,7 @@ impl LPPool { } else { token_amount_usd .safe_mul(dlp_total_supply as u128)? - .safe_div(self.last_aum)? + .safe_div(self.last_aum.into())? .safe_div(token_precision_denominator)? }; @@ -399,6 +404,7 @@ impl LPPool { // Apply proportion to AUM and convert to token amount let out_amount = self .last_aum + .as_u128() .safe_mul(proportion)? .safe_mul(token_precision_denominator)? .safe_div(PERCENTAGE_PRECISION)? @@ -625,7 +631,12 @@ impl LPPool { let trade_ratio: i128 = notional_trade_size .abs() .safe_mul(PERCENTAGE_PRECISION_I128)? - .safe_div(self.last_aum.max(MIN_AUM_EXECUTION_FEE).cast::()?)?; + .safe_div( + self.last_aum + .as_u128() + .max(MIN_AUM_EXECUTION_FEE) + .cast::()?, + )?; // Linear fee computation amount let in_fee_execution_linear = @@ -708,15 +719,17 @@ impl LPPool { self.target_position_delay_fee_bps_per_10_slots, )?; - Ok(oracle_uncertainty_fee + oracle_uncertainty_fee .safe_add(position_uncertainty_fee)? - .cast::()?) + .cast::() } pub fn record_mint_redeem_fees(&mut self, amount: i64) -> DriftResult { self.total_mint_redeem_fees_paid = self .total_mint_redeem_fees_paid - .safe_add(amount.cast::()?)?; + .as_i128() + .safe_add(amount as i128)? + .into(); Ok(()) } @@ -832,7 +845,7 @@ impl LPPool { } let aum_u128 = aum.max(0).cast::()?; - self.last_aum = aum_u128; + self.set_last_aum(aum_u128); self.last_aum_slot = slot; Ok((aum_u128, crypto_delta, derivative_groups)) @@ -847,6 +860,7 @@ impl LPPool { } } +#[legacy_layout] #[zero_copy(unsafe)] #[derive(Default, Eq, PartialEq, Debug, BorshDeserialize, BorshSerialize)] #[repr(C)] @@ -875,16 +889,16 @@ impl SpotBalance for ConstituentSpotBalance { } fn balance(&self) -> u128 { - self.scaled_balance + self.scaled_balance.into() } fn increase_balance(&mut self, delta: u128) -> DriftResult { - self.scaled_balance = self.scaled_balance.safe_add(delta)?; + self.set_scaled_balance(self.scaled_balance().safe_add(delta)?); Ok(()) } fn decrease_balance(&mut self, delta: u128) -> DriftResult { - self.scaled_balance = self.scaled_balance.safe_sub(delta)?; + self.set_scaled_balance(self.scaled_balance().safe_sub(delta)?); Ok(()) } @@ -896,7 +910,7 @@ impl SpotBalance for ConstituentSpotBalance { impl ConstituentSpotBalance { pub fn get_token_amount(&self, spot_market: &SpotMarket) -> DriftResult { - get_token_amount(self.scaled_balance, spot_market, &self.balance_type) + get_token_amount(self.scaled_balance.into(), spot_market, &self.balance_type) } pub fn get_signed_token_amount(&self, spot_market: &SpotMarket) -> DriftResult { @@ -905,6 +919,7 @@ impl ConstituentSpotBalance { } } +#[legacy_layout] #[account(zero_copy(unsafe))] #[derive(Debug)] #[repr(C)] @@ -990,7 +1005,7 @@ impl Default for Constituent { mint: Pubkey::default(), lp_pool: Pubkey::default(), vault: Pubkey::default(), - total_swap_fees: 0, + total_swap_fees: 0.into(), spot_balance: ConstituentSpotBalance::default(), last_spot_balance_token_amount: 0, cumulative_spot_interest_accrued_token_amount: 0, @@ -1096,7 +1111,7 @@ impl Constituent { } pub fn record_swap_fees(&mut self, amount: i128) -> DriftResult { - self.total_swap_fees = self.total_swap_fees.safe_add(amount)?; + self.total_swap_fees = self.total_swap_fees.as_i128().safe_add(amount)?.into(); Ok(()) } diff --git a/programs/drift/src/state/lp_pool/tests.rs b/programs/drift/src/state/lp_pool/tests.rs index 3e76383af9..c090342007 100644 --- a/programs/drift/src/state/lp_pool/tests.rs +++ b/programs/drift/src/state/lp_pool/tests.rs @@ -581,7 +581,7 @@ mod swap_tests { out_volatility: u64, ) { let lp_pool = LPPool { - last_aum: 1_000_000_000_000, + last_aum: 1_000_000_000_000.into(), target_oracle_delay_fee_bps_per_10_slots: 2, target_position_delay_fee_bps_per_10_slots: 10, ..LPPool::default() @@ -596,10 +596,12 @@ mod swap_tests { ..OraclePriceData::default() }; - let in_notional = (in_current_weight as u128) * lp_pool.last_aum / PERCENTAGE_PRECISION; + let in_notional = + (in_current_weight as u128) * lp_pool.last_aum.as_u128() / PERCENTAGE_PRECISION; let in_token_amount = in_notional * 10_u128.pow(in_decimals) / oracle_0.price as u128; - let out_notional = (out_current_weight as u128) * lp_pool.last_aum / PERCENTAGE_PRECISION; + let out_notional = + (out_current_weight as u128) * lp_pool.last_aum.as_u128() / PERCENTAGE_PRECISION; let out_token_amount = out_notional * 10_u128.pow(out_decimals) / oracle_1.price as u128; let constituent_0 = Constituent { @@ -748,7 +750,7 @@ mod swap_tests { max_weight_deviation: PERCENTAGE_PRECISION_I64 / 10, // 10% spot_market_index: 0, spot_balance: ConstituentSpotBalance { - scaled_balance: 500_000, + scaled_balance: 500_000.into(), cumulative_deposits: 1_000_000, balance_type: SpotBalanceType::Deposit, market_index: 0, @@ -762,7 +764,7 @@ mod swap_tests { let spot_market = SpotMarket { market_index: 0, decimals: 6, - cumulative_deposit_interest: 10_000_000_000_000, + cumulative_deposit_interest: 10_000_000_000_000.into(), ..SpotMarket::default() }; @@ -810,7 +812,7 @@ mod swap_tests { volatility: u64, ) { let lp_pool = LPPool { - last_aum, + last_aum: last_aum.into(), _padding: 0, min_mint_fee: 0, ..LPPool::default() @@ -834,7 +836,7 @@ mod swap_tests { max_weight_deviation: PERCENTAGE_PRECISION_I64 / 10, spot_market_index: 0, spot_balance: ConstituentSpotBalance { - scaled_balance: 0, + scaled_balance: 0.into(), cumulative_deposits: 0, balance_type: SpotBalanceType::Deposit, market_index: 0, @@ -1002,7 +1004,7 @@ mod swap_tests { volatility: u64, ) { let lp_pool = LPPool { - last_aum, + last_aum: last_aum.into(), _padding: 0, min_mint_fee: 100, // 1 bps ..LPPool::default() @@ -1026,7 +1028,7 @@ mod swap_tests { max_weight_deviation: PERCENTAGE_PRECISION_I64 / 10, spot_market_index: 0, spot_balance: ConstituentSpotBalance { - scaled_balance: 0, + scaled_balance: 0.into(), cumulative_deposits: 0, balance_type: SpotBalanceType::Deposit, market_index: 0, @@ -1213,7 +1215,7 @@ mod swap_tests { out_target_weight: i64, ) -> (u128, u128, i128, i128, i128, i128) { let lp_pool = LPPool { - last_aum: 1_000_000_000_000, + last_aum: 1_000_000_000_000.into(), ..LPPool::default() }; @@ -1231,16 +1233,18 @@ mod swap_tests { let in_token_amount = in_notional * 10_i128.pow(6) / oracle_0.price as i128; let in_spot_balance = if in_token_amount > 0 { ConstituentSpotBalance { - scaled_balance: (in_token_amount.abs() as u128) - * (SPOT_BALANCE_PRECISION / 10_u128.pow(6)), + scaled_balance: ((in_token_amount.abs() as u128) + * (SPOT_BALANCE_PRECISION / 10_u128.pow(6))) + .into(), balance_type: SpotBalanceType::Deposit, market_index: 0, ..ConstituentSpotBalance::default() } } else { ConstituentSpotBalance { - scaled_balance: (in_token_amount.abs() as u128) - * (SPOT_BALANCE_PRECISION / 10_u128.pow(6)), + scaled_balance: ((in_token_amount.abs() as u128) + * (SPOT_BALANCE_PRECISION / 10_u128.pow(6))) + .into(), balance_type: SpotBalanceType::Borrow, market_index: 0, ..ConstituentSpotBalance::default() @@ -1252,16 +1256,18 @@ mod swap_tests { let out_token_amount = out_notional * 10_i128.pow(6) / oracle_1.price as i128; let out_spot_balance = if out_token_amount > 0 { ConstituentSpotBalance { - scaled_balance: (out_token_amount.abs() as u128) - * (SPOT_BALANCE_PRECISION / 10_u128.pow(6)), + scaled_balance: ((out_token_amount.abs() as u128) + * (SPOT_BALANCE_PRECISION / 10_u128.pow(6))) + .into(), balance_type: SpotBalanceType::Deposit, market_index: 0, ..ConstituentSpotBalance::default() } } else { ConstituentSpotBalance { - scaled_balance: (out_token_amount.abs() as u128) - * (SPOT_BALANCE_PRECISION / 10_u128.pow(6)), + scaled_balance: ((out_token_amount.abs() as u128) + * (SPOT_BALANCE_PRECISION / 10_u128.pow(6))) + .into(), balance_type: SpotBalanceType::Deposit, market_index: 0, ..ConstituentSpotBalance::default() @@ -1589,7 +1595,7 @@ mod swap_fee_tests { #[test] fn test_lp_pool_get_linear_fee_execution() { let lp_pool = LPPool { - last_aum: 10_000_000 * QUOTE_PRECISION, // $10,000,000 + last_aum: (10_000_000 * QUOTE_PRECISION).into(), // $10,000,000 ..LPPool::default() }; @@ -1610,7 +1616,7 @@ mod swap_fee_tests { #[test] fn test_lp_pool_get_quadratic_fee_execution() { let lp_pool = LPPool { - last_aum: 10_000_000 * QUOTE_PRECISION, // $10,000,000 + last_aum: (10_000_000 * QUOTE_PRECISION).into(), // $10,000,000 ..LPPool::default() }; @@ -1631,7 +1637,7 @@ mod swap_fee_tests { #[test] fn test_lp_pool_get_quadratic_fee_inventory() { let lp_pool = LPPool { - last_aum: 10_000_000 * QUOTE_PRECISION, // $10,000,000 + last_aum: (10_000_000 * QUOTE_PRECISION).into(), // $10,000,000 ..LPPool::default() }; @@ -1657,7 +1663,7 @@ mod swap_fee_tests { #[test] fn test_target_delays() { let lp_pool = LPPool { - last_aum: 10_000_000 * QUOTE_PRECISION, // $10,000,000 + last_aum: (10_000_000 * QUOTE_PRECISION).into(), // $10,000,000 target_oracle_delay_fee_bps_per_10_slots: 2, target_position_delay_fee_bps_per_10_slots: 10, ..LPPool::default() @@ -2534,7 +2540,7 @@ mod update_aum_tests { let mut usdc_spot_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, historical_oracle_data: HistoricalOracleData::default_quote_oracle(), ..SpotMarket::default() @@ -2545,7 +2551,7 @@ mod update_aum_tests { market_index: 1, oracle_source: OracleSource::PythLazer, oracle: sol_oracle_pubkey, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, historical_oracle_data: HistoricalOracleData::default_price(200 * PRICE_PRECISION_I64), ..SpotMarket::default() @@ -2556,7 +2562,7 @@ mod update_aum_tests { market_index: 2, oracle_source: OracleSource::PythLazer, oracle: btc_oracle_pubkey, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 8, historical_oracle_data: HistoricalOracleData::default_price( 100_000 * PRICE_PRECISION_I64, @@ -2569,7 +2575,7 @@ mod update_aum_tests { market_index: 3, oracle_source: OracleSource::PythLazer, oracle: bonk_oracle_pubkey, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 5, historical_oracle_data: HistoricalOracleData::default_price(22), ..SpotMarket::default() @@ -2671,7 +2677,8 @@ mod update_aum_tests { // Verify LP pool state was updated assert_eq!( - lp_pool.last_aum, aum, + lp_pool.last_aum, + aum.into(), "{}: last_aum should match calculated AUM", test_name ); @@ -2942,7 +2949,7 @@ mod update_constituent_target_base_for_derivatives_tests { market_index: parent_index, oracle_source: OracleSource::PythLazer, oracle: parent_oracle_pubkey, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, historical_oracle_data: HistoricalOracleData::default_price(parent_oracle.price), ..SpotMarket::default() @@ -2957,7 +2964,7 @@ mod update_constituent_target_base_for_derivatives_tests { market_index: derivative1_index, oracle_source: OracleSource::PythLazer, oracle: derivative1_oracle_pubkey, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, historical_oracle_data: HistoricalOracleData::default_price(derivative1_oracle.price), ..SpotMarket::default() @@ -2972,7 +2979,7 @@ mod update_constituent_target_base_for_derivatives_tests { market_index: derivative2_index, oracle_source: OracleSource::PythLazer, oracle: derivative2_oracle_pubkey, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, historical_oracle_data: HistoricalOracleData::default_price(derivative2_oracle.price), ..SpotMarket::default() @@ -2987,7 +2994,7 @@ mod update_constituent_target_base_for_derivatives_tests { market_index: derivative3_index, oracle_source: OracleSource::PythLazer, oracle: derivative3_oracle_pubkey, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, historical_oracle_data: HistoricalOracleData::default_price(derivative3_oracle.price), ..SpotMarket::default() @@ -3258,7 +3265,7 @@ mod update_constituent_target_base_for_derivatives_tests { market_index: parent_index, oracle_source: OracleSource::PythLazer, oracle: parent_oracle_pubkey, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, historical_oracle_data: HistoricalOracleData::default_price(parent_oracle.price), ..SpotMarket::default() @@ -3273,7 +3280,7 @@ mod update_constituent_target_base_for_derivatives_tests { market_index: derivative_index, oracle_source: OracleSource::PythLazer, oracle: derivative_oracle_pubkey, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, historical_oracle_data: HistoricalOracleData::default_price(derivative_oracle.price), ..SpotMarket::default() @@ -3547,7 +3554,7 @@ mod update_constituent_target_base_for_derivatives_tests { market_index: parent_index, oracle_source: OracleSource::PythLazer, oracle: parent_oracle_pubkey, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, historical_oracle_data: HistoricalOracleData::default_price(parent_oracle.price), ..SpotMarket::default() @@ -3562,7 +3569,7 @@ mod update_constituent_target_base_for_derivatives_tests { market_index: derivative_index, oracle_source: OracleSource::PythLazer, oracle: derivative_oracle_pubkey, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, historical_oracle_data: HistoricalOracleData::default_price(derivative_oracle.price), ..SpotMarket::default() @@ -3761,7 +3768,7 @@ mod update_constituent_target_base_for_derivatives_tests { market_index: parent_index, oracle_source: OracleSource::PythLazer, oracle: parent_oracle_pubkey, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, historical_oracle_data: HistoricalOracleData::default_price(parent_oracle.price), ..SpotMarket::default() @@ -3776,7 +3783,7 @@ mod update_constituent_target_base_for_derivatives_tests { market_index: derivative1_index, oracle_source: OracleSource::PythLazer, oracle: derivative1_oracle_pubkey, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, historical_oracle_data: HistoricalOracleData::default_price(derivative1_oracle.price), ..SpotMarket::default() @@ -3791,7 +3798,7 @@ mod update_constituent_target_base_for_derivatives_tests { market_index: derivative2_index, oracle_source: OracleSource::PythLazer, oracle: derivative2_oracle_pubkey, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 9, historical_oracle_data: HistoricalOracleData::default_price(derivative2_oracle.price), ..SpotMarket::default() diff --git a/programs/drift/src/state/oracle.rs b/programs/drift/src/state/oracle.rs index 0275a5549a..43e6d55e51 100644 --- a/programs/drift/src/state/oracle.rs +++ b/programs/drift/src/state/oracle.rs @@ -562,13 +562,13 @@ pub fn get_sb_on_demand_price( let pull_feed_account_info: Ref = load_ref(price_oracle).or(Err(ErrorCode::UnableToLoadOracle))?; - let latest_oracle_submssions: Vec = + let latest_oracle_submissions: Vec = pull_feed_account_info.latest_submissions(); - let average_price = latest_oracle_submssions + let average_price = latest_oracle_submissions .iter() - .map(|submission| submission.value) + .map(|submission| submission.value()) .sum::() - / latest_oracle_submssions.len() as i128; + / latest_oracle_submissions.len() as i128; let price = convert_sb_i128(&average_price)?.cast::()?; @@ -582,7 +582,7 @@ pub fn get_sb_on_demand_price( let delay = clock_slot .cast::()? - .safe_sub(latest_oracle_submssions[0].landed_at.cast()?)?; + .safe_sub(latest_oracle_submissions[0].landed_at.cast()?)?; let has_sufficient_number_of_data_points = true; diff --git a/programs/drift/src/state/oracle/tests.rs b/programs/drift/src/state/oracle/tests.rs index 8d64f725b4..e29fbd4adb 100644 --- a/programs/drift/src/state/oracle/tests.rs +++ b/programs/drift/src/state/oracle/tests.rs @@ -173,11 +173,11 @@ fn use_mm_oracle() { let mut market = PerpMarket { market_index: 0, amm: AMM { - base_asset_reserve: 512295081967, - quote_asset_reserve: 488 * AMM_RESERVE_PRECISION, - sqrt_k: 500 * AMM_RESERVE_PRECISION, - peg_multiplier: 22_100_000_000, - base_asset_amount_with_amm: (12295081967_i128), + base_asset_reserve: 512295081967.into(), + quote_asset_reserve: (488 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (500 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 22_100_000_000.into(), + base_asset_amount_with_amm: (12295081967_i128).into(), max_spread: 1000, mm_oracle_price: 130 * PRICE_PRECISION_I64 + 973, mm_oracle_slot: slot, @@ -267,11 +267,11 @@ fn mm_oracle_confidence() { let market = PerpMarket { market_index: 0, amm: AMM { - base_asset_reserve: 512295081967, - quote_asset_reserve: 488 * AMM_RESERVE_PRECISION, - sqrt_k: 500 * AMM_RESERVE_PRECISION, - peg_multiplier: 22_100_000_000, - base_asset_amount_with_amm: (12295081967_i128), + base_asset_reserve: 512295081967.into(), + quote_asset_reserve: (488 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (500 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 22_100_000_000.into(), + base_asset_amount_with_amm: (12295081967_i128).into(), max_spread: 1000, mm_oracle_price: 130 * PRICE_PRECISION_I64 + 999, mm_oracle_slot: slot, diff --git a/programs/drift/src/state/oracle_map.rs b/programs/drift/src/state/oracle_map.rs index 48605e0403..af9644fd46 100644 --- a/programs/drift/src/state/oracle_map.rs +++ b/programs/drift/src/state/oracle_map.rs @@ -12,7 +12,6 @@ use crate::state::user::MarketType; use anchor_lang::prelude::{AccountInfo, Pubkey}; use anchor_lang::Discriminator; use anchor_lang::Key; -use arrayref::array_ref; use std::collections::BTreeMap; use std::iter::Peekable; use std::slice::Iter; diff --git a/programs/drift/src/state/order_params.rs b/programs/drift/src/state/order_params.rs index a5b81b8bb6..5cb58a08bc 100644 --- a/programs/drift/src/state/order_params.rs +++ b/programs/drift/src/state/order_params.rs @@ -10,8 +10,10 @@ use crate::math::safe_unwrap::SafeUnwrap; use crate::state::events::OrderActionExplanation; use crate::state::perp_market::{ContractTier, PerpMarket}; use crate::state::user::{MarketType, OrderTriggerCondition, OrderType}; -use anchor_lang::prelude::*; -use borsh::{BorshDeserialize, BorshSerialize}; +use anchor_lang::prelude::{ + borsh::{BorshDeserialize, BorshSerialize}, + *, +}; use std::ops::Div; #[cfg(test)] diff --git a/programs/drift/src/state/order_params/tests.rs b/programs/drift/src/state/order_params/tests.rs index d2d54af789..0b3a8a775a 100644 --- a/programs/drift/src/state/order_params/tests.rs +++ b/programs/drift/src/state/order_params/tests.rs @@ -53,12 +53,12 @@ mod update_perp_auction_params { fn test_extreme_sanitize_oracle_order() { let oracle_price = 145 * PRICE_PRECISION_I64; let mut amm = AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), short_spread: (BID_ASK_SPREAD_PRECISION / 100) as u32, long_spread: (BID_ASK_SPREAD_PRECISION / 100) as u32, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), volume_24h: 1_000_000 * QUOTE_PRECISION_U64, ..AMM::default() @@ -149,12 +149,12 @@ mod update_perp_auction_params { fn test_signed_msg_orders_oracle() { let oracle_price = 100 * PRICE_PRECISION_I64; let mut amm = AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), short_spread: (BID_ASK_SPREAD_PRECISION / 100) as u32, long_spread: (BID_ASK_SPREAD_PRECISION / 100) as u32, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), volume_24h: 1_000_000 * QUOTE_PRECISION_U64, ..AMM::default() @@ -272,12 +272,12 @@ mod update_perp_auction_params { fn test_signed_msg_orders_limit() { let oracle_price = 100 * PRICE_PRECISION_I64; let mut amm = AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), short_spread: (BID_ASK_SPREAD_PRECISION / 100) as u32, long_spread: (BID_ASK_SPREAD_PRECISION / 100) as u32, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), volume_24h: 1_000_000 * QUOTE_PRECISION_U64, ..AMM::default() @@ -398,12 +398,12 @@ mod update_perp_auction_params { fn test_extreme_sanitize_oracle_order_huge_market_prem() { let oracle_price = 145 * PRICE_PRECISION_I64; let mut amm = AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), short_spread: (BID_ASK_SPREAD_PRECISION / 100) as u32, long_spread: (BID_ASK_SPREAD_PRECISION / 100) as u32, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), volume_24h: 1_000_000 * QUOTE_PRECISION_U64, ..AMM::default() @@ -446,12 +446,12 @@ mod update_perp_auction_params { fn test_sanitize_limit() { let oracle_price = 100 * PRICE_PRECISION_I64; let mut amm = AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), short_spread: (BID_ASK_SPREAD_PRECISION / 100) as u32, long_spread: (BID_ASK_SPREAD_PRECISION / 100) as u32, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), volume_24h: 1_000_000 * QUOTE_PRECISION_U64, ..AMM::default() @@ -624,12 +624,12 @@ mod update_perp_auction_params { fn test_sanitize_oracle_limit() { let oracle_price = 100 * PRICE_PRECISION_I64; let mut amm = AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), short_spread: (BID_ASK_SPREAD_PRECISION / 1000) as u32, long_spread: (BID_ASK_SPREAD_PRECISION / 1000) as u32, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), volume_24h: 1_000_000 * QUOTE_PRECISION_U64, ..AMM::default() @@ -779,12 +779,12 @@ mod update_perp_auction_params { fn test_market_sanitize() { let oracle_price = 99 * PRICE_PRECISION_I64; let mut amm = AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), short_spread: (BID_ASK_SPREAD_PRECISION / 100) as u32, long_spread: (BID_ASK_SPREAD_PRECISION / 100) as u32, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 99 * PEG_PRECISION, + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (99 * PEG_PRECISION).into(), volume_24h: 1_000_000 * QUOTE_PRECISION_U64, ..AMM::default() @@ -889,12 +889,12 @@ mod update_perp_auction_params { fn test_oracle_market_sanitize() { let oracle_price = 99 * PRICE_PRECISION_I64; let mut amm = AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), short_spread: (BID_ASK_SPREAD_PRECISION / 100) as u32, long_spread: (BID_ASK_SPREAD_PRECISION / 100) as u32, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), volume_24h: 1_000_000 * QUOTE_PRECISION_U64, ..AMM::default() @@ -982,12 +982,12 @@ mod update_perp_auction_params { fn test_market_sanatize_no_auction_params() { let oracle_price = 99 * PRICE_PRECISION_I64; let mut amm = AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), short_spread: (BID_ASK_SPREAD_PRECISION / 100) as u32, long_spread: (BID_ASK_SPREAD_PRECISION / 100) as u32, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), volume_24h: 1_000_000 * QUOTE_PRECISION_U64, ..AMM::default() @@ -1132,12 +1132,12 @@ mod update_perp_auction_params { fn test_oracle_market_sanitize_no_auction_params() { let oracle_price = 99 * PRICE_PRECISION_I64; let mut amm = AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), short_spread: (BID_ASK_SPREAD_PRECISION / 100) as u32, long_spread: (BID_ASK_SPREAD_PRECISION / 100) as u32, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), volume_24h: 1_000_000 * QUOTE_PRECISION_U64, ..AMM::default() }; diff --git a/programs/drift/src/state/perp_market.rs b/programs/drift/src/state/perp_market.rs index 5a6b441c1c..9189157d74 100644 --- a/programs/drift/src/state/perp_market.rs +++ b/programs/drift/src/state/perp_market.rs @@ -2,7 +2,10 @@ use crate::msg; use crate::state::fill_mode::FillMode; use crate::state::pyth_lazer_oracle::PythLazerOracle; use crate::state::user::{MarketType, Order}; -use anchor_lang::prelude::*; +use anchor_lang::prelude::{ + borsh::{BorshDeserialize, BorshSerialize}, + *, +}; use crate::state::state::{State, ValidityGuardRails}; use std::cmp::max; @@ -36,10 +39,9 @@ use crate::state::oracle::{ }; use crate::state::spot_market::{AssetTier, SpotBalance, SpotBalanceType}; use crate::state::traits::{MarketIndexOffset, Size}; -use borsh::{BorshDeserialize, BorshSerialize}; use crate::state::paused_operations::PerpOperation; -use drift_macros::assert_no_slop; +use drift_macros::{assert_no_slop, legacy_layout}; use static_assertions::const_assert_eq; use super::oracle_map::OracleIdentifier; @@ -154,6 +156,7 @@ impl ContractTier { } } +#[legacy_layout] #[account(zero_copy(unsafe))] #[derive(Eq, PartialEq, Debug)] #[repr(C)] @@ -373,7 +376,7 @@ impl PerpMarket { .net_revenue_since_last_funding .cast::()? .safe_mul(PERCENTAGE_PRECISION_I128)? - .safe_div(self.amm.total_fee_minus_distributions.max(1))?; + .safe_div(self.amm.total_fee_minus_distributions.as_i128().max(1))?; let percent_drawdown_limit_breached = match self.contract_tier { ContractTier::A => percent_drawdown <= -PERCENTAGE_PRECISION_I128 / 50, @@ -697,6 +700,7 @@ impl PerpMarket { let peg_sqrt = (self .amm .peg_multiplier + .as_u128() .safe_mul(PEG_PRECISION)? .saturating_add(1)) .nth_root(2) @@ -706,8 +710,9 @@ impl PerpMarket { let mut quote_asset_reserve_upper_bound = self .amm .sqrt_k + .as_u128() .safe_mul(peg_sqrt)? - .safe_div(self.amm.peg_multiplier)?; + .safe_div(self.amm.peg_multiplier.into())?; // for price [0,1] maintain following invariants: if direction == PositionDirection::Long { @@ -715,19 +720,21 @@ impl PerpMarket { quote_asset_reserve_lower_bound = self .amm .sqrt_k + .as_u128() .safe_mul(22361)? .safe_mul(peg_sqrt)? .safe_div(100000)? - .safe_div(self.amm.peg_multiplier)? + .safe_div(self.amm.peg_multiplier.into())? } else { // highest bid price is $0.95 quote_asset_reserve_upper_bound = self .amm .sqrt_k + .as_u128() .safe_mul(97467)? .safe_mul(peg_sqrt)? .safe_div(100000)? - .safe_div(self.amm.peg_multiplier)? + .safe_div(self.amm.peg_multiplier.into())? } Ok(( @@ -1024,6 +1031,7 @@ pub struct InsuranceClaim { pub last_revenue_withdraw_ts: i64, } +#[legacy_layout] #[zero_copy(unsafe)] #[derive(Default, Eq, PartialEq, Debug)] #[repr(C)] @@ -1047,16 +1055,16 @@ impl SpotBalance for PoolBalance { } fn balance(&self) -> u128 { - self.scaled_balance + self.scaled_balance.as_u128() } fn increase_balance(&mut self, delta: u128) -> DriftResult { - self.scaled_balance = self.scaled_balance.safe_add(delta)?; + self.scaled_balance = self.scaled_balance().safe_add(delta)?.into(); Ok(()) } fn decrease_balance(&mut self, delta: u128) -> DriftResult { - self.scaled_balance = self.scaled_balance.safe_sub(delta)?; + self.scaled_balance = self.scaled_balance().safe_sub(delta)?.into(); Ok(()) } @@ -1066,7 +1074,8 @@ impl SpotBalance for PoolBalance { } #[assert_no_slop] -#[zero_copy(unsafe)] +#[legacy_layout] +#[account(zero_copy(unsafe))] #[derive(Debug, PartialEq, Eq)] #[repr(C)] pub struct AMM { @@ -1214,7 +1223,7 @@ pub struct AMM { pub net_revenue_since_last_funding: i64, /// the last funding rate update unix_timestamp pub last_funding_rate_ts: i64, - /// the peridocity of the funding rate updates + /// the periodicity of the funding rate updates pub funding_period: i64, /// the base step size (increment) of orders /// precision: BASE_PRECISION @@ -1295,45 +1304,45 @@ impl Default for AMM { AMM { oracle: Pubkey::default(), historical_oracle_data: HistoricalOracleData::default(), - base_asset_amount_per_lp: 0, - quote_asset_amount_per_lp: 0, + base_asset_amount_per_lp: 0.into(), + quote_asset_amount_per_lp: 0.into(), fee_pool: PoolBalance::default(), - base_asset_reserve: 0, - quote_asset_reserve: 0, - concentration_coef: 0, - min_base_asset_reserve: 0, - max_base_asset_reserve: 0, - sqrt_k: 0, - peg_multiplier: 0, - terminal_quote_asset_reserve: 0, - base_asset_amount_long: 0, - base_asset_amount_short: 0, - base_asset_amount_with_amm: 0, - base_asset_amount_with_unsettled_lp: 0, - max_open_interest: 0, - quote_asset_amount: 0, - quote_entry_amount_long: 0, - quote_entry_amount_short: 0, - quote_break_even_amount_long: 0, - quote_break_even_amount_short: 0, - user_lp_shares: 0, - last_funding_rate: 0, - last_funding_rate_long: 0, - last_funding_rate_short: 0, - last_24h_avg_funding_rate: 0, - total_fee: 0, - total_mm_fee: 0, - total_exchange_fee: 0, - total_fee_minus_distributions: 0, - total_fee_withdrawn: 0, - total_liquidation_fee: 0, - cumulative_funding_rate_long: 0, - cumulative_funding_rate_short: 0, - total_social_loss: 0, - ask_base_asset_reserve: 0, - ask_quote_asset_reserve: 0, - bid_base_asset_reserve: 0, - bid_quote_asset_reserve: 0, + base_asset_reserve: 0.into(), + quote_asset_reserve: 0.into(), + concentration_coef: 0.into(), + min_base_asset_reserve: 0.into(), + max_base_asset_reserve: 0.into(), + sqrt_k: 0.into(), + peg_multiplier: 0.into(), + terminal_quote_asset_reserve: 0.into(), + base_asset_amount_long: 0.into(), + base_asset_amount_short: 0.into(), + base_asset_amount_with_amm: 0.into(), + base_asset_amount_with_unsettled_lp: 0.into(), + max_open_interest: 0.into(), + quote_asset_amount: 0.into(), + quote_entry_amount_long: 0.into(), + quote_entry_amount_short: 0.into(), + quote_break_even_amount_long: 0.into(), + quote_break_even_amount_short: 0.into(), + user_lp_shares: 0.into(), + last_funding_rate: 0.into(), + last_funding_rate_long: 0.into(), + last_funding_rate_short: 0.into(), + last_24h_avg_funding_rate: 0.into(), + total_fee: 0.into(), + total_mm_fee: 0.into(), + total_exchange_fee: 0.into(), + total_fee_minus_distributions: 0.into(), + total_fee_withdrawn: 0.into(), + total_liquidation_fee: 0.into(), + cumulative_funding_rate_long: 0.into(), + cumulative_funding_rate_short: 0.into(), + total_social_loss: 0.into(), + ask_base_asset_reserve: 0.into(), + ask_quote_asset_reserve: 0.into(), + bid_base_asset_reserve: 0.into(), + bid_quote_asset_reserve: 0.into(), last_oracle_normalised_price: 0, last_oracle_reserve_price_spread_pct: 0, last_bid_price_twap: 0, @@ -1442,10 +1451,10 @@ impl AMM { } pub fn get_lower_bound_sqrt_k(self) -> DriftResult { - Ok(self.sqrt_k.min( - (self.min_order_size.cast::()?) - .max(self.base_asset_amount_with_amm.unsigned_abs()), - )) + Ok(self + .sqrt_k + .as_u128() + .min((self.min_order_size as u128).max(self.base_asset_amount_with_amm.unsigned_abs()))) } pub fn get_protocol_owned_position(self) -> DriftResult { @@ -1471,10 +1480,10 @@ impl AMM { pub fn amm_wants_to_jit_make(&self, taker_direction: PositionDirection) -> DriftResult { let amm_wants_to_jit_make = match taker_direction { PositionDirection::Long => { - self.base_asset_amount_with_amm < -(self.order_step_size.cast()?) + self.base_asset_amount_with_amm.as_i128() < -(self.order_step_size as i128) } PositionDirection::Short => { - self.base_asset_amount_with_amm > (self.order_step_size.cast()?) + self.base_asset_amount_with_amm.as_i128() > self.order_step_size as i128 } }; Ok(amm_wants_to_jit_make && self.amm_jit_is_active()) @@ -1486,9 +1495,9 @@ impl AMM { if amm_wants_to_jit_make { // inventory scale let (max_bids, max_asks) = amm::_calculate_market_open_bids_asks( - self.base_asset_reserve, - self.min_base_asset_reserve, - self.max_base_asset_reserve, + self.base_asset_reserve.into(), + self.min_base_asset_reserve.into(), + self.max_base_asset_reserve.into(), )?; let protocol_owned_min_side_liquidity = max_bids.min(max_asks.abs()); @@ -1506,9 +1515,9 @@ impl AMM { pub fn reserve_price(&self) -> DriftResult { amm::calculate_price( - self.quote_asset_reserve, - self.base_asset_reserve, - self.peg_multiplier, + self.quote_asset_reserve.into(), + self.base_asset_reserve.into(), + self.peg_multiplier.into(), ) } @@ -1569,7 +1578,7 @@ impl AMM { .base_asset_amount_with_amm .unsigned_abs() .max(min_order_size_u128) - < self.sqrt_k) + < self.sqrt_k.into()) && (min_order_size_u128 < max_bids.unsigned_abs().max(max_asks.unsigned_abs())); Ok(can_lower) @@ -1755,16 +1764,16 @@ impl AMM { let default_reserves = 100 * AMM_RESERVE_PRECISION; // make sure tests dont have the default sqrt_k = 0 AMM { - base_asset_reserve: default_reserves, - quote_asset_reserve: default_reserves, - sqrt_k: default_reserves, - concentration_coef: MAX_CONCENTRATION_COEFFICIENT, + base_asset_reserve: default_reserves.into(), + quote_asset_reserve: default_reserves.into(), + sqrt_k: default_reserves.into(), + concentration_coef: MAX_CONCENTRATION_COEFFICIENT.into(), order_step_size: 1, order_tick_size: 1, - max_base_asset_reserve: u64::MAX as u128, - min_base_asset_reserve: 0, - terminal_quote_asset_reserve: default_reserves, - peg_multiplier: crate::math::constants::PEG_PRECISION, + max_base_asset_reserve: (u64::MAX as u128).into(), + min_base_asset_reserve: 0.into(), + terminal_quote_asset_reserve: default_reserves.into(), + peg_multiplier: crate::math::constants::PEG_PRECISION.into(), max_fill_reserve_fraction: 1, max_spread: 1000, historical_oracle_data: HistoricalOracleData { @@ -1780,21 +1789,21 @@ impl AMM { use crate::math::constants::PRICE_PRECISION_I64; AMM { - base_asset_reserve: 65 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 63015384615, - terminal_quote_asset_reserve: 64 * AMM_RESERVE_PRECISION, - sqrt_k: 64 * AMM_RESERVE_PRECISION, + base_asset_reserve: (65 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: 63015384615.into(), + terminal_quote_asset_reserve: (64 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (64 * AMM_RESERVE_PRECISION).into(), - peg_multiplier: 19_400_000_000, + peg_multiplier: 19_400_000_000.into(), - concentration_coef: MAX_CONCENTRATION_COEFFICIENT, - max_base_asset_reserve: 90 * AMM_RESERVE_PRECISION, - min_base_asset_reserve: 45 * AMM_RESERVE_PRECISION, + concentration_coef: MAX_CONCENTRATION_COEFFICIENT.into(), + max_base_asset_reserve: (90 * AMM_RESERVE_PRECISION).into(), + min_base_asset_reserve: (45 * AMM_RESERVE_PRECISION).into(), - base_asset_amount_with_amm: -(AMM_RESERVE_PRECISION as i128), + base_asset_amount_with_amm: (AMM_RESERVE_PRECISION as i128 * -1).into(), mark_std: PRICE_PRECISION as u64, - quote_asset_amount: 19_000_000_000, // short 1 BTC @ $19000 + quote_asset_amount: 19_000_000_000.into(), // short 1 BTC @ $19000 historical_oracle_data: HistoricalOracleData { last_oracle_price: 19_400 * PRICE_PRECISION_I64, last_oracle_price_twap: 19_400 * PRICE_PRECISION_I64, diff --git a/programs/drift/src/state/perp_market/tests.rs b/programs/drift/src/state/perp_market/tests.rs index 3f09e5c58c..6a489f491d 100644 --- a/programs/drift/src/state/perp_market/tests.rs +++ b/programs/drift/src/state/perp_market/tests.rs @@ -7,12 +7,12 @@ mod amm { #[test] fn last_ask_premium() { let mut amm = AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), short_spread: (BID_ASK_SPREAD_PRECISION / 10) as u32, long_spread: (BID_ASK_SPREAD_PRECISION / 10) as u32, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), ..AMM::default() }; amm.historical_oracle_data.last_oracle_price = 100 * PRICE_PRECISION_I64; @@ -25,12 +25,12 @@ mod amm { #[test] fn last_bid_discount() { let mut amm = AMM { - base_asset_reserve: 100 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 100 * AMM_RESERVE_PRECISION, + base_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (100 * AMM_RESERVE_PRECISION).into(), short_spread: (BID_ASK_SPREAD_PRECISION / 10) as u32, long_spread: (BID_ASK_SPREAD_PRECISION / 10) as u32, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 100 * PEG_PRECISION, + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: (100 * PEG_PRECISION).into(), ..AMM::default() }; amm.historical_oracle_data.last_oracle_price = 100 * PRICE_PRECISION_I64; @@ -590,13 +590,13 @@ mod amm_can_fill_order_tests { assert!(!can1); // valid oracle for immediate and user can skip, market can skip due to low inventory => can fill - market.amm.base_asset_amount_with_amm = -2; // taker long improves balance + market.amm.set_base_asset_amount_with_amm(-2); // taker long improves balance market.amm.order_step_size = 1; - market.amm.base_asset_reserve = 1_000_000; - market.amm.quote_asset_reserve = 1_000_000; - market.amm.sqrt_k = 1_000_000; - market.amm.max_base_asset_reserve = 2_000_000; - market.amm.min_base_asset_reserve = 0; + market.amm.set_base_asset_reserve(1_000_000); + market.amm.set_quote_asset_reserve(1_000_000); + market.amm.set_sqrt_k(1_000_000); + market.amm.set_max_base_asset_reserve(2_000_000); + market.amm.set_min_base_asset_reserve(0); let can2 = market .amm_can_fill_order( diff --git a/programs/drift/src/state/perp_market_map.rs b/programs/drift/src/state/perp_market_map.rs index 28367fb7ae..b9770cb3f2 100644 --- a/programs/drift/src/state/perp_market_map.rs +++ b/programs/drift/src/state/perp_market_map.rs @@ -25,7 +25,7 @@ pub struct PerpMarketMap<'a>(pub BTreeMap>); impl<'a> PerpMarketMap<'a> { #[track_caller] #[inline(always)] - pub fn get_ref(&self, market_index: &u16) -> DriftResult> { + pub fn get_ref(&self, market_index: &u16) -> DriftResult> { let loader = match self.0.get(market_index) { Some(loader) => loader, None => { @@ -58,7 +58,7 @@ impl<'a> PerpMarketMap<'a> { #[track_caller] #[inline(always)] - pub fn get_ref_mut(&self, market_index: &u16) -> DriftResult> { + pub fn get_ref_mut(&self, market_index: &u16) -> DriftResult> { let loader = match self.0.get(market_index) { Some(loader) => loader, None => { diff --git a/programs/drift/src/state/revenue_share.rs b/programs/drift/src/state/revenue_share.rs index 7997b2fb4e..f63efbf8be 100644 --- a/programs/drift/src/state/revenue_share.rs +++ b/programs/drift/src/state/revenue_share.rs @@ -1,9 +1,10 @@ use std::cell::{Ref, RefMut}; use anchor_lang::prelude::{ - account, zero_copy, AccountInfo, AnchorDeserialize, AnchorSerialize, Discriminator, Pubkey, + account, + borsh::{BorshDeserialize, BorshSerialize}, + zero_copy, AccountInfo, AnchorDeserialize, AnchorSerialize, Discriminator, Pubkey, }; -use borsh::{BorshDeserialize, BorshSerialize}; use crate::error::{DriftResult, ErrorCode}; use crate::math::casting::Cast; @@ -512,12 +513,12 @@ impl<'a> RevenueShareEscrowZeroCopyMut<'a> { } pub trait RevenueShareEscrowLoader<'a> { - fn load_zc(&self) -> DriftResult; - fn load_zc_mut(&self) -> DriftResult; + fn load_zc(&self) -> DriftResult>; + fn load_zc_mut(&self) -> DriftResult>; } impl<'a> RevenueShareEscrowLoader<'a> for AccountInfo<'a> { - fn load_zc(&self) -> DriftResult { + fn load_zc(&self) -> DriftResult> { let owner = self.owner; validate!( @@ -543,7 +544,7 @@ impl<'a> RevenueShareEscrowLoader<'a> for AccountInfo<'a> { }) } - fn load_zc_mut(&self) -> DriftResult { + fn load_zc_mut(&self) -> DriftResult> { let owner = self.owner; validate!( diff --git a/programs/drift/src/state/revenue_share_map.rs b/programs/drift/src/state/revenue_share_map.rs index 912e8a43ea..39347bb048 100644 --- a/programs/drift/src/state/revenue_share_map.rs +++ b/programs/drift/src/state/revenue_share_map.rs @@ -71,7 +71,7 @@ impl<'a> RevenueShareMap<'a> { #[track_caller] #[inline(always)] - pub fn get_user_ref_mut(&self, authority: &Pubkey) -> DriftResult> { + pub fn get_user_ref_mut(&self, authority: &Pubkey) -> DriftResult> { let loader = match self.0.get(authority).and_then(|e| e.user.as_ref()) { Some(loader) => loader, None => { @@ -107,7 +107,7 @@ impl<'a> RevenueShareMap<'a> { pub fn get_revenue_share_account_mut( &self, authority: &Pubkey, - ) -> DriftResult> { + ) -> DriftResult> { let loader = match self.0.get(authority).and_then(|e| e.revenue_share.as_ref()) { Some(loader) => loader, None => { diff --git a/programs/drift/src/state/settle_pnl_mode.rs b/programs/drift/src/state/settle_pnl_mode.rs index f13002d7db..eafa1837b9 100644 --- a/programs/drift/src/state/settle_pnl_mode.rs +++ b/programs/drift/src/state/settle_pnl_mode.rs @@ -1,6 +1,6 @@ use crate::error::{DriftResult, ErrorCode}; use crate::msg; -use borsh::{BorshDeserialize, BorshSerialize}; +use anchor_lang::prelude::{borsh::BorshDeserialize, borsh::BorshSerialize}; use std::panic::Location; #[cfg(test)] diff --git a/programs/drift/src/state/signed_msg_user.rs b/programs/drift/src/state/signed_msg_user.rs index 2ab4bef97b..f8c922faff 100644 --- a/programs/drift/src/state/signed_msg_user.rs +++ b/programs/drift/src/state/signed_msg_user.rs @@ -169,12 +169,12 @@ impl<'a> SignedMsgUserOrdersZeroCopyMut<'a> { } pub trait SignedMsgUserOrdersLoader<'a> { - fn load(&self) -> DriftResult; - fn load_mut(&self) -> DriftResult; + fn load(&self) -> DriftResult>; + fn load_mut(&self) -> DriftResult>; } impl<'a> SignedMsgUserOrdersLoader<'a> for AccountInfo<'a> { - fn load(&self) -> DriftResult { + fn load(&self) -> DriftResult> { let owner = self.owner; validate!( @@ -199,7 +199,7 @@ impl<'a> SignedMsgUserOrdersLoader<'a> for AccountInfo<'a> { }) } - fn load_mut(&self) -> DriftResult { + fn load_mut(&self) -> DriftResult> { let owner = self.owner; validate!( diff --git a/programs/drift/src/state/spot_market.rs b/programs/drift/src/state/spot_market.rs index c42000a7b1..e3d503d29c 100644 --- a/programs/drift/src/state/spot_market.rs +++ b/programs/drift/src/state/spot_market.rs @@ -1,11 +1,6 @@ use std::fmt; use std::fmt::{Display, Formatter}; -use anchor_lang::prelude::*; -use anchor_spl::token::spl_token; -use anchor_spl::token_2022::spl_token_2022; -use borsh::{BorshDeserialize, BorshSerialize}; - use crate::error::{DriftResult, ErrorCode}; use crate::math::casting::Cast; use crate::math::constants::{ @@ -20,6 +15,13 @@ use crate::math::margin::{ }; use crate::math::safe_math::SafeMath; use crate::math::spot_balance::{calculate_utilization, get_token_amount, get_token_value}; +use anchor_lang::prelude::{ + borsh::{BorshDeserialize, BorshSerialize}, + *, +}; +use anchor_spl::token::spl_token; +use anchor_spl::token_2022::spl_token_2022; +use drift_macros::legacy_layout; use crate::math::stats::calculate_new_twap; use crate::state::oracle::{HistoricalIndexData, HistoricalOracleData, OracleSource}; @@ -30,6 +32,7 @@ use crate::validate; use super::oracle_map::OracleIdentifier; +#[legacy_layout] #[account(zero_copy(unsafe))] #[derive(PartialEq, Eq, Debug)] #[repr(C)] @@ -223,13 +226,13 @@ impl Default for SpotMarket { revenue_pool: PoolBalance::default(), spot_fee_pool: PoolBalance::default(), insurance_fund: InsuranceFund::default(), - total_spot_fee: 0, - deposit_balance: 0, - borrow_balance: 0, - cumulative_deposit_interest: 0, - cumulative_borrow_interest: 0, - total_social_loss: 0, - total_quote_social_loss: 0, + total_spot_fee: 0.into(), + deposit_balance: 0.into(), + borrow_balance: 0.into(), + cumulative_deposit_interest: 0.into(), + cumulative_borrow_interest: 0.into(), + total_social_loss: 0.into(), + total_quote_social_loss: 0.into(), withdraw_guard_threshold: 0, max_token_deposits: 0, deposit_token_twap: 0, @@ -449,11 +452,11 @@ impl SpotMarket { } pub fn get_deposits(&self) -> DriftResult { - get_token_amount(self.deposit_balance, self, &SpotBalanceType::Deposit) + get_token_amount(self.deposit_balance.into(), self, &SpotBalanceType::Deposit) } pub fn get_borrows(&self) -> DriftResult { - get_token_amount(self.borrow_balance, self, &SpotBalanceType::Borrow) + get_token_amount(self.borrow_balance.into(), self, &SpotBalanceType::Borrow) } pub fn get_tvl(&self) -> DriftResult { @@ -500,10 +503,10 @@ impl SpotMarket { pub fn get_available_deposits(&self) -> DriftResult { let deposit_token_amount = - get_token_amount(self.deposit_balance, self, &SpotBalanceType::Deposit)?; + get_token_amount(self.deposit_balance.into(), self, &SpotBalanceType::Deposit)?; let borrow_token_amount = - get_token_amount(self.borrow_balance, self, &SpotBalanceType::Borrow)?; + get_token_amount(self.borrow_balance.into(), self, &SpotBalanceType::Borrow)?; deposit_token_amount.safe_sub(borrow_token_amount) } @@ -513,11 +516,14 @@ impl SpotMarket { } pub fn get_utilization(self) -> DriftResult { - let deposit_token_amount = - get_token_amount(self.deposit_balance, &self, &SpotBalanceType::Deposit)?; + let deposit_token_amount = get_token_amount( + self.deposit_balance.into(), + &self, + &SpotBalanceType::Deposit, + )?; let borrow_token_amount = - get_token_amount(self.borrow_balance, &self, &SpotBalanceType::Borrow)?; + get_token_amount(self.borrow_balance.into(), &self, &SpotBalanceType::Borrow)?; calculate_utilization(deposit_token_amount, borrow_token_amount) } @@ -597,8 +603,8 @@ impl SpotMarket { pub fn default_base_market() -> Self { SpotMarket { market_index: 1, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), initial_liability_weight: 12000, maintenance_liability_weight: 11000, initial_asset_weight: 8000, @@ -613,8 +619,8 @@ impl SpotMarket { pub fn default_quote_market() -> Self { SpotMarket { - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, - cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), + cumulative_borrow_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_liability_weight: 10000, maintenance_liability_weight: 10000, @@ -686,6 +692,7 @@ pub enum AssetTier { Unlisted, } +#[legacy_layout] #[zero_copy(unsafe)] #[derive(Default, Eq, PartialEq, Debug)] #[repr(C)] @@ -703,7 +710,9 @@ pub struct InsuranceFund { impl InsuranceFund { pub fn get_protocol_shares(&self) -> DriftResult { - self.total_shares.safe_sub(self.user_shares) + self.total_shares + .as_u128() + .safe_sub(self.user_shares.as_u128()) } } diff --git a/programs/drift/src/state/spot_market_map.rs b/programs/drift/src/state/spot_market_map.rs index c555a939cc..cb34ed06f6 100644 --- a/programs/drift/src/state/spot_market_map.rs +++ b/programs/drift/src/state/spot_market_map.rs @@ -24,7 +24,7 @@ pub struct SpotMarketMap<'a>( impl<'a> SpotMarketMap<'a> { #[track_caller] #[inline(always)] - pub fn get_ref(&self, market_index: &u16) -> DriftResult> { + pub fn get_ref(&self, market_index: &u16) -> DriftResult> { let loader = match self.0.get(market_index) { Some(loader) => loader, None => { @@ -57,7 +57,7 @@ impl<'a> SpotMarketMap<'a> { #[track_caller] #[inline(always)] - pub fn get_ref_mut(&self, market_index: &u16) -> DriftResult> { + pub fn get_ref_mut(&self, market_index: &u16) -> DriftResult> { if !self.1.contains(market_index) { let caller = Location::caller(); msg!( @@ -101,7 +101,7 @@ impl<'a> SpotMarketMap<'a> { #[track_caller] #[inline(always)] - pub fn get_quote_spot_market(&self) -> DriftResult> { + pub fn get_quote_spot_market(&self) -> DriftResult> { let loader = match self.0.get("E_SPOT_MARKET_INDEX) { Some(loader) => loader, None => { @@ -134,7 +134,7 @@ impl<'a> SpotMarketMap<'a> { #[track_caller] #[inline(always)] - pub fn get_quote_spot_market_mut(&self) -> DriftResult> { + pub fn get_quote_spot_market_mut(&self) -> DriftResult> { if !self.1.contains("E_SPOT_MARKET_INDEX) { let caller = Location::caller(); msg!( diff --git a/programs/drift/src/state/state.rs b/programs/drift/src/state/state.rs index 07e9213396..14afc90bd9 100644 --- a/programs/drift/src/state/state.rs +++ b/programs/drift/src/state/state.rs @@ -1,4 +1,5 @@ use anchor_lang::prelude::*; +use drift_macros::legacy_layout; use enumflags2::BitFlags; use crate::error::DriftResult; @@ -13,6 +14,7 @@ use crate::state::traits::Size; #[cfg(test)] mod tests; +#[legacy_layout] #[account] #[derive(Default)] #[repr(C)] @@ -254,6 +256,7 @@ impl Default for FeeTier { } } +#[legacy_layout] #[derive(AnchorSerialize, AnchorDeserialize, Default, Clone, Debug)] pub struct OrderFillerRewardStructure { pub reward_numerator: u32, @@ -329,7 +332,7 @@ impl FeeStructure { filler_reward_structure: OrderFillerRewardStructure { reward_numerator: 10, reward_denominator: FEE_PERCENTAGE_DENOMINATOR, - time_based_reward_lower_bound: 10_000, // 1 cent + time_based_reward_lower_bound: 10_000.into(), // 1 cent }, flat_filler_fee: 10_000, referrer_reward_epoch_upper_bound: MAX_REFERRER_REWARD_EPOCH_UPPER_BOUND, @@ -353,7 +356,7 @@ impl FeeStructure { filler_reward_structure: OrderFillerRewardStructure { reward_numerator: 10, reward_denominator: FEE_PERCENTAGE_DENOMINATOR, - time_based_reward_lower_bound: 10_000, // 1 cent + time_based_reward_lower_bound: 10_000.into(), // 1 cent }, flat_filler_fee: 10_000, referrer_reward_epoch_upper_bound: MAX_REFERRER_REWARD_EPOCH_UPPER_BOUND, @@ -380,7 +383,7 @@ impl FeeStructure { filler_reward_structure: OrderFillerRewardStructure { reward_numerator: 10, reward_denominator: FEE_PERCENTAGE_DENOMINATOR, - time_based_reward_lower_bound: 10_000, // 1 cent + time_based_reward_lower_bound: 10_000.into(), // 1 cent }, ..FeeStructure::perps_default() } diff --git a/programs/drift/src/state/user.rs b/programs/drift/src/state/user.rs index a89467cce3..24aeaf0455 100644 --- a/programs/drift/src/state/user.rs +++ b/programs/drift/src/state/user.rs @@ -28,9 +28,13 @@ use crate::state::spot_market::{SpotBalance, SpotBalanceType, SpotMarket}; use crate::state::traits::Size; use crate::validate; use crate::{get_then_update_id, ID}; -use anchor_lang::prelude::*; -use borsh::{BorshDeserialize, BorshSerialize}; +use anchor_lang::prelude::{ + borsh::{BorshDeserialize, BorshSerialize}, + *, +}; use bytemuck::{Pod, Zeroable}; +use drift_macros::legacy_layout; +use num_traits::Zero; use std::cmp::max; use std::fmt; use std::ops::Neg; @@ -2085,6 +2089,8 @@ impl FuelOverflowStatus { status & FuelOverflowStatus::Exists as u8 != 0 } } + +#[legacy_layout] #[account(zero_copy(unsafe))] #[derive(Default, Debug)] #[repr(C)] @@ -2111,39 +2117,56 @@ impl FuelOverflow { pub fn update_from_user_stats(&mut self, user_stats: &UserStats, now: u32) -> DriftResult<()> { self.fuel_insurance = self .fuel_insurance - .safe_add(user_stats.fuel_insurance.cast()?)?; + .as_u128() + .safe_add(user_stats.fuel_insurance as u128)? + .into(); self.fuel_deposits = self .fuel_deposits - .safe_add(user_stats.fuel_deposits.cast()?)?; + .as_u128() + .safe_add(user_stats.fuel_deposits as u128)? + .into(); self.fuel_borrows = self .fuel_borrows - .safe_add(user_stats.fuel_borrows.cast()?)?; + .as_u128() + .safe_add(user_stats.fuel_borrows as u128)? + .into(); self.fuel_positions = self .fuel_positions - .safe_add(user_stats.fuel_positions.cast()?)?; - self.fuel_taker = self.fuel_taker.safe_add(user_stats.fuel_taker.cast()?)?; - self.fuel_maker = self.fuel_maker.safe_add(user_stats.fuel_maker.cast()?)?; + .as_u128() + .safe_add(user_stats.fuel_positions as u128)? + .into(); + self.fuel_taker = self + .fuel_taker + .as_u128() + .safe_add(user_stats.fuel_taker as u128)? + .into(); + self.fuel_maker = self + .fuel_maker + .as_u128() + .safe_add(user_stats.fuel_maker as u128)? + .into(); self.last_fuel_sweep_ts = now; Ok(()) } pub fn reset_fuel(&mut self, now: u32) { - self.fuel_insurance = 0; - self.fuel_deposits = 0; - self.fuel_borrows = 0; - self.fuel_positions = 0; - self.fuel_taker = 0; - self.fuel_maker = 0; + self.fuel_insurance = Zero::zero(); + self.fuel_deposits = Zero::zero(); + self.fuel_borrows = Zero::zero(); + self.fuel_positions = Zero::zero(); + self.fuel_taker = Zero::zero(); + self.fuel_maker = Zero::zero(); self.last_reset_ts = now; } pub fn total_fuel(&self) -> DriftResult { self.fuel_insurance - .safe_add(self.fuel_deposits)? - .safe_add(self.fuel_borrows)? - .safe_add(self.fuel_positions)? - .safe_add(self.fuel_taker)? - .safe_add(self.fuel_maker) + .as_u128() + .safe_add(self.fuel_deposits.as_u128())? + .safe_add(self.fuel_borrows.as_u128())? + .safe_add(self.fuel_positions.as_u128())? + .safe_add(self.fuel_taker.as_u128())? + .safe_add(self.fuel_maker.as_u128()) } } diff --git a/programs/drift/src/state/user/tests.rs b/programs/drift/src/state/user/tests.rs index 4b2392d1ba..335bbfd24a 100644 --- a/programs/drift/src/state/user/tests.rs +++ b/programs/drift/src/state/user/tests.rs @@ -214,30 +214,30 @@ mod get_claimable_pnl { let usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 1000 * SPOT_BALANCE_PRECISION, + deposit_balance: (1000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, ..SpotMarket::default() }; let perp_market = PerpMarket { amm: AMM { - base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 150_000, - concentration_coef: MAX_CONCENTRATION_COEFFICIENT, - total_fee_minus_distributions: 1000 * QUOTE_PRECISION_I128, + base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 150_000.into(), + concentration_coef: MAX_CONCENTRATION_COEFFICIENT.into(), + total_fee_minus_distributions: (1000 * QUOTE_PRECISION_I128).into(), curve_update_intensity: 100, - base_asset_amount_with_amm: AMM_RESERVE_PRECISION as i128, - quote_asset_amount: -100 * QUOTE_PRECISION_I128, + base_asset_amount_with_amm: (AMM_RESERVE_PRECISION as i128).into(), + quote_asset_amount: (-100 * QUOTE_PRECISION_I128).into(), ..AMM::default() }, pnl_pool: PoolBalance { - scaled_balance: (10 * SPOT_BALANCE_PRECISION), + scaled_balance: (10 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -280,7 +280,7 @@ mod get_claimable_pnl { let oracle_price = 150 * PRICE_PRECISION_I64; let pnl_pool_token_amount = get_token_amount( - perp_market.pnl_pool.scaled_balance, + perp_market.pnl_pool.scaled_balance(), &usdc_market, perp_market.pnl_pool.balance_type(), ) @@ -320,30 +320,30 @@ mod get_claimable_pnl { let usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 1000 * SPOT_BALANCE_PRECISION, + deposit_balance: (1000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, ..SpotMarket::default() }; let mut perp_market = PerpMarket { amm: AMM { - base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 150_000, - concentration_coef: MAX_CONCENTRATION_COEFFICIENT, - total_fee_minus_distributions: 1000 * QUOTE_PRECISION_I128, + base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 150_000.into(), + concentration_coef: MAX_CONCENTRATION_COEFFICIENT.into(), + total_fee_minus_distributions: (1000 * QUOTE_PRECISION_I128).into(), curve_update_intensity: 100, - base_asset_amount_with_amm: AMM_RESERVE_PRECISION as i128, - quote_asset_amount: -99 * QUOTE_PRECISION_I128, + base_asset_amount_with_amm: (AMM_RESERVE_PRECISION as i128).into(), + quote_asset_amount: (-99 * QUOTE_PRECISION_I128).into(), ..AMM::default() }, pnl_pool: PoolBalance { - scaled_balance: (60 * SPOT_BALANCE_PRECISION), + scaled_balance: (60 * SPOT_BALANCE_PRECISION).into(), market_index: QUOTE_SPOT_MARKET_INDEX, ..PoolBalance::default() }, @@ -386,7 +386,7 @@ mod get_claimable_pnl { let oracle_price = 150 * PRICE_PRECISION_I64; let pnl_pool_token_amount = get_token_amount( - perp_market.pnl_pool.scaled_balance, + perp_market.pnl_pool.scaled_balance(), &usdc_market, perp_market.pnl_pool.balance_type(), ) @@ -440,7 +440,9 @@ mod get_claimable_pnl { ); assert_eq!(unsettled_pnl3, 9_000_000); - perp_market.amm.quote_asset_amount = -100 * QUOTE_PRECISION_I128; + perp_market + .amm + .set_quote_asset_amount(-100 * QUOTE_PRECISION_I128); let net_user_pnl = calculate_net_user_pnl(&perp_market.amm, oracle_price).unwrap(); assert_eq!(net_user_pnl, 50000000); let max_pnl_pool_excess = if net_user_pnl < pnl_pool_token_amount { @@ -471,30 +473,30 @@ mod get_claimable_pnl { let usdc_market = SpotMarket { market_index: 0, oracle_source: OracleSource::QuoteAsset, - cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION, + cumulative_deposit_interest: SPOT_CUMULATIVE_INTEREST_PRECISION.into(), decimals: 6, initial_asset_weight: SPOT_WEIGHT_PRECISION, maintenance_asset_weight: SPOT_WEIGHT_PRECISION, - deposit_balance: 1000 * SPOT_BALANCE_PRECISION, + deposit_balance: (1000 * SPOT_BALANCE_PRECISION).into(), liquidator_fee: 0, ..SpotMarket::default() }; let perp_market = PerpMarket { amm: AMM { - base_asset_reserve: 99 * AMM_RESERVE_PRECISION, - quote_asset_reserve: 101 * AMM_RESERVE_PRECISION, - sqrt_k: 100 * AMM_RESERVE_PRECISION, - peg_multiplier: 150_000, - concentration_coef: MAX_CONCENTRATION_COEFFICIENT, - total_fee_minus_distributions: 1000 * QUOTE_PRECISION_I128, + base_asset_reserve: (99 * AMM_RESERVE_PRECISION).into(), + quote_asset_reserve: (101 * AMM_RESERVE_PRECISION).into(), + sqrt_k: (100 * AMM_RESERVE_PRECISION).into(), + peg_multiplier: 150_000.into(), + concentration_coef: MAX_CONCENTRATION_COEFFICIENT.into(), + total_fee_minus_distributions: (1000 * QUOTE_PRECISION_I128).into(), curve_update_intensity: 100, - base_asset_amount_with_amm: AMM_RESERVE_PRECISION as i128, - quote_asset_amount: -100 * QUOTE_PRECISION_I128, + base_asset_amount_with_amm: (AMM_RESERVE_PRECISION as i128).into(), + quote_asset_amount: (-100 * QUOTE_PRECISION_I128).into(), ..AMM::default() }, pnl_pool: PoolBalance { - scaled_balance: (1000 * SPOT_BALANCE_PRECISION), + scaled_balance: (1000 * SPOT_BALANCE_PRECISION).into(), market_index: 0, ..PoolBalance::default() }, @@ -537,7 +539,7 @@ mod get_claimable_pnl { let oracle_price = 160 * PRICE_PRECISION_I64; let pnl_pool_token_amount = get_token_amount( - perp_market.pnl_pool.scaled_balance, + perp_market.pnl_pool.scaled_balance(), &usdc_market, perp_market.pnl_pool.balance_type(), ) diff --git a/programs/drift/src/state/user_map.rs b/programs/drift/src/state/user_map.rs index 8f71c992dc..b5a850955e 100644 --- a/programs/drift/src/state/user_map.rs +++ b/programs/drift/src/state/user_map.rs @@ -21,7 +21,7 @@ pub struct UserMap<'a>(pub BTreeMap>); impl<'a> UserMap<'a> { #[track_caller] #[inline(always)] - pub fn get_ref(&self, user: &Pubkey) -> DriftResult> { + pub fn get_ref(&self, user: &Pubkey) -> DriftResult> { let loader = match self.0.get(user) { Some(loader) => loader, None => { @@ -54,7 +54,7 @@ impl<'a> UserMap<'a> { #[track_caller] #[inline(always)] - pub fn get_ref_mut(&self, user: &Pubkey) -> DriftResult> { + pub fn get_ref_mut(&self, user: &Pubkey) -> DriftResult> { let loader = match self.0.get(user) { Some(loader) => loader, None => { @@ -143,7 +143,7 @@ pub struct UserStatsMap<'a>(pub BTreeMap>); impl<'a> UserStatsMap<'a> { #[track_caller] #[inline(always)] - pub fn get_ref(&self, authority: &Pubkey) -> DriftResult> { + pub fn get_ref(&self, authority: &Pubkey) -> DriftResult> { let loader = match self.0.get(authority) { Some(loader) => loader, None => { @@ -176,7 +176,7 @@ impl<'a> UserStatsMap<'a> { #[track_caller] #[inline(always)] - pub fn get_ref_mut(&self, authority: &Pubkey) -> DriftResult> { + pub fn get_ref_mut(&self, authority: &Pubkey) -> DriftResult> { let loader = match self.0.get(authority) { Some(loader) => loader, None => { diff --git a/programs/drift/src/validation/perp_market.rs b/programs/drift/src/validation/perp_market.rs index 30bce4dedd..7945136ce4 100644 --- a/programs/drift/src/validation/perp_market.rs +++ b/programs/drift/src/validation/perp_market.rs @@ -12,13 +12,13 @@ use crate::validate; pub fn validate_perp_market(market: &PerpMarket) -> DriftResult { let (_, remainder_base_asset_amount_long) = crate::math::orders::standardize_base_asset_amount_with_remainder_i128( - market.amm.base_asset_amount_long, + market.amm.base_asset_amount_long(), market.amm.order_step_size.cast()?, )?; let (_, remainder_base_asset_amount_short) = crate::math::orders::standardize_base_asset_amount_with_remainder_i128( - market.amm.base_asset_amount_short, + market.amm.base_asset_amount_short(), market.amm.order_step_size.cast()?, )?; @@ -31,29 +31,29 @@ pub fn validate_perp_market(market: &PerpMarket) -> DriftResult { market.amm.order_step_size )?; validate!( - (market.amm.base_asset_amount_long + market.amm.base_asset_amount_short) - == market.amm.base_asset_amount_with_amm, + (market.amm.base_asset_amount_long() + market.amm.base_asset_amount_short()) + == market.amm.base_asset_amount_with_amm(), ErrorCode::InvalidAmmDetected, "Market NET_BAA Error: market.amm.base_asset_amount_long={}, + market.amm.base_asset_amount_short={} != market.amm.base_asset_amount_with_amm={}", - market.amm.base_asset_amount_long, - market.amm.base_asset_amount_short, - market.amm.base_asset_amount_with_amm, + market.amm.base_asset_amount_long(), + market.amm.base_asset_amount_short(), + market.amm.base_asset_amount_with_amm(), )?; validate!( - market.amm.base_asset_amount_with_amm <= (MAX_BASE_ASSET_AMOUNT_WITH_AMM as i128), + market.amm.base_asset_amount_with_amm() <= (MAX_BASE_ASSET_AMOUNT_WITH_AMM as i128), ErrorCode::InvalidAmmDetected, "market {} market.amm.base_asset_amount_with_amm={} is too large", market.market_index, - market.amm.base_asset_amount_with_amm + market.amm.base_asset_amount_with_amm() )?; validate!( - market.amm.peg_multiplier > 0, + market.amm.peg_multiplier() > 0, ErrorCode::InvalidAmmDetected, "market {} peg_multiplier out of wack", market.market_index, @@ -61,35 +61,35 @@ pub fn validate_perp_market(market: &PerpMarket) -> DriftResult { if market.status != MarketStatus::ReduceOnly { validate!( - market.amm.sqrt_k > market.amm.base_asset_amount_with_amm.unsigned_abs(), + market.amm.sqrt_k() > market.amm.base_asset_amount_with_amm().unsigned_abs(), ErrorCode::InvalidAmmDetected, "market {} k out of wack: k={}, net_baa={}", market.market_index, - market.amm.sqrt_k, - market.amm.base_asset_amount_with_amm + market.amm.sqrt_k(), + market.amm.base_asset_amount_with_amm() )?; } validate!( - market.amm.sqrt_k >= market.amm.base_asset_reserve - || market.amm.sqrt_k >= market.amm.quote_asset_reserve, + market.amm.sqrt_k() >= market.amm.base_asset_reserve() + || market.amm.sqrt_k() >= market.amm.quote_asset_reserve(), ErrorCode::InvalidAmmDetected, "market {} k out of wack: k={}, bar={}, qar={}", market.market_index, - market.amm.sqrt_k, - market.amm.base_asset_reserve, - market.amm.quote_asset_reserve + market.amm.sqrt_k(), + market.amm.base_asset_reserve(), + market.amm.quote_asset_reserve() )?; - let invariant_sqrt_u192 = crate::math::bn::U192::from(market.amm.sqrt_k); + let invariant_sqrt_u192 = crate::math::bn::U192::from(market.amm.sqrt_k()); let invariant = invariant_sqrt_u192.safe_mul(invariant_sqrt_u192)?; let quote_asset_reserve = invariant - .safe_div(crate::math::bn::U192::from(market.amm.base_asset_reserve))? + .safe_div(crate::math::bn::U192::from(market.amm.base_asset_reserve()))? .try_to_u128()?; let rounding_diff = quote_asset_reserve .cast::()? - .safe_sub(market.amm.quote_asset_reserve.cast()?)? + .safe_sub(market.amm.quote_asset_reserve().cast()?)? .abs(); validate!( @@ -98,8 +98,8 @@ pub fn validate_perp_market(market: &PerpMarket) -> DriftResult { "market {} amm qar/bar/k invalid: k={}, bar={}, qar={}, qar'={} (rounding: {})", market.market_index, invariant, - market.amm.base_asset_reserve, - market.amm.quote_asset_reserve, + market.amm.base_asset_reserve(), + market.amm.quote_asset_reserve(), quote_asset_reserve, rounding_diff )?; @@ -109,30 +109,30 @@ pub fn validate_perp_market(market: &PerpMarket) -> DriftResult { // bid quote/base < reserve q/b if market.amm.reference_price_offset <= 0 { validate!( - market.amm.bid_base_asset_reserve >= market.amm.base_asset_reserve - && market.amm.bid_quote_asset_reserve <= market.amm.quote_asset_reserve, + market.amm.bid_base_asset_reserve() >= market.amm.base_asset_reserve() + && market.amm.bid_quote_asset_reserve() <= market.amm.quote_asset_reserve(), ErrorCode::InvalidAmmDetected, "market {} amm bid reserves invalid: {} -> {}, quote: {} -> {}", market.market_index, - market.amm.bid_base_asset_reserve, - market.amm.base_asset_reserve, - market.amm.bid_quote_asset_reserve, - market.amm.quote_asset_reserve + market.amm.bid_base_asset_reserve(), + market.amm.base_asset_reserve(), + market.amm.bid_quote_asset_reserve(), + market.amm.quote_asset_reserve() )?; } if market.amm.reference_price_offset >= 0 { // ask quote/base > reserve q/b validate!( - market.amm.ask_base_asset_reserve <= market.amm.base_asset_reserve - && market.amm.ask_quote_asset_reserve >= market.amm.quote_asset_reserve, + market.amm.ask_base_asset_reserve() <= market.amm.base_asset_reserve() + && market.amm.ask_quote_asset_reserve() >= market.amm.quote_asset_reserve(), ErrorCode::InvalidAmmDetected, "market {} amm ask reserves invalid: {} -> {}, quote: {} -> {}", market.market_index, - market.amm.ask_base_asset_reserve, - market.amm.base_asset_reserve, - market.amm.ask_quote_asset_reserve, - market.amm.quote_asset_reserve + market.amm.ask_base_asset_reserve(), + market.amm.base_asset_reserve(), + market.amm.ask_quote_asset_reserve(), + market.amm.quote_asset_reserve() )?; } } @@ -164,32 +164,32 @@ pub fn validate_perp_market(market: &PerpMarket) -> DriftResult { market.amm.max_spread, )?; - if market.amm.base_asset_amount_with_amm > 0 { + if market.amm.base_asset_amount_with_amm() > 0 { // users are long = removed base and added quote = qar increased // bid quote/base < reserve q/b validate!( - market.amm.terminal_quote_asset_reserve <= market.amm.quote_asset_reserve, + market.amm.terminal_quote_asset_reserve() <= market.amm.quote_asset_reserve(), ErrorCode::InvalidAmmDetected, "market {} terminal_quote_asset_reserve out of wack", market.market_index, )?; - } else if market.amm.base_asset_amount_with_amm < 0 { + } else if market.amm.base_asset_amount_with_amm() < 0 { validate!( - market.amm.terminal_quote_asset_reserve >= market.amm.quote_asset_reserve, + market.amm.terminal_quote_asset_reserve() >= market.amm.quote_asset_reserve(), ErrorCode::InvalidAmmDetected, "market {} terminal_quote_asset_reserve out of wack (terminal <) {} > {}", market.market_index, - market.amm.terminal_quote_asset_reserve, - market.amm.quote_asset_reserve + market.amm.terminal_quote_asset_reserve(), + market.amm.quote_asset_reserve() )?; } else { validate!( - market.amm.terminal_quote_asset_reserve == market.amm.quote_asset_reserve, + market.amm.terminal_quote_asset_reserve() == market.amm.quote_asset_reserve(), ErrorCode::InvalidAmmDetected, "market {} terminal_quote_asset_reserve out of wack {}!={}", market.market_index, - market.amm.terminal_quote_asset_reserve, - market.amm.quote_asset_reserve + market.amm.terminal_quote_asset_reserve(), + market.amm.quote_asset_reserve() )?; } @@ -224,17 +224,17 @@ pub fn validate_perp_market(market: &PerpMarket) -> DriftResult { pub fn validate_amm_account_for_fill(amm: &AMM, direction: PositionDirection) -> DriftResult { if direction == PositionDirection::Long { validate!( - amm.base_asset_reserve >= amm.min_base_asset_reserve, + amm.base_asset_reserve() >= amm.min_base_asset_reserve(), ErrorCode::InvalidAmmForFillDetected, "Market baa below min_base_asset_reserve: {} < {}", - amm.base_asset_reserve, - amm.min_base_asset_reserve, + amm.base_asset_reserve(), + amm.min_base_asset_reserve(), )?; } if direction == PositionDirection::Short { validate!( - amm.base_asset_reserve <= amm.max_base_asset_reserve, + amm.base_asset_reserve() <= amm.max_base_asset_reserve(), ErrorCode::InvalidAmmForFillDetected, "Market baa above max_base_asset_reserve" )?; diff --git a/programs/drift/src/validation/sig_verification.rs b/programs/drift/src/validation/sig_verification.rs index c2b6d79be9..6ce2d6d4ca 100644 --- a/programs/drift/src/validation/sig_verification.rs +++ b/programs/drift/src/validation/sig_verification.rs @@ -3,7 +3,7 @@ use crate::state::order_params::{ OrderParams, SignedMsgOrderParamsDelegateMessage, SignedMsgOrderParamsMessage, SignedMsgTriggerOrderParams, }; -use anchor_lang::prelude::*; +use anchor_lang::prelude::{borsh::BorshDeserialize, *}; use bytemuck::try_cast_slice; use bytemuck::{Pod, Zeroable}; use byteorder::ByteOrder; diff --git a/programs/openbook_v2/src/lib.rs b/programs/openbook_v2/src/lib.rs index d6ea487c61..f16103f8f5 100644 --- a/programs/openbook_v2/src/lib.rs +++ b/programs/openbook_v2/src/lib.rs @@ -1,4 +1,5 @@ #![allow(clippy::too_many_arguments)] +#![allow(unexpected_cfgs)] use anchor_lang::prelude::{ borsh::{BorshDeserialize, BorshSerialize}, diff --git a/programs/pyth/src/lib.rs b/programs/pyth/src/lib.rs index 199249ce04..bf76a45ae4 100644 --- a/programs/pyth/src/lib.rs +++ b/programs/pyth/src/lib.rs @@ -1,3 +1,4 @@ +#![allow(unexpected_cfgs)] use anchor_lang::prelude::*; pub mod pc; use pc::Price; diff --git a/programs/switchboard-on-demand/src/lib.rs b/programs/switchboard-on-demand/src/lib.rs index a251ad5e27..f651bf670b 100644 --- a/programs/switchboard-on-demand/src/lib.rs +++ b/programs/switchboard-on-demand/src/lib.rs @@ -1,3 +1,5 @@ +#![allow(unexpected_cfgs)] + use anchor_lang::declare_id; use anchor_lang::prelude::*; use anchor_lang::program; @@ -25,28 +27,28 @@ pub struct CompactResult { #[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)] pub struct CurrentResult { /// The median value of the submissions needed for quorum size - pub value: i128, + value: [u8; 16], /// The standard deviation of the submissions needed for quorum size - pub std_dev: i128, + std_dev: [u8; 16], /// The mean of the submissions needed for quorum size - pub mean: i128, + mean: [u8; 16], /// The range of the submissions needed for quorum size - pub range: i128, + range: [u8; 16], /// The minimum value of the submissions needed for quorum size - pub min_value: i128, + min_value: [u8; 16], /// The maximum value of the submissions needed for quorum size - pub max_value: i128, + max_value: [u8; 16], /// The number of samples used to calculate this result - pub num_samples: u8, + num_samples: u8, /// The index of the submission that was used to calculate this result - pub submission_idx: u8, - pub padding1: [u8; 6], + submission_idx: u8, + padding1: [u8; 6], /// The slot at which this value was signed. - pub slot: u64, + slot: u64, /// The slot at which the first considered submission was made - pub min_slot: u64, + min_slot: u64, /// The slot at which the last considered submission was made - pub max_slot: u64, + max_slot: u64, } impl CurrentResult { /// The median value of the submissions needed for quorum size @@ -54,7 +56,7 @@ impl CurrentResult { if self.slot == 0 { return None; } - Some(self.value) + Some(i128::from_le_bytes(self.value)) } /// The standard deviation of the submissions needed for quorum size @@ -62,7 +64,7 @@ impl CurrentResult { if self.slot == 0 { return None; } - Some(self.std_dev) + Some(i128::from_le_bytes(self.std_dev)) } /// The mean of the submissions needed for quorum size @@ -70,7 +72,7 @@ impl CurrentResult { if self.slot == 0 { return None; } - Some(self.mean) + Some(i128::from_le_bytes(self.mean)) } /// The range of the submissions needed for quorum size @@ -78,7 +80,7 @@ impl CurrentResult { if self.slot == 0 { return None; } - Some(self.range) + Some(i128::from_le_bytes(self.range)) } /// The minimum value of the submissions needed for quorum size @@ -86,7 +88,7 @@ impl CurrentResult { if self.slot == 0 { return None; } - Some(self.min_value) + Some(i128::from_le_bytes(self.min_value)) } /// The maximum value of the submissions needed for quorum size @@ -94,7 +96,7 @@ impl CurrentResult { if self.slot == 0 { return None; } - Some(self.max_value) + Some(i128::from_le_bytes(self.max_value)) } pub fn result_slot(&self) -> Option { @@ -129,7 +131,7 @@ pub struct OracleSubmission { /// The slot at which this value was landed on chain. pub landed_at: u64, /// The value that was submitted. - pub value: i128, + value: [u8; 16], } impl OracleSubmission { @@ -138,7 +140,7 @@ impl OracleSubmission { } pub fn value(&self) -> i128 { - self.value + i128::from_le_bytes(self.value) } } diff --git a/programs/switchboard/src/lib.rs b/programs/switchboard/src/lib.rs index f95c5969fe..7fc8682659 100644 --- a/programs/switchboard/src/lib.rs +++ b/programs/switchboard/src/lib.rs @@ -1,3 +1,5 @@ +#![allow(unexpected_cfgs)] + use anchor_lang::prelude::*; declare_id!("SW1TCH7qEPTdLsDHRgPuMQjbQxKdH2aBStViMFnt64f"); diff --git a/programs/token_faucet/src/lib.rs b/programs/token_faucet/src/lib.rs index bd873ae03a..e3ade3135f 100644 --- a/programs/token_faucet/src/lib.rs +++ b/programs/token_faucet/src/lib.rs @@ -1,3 +1,5 @@ +#![allow(unexpected_cfgs)] + use anchor_lang::prelude::*; use anchor_spl::token::{self, Mint, Token, TokenAccount};