Skip to content

Commit

Permalink
Multihop, Pool: this PR brakes a lot of other tests, but the main foc…
Browse files Browse the repository at this point in the history
…us is . We should be throwing the expected panic msg, somehow we don't catch that
  • Loading branch information
gangov committed Nov 4, 2023
1 parent 881b5c4 commit cea61f1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions contracts/multihop/src/tests/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ fn swap_single_pool_no_fees() {
}

#[test]
// #[should_panic(expected = "Pool: Assert max spread: spread exceeds maximum allowed")]
#[should_panic]
#[should_panic(expected = "Pool: Assert max spread: spread exceeds maximum allowed")]
// #[should_panic]
fn swap_should_fail_when_spread_exceeds_the_limit() {
let env = Env::default();
let admin = Address::random(&env);
Expand Down
25 changes: 25 additions & 0 deletions contracts/pool/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,8 @@ fn do_swap(
}
}

env.events()
.publish(("debug", "634"), max_spread.unwrap());
let belief_price = belief_price.map(Decimal::percent);
let max_spread = Decimal::bps(max_spread.map_or_else(|| config.max_allowed_spread_bps, |x| x));

Expand All @@ -648,6 +650,7 @@ fn do_swap(
};

// 1. We calculate the referral_fee below. If none referral fee will be 0
env.events().publish(("debug", "653"), Some(()));
let compute_swap: ComputeSwap = compute_swap(
pool_balance_sell,
pool_balance_buy,
Expand All @@ -656,6 +659,7 @@ fn do_swap(
referral_fee_bps,
);

env.events().publish(("debug", "662"), Some(()));
assert_max_spread(
&env,
belief_price,
Expand All @@ -673,20 +677,23 @@ fn do_swap(
};

// transfer tokens to swap
env.events().publish(("debug", "680"), Some(()));
token_contract::Client::new(&env, &sell_token).transfer(
&sender,
&env.current_contract_address(),
&offer_amount,
);

// return swapped tokens to user
env.events().publish(("debug", "688"), Some(()));
token_contract::Client::new(&env, &buy_token).transfer(
&env.current_contract_address(),
&sender,
&compute_swap.return_amount,
);

// send commission to fee recipient
env.events().publish(("debug", "696"), Some(()));
token_contract::Client::new(&env, &buy_token).transfer(
&env.current_contract_address(),
&config.fee_recipient,
Expand All @@ -695,6 +702,8 @@ fn do_swap(

// 2. If referral is present and return amount is larger than 0 we send referral fee commision
// to fee recipient
env.events()
.publish(("debug", "706"), compute_swap.spread_amount);
if let Some(Referral { address, fee }) = referral {
if fee > 0 {
token_contract::Client::new(&env, &buy_token).transfer(
Expand All @@ -705,6 +714,7 @@ fn do_swap(
}
}

env.events().publish(("debug", "717"), Some(()));
// user is offering to sell A, so they will receive B
// A balance is bigger, B balance is smaller
let (balance_a, balance_b) = if offer_asset == config.token_a {
Expand Down Expand Up @@ -739,6 +749,8 @@ fn do_swap(
("swap", "referral_fee_amount"),
compute_swap.referral_fee_amount,
);
env.events()
.publish(("debug: end of do_swap", "753"), compute_swap.spread_amount);
compute_swap.return_amount
}

Expand Down Expand Up @@ -895,23 +907,36 @@ pub fn assert_max_spread(
) {
// Calculate the expected return if a belief price is provided
let expected_return = belief_price.map(|price| offer_amount * price);
env.events().publish(("debug", "910"), Some(()));

// Total return is the sum of the amount received and the spread
let total_return = return_amount + spread_amount;
env.events().publish(("debug", "914"), Some(()));

// Calculate the spread ratio, the fraction of the return that is due to spread
// If the user has specified a belief price, use it to calculate the expected return
// Otherwise, use the total return
let spread_ratio = if let Some(expected_return) = expected_return {
env.events().publish(("debug", "920"), Some(()));
Decimal::from_ratio(spread_amount, expected_return)
} else {
env.events().publish(("debug", "923"), Some(()));
Decimal::from_ratio(spread_amount, total_return)
};

env.events().publish(("debug", "927"), Some(()));
env.events()
.publish(("debug", "929"), Decimal::to_string(&spread_ratio, env));
env.events()
.publish(("debug", "931"), Decimal::to_string(&max_spread, env));
env.events()
.publish(("debug", "933"), spread_ratio > max_spread);
if spread_ratio > max_spread {
env.events().publish(("debug", "935"), Some(()));
log!(env, "Spread exceeds maximum allowed");
panic!("Pool: Assert max spread: spread exceeds maximum allowed");
}
env.events().publish(("debug: assert max spread", "939"), Some(()));
}

/// Computes the result of a swap operation.
Expand Down

0 comments on commit cea61f1

Please sign in to comment.