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

Fix update pool tick and add tick limit check to calculate swap #121

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified sdk/contracts/invariant.wasm
Binary file not shown.
11 changes: 2 additions & 9 deletions sdk/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,5 @@

set -e

npm run node:start &
sleep 5
npm run test &
test_pid=$!

wait $test_pid
test_status=$?

exit $test_status
npm run node:start:d &&
npm run test
2 changes: 1 addition & 1 deletion src/contracts/storage/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl Pool {
let tick_index = match tick {
UpdatePoolTick::TickInitialized(tick) => {
if !x_to_y || is_enough_amount_to_cross {
let _ = tick.cross(self, current_timestamp);
tick.cross(self, current_timestamp).unwrap();
has_crossed = true;
} else if !remaining_amount.is_zero() {
if by_amount_in {
Expand Down
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern crate alloc;
pub mod contracts;
pub mod math;

use math::sqrt_price::{get_max_tick, get_min_tick};
pub use odra_modules::erc20::{Erc20, Erc20Deployer, Erc20Ref};

#[cfg(test)]
Expand Down Expand Up @@ -135,6 +136,12 @@ impl Invariant {
let event_start_sqrt_price = pool.sqrt_price;
let mut event_fee_amount = TokenAmount::new(U256::from(0));

let tick_limit = if x_to_y {
get_min_tick(pool_key.fee_tier.tick_spacing)
} else {
get_max_tick(pool_key.fee_tier.tick_spacing)
};

while !remaining_amount.is_zero() {
let (swap_limit, limiting_tick) = self.tickmap.get_closer_limit(
sqrt_price_limit,
Expand Down Expand Up @@ -206,6 +213,15 @@ impl Invariant {
ticks.push(tick)
}
}

let reached_tick_limit = match x_to_y {
true => pool.current_tick_index <= tick_limit,
false => pool.current_tick_index >= tick_limit,
};

if reached_tick_limit {
return Err(InvariantError::TickLimitReached);
}
}

if total_amount_out.get().is_zero() {
Expand Down
Loading