-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
program: pick margin type for end swap check based on difference in f… #528
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
21720f4
program: pick margin type for end swap check based on difference in f…
crispheaney 39aa7dd
use amount_out_after_fee for reduce only check
crispheaney 290ca83
tweak use of price
crispheaney 5484709
dont let cpis to drift happen in inner ix
crispheaney 1885650
explicitly don't let in and out spot market be the same
crispheaney 67830b3
tweak msg for allowed program
crispheaney 65cfab6
validate remaining accounts
crispheaney 9cba544
CHANGELOG
crispheaney 3c2c9fc
Merge branch 'master' into crispheaney/swap-improvements
crispheaney File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this keeps happening (changes from other prs) cause anchor test suite isnt ran locally in branch before merge?