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

Feat/higher decimals #14

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
Open
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
496 changes: 373 additions & 123 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ resolver = '2'

members = ["packages/*", "contracts/*", "wasm"]


[workspace.package]
version = "0.2.0"
authors = ["Oraichain Labs"]
Expand Down
7 changes: 3 additions & 4 deletions contracts/oraiswap-v3/src/entrypoints/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use cosmwasm_std::{
StdResult,
};
use cw20::Expiration;
use decimal::Decimal;
use decimal::{Decimal, Factories};

/// Allows an admin to adjust admin.
///
Expand Down Expand Up @@ -492,7 +492,7 @@ pub fn swap_route(
swaps.clone(),
)?;

let min_amount_out = calculate_min_amount_out(expected_amount_out, slippage);
let min_amount_out = calculate_min_amount_out(expected_amount_out, slippage)?;

if amount_out < min_amount_out {
return Err(ContractError::AmountUnderMinimumAmountOut);
Expand Down Expand Up @@ -1167,7 +1167,6 @@ pub fn handle_mint(
}

// only owner can execute
#[allow(clippy::too_many_arguments)]
pub fn create_incentive(
deps: DepsMut,
env: Env,
Expand Down Expand Up @@ -1195,7 +1194,7 @@ pub fn create_incentive(
reward_token: reward_token.clone(),
remaining,
start_timestamp: start_timestamp.unwrap_or(env.block.time.seconds()),
incentive_growth_global: FeeGrowth(0),
incentive_growth_global: FeeGrowth::from_integer(0),
last_updated: env.block.time.seconds(),
};
pool.incentives.push(incentive);
Expand Down
20 changes: 10 additions & 10 deletions contracts/oraiswap-v3/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ mod tests {
use crate::state;
use oraiswap_v3_common::math::percentage::Percentage;
use oraiswap_v3_common::math::sqrt_price::SqrtPrice;
use oraiswap_v3_common::math::TICK_SEARCH_RANGE;
use oraiswap_v3_common::math::{MIN_TICK, TICK_SEARCH_RANGE};
use oraiswap_v3_common::storage::FeeTier;

use cosmwasm_std::testing::mock_dependencies;
Expand Down Expand Up @@ -750,7 +750,7 @@ mod tests {

flip_bitmap(deps.as_mut().storage, true, MAX_TICK - 10, 1, pool_key).unwrap();
assert_eq!(
next_initialized(deps.as_ref().storage, -MAX_TICK + 1, 1, pool_key),
next_initialized(deps.as_ref().storage, MIN_TICK + 1, 1, pool_key),
None
);
}
Expand All @@ -767,7 +767,7 @@ mod tests {
let pool_key = &PoolKey::new(token_0, token_1, fee_tier).unwrap();

assert_eq!(
next_initialized(deps.as_ref().storage, MAX_TICK - 22, 4, pool_key),
next_initialized(deps.as_ref().storage, MAX_TICK - 23, 4, pool_key),
None
);
}
Expand Down Expand Up @@ -931,7 +931,7 @@ mod tests {
};
let pool_key = &PoolKey::new(token_0, token_1, fee_tier).unwrap();

flip_bitmap(deps.as_mut().storage, true, -MAX_TICK + 1, 1, pool_key).unwrap();
flip_bitmap(deps.as_mut().storage, true, MIN_TICK + 1, 1, pool_key).unwrap();
assert_eq!(
prev_initialized(deps.as_ref().storage, MAX_TICK - 1, 1, pool_key),
None
Expand All @@ -949,10 +949,10 @@ mod tests {
};
let pool_key = &PoolKey::new(token_0, token_1, fee_tier).unwrap();

flip_bitmap(deps.as_mut().storage, true, -MAX_TICK + 63, 1, pool_key).unwrap();
flip_bitmap(deps.as_mut().storage, true, MIN_TICK + 63, 1, pool_key).unwrap();
assert_eq!(
prev_initialized(deps.as_ref().storage, -MAX_TICK + 128, 1, pool_key),
Some(-MAX_TICK + 63)
prev_initialized(deps.as_ref().storage, MIN_TICK + 128, 1, pool_key),
Some(MIN_TICK + 63)
);
}

Expand Down Expand Up @@ -988,14 +988,14 @@ mod tests {
{
let step = 5u16;
let result = get_search_limit(MAX_TICK - 22, step, true);
let expected = MAX_TICK - 3;
let expected = MAX_TICK;
assert_eq!(result, expected);
}
// At the price limit
{
let step = 5u16;
let result = get_search_limit(MAX_TICK - 3, step, true);
let expected = MAX_TICK - 3;
let result = get_search_limit(MAX_TICK, step, true);
let expected = MAX_TICK;
assert_eq!(result, expected);
}
}
Expand Down
14 changes: 10 additions & 4 deletions contracts/oraiswap-v3/src/tests/cross.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_std::coins;
use decimal::{Decimal, Factories};
use decimal::*;
use oraiswap_v3_common::{
math::{fee_growth::FeeGrowth, liquidity::Liquidity, percentage::Percentage},
storage::{FeeTier, PoolKey},
Expand Down Expand Up @@ -56,10 +56,16 @@ fn test_cross() {
Liquidity::from_integer(1000000)
);

assert_eq!(upper_tick.fee_growth_outside_x, FeeGrowth::new(0));
assert_eq!(
upper_tick.fee_growth_outside_x,
FeeGrowth::new(U256::from(0))
);
assert_eq!(
middle_tick.fee_growth_outside_x,
FeeGrowth::new(30000000000000000000000)
FeeGrowth::new(30000000000000000000000_u128.into())
);
assert_eq!(
lower_tick.fee_growth_outside_x,
FeeGrowth::new(U256::from(0))
);
assert_eq!(lower_tick.fee_growth_outside_x, FeeGrowth::new(0));
}
35 changes: 25 additions & 10 deletions contracts/oraiswap-v3/src/tests/cross_both_size.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_std::coins;
use decimal::{Decimal, Factories};
use decimal::*;
use oraiswap_v3_common::{
error::ContractError,
math::{
Expand Down Expand Up @@ -230,17 +230,23 @@ fn test_cross_both_side() {
assert_eq!(pool.current_tick_index, -20);
assert_eq!(
pool.fee_growth_global_x,
FeeGrowth::new(29991002699190242927121)
FeeGrowth::new(29991002699190242927121_u128.into())
);
assert_eq!(pool.fee_growth_global_y, FeeGrowth::new(0));
assert_eq!(pool.fee_growth_global_y, FeeGrowth::new(U256::from(0)));
assert_eq!(pool.fee_protocol_token_x, TokenAmount(4));
assert_eq!(pool.fee_protocol_token_y, TokenAmount(2));
assert_eq!(pool.liquidity, expected_liquidity);
assert_eq!(pool.sqrt_price, SqrtPrice::new(999500149964999999999999));
assert_eq!(pool.sqrt_price, SqrtPrice::new(999500149965006998740208));

let final_last_tick = app.get_tick(dex.as_str(), &pool_key, -20).unwrap();
assert_eq!(final_last_tick.fee_growth_outside_x, FeeGrowth::new(0));
assert_eq!(final_last_tick.fee_growth_outside_y, FeeGrowth::new(0));
assert_eq!(
final_last_tick.fee_growth_outside_x,
FeeGrowth::new(U256::from(0))
);
assert_eq!(
final_last_tick.fee_growth_outside_y,
FeeGrowth::new(U256::from(0))
);
assert_eq!(
final_last_tick.liquidity_change,
expected_liquidity_change_on_last_tick
Expand All @@ -249,14 +255,23 @@ fn test_cross_both_side() {
let final_lower_tick = app.get_tick(dex.as_str(), &pool_key, -10).unwrap();
assert_eq!(
final_lower_tick.fee_growth_outside_x,
FeeGrowth::new(29991002699190242927121)
FeeGrowth::new(29991002699190242927121_u128.into())
);
assert_eq!(
final_lower_tick.fee_growth_outside_y,
FeeGrowth::new(U256::from(0))
);
assert_eq!(final_lower_tick.fee_growth_outside_y, FeeGrowth::new(0));
assert_eq!(final_lower_tick.liquidity_change, Liquidity::new(0));

let final_upper_tick = app.get_tick(dex.as_str(), &pool_key, 10).unwrap();
assert_eq!(final_upper_tick.fee_growth_outside_x, FeeGrowth::new(0));
assert_eq!(final_upper_tick.fee_growth_outside_y, FeeGrowth::new(0));
assert_eq!(
final_upper_tick.fee_growth_outside_x,
FeeGrowth::new(U256::from(0))
);
assert_eq!(
final_upper_tick.fee_growth_outside_y,
FeeGrowth::new(U256::from(0))
);
assert_eq!(
final_upper_tick.liquidity_change,
expected_liquidity_change_on_upper_tick
Expand Down
2 changes: 1 addition & 1 deletion contracts/oraiswap-v3/src/tests/get_liquidity_ticks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn test_get_liquidity_ticks() {
assert_eq!(ticks_amount, 2);

let tickmap: Vec<(u16, u64)> = get_tickmap!(app, dex, &pool_key, -10, 10, false).unwrap();
assert_eq!(tickmap.len(), 2);
assert_eq!(tickmap.len(), 1);
let mut ticks = vec![];
tickmap.iter().for_each(|(chunk_index, chunk)| {
for i in 0..(CHUNK_SIZE as u8) {
Expand Down
8 changes: 4 additions & 4 deletions contracts/oraiswap-v3/src/tests/get_position_ticks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ fn test_query_all_positions() {
liquidity: Liquidity(10),
lower_tick_index: -10,
upper_tick_index: 10,
fee_growth_inside_x: FeeGrowth(0),
fee_growth_inside_y: FeeGrowth(0),
fee_growth_inside_x: FeeGrowth::from_integer(0),
fee_growth_inside_y: FeeGrowth::from_integer(0),
last_block_number: positions[0].last_block_number,
tokens_owed_x: TokenAmount(0),
tokens_owed_y: TokenAmount(0),
Expand All @@ -314,8 +314,8 @@ fn test_query_all_positions() {
liquidity: Liquidity(10),
lower_tick_index: -100,
upper_tick_index: 100,
fee_growth_inside_x: FeeGrowth(0),
fee_growth_inside_y: FeeGrowth(0),
fee_growth_inside_x: FeeGrowth::from_integer(0),
fee_growth_inside_y: FeeGrowth::from_integer(0),
last_block_number: positions[1].last_block_number,
tokens_owed_x: TokenAmount(0),
tokens_owed_y: TokenAmount(0),
Expand Down
46 changes: 26 additions & 20 deletions contracts/oraiswap-v3/src/tests/get_tickmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ fn test_get_tickmap() {
app,
dex,
pool_key,
-58,
5,
-47,
16,
liquidity_delta,
pool.sqrt_price,
SqrtPrice::max_instance(),
Expand All @@ -70,7 +70,7 @@ fn test_get_tickmap() {
assert_eq!(
tickmap[0],
(
3465,
10397,
0b1000000000000000000000000000000000000000000000000000000000000001
)
);
Expand Down Expand Up @@ -150,11 +150,14 @@ fn test_get_tickmap_tick_spacing_over_one() {
assert_eq!(tickmap[0], (0, 0b1));
assert_eq!(
tickmap[1],
(346, 0b1100000000000000000000000000000000000000)
(1039, 0b1100000000000000000000000000000000000000000000000000)
);
assert_eq!(
tickmap[2],
(get_max_chunk(fee_tier.tick_spacing), 0b10000000000)
(
get_max_chunk(fee_tier.tick_spacing),
0b10000000000000000000000000000000000
)
);
assert_eq!(tickmap.len(), 3);
}
Expand Down Expand Up @@ -197,8 +200,8 @@ fn test_get_tickmap_edge_ticks_intialized() {
app,
dex,
pool_key,
-221818,
-221817,
get_min_tick(fee_tier.tick_spacing),
get_min_tick(fee_tier.tick_spacing) + 1,
liquidity_delta,
pool.sqrt_price,
SqrtPrice::max_instance(),
Expand All @@ -210,8 +213,8 @@ fn test_get_tickmap_edge_ticks_intialized() {
app,
dex,
pool_key,
221817,
221818,
get_max_tick(fee_tier.tick_spacing) - 1,
get_max_tick(fee_tier.tick_spacing),
liquidity_delta,
pool.sqrt_price,
SqrtPrice::max_instance(),
Expand All @@ -234,7 +237,7 @@ fn test_get_tickmap_edge_ticks_intialized() {
tickmap[1],
(
get_max_chunk(fee_tier.tick_spacing),
0b11000000000000000000000000000000000000000000000000000
0b1100000000000000000000000000000
)
);
assert_eq!(tickmap.len(), 2);
Expand All @@ -253,7 +256,7 @@ fn test_get_tickmap_edge_ticks_intialized() {
tickmap[1],
(
get_max_chunk(fee_tier.tick_spacing),
0b11000000000000000000000000000000000000000000000000000
0b1100000000000000000000000000000
)
);
assert_eq!(tickmap.len(), 2);
Expand All @@ -272,7 +275,7 @@ fn test_get_tickmap_edge_ticks_intialized() {
tickmap[1],
(
get_max_chunk(fee_tier.tick_spacing),
0b11000000000000000000000000000000000000000000000000000
0b1100000000000000000000000000000
)
);
assert_eq!(tickmap.len(), 2);
Expand All @@ -291,7 +294,7 @@ fn test_get_tickmap_edge_ticks_intialized() {
tickmap[0],
(
get_max_chunk(fee_tier.tick_spacing),
0b11000000000000000000000000000000000000000000000000000
0b1100000000000000000000000000000
)
);
assert_eq!(tickmap.len(), 2);
Expand Down Expand Up @@ -358,8 +361,14 @@ fn test_get_tickmap_more_chunks_above() {
.unwrap();

for (i, _) in (0..tickmap.len()).enumerate() {
let current = 3466 + i as u16;
assert_eq!(tickmap[i], (current, 0b11));
let current = 10397 + i as u16;
assert_eq!(
tickmap[i],
(
current,
0b1100000000000000000000000000000000000000000000000000000
)
);
}
}

Expand Down Expand Up @@ -422,13 +431,10 @@ fn test_get_tickmap_more_chunks_below() {
)
.unwrap();
for (i, _) in (0..tickmap.len()).enumerate() {
let current = 2644 + i as u16;
let current = 9576 + i as u16;
assert_eq!(
tickmap[i],
(
current,
0b110000000000000000000000000000000000000000000000000000000000
)
(current, 0b1100000000000000000000000000000000000000000000000)
);
}
}
Expand Down
Loading