Skip to content

Commit

Permalink
Merge branch 'master' into fix/stablewap-tradable-state
Browse files Browse the repository at this point in the history
  • Loading branch information
enthusiastmartin authored Jul 7, 2023
2 parents d97bf23 + be86148 commit ab6dbbd
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion math/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ license = 'Apache-2.0'
name = "hydra-dx-math"
description = "A collection of utilities to make performing liquidity pool calculations more convenient."
repository = 'https://github.com/galacticcouncil/hydradx-math'
version = "7.4.0"
version = "7.4.1"

[dependencies]
primitive-types = {default-features = false, version = '0.12.0'}
Expand Down
16 changes: 14 additions & 2 deletions math/src/stableswap/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub fn calculate_withdraw_one_asset<const N: u8, const N_Y: u8>(
reserve.checked_mul(d1)?.checked_div(d_hp)?.checked_sub(y_hp)?
} else {
// dx_expected = xp[j] - xp[j] * d1 / d0
reserve.checked_sub(xp_hp[idx].checked_mul(d1)?.checked_div(d_hp)?)?
reserve.checked_sub(reserve.checked_mul(d1)?.checked_div(d_hp)?)?
};

let expected = Balance::try_from(dx_expected).ok()?;
Expand Down Expand Up @@ -251,8 +251,14 @@ pub(crate) fn calculate_y_given_out<const N: u8, const N_Y: u8>(
pub fn calculate_d<const N: u8>(xp: &[Balance], amplification: Balance) -> Option<Balance> {
let two_u256 = to_u256!(2_u128);

//let mut xp_hp: [U256; 2] = [to_u256!(xp[0]), to_u256!(xp[1])];
// Filter out zero balance assets, and return error if there is one.
// Either all assets are zero balance, or none are zero balance.
// Otherwise, it breaks the math.
let mut xp_hp: Vec<U256> = xp.iter().filter(|v| !(*v).is_zero()).map(|v| to_u256!(*v)).collect();
if xp_hp.len() != xp.len() && !xp_hp.is_empty() {
return None;
}

xp_hp.sort();

let ann = calculate_ann(xp_hp.len(), amplification)?;
Expand Down Expand Up @@ -307,7 +313,13 @@ pub fn calculate_d<const N: u8>(xp: &[Balance], amplification: Balance) -> Optio
}

pub(crate) fn calculate_y<const N: u8>(xp: &[Balance], d: Balance, amplification: Balance) -> Option<Balance> {
// Filter out zero balance assets, and return error if there is one.
// Either all assets are zero balance, or none are zero balance.
// Otherwise, it breaks the math.
let mut xp_hp: Vec<U256> = xp.iter().filter(|v| !(*v).is_zero()).map(|v| to_u256!(*v)).collect();
if xp_hp.len() != xp.len() && !xp_hp.is_empty() {
return None;
}
xp_hp.sort();

let ann = calculate_ann(xp_hp.len().checked_add(1)?, amplification)?;
Expand Down
21 changes: 0 additions & 21 deletions math/src/stableswap/tests/invariants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,24 +145,3 @@ proptest! {
assert!(y >= reserve_a);
}
}

proptest! {
#![proptest_config(ProptestConfig::with_cases(1000))]
#[test]
fn round_trip_d_y_4(reserve_a in asset_reserve(),
reserve_b in asset_reserve(),
reserve_c in asset_reserve(),
reserve_e in asset_reserve(),
amp in amplification(),
) {
let ann = amp * 256u128; // 4^4

let reserve_d = 0u128;

let d = calculate_d::<D_ITERATIONS>(&[reserve_a, reserve_b, reserve_c, reserve_d, reserve_e], ann).unwrap();
let y = calculate_y::<Y_ITERATIONS>(&[reserve_b, reserve_c, reserve_d, reserve_e], d, ann).unwrap();

assert!(y - 4 <= reserve_a);
assert!(y >= reserve_a);
}
}
10 changes: 8 additions & 2 deletions math/src/stableswap/tests/two_assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ fn test_d_with_zero_reserves() {
assert_eq!(calculate_d::<D_ITERATIONS>(&reserves, 1), Some(0u128));
}

#[test]
fn test_d_with_one_zero_reserves() {
let reserves = [1000u128, 0u128];
assert_eq!(calculate_d::<D_ITERATIONS>(&reserves, 1), None);
}

#[test]
fn test_y_given_in() {
let reserves = [1000u128, 2000u128];
Expand Down Expand Up @@ -94,12 +100,12 @@ fn test_shares() {
let amp = 100u128;

let initial_reserves = &[0u128, 0u128];
let updated_reserves = &[1000 * ONE, 0u128];
let updated_reserves = &[1000 * ONE, 500u128];

let result = calculate_shares::<D_ITERATIONS>(initial_reserves, updated_reserves, amp, 0u128);

assert!(result.is_some());
assert_eq!(result.unwrap(), 1_000_000_000_000_000u128);
assert_eq!(result.unwrap(), 928031226918u128);
}
#[test]
fn remove_one_asset_should_work() {
Expand Down

0 comments on commit ab6dbbd

Please sign in to comment.