Skip to content

Commit

Permalink
Pool stable: Adjust reverse swap simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
ueco-jb committed Jan 19, 2024
1 parent c610d60 commit d4f59e2
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions contracts/pool_stable/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ impl StableLiquidityPoolTrait for StableLiquidityPool {
};

let (offer_amount, spread_amount, commission_amount) = compute_offer_amount(
&env,
pool_balance_offer,
pool_balance_ask,
ask_amount,
Expand Down Expand Up @@ -742,25 +743,30 @@ pub fn compute_swap(
/// * **ask_amount** amount of ask assets to swap to.
/// * **commission_rate** total amount of fees charged for the swap.
pub fn compute_offer_amount(
env: &Env,
offer_pool: i128,
ask_pool: i128,
ask_amount: i128,
commission_rate: Decimal,
) -> (i128, i128, i128) {
// Calculate the cross product of offer_pool and ask_pool
let cp: i128 = offer_pool * ask_pool;

// Calculate one minus the commission rate
let one_minus_commission = Decimal::one() - commission_rate;
let amp_parameters = get_amp(&env).unwrap();
let amp = compute_current_amp(&env, &amp_parameters);

// Calculate the inverse of one minus the commission rate
let inv_one_minus_commission = Decimal::one() / one_minus_commission;
let new_offer_pool = calc_y(
amp as u128,
Decimal::from_atomics(ask_pool - ask_amount, 6),
&[
Decimal::from_atomics(offer_pool, 6),
Decimal::from_atomics(ask_pool, 6),
],
6,
);

// Calculate the resulting amount of ask assets after the swap
let offer_amount: i128 = cp / (ask_pool - (ask_amount * inv_one_minus_commission)) - offer_pool;
let offer_amount = new_offer_pool - offer_pool;

let one_minus_commission = Decimal::one() - commission_rate;
let inv_one_minus_commission = Decimal::one() / one_minus_commission;
let ask_before_commission = ask_amount * inv_one_minus_commission;

// Calculate the spread amount, representing the difference between the expected and actual swap amounts
let spread_amount: i128 = (offer_amount * ask_pool / offer_pool) - ask_before_commission;

Expand Down

0 comments on commit d4f59e2

Please sign in to comment.