Skip to content

Commit

Permalink
fix: issue with precision
Browse files Browse the repository at this point in the history
  • Loading branch information
afsardo committed Dec 7, 2023
1 parent 3fd0707 commit f6618df
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/astroport/pair_concentrated/math/math_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::astroport::pair_concentrated::consts::{MAX_ITER, N, N_POW2, TOL};
use cosmwasm_std::{Decimal256, StdError, StdResult};
use itertools::Itertools;

/// Internal constant to increase calculation accuracy. (1000.0)
const PADDING: Decimal256 = Decimal256::raw(1000000000000000000000);
/// Internal constant to increase calculation accuracy.
const PADDING: Decimal256 = Decimal256::raw(1e36 as u128);

pub fn geometric_mean(x: &[Decimal256]) -> Decimal256 {
(x[0] * x[1]).sqrt()
Expand Down Expand Up @@ -87,8 +87,8 @@ pub(crate) fn df_dx(

let k = a_gamma_pow2 * k0 / gamma_one_k0_pow2;
let k0_x = x_r * N_POW2;
let k_x = k0_x * a_gamma_pow2 * (gamma + Decimal256::one() + k0)
/ (d_pow2 * gamma_one_k0 * gamma_one_k0_pow2);
let k_x = k0_x * a_gamma_pow2 * (gamma + Decimal256::one() + k0) * PADDING
/ (PADDING * d_pow2 * gamma_one_k0 * gamma_one_k0_pow2);

(k_x * (x[0] + x[1]) + k) * d + x_r - k_x * d_pow2
}
Expand Down
13 changes: 1 addition & 12 deletions src/astroport/pair_concentrated/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{
};
use crate::astroport::cosmwasm_ext::{Decimal256Ext, DecimalToInteger};
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Decimal, Decimal256, Fraction, StdError, StdResult, Uint128};
use cosmwasm_std::{Decimal, Decimal256, StdError, StdResult, Uint128};

#[cw_serde]
pub struct SwapSimulationResponse {
Expand Down Expand Up @@ -80,7 +80,6 @@ pub fn simulate(
#[cw_serde]
pub struct SwapResult {
pub new_y: Decimal256,
pub price: Decimal256,
pub offer_amount: Decimal256,
pub dy: Decimal256,
pub spread_fee: Decimal256,
Expand Down Expand Up @@ -120,7 +119,6 @@ fn compute_swap(
future_amp,
future_gamma,
);

let d = calc_d(&ixs, &amp_gamma)?;

if offer_ind == 1 {
Expand All @@ -133,14 +131,6 @@ fn compute_swap(
let mut dy = ixs[ask_ind] - new_y;
ixs[ask_ind] = new_y;

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

// Derive spread using oracle price
let spread_fee = if ask_ind == 1 {
dy /= price_scale;
(offer_amount / oracle_price).saturating_sub(dy)
Expand All @@ -154,7 +144,6 @@ fn compute_swap(

Ok(SwapResult {
new_y,
price,
offer_amount,
dy,
spread_fee,
Expand Down

0 comments on commit f6618df

Please sign in to comment.