-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
57 additions
and
147 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
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
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
3 changes: 1 addition & 2 deletions
3
contracts/liquidity_hub/pool-network/terraswap_router/src/testing/tests.rs
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,42 @@ | ||
use cosmwasm_std::{Decimal, Decimal256, Fraction, StdError, StdResult, Uint128}; | ||
use std::str::FromStr; | ||
|
||
/// Default swap slippage in case max_spread is not specified | ||
pub const DEFAULT_SLIPPAGE: &str = "0.01"; | ||
/// Cap on the maximum swap slippage that is allowed. If max_spread goes over this limit, it will | ||
/// be capped to this value. | ||
pub const MAX_ALLOWED_SLIPPAGE: &str = "0.5"; | ||
|
||
/// If `belief_price` and `max_spread` both are given, | ||
/// we compute new spread else we just use pool network | ||
/// spread to check `max_spread` | ||
pub fn assert_max_spread( | ||
belief_price: Option<Decimal>, | ||
max_spread: Option<Decimal>, | ||
offer_amount: Uint128, | ||
return_amount: Uint128, | ||
spread_amount: Uint128, | ||
) -> StdResult<()> { | ||
let max_spread: Decimal256 = max_spread | ||
.unwrap_or(Decimal::from_str(DEFAULT_SLIPPAGE)?) | ||
.min(Decimal::from_str(MAX_ALLOWED_SLIPPAGE)?) | ||
.into(); | ||
|
||
if let Some(belief_price) = belief_price { | ||
let expected_return = offer_amount | ||
* belief_price | ||
.inv() | ||
.ok_or_else(|| StdError::generic_err("Belief price can't be zero"))?; | ||
let spread_amount = expected_return.saturating_sub(return_amount); | ||
|
||
if return_amount < expected_return | ||
&& Decimal256::from_ratio(spread_amount, expected_return) > max_spread | ||
{ | ||
return Err(StdError::generic_err("Spread limit exceeded")); | ||
} | ||
} else if Decimal256::from_ratio(spread_amount, return_amount + spread_amount) > max_spread { | ||
return Err(StdError::generic_err("Spread limit exceeded")); | ||
} | ||
|
||
Ok(()) | ||
} |