-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
program: safety improvements for swaps (#528)
* program: pick margin type for end swap check based on difference in free collateral contribution * use amount_out_after_fee for reduce only check * tweak use of price * dont let cpis to drift happen in inner ix * explicitly don't let in and out spot market be the same * tweak msg for allowed program * validate remaining accounts * CHANGELOG
- Loading branch information
1 parent
5bce22b
commit c6e7839
Showing
5 changed files
with
255 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#[cfg(test)] | ||
mod test { | ||
use crate::math::constants::PRICE_PRECISION_I64; | ||
use crate::math::margin::MarginRequirementType; | ||
|
||
use crate::math::spot_swap::select_margin_type_for_swap; | ||
use crate::state::spot_market::SpotMarket; | ||
|
||
#[test] | ||
pub fn sell_usdc_buy_sol_decrease_health() { | ||
let usdc_spot_market = SpotMarket::default_quote_market(); | ||
|
||
let sol_spot_market = SpotMarket::default_base_market(); | ||
|
||
let usdc_price = PRICE_PRECISION_I64; | ||
let sol_price = 100 * PRICE_PRECISION_I64; | ||
|
||
let usdc_before = 100 * 10_i128.pow(usdc_spot_market.decimals); | ||
let sol_before = 0_i128; | ||
|
||
let usdc_after = -100 * 10_i128.pow(usdc_spot_market.decimals); | ||
let sol_after = 2 * 10_i128.pow(sol_spot_market.decimals); | ||
|
||
let margin_type = select_margin_type_for_swap( | ||
&usdc_spot_market, | ||
&sol_spot_market, | ||
usdc_price, | ||
sol_price, | ||
usdc_before, | ||
sol_before, | ||
usdc_after, | ||
sol_after, | ||
) | ||
.unwrap(); | ||
|
||
assert_eq!(margin_type, MarginRequirementType::Initial); | ||
} | ||
|
||
#[test] | ||
pub fn sell_usdc_buy_sol_increase_health() { | ||
let usdc_spot_market = SpotMarket::default_quote_market(); | ||
|
||
let sol_spot_market = SpotMarket::default_base_market(); | ||
|
||
let usdc_price = PRICE_PRECISION_I64; | ||
let sol_price = 100 * PRICE_PRECISION_I64; | ||
|
||
// close sol borrow by selling usdc | ||
let usdc_before = 200 * 10_i128.pow(usdc_spot_market.decimals); | ||
let sol_before = -(10_i128.pow(sol_spot_market.decimals)); | ||
|
||
let usdc_after = 100 * 10_i128.pow(usdc_spot_market.decimals); | ||
let sol_after = 0_i128; | ||
|
||
let margin_type = select_margin_type_for_swap( | ||
&usdc_spot_market, | ||
&sol_spot_market, | ||
usdc_price, | ||
sol_price, | ||
usdc_before, | ||
sol_before, | ||
usdc_after, | ||
sol_after, | ||
) | ||
.unwrap(); | ||
|
||
assert_eq!(margin_type, MarginRequirementType::Maintenance); | ||
} | ||
|
||
#[test] | ||
pub fn buy_usdc_sell_sol_decrease_health() { | ||
let usdc_spot_market = SpotMarket::default_quote_market(); | ||
|
||
let sol_spot_market = SpotMarket::default_base_market(); | ||
|
||
let usdc_price = PRICE_PRECISION_I64; | ||
let sol_price = 100 * PRICE_PRECISION_I64; | ||
|
||
let usdc_before = 0_i128; | ||
let sol_before = 10_i128.pow(sol_spot_market.decimals); | ||
|
||
let usdc_after = 200 * 10_i128.pow(usdc_spot_market.decimals); | ||
let sol_after = -(10_i128.pow(sol_spot_market.decimals)); | ||
|
||
let margin_type = select_margin_type_for_swap( | ||
&usdc_spot_market, | ||
&sol_spot_market, | ||
usdc_price, | ||
sol_price, | ||
usdc_before, | ||
sol_before, | ||
usdc_after, | ||
sol_after, | ||
) | ||
.unwrap(); | ||
|
||
assert_eq!(margin_type, MarginRequirementType::Initial); | ||
} | ||
|
||
#[test] | ||
pub fn buy_usdc_sell_sol_increase_health() { | ||
let usdc_spot_market = SpotMarket::default_quote_market(); | ||
|
||
let sol_spot_market = SpotMarket::default_base_market(); | ||
|
||
let usdc_price = PRICE_PRECISION_I64; | ||
let sol_price = 100 * PRICE_PRECISION_I64; | ||
|
||
let usdc_before = -100 * 10_i128.pow(usdc_spot_market.decimals); | ||
let sol_before = 2 * 10_i128.pow(sol_spot_market.decimals); | ||
|
||
let usdc_after = 0_i128; | ||
let sol_after = 10_i128.pow(sol_spot_market.decimals); | ||
|
||
let margin_type = select_margin_type_for_swap( | ||
&usdc_spot_market, | ||
&sol_spot_market, | ||
usdc_price, | ||
sol_price, | ||
usdc_before, | ||
sol_before, | ||
usdc_after, | ||
sol_after, | ||
) | ||
.unwrap(); | ||
|
||
assert_eq!(margin_type, MarginRequirementType::Maintenance); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters