Skip to content

Commit

Permalink
update query tickmap
Browse files Browse the repository at this point in the history
  • Loading branch information
tubackkhoa committed Aug 29, 2024
1 parent 62a1191 commit e482fc8
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 20 deletions.
6 changes: 3 additions & 3 deletions contracts/oraiswap-v3/src/entrypoints/common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_std::{
Addr, Api, CosmosMsg, Deps, DepsMut, Env, MessageInfo, Order, Storage, Timestamp,
Addr, Api, CosmosMsg, Deps, DepsMut, Env, MessageInfo, Order, Storage, Timestamp, Uint64,
};

use cw20::Expiration;
Expand Down Expand Up @@ -315,7 +315,7 @@ pub fn tickmap_slice(
max_chunk: u16,
pool_key: &PoolKey,
limit: usize,
) -> Vec<(u16, u64)> {
) -> Vec<(u16, Uint64)> {
let pool_key = pool_key.key();
let mut min_key = min_chunk.to_be_bytes().to_vec();
let mut max_key = max_chunk.to_be_bytes().to_vec();
Expand All @@ -328,7 +328,7 @@ pub fn tickmap_slice(
.filter_map(|item| {
if let Ok((k, v)) = item {
if pool_key.eq(&k[2..]) {
return Some((u16::from_be_bytes([k[0], k[1]]), v));
return Some((u16::from_be_bytes([k[0], k[1]]), v.into()));
}
}
None
Expand Down
4 changes: 2 additions & 2 deletions contracts/oraiswap-v3/src/entrypoints/query.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{Addr, Binary, Deps, Env, Order, StdResult};
use cosmwasm_std::{Addr, Binary, Deps, Env, Order, StdResult, Uint64};
use cw_storage_plus::Bound;

use crate::{
Expand Down Expand Up @@ -234,7 +234,7 @@ pub fn get_tickmap(
lower_tick_index: i32,
upper_tick_index: i32,
x_to_y: bool,
) -> Result<Vec<(u16, u64)>, ContractError> {
) -> Result<Vec<(u16, Uint64)>, ContractError> {
let tick_spacing = pool_key.fee_tier.tick_spacing;
let (start_chunk, _) = tick_to_position(lower_tick_index, tick_spacing);
let (end_chunk, _) = tick_to_position(upper_tick_index, tick_spacing);
Expand Down
4 changes: 2 additions & 2 deletions contracts/oraiswap-v3/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(unused_imports)]
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{Addr, Binary};
use cosmwasm_std::{Addr, Binary, Uint64};
use cw20::Expiration;

use crate::interface::{
Expand Down Expand Up @@ -211,7 +211,7 @@ pub enum QueryMsg {
#[returns(u32)]
UserPositionAmount { owner: Addr },

#[returns(Vec<(u16, u64)>)]
#[returns(Vec<(u16, Uint64)>)]
TickMap {
pool_key: PoolKey,
lower_tick_index: i32,
Expand Down
2 changes: 1 addition & 1 deletion contracts/oraiswap-v3/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,6 @@ mod tests {
}

let ret = tickmap_slice(deps.as_ref().storage, min_chunk, max_chunk, &pool_key_1, 3);
assert_eq!(ret, [(0, 1), (1, 1), (2, 1)]);
assert_eq!(ret, [(0, 1u64.into()), (1, 1u64.into()), (2, 1u64.into())]);
}
}
2 changes: 0 additions & 2 deletions contracts/oraiswap-v3/src/tests/get_tickmap.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use cosmwasm_std::coins;
use cosmwasm_std::Addr;
use decimal::{Decimal, Factories};

use crate::get_max_chunk;
Expand All @@ -8,7 +7,6 @@ use crate::sqrt_price::get_min_tick;
use crate::tests::helper::FEE_DENOM;
use crate::{
liquidity::Liquidity,
msg,
percentage::Percentage,
sqrt_price::{calculate_sqrt_price, SqrtPrice},
tests::helper::{macros::*, MockApp},
Expand Down
39 changes: 29 additions & 10 deletions contracts/oraiswap-v3/src/tests/helper.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{Addr, Binary, Coin, Event, StdResult};
use cosmwasm_std::{Addr, Binary, Coin, Event, StdResult, Uint64};
use cosmwasm_testing_util::{ExecuteResponse, MockResult};

use crate::{
Expand Down Expand Up @@ -461,6 +461,27 @@ impl MockApp {
&msg::QueryMsg::AllPosition { limit, start_after },
)
}

pub fn query_tickmaps(
&self,
dex: &str,
pool_key: &PoolKey,
lower_tick: i32,
upper_tick: i32,
x_to_y: bool,
) -> StdResult<Vec<(u16, u64)>> {
let tickmaps: Vec<(u16, Uint64)> = self.query(
Addr::unchecked(dex),
&msg::QueryMsg::TickMap {
pool_key: pool_key.clone(),
lower_tick_index: lower_tick,
upper_tick_index: upper_tick,
x_to_y,
},
)?;

Ok(tickmaps.into_iter().map(|(k, v)| (k, v.u64())).collect())
}
}

pub fn extract_amount(events: &[Event], key: &str) -> Option<TokenAmount> {
Expand Down Expand Up @@ -1215,15 +1236,13 @@ pub mod macros {
pub(crate) use get_liquidity_ticks_amount;

macro_rules! get_tickmap {
($app:ident, $dex_address:expr, $pool_key:expr, $lower_tick_index:expr, $upper_tick_index:expr , $x_to_y:expr) => {{
$app.query(
Addr::unchecked($dex_address.as_str()),
&msg::QueryMsg::TickMap {
pool_key: $pool_key.clone(),
lower_tick_index: $lower_tick_index,
upper_tick_index: $upper_tick_index,
x_to_y: $x_to_y,
},
($app:ident, $dex_address:expr, $pool_key:expr, $lower_tick_index:expr, $upper_tick_index:expr, $x_to_y:expr) => {{
$app.query_tickmaps(
$dex_address.as_str(),
&$pool_key,
$lower_tick_index,
$upper_tick_index,
$x_to_y,
)
}};
}
Expand Down

0 comments on commit e482fc8

Please sign in to comment.