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

Pull from Master #2

Merged
merged 4 commits into from
Aug 14, 2022
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
4 changes: 2 additions & 2 deletions js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bonfida/aaob",
"version": "0.1.2",
"version": "0.1.3",
"license": "MIT",
"repository": {
"type": "git"
Expand Down Expand Up @@ -49,4 +49,4 @@
"borsh": "^0.6.0",
"bs58": "4.0.1"
}
}
}
10 changes: 5 additions & 5 deletions program/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ num-traits = "0.2"
num-derive = "0.3"
enumflags2 = "0.7.1"
spl-token = {version="3.2.0", features= ["no-entrypoint"]}
bonfida-utils = "0.2.11"
bonfida-utils = {git = "https://github.com/Bonfida/bonfida-utils.git", branch = "add-rounding-direction"}

[dev-dependencies]
hexdump = "0.1.0"
Expand Down
6 changes: 3 additions & 3 deletions program/src/processor/cancel_order.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Cancel an existing order in the orderbook.

use bonfida_utils::fp_math::fp32_mul;
use bonfida_utils::fp_math::fp32_mul_floor;
use bonfida_utils::{BorshSize, InstructionsAccount};
use borsh::{BorshDeserialize, BorshSerialize};
use bytemuck::Pod;
Expand Down Expand Up @@ -97,8 +97,8 @@ where
.remove_by_key(params.order_id)
.ok_or(AoError::OrderNotFound)?;
let total_base_qty = leaf_node.base_quantity;
let total_quote_qty =
fp32_mul(leaf_node.base_quantity, leaf_node.price()).ok_or(AoError::NumericalOverflow)?;
let total_quote_qty = fp32_mul_floor(leaf_node.base_quantity, leaf_node.price())
.ok_or(AoError::NumericalOverflow)?;

let order_summary = OrderSummary {
posted_order_id: None,
Expand Down
4 changes: 2 additions & 2 deletions program/src/processor/mass_cancel_orders.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Cancel a series of existing orders in the orderbook.

use bonfida_utils::{fp_math::fp32_mul, BorshSize, InstructionsAccount};
use bonfida_utils::{fp_math::fp32_mul_floor, BorshSize, InstructionsAccount};
use borsh::{BorshDeserialize, BorshSerialize};
use bytemuck::Pod;
use solana_program::{
Expand Down Expand Up @@ -101,7 +101,7 @@ where
let slab = order_book.get_tree(get_side_from_order_id(order_id));
let (leaf_node, _) = slab.remove_by_key(order_id).ok_or(AoError::OrderNotFound)?;
total_base_qty = total_base_qty.checked_add(leaf_node.base_quantity).unwrap();
total_quote_qty = fp32_mul(leaf_node.base_quantity, leaf_node.price())
total_quote_qty = fp32_mul_floor(leaf_node.base_quantity, leaf_node.price())
.and_then(|n| n.checked_add(total_quote_qty))
.unwrap();
}
Expand Down
7 changes: 7 additions & 0 deletions program/src/processor/new_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ where
return Err(AoError::InvalidLimitPrice.into());
}

if params.post_allowed && params.limit_price < market_state.tick_size {
msg!(
"Can't attempt to post an order of price less than market tick size to the orderbook!"
);
return Err(AoError::InvalidLimitPrice.into());
}

let mut bids_guard = accounts.bids.data.borrow_mut();
let mut asks_guard = accounts.asks.data.borrow_mut();

Expand Down
17 changes: 12 additions & 5 deletions program/src/state/orderbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
AccountTag, SelfTradeBehavior, Side,
},
};
use bonfida_utils::fp_math::{fp32_div, fp32_mul};
use bonfida_utils::fp_math::{fp32_div, fp32_mul_ceil, fp32_mul_floor};
use borsh::{BorshDeserialize, BorshSerialize};
use bytemuck::Pod;
use solana_program::{msg, program_error::ProgramError};
Expand Down Expand Up @@ -169,8 +169,12 @@ where
break;
}

let quote_maker_qty =
fp32_mul(base_trade_qty, trade_price).ok_or(AoError::NumericalOverflow)?;
let quote_maker_qty = match side {
Side::Bid => fp32_mul_ceil(base_trade_qty, trade_price),
Side::Ask => fp32_mul_floor(base_trade_qty, trade_price),
}
.map(|q| std::cmp::min(q, quote_qty_remaining))
.ok_or(AoError::NumericalOverflow)?;

if quote_maker_qty == 0 {
break;
Expand Down Expand Up @@ -312,8 +316,11 @@ where
};
*self.get_tree(side).get_callback_info_mut(k) = callback_info;
base_qty_remaining -= base_qty_to_post;
quote_qty_remaining -=
fp32_mul(base_qty_to_post, limit_price).ok_or(AoError::NumericalOverflow)?;
quote_qty_remaining -= match side {
Side::Bid => fp32_mul_ceil(base_qty_to_post, limit_price),
Side::Ask => fp32_mul_floor(base_qty_to_post, limit_price),
}
.ok_or(AoError::NumericalOverflow)?;
Ok(OrderSummary {
posted_order_id: Some(new_leaf_order_id),
total_base_qty: max_base_qty - base_qty_remaining,
Expand Down