Skip to content

Commit

Permalink
fix(PCL): spread calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
epanchee authored Oct 18, 2023
1 parent 3559eb4 commit 062bb4b
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 24 deletions.
8 changes: 4 additions & 4 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 contracts/pair_concentrated/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "astroport-pair-concentrated"
version = "2.2.0"
version = "2.2.1"
authors = ["Astroport"]
edition = "2021"
description = "The Astroport concentrated liquidity pair"
Expand Down
2 changes: 1 addition & 1 deletion contracts/pair_concentrated_inj/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "astroport-pair-concentrated-injective"
version = "2.2.0"
version = "2.2.1"
authors = ["Astroport"]
edition = "2021"
description = "The Astroport concentrated liquidity pair which supports Injective orderbook integration"
Expand Down
2 changes: 1 addition & 1 deletion contracts/pair_concentrated_inj/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::orderbook::state::OrderbookState;
use crate::state::CONFIG;

const MIGRATE_FROM: &str = "astroport-pair-concentrated";
const MIGRATION_VERSION: &str = "2.2.0";
const MIGRATION_VERSION: &str = "2.2.1";

/// Manages the contract migration.
#[cfg_attr(not(feature = "library"), entry_point)]
Expand Down
2 changes: 1 addition & 1 deletion contracts/pair_stable/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1392,7 +1392,7 @@ proptest! {
let diff = (sim_result as i128 - result.return_amount.u128() as i128).abs();

assert!(
diff <= 9,
diff <= 10,
"result={}, sim_result={}, amp={}, amount_in={}, balance_in={}, balance_out={}, diff={}",
result.return_amount,
sim_result,
Expand Down
2 changes: 1 addition & 1 deletion packages/astroport/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "astroport"
version = "3.6.1"
version = "3.6.2"
authors = ["Astroport"]
edition = "2021"
description = "Common Astroport types, queriers and other utils"
Expand Down
2 changes: 1 addition & 1 deletion packages/astroport_pcl_common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "astroport-pcl-common"
version = "1.0.0"
version = "1.0.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
24 changes: 10 additions & 14 deletions packages/astroport_pcl_common/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ pub fn calc_last_prices(xs: &[Decimal256], config: &Config, env: &Env) -> StdRes
/// Calculate swap result.
pub fn compute_swap(
xs: &[Decimal256],
offer_amount: Decimal256,
mut offer_amount: Decimal256,
ask_ind: usize,
config: &Config,
env: &Env,
Expand All @@ -258,27 +258,23 @@ pub fn compute_swap(
let amp_gamma = config.pool_state.get_amp_gamma(env);
let d = calc_d(&ixs, &amp_gamma)?;

let offer_amount = if offer_ind == 1 {
offer_amount * config.pool_state.price_state.price_scale
} else {
offer_amount
};
if offer_ind == 1 {
offer_amount *= config.pool_state.price_state.price_scale;
}

ixs[offer_ind] += offer_amount;

let new_y = calc_y(&ixs, d, &amp_gamma, ask_ind)?;
let mut dy = ixs[ask_ind] - new_y;
ixs[ask_ind] = new_y;

let price = if ask_ind == 1 {
dy /= config.pool_state.price_state.price_scale;
config.pool_state.price_state.price_scale.inv().unwrap()
} else {
config.pool_state.price_state.price_scale
};

// Since price_scale moves slower than real price spread fee may become negative
let spread_fee = (offer_amount * price).saturating_sub(dy);
let mut spread_fee = offer_amount.saturating_sub(dy);

if ask_ind == 1 {
dy /= config.pool_state.price_state.price_scale;
spread_fee /= config.pool_state.price_state.price_scale;
}

let fee_rate = config.pool_params.fee(&ixs);
let total_fee = fee_rate * dy;
Expand Down

0 comments on commit 062bb4b

Please sign in to comment.