Skip to content

Commit

Permalink
Merge pull request #42 from oraichain/hotfix/return-error-if-exceed-s…
Browse files Browse the repository at this point in the history
…lippage

Hotfix/return error if exceed slippage
  • Loading branch information
ducphamle2 authored Mar 6, 2024
2 parents ffa97d0 + ce11d97 commit 1b5dd26
Show file tree
Hide file tree
Showing 4 changed files with 600 additions and 587 deletions.
20 changes: 11 additions & 9 deletions contracts/oraiswap_limit_order/src/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn submit_order(
assets[1].assert_if_asset_is_zero()?;

let offer_amount = assets[0].amount;
let mut ask_amount = assets[1].amount;
let ask_amount = assets[1].amount;

let (highest_buy_price, buy_found, _) =
orderbook_pair.highest_price(deps.storage, OrderDirection::Buy);
Expand All @@ -49,26 +49,26 @@ pub fn submit_order(
if buy_found && sell_found {
match direction {
OrderDirection::Buy => {
let mut price = Decimal::from_ratio(offer_amount, ask_amount);
let price = Decimal::from_ratio(offer_amount, ask_amount);
let spread_price = lowest_sell_price * sell_spread_factor;
if price.ge(&(spread_price)) {
price = spread_price;
ask_amount = Uint128::from(offer_amount * Decimal::one().atomics())
.checked_div(price.atomics())
.unwrap_or_default();
return Err(ContractError::PriceNotGreaterThan {
price: spread_price,
});
}
}
OrderDirection::Sell => {
let mut price = Decimal::from_ratio(ask_amount, offer_amount);
let price = Decimal::from_ratio(ask_amount, offer_amount);
let spread_price = highest_buy_price * buy_spread_factor;
if spread_price.is_zero() {
return Err(ContractError::PriceMustNotBeZero {
price: spread_price,
});
}
if spread_price.ge(&price) {
price = spread_price;
ask_amount = Uint128::from(offer_amount * price);
return Err(ContractError::PriceNotLessThan {
price: spread_price,
});
}
}
};
Expand Down Expand Up @@ -276,6 +276,8 @@ fn to_events(order: &OrderWithFee, human_bidder: String) -> Event {
attr("filled_ask_amount", order.filled_ask_amount.to_string()),
attr("reward_fee", order.reward_fee),
attr("relayer_fee", order.relayer_fee),
attr("filled_offer_this_round", order.filled_offer_this_round),
attr("filled_ask_this_round", order.filled_ask_this_round),
]
.to_vec();
Event::new("matched_order").add_attributes(attrs)
Expand Down
7 changes: 7 additions & 0 deletions contracts/oraiswap_limit_order/src/orderbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub struct OrderWithFee {
pub filled_ask_amount: Uint128,
pub reward_fee: Uint128,
pub relayer_fee: Uint128,
pub filled_offer_this_round: Uint128,
pub filled_ask_this_round: Uint128,
}

#[cw_serde]
Expand Down Expand Up @@ -144,6 +146,9 @@ impl OrderWithFee {
self.filled_ask_amount += ask_amount;
self.filled_offer_amount += offer_amount;

self.filled_ask_this_round = ask_amount;
self.filled_offer_this_round = offer_amount;

if self.offer_amount.checked_sub(self.filled_offer_amount)? < MIN_VOLUME.into()
|| self.ask_amount.checked_sub(self.filled_ask_amount)? < MIN_VOLUME.into()
{
Expand Down Expand Up @@ -550,6 +555,8 @@ impl BulkOrders {
filled_ask_amount: order.filled_ask_amount,
relayer_fee: Uint128::zero(),
reward_fee: Uint128::zero(),
filled_ask_this_round: Uint128::zero(),
filled_offer_this_round: Uint128::zero(),
})
.collect(),
volume,
Expand Down
Loading

0 comments on commit 1b5dd26

Please sign in to comment.