Skip to content

Commit

Permalink
Fix rounding on cancel (#310)
Browse files Browse the repository at this point in the history
* Fix on cancel rounding

* add back rule

* fmt
  • Loading branch information
brittcyr authored Dec 4, 2024
1 parent a65b752 commit f8f4e53
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
6 changes: 6 additions & 0 deletions programs/manifest/rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@
"prover_options": [],
"cargo_features": []
},
{
"name": "rule_cancel_order_by_index_bid",
"expected_result": "Verified",
"prover_options": [],
"cargo_features": []
},
{
"name": "rule_cancel_order_by_index_ask",
"expected_result": "Verified",
Expand Down
5 changes: 4 additions & 1 deletion programs/manifest/src/state/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1362,10 +1362,13 @@ impl<

let resting_order: &RestingOrder = get_helper_order(dynamic, order_index).get_value();
let is_bid: bool = resting_order.get_is_bid();

// Important to round up because there was an extra atom taken for full
// taker rounding when the order was placed.
let amount_atoms: u64 = if is_bid {
(resting_order
.get_price()
.checked_quote_for_base(resting_order.get_num_base_atoms(), false)
.checked_quote_for_base(resting_order.get_num_base_atoms(), true)
.unwrap())
.into()
} else {
Expand Down
22 changes: 22 additions & 0 deletions programs/manifest/tests/cases/cancel_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,28 @@ async fn cancel_order_bid_test() -> anyhow::Result<()> {
Ok(())
}

// This test failed when the rounding on cancel was incorrect. Now it passes. It
// shows that all funds are retrievable.
#[tokio::test]
async fn cancel_order_rounding_test() -> anyhow::Result<()> {
let mut test_fixture: TestFixture = TestFixture::new().await;
test_fixture.claim_seat().await?;
test_fixture
.deposit(Token::USDC, 2 * USDC_UNIT_SIZE)
.await?;

test_fixture
.place_order(Side::Bid, 1, 11, -1, u32::MAX, OrderType::Limit)
.await?;

test_fixture.cancel_order(0).await?;
test_fixture
.withdraw(Token::USDC, 2 * USDC_UNIT_SIZE)
.await?;

Ok(())
}

#[tokio::test]
async fn cancel_order_fail_other_trader_order_test() -> anyhow::Result<()> {
let mut test_fixture: TestFixture = TestFixture::new().await;
Expand Down

0 comments on commit f8f4e53

Please sign in to comment.