Skip to content
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

chore: use latest math package #99

Merged
merged 3 commits into from Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 33 additions & 2 deletions 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 pallets/lbp/Cargo.toml
Expand Up @@ -25,7 +25,7 @@ version = '2.0.0'
primitive-types = {default-features = false, version = '0.8.0'}
serde = {features = ['derive'], optional = true, version = '1.0.101'}

hydra-dx-math = {default-features = false, version = "3.1.0"}
hydra-dx-math = {default-features = false, version = "3.3.0"}

## Local dependencies
primitives = { default-features = false, path = "../../primitives" }
Expand Down
8 changes: 4 additions & 4 deletions pallets/lbp/src/benchmarking.rs
Expand Up @@ -234,8 +234,8 @@ benchmarks! {
}: _(RawOrigin::Signed(caller.clone()), asset_in, asset_out, amount, max_limit)
verify{
assert_eq!(T::MultiCurrency::free_balance(asset_in, &caller), 999998900000000);
assert_eq!(T::MultiCurrency::free_balance(asset_out, &caller), 999998095629369);
assert_eq!(T::MultiCurrency::free_balance(asset_out, &fee_receiver), 1000000000191642);
assert_eq!(T::MultiCurrency::free_balance(asset_out, &caller), 999998092888673);
assert_eq!(T::MultiCurrency::free_balance(asset_out, &fee_receiver), 1000000000186149);
}

buy {
Expand Down Expand Up @@ -273,8 +273,8 @@ benchmarks! {
}: _(RawOrigin::Signed(caller.clone()), asset_out, asset_in, amount, max_limit)
verify{
assert_eq!(T::MultiCurrency::free_balance(asset_out, &caller), 999999100000000);
assert_eq!(T::MultiCurrency::free_balance(asset_in, &caller), 999997888212763);
assert_eq!(T::MultiCurrency::free_balance(asset_in, &fee_receiver), 1000000000223128);
assert_eq!(T::MultiCurrency::free_balance(asset_in, &caller), 999997891598523);
assert_eq!(T::MultiCurrency::free_balance(asset_in, &fee_receiver), 1000000000216370);
}
}

Expand Down
12 changes: 6 additions & 6 deletions pallets/lbp/src/tests.rs
Expand Up @@ -2314,14 +2314,14 @@ fn buy_should_work() {
pool
);

assert_eq!(Currency::free_balance(asset_in, &who), 999_999_986_328_038);
assert_eq!(Currency::free_balance(asset_in, &who), 999_999_985_546_560);
assert_eq!(Currency::free_balance(asset_out, &who), 1_000_000_010_000_000);

assert_eq!(Currency::free_balance(asset_in, &pool_id), 1_013_644_673);
assert_eq!(Currency::free_balance(asset_in, &pool_id), 1_014_424_591);
assert_eq!(Currency::free_balance(asset_out, &pool_id), 1_990_000_000);

expect_events(vec![Event::BuyExecuted(
who, asset_out, asset_in, 13_644_673, 10_000_000, asset_in, 27_289,
who, asset_out, asset_in, 14_424_591, 10_000_000, asset_in, 28_849,
)
.into()]);
});
Expand Down Expand Up @@ -2373,13 +2373,13 @@ fn sell_should_work() {
);

assert_eq!(Currency::free_balance(asset_in, &who), INITIAL_BALANCE - 10_000_000);
assert_eq!(Currency::free_balance(asset_out, &who), 1_000_000_007_332_174);
assert_eq!(Currency::free_balance(asset_out, &who), 1_000_000_006_939_210);

assert_eq!(Currency::free_balance(asset_in, &pool_id), 1_010_000_000);
assert_eq!(Currency::free_balance(asset_out, &pool_id), 1_992_653_133);
assert_eq!(Currency::free_balance(asset_out, &pool_id), 1_993_046_884);

expect_events(vec![Event::SellExecuted(
who, asset_in, asset_out, 10_000_000, 7_332_174, asset_out, 14_693,
who, asset_in, asset_out, 10_000_000, 6_939_210, asset_out, 13_906,
)
.into()]);
});
Expand Down
2 changes: 1 addition & 1 deletion pallets/xyk/Cargo.toml
Expand Up @@ -25,7 +25,7 @@ version = '2.0.0'
primitive-types = {default-features = false, version = '0.8.0'}
serde = {features = ['derive'], optional = true, version = '1.0.101'}

hydra-dx-math = {default-features = false, version = "3.1.0"}
hydra-dx-math = {default-features = false, version = "3.3.0"}

# Local dependencies
pallet-asset-registry = {path = '../asset-registry', default-features = false}
Expand Down
17 changes: 9 additions & 8 deletions pallets/xyk/src/lib.rs
Expand Up @@ -322,8 +322,9 @@ pub mod pallet {
let asset_b_reserve = T::Currency::free_balance(asset_b, &pair_account);
let total_liquidity = Self::total_liquidity(&pair_account);

let amount_b_required = hydra_dx_math::calculate_liquidity_in(asset_a_reserve, asset_b_reserve, amount_a)
.map_err(|_| Error::<T>::AddAssetAmountInvalid)?;
let amount_b_required =
hydra_dx_math::xyk::calculate_liquidity_in(asset_a_reserve, asset_b_reserve, amount_a)
.map_err(|_| Error::<T>::AddAssetAmountInvalid)?;

let shares_added = if asset_a < asset_b { amount_a } else { amount_b_required };

Expand Down Expand Up @@ -412,7 +413,7 @@ pub mod pallet {
let asset_a_reserve = T::Currency::free_balance(asset_a, &pair_account);
let asset_b_reserve = T::Currency::free_balance(asset_b, &pair_account);

let liquidity_out = hydra_dx_math::calculate_liquidity_out(
let liquidity_out = hydra_dx_math::xyk::calculate_liquidity_out(
asset_a_reserve,
asset_b_reserve,
liquidity_amount,
Expand Down Expand Up @@ -584,7 +585,7 @@ impl<T: Config> AMM<T::AccountId, AssetId, AssetPair, Balance> for Pallet<T> {
let asset_a_reserve = T::Currency::free_balance(asset_a, &pair_account);
let asset_b_reserve = T::Currency::free_balance(asset_b, &pair_account);

hydra_dx_math::calculate_spot_price(asset_a_reserve, asset_b_reserve, amount)
hydra_dx_math::xyk::calculate_spot_price(asset_a_reserve, asset_b_reserve, amount)
.unwrap_or_else(|_| Balance::zero())
}

Expand Down Expand Up @@ -629,7 +630,7 @@ impl<T: Config> AMM<T::AccountId, AssetId, AssetPair, Balance> for Pallet<T> {
Error::<T>::MaxInRatioExceeded
);

let amount_out = hydra_dx_math::calculate_out_given_in(asset_in_reserve, asset_out_reserve, amount)
let amount_out = hydra_dx_math::xyk::calculate_out_given_in(asset_in_reserve, asset_out_reserve, amount)
.map_err(|_| Error::<T>::SellAssetAmountInvalid)?;

let transfer_fee = if discount {
Expand Down Expand Up @@ -661,7 +662,7 @@ impl<T: Config> AMM<T::AccountId, AssetId, AssetPair, Balance> for Pallet<T> {
let asset_reserve = T::Currency::free_balance(assets.asset_in, &native_pair_account);

let native_fee_spot_price =
hydra_dx_math::calculate_spot_price(asset_reserve, native_reserve, transfer_fee)
hydra_dx_math::xyk::calculate_spot_price(asset_reserve, native_reserve, transfer_fee)
.map_err(|_| Error::<T>::CannotApplyDiscount)?;

ensure!(
Expand Down Expand Up @@ -763,7 +764,7 @@ impl<T: Config> AMM<T::AccountId, AssetId, AssetPair, Balance> for Pallet<T> {
);
}

let buy_price = hydra_dx_math::calculate_in_given_out(asset_out_reserve, asset_in_reserve, amount)
let buy_price = hydra_dx_math::xyk::calculate_in_given_out(asset_out_reserve, asset_in_reserve, amount)
.map_err(|_| Error::<T>::BuyAssetAmountInvalid)?;

let transfer_fee = if discount {
Expand Down Expand Up @@ -795,7 +796,7 @@ impl<T: Config> AMM<T::AccountId, AssetId, AssetPair, Balance> for Pallet<T> {
let asset_reserve = T::Currency::free_balance(assets.asset_out, &native_pair_account);

let native_fee_spot_price =
hydra_dx_math::calculate_spot_price(asset_reserve, native_reserve, transfer_fee)
hydra_dx_math::xyk::calculate_spot_price(asset_reserve, native_reserve, transfer_fee)
.map_err(|_| Error::<T>::CannotApplyDiscount)?;

ensure!(
Expand Down
8 changes: 4 additions & 4 deletions pallets/xyk/src/tests.rs
Expand Up @@ -1535,7 +1535,7 @@ fn test_calculate_out_given_in() {
let in_reserve: Balance = 10000000000000;
let out_reserve: Balance = 100000;
let in_amount: Balance = 100000000000;
let result = hydra_dx_math::calculate_out_given_in(in_reserve, out_reserve, in_amount);
let result = hydra_dx_math::xyk::calculate_out_given_in(in_reserve, out_reserve, in_amount);
assert_eq!(result, Ok(990));
});
}
Expand All @@ -1546,7 +1546,7 @@ fn test_calculate_out_given_in_invalid() {
let in_reserve: Balance = 0;
let out_reserve: Balance = 1000;
let in_amount: Balance = 0;
let result = hydra_dx_math::calculate_out_given_in(in_reserve, out_reserve, in_amount);
let result = hydra_dx_math::xyk::calculate_out_given_in(in_reserve, out_reserve, in_amount);
assert_eq!(result, Ok(0));
});
}
Expand All @@ -1557,7 +1557,7 @@ fn test_calculate_in_given_out_insufficient_pool_balance() {
let in_reserve: Balance = 10000000000000;
let out_reserve: Balance = 100000;
let out_amount: Balance = 100000000000;
let result = hydra_dx_math::calculate_in_given_out(out_reserve, in_reserve, out_amount);
let result = hydra_dx_math::xyk::calculate_in_given_out(out_reserve, in_reserve, out_amount);
assert_eq!(result, Err(MathError::InsufficientOutReserve));
});
}
Expand All @@ -1568,7 +1568,7 @@ fn test_calculate_in_given_out() {
let in_reserve: Balance = 10000000000000;
let out_reserve: Balance = 10000000;
let out_amount: Balance = 1000000;
let result = hydra_dx_math::calculate_in_given_out(out_reserve, in_reserve, out_amount);
let result = hydra_dx_math::xyk::calculate_in_given_out(out_reserve, in_reserve, out_amount);
assert_eq!(result, Ok(1111111111112));
});
}
Expand Down