Skip to content

Commit

Permalink
add: quote_route
Browse files Browse the repository at this point in the history
  • Loading branch information
vuonghuuhung committed Jun 6, 2024
1 parent 751d51e commit aa26986
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 13 deletions.
63 changes: 53 additions & 10 deletions contracts/oraiswap-v3/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ use cosmwasm_std::entry_point;
use decimal::{CheckedOps, Decimal};

use crate::error::ContractError;
use crate::interface::CalculateSwapResult;
use crate::interface::{CalculateSwapResult, SwapHop};
use crate::liquidity::Liquidity;
use crate::token_amount::TokenAmount;
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
use crate::percentage::Percentage;
use crate::sqrt_price::{get_max_tick, get_min_tick, SqrtPrice};
use crate::state::{add_position, add_tick, flip_bitmap, get_closer_limit, get_tick, update_tick, CONFIG, POOLS};
use crate::{check_tick, compute_swap_step, Config, PoolKey, Position, Tick, UpdatePoolTick, MAX_SQRT_PRICE, MIN_SQRT_PRICE};
use crate::state::{
add_position, add_tick, flip_bitmap, get_closer_limit, get_tick, update_tick, CONFIG, POOLS,
};
use crate::token_amount::TokenAmount;
use crate::{
check_tick, compute_swap_step, Config, PoolKey, Position, Tick,
UpdatePoolTick, MAX_SQRT_PRICE, MIN_SQRT_PRICE,
};

use cosmwasm_std::{
to_binary, Addr, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult, Storage, WasmMsg,
Expand Down Expand Up @@ -93,9 +98,12 @@ pub fn execute(
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::ProtocolFee {} => to_binary(&get_protocol_fee(deps)?),
QueryMsg::QuoteRoute { amount_in, swaps } => {
to_binary(&quote_route(deps.storage, env, amount_in, swaps)?)
}
}
}

Expand Down Expand Up @@ -284,7 +292,7 @@ fn create_position(
]))
}

pub fn calculate_swap(
fn calculate_swap(
store: &dyn Storage,
current_timestamp: u64,
pool_key: PoolKey,
Expand All @@ -302,8 +310,7 @@ pub fn calculate_swap(
let mut pool = POOLS.load(store, &pool_key.key())?;

if x_to_y {
if pool.sqrt_price <= sqrt_price_limit
|| sqrt_price_limit > SqrtPrice::new(MAX_SQRT_PRICE)
if pool.sqrt_price <= sqrt_price_limit || sqrt_price_limit > SqrtPrice::new(MAX_SQRT_PRICE)
{
return Err(ContractError::WrongLimit {});
}
Expand Down Expand Up @@ -428,7 +435,7 @@ pub fn calculate_swap(
})
}

pub fn swap(
fn swap(
deps: DepsMut,
env: Env,
info: MessageInfo,
Expand Down Expand Up @@ -499,5 +506,41 @@ pub fn swap(
});
}

Ok(Response::new().add_messages(msgs).add_attribute("action", "swap"))
Ok(Response::new()
.add_messages(msgs)
.add_attribute("action", "swap")
.add_attribute("amount_out", calculate_swap_result.amount_out.to_string()))
}

fn quote_route(
store: &dyn Storage,
env: Env,
amount_in: TokenAmount,
swaps: Vec<SwapHop>,
) -> StdResult<TokenAmount> {
let mut next_swap_amount = amount_in;

for swap_hop in swaps.iter() {
let SwapHop { pool_key, x_to_y } = swap_hop;

let sqrt_price_limit = if *x_to_y {
SqrtPrice::new(MIN_SQRT_PRICE)
} else {
SqrtPrice::new(MAX_SQRT_PRICE)
};

let res = calculate_swap(
store,
env.block.time.nanos(),
pool_key.clone(),
*x_to_y,
next_swap_amount,
true,
sqrt_price_limit,
).unwrap();

next_swap_amount = res.amount_out;
}

Ok(next_swap_amount)
}
6 changes: 6 additions & 0 deletions contracts/oraiswap-v3/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,10 @@ pub enum ContractError {

#[error("NoGainSwap")]
NoGainSwap,

#[error("SwapFailed")]
SwapFailed,

#[error("AmountUnderMinimumAmountOut")]
AmountUnderMinimumAmountOut
}
9 changes: 8 additions & 1 deletion contracts/oraiswap-v3/src/interface.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Addr, Uint128};

use crate::{sqrt_price::SqrtPrice, token_amount::TokenAmount, Pool, Tick};
use crate::{sqrt_price::SqrtPrice, token_amount::TokenAmount, Pool, PoolKey, Tick};

#[cw_serde]
pub struct Asset {
Expand All @@ -22,6 +22,7 @@ pub struct NftExtentions {
pub tick_upper: i32,
}

#[cw_serde]
pub struct CalculateSwapResult {
pub amount_in: TokenAmount,
pub amount_out: TokenAmount,
Expand All @@ -32,6 +33,12 @@ pub struct CalculateSwapResult {
pub ticks: Vec<Tick>,
}

#[cw_serde]
pub struct SwapHop {
pub pool_key: PoolKey,
pub x_to_y: bool,
}

#[cw_serde]
pub enum Cw721BaseExecuteMsg<T> {
Mint {
Expand Down
10 changes: 8 additions & 2 deletions contracts/oraiswap-v3/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::Addr;

use crate::{liquidity::Liquidity, percentage::Percentage, sqrt_price::SqrtPrice, token_amount::TokenAmount, PoolKey};
use crate::{interface::SwapHop, liquidity::Liquidity, percentage::Percentage, sqrt_price::SqrtPrice, token_amount::TokenAmount, PoolKey};

#[cw_serde]
pub struct InstantiateMsg {
Expand Down Expand Up @@ -34,7 +34,7 @@ pub enum ExecuteMsg {
amount: TokenAmount,
by_amount_in: bool,
sqrt_price_limit: SqrtPrice,
}
},
}

#[cw_serde]
Expand All @@ -45,4 +45,10 @@ pub struct MigrateMsg {}
pub enum QueryMsg {
#[returns(Percentage)]
ProtocolFee {},

#[returns(TokenAmount)]
QuoteRoute {
amount_in: TokenAmount,
swaps: Vec<SwapHop>,
}
}

0 comments on commit aa26986

Please sign in to comment.