Skip to content

Commit

Permalink
Update global matching (#311)
Browse files Browse the repository at this point in the history
* Update global matching

* fmt
  • Loading branch information
brittcyr authored Dec 4, 2024
1 parent f8f4e53 commit 1543575
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
6 changes: 4 additions & 2 deletions programs/manifest/src/state/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -983,13 +983,15 @@ impl<
break;
}
}
// When is_bid, the taker is supplying quote, so the global
// maker needs to supply base.
let has_enough_tokens: bool = try_to_move_global_tokens(
global_trade_accounts_opt,
&maker,
GlobalAtoms::new(if is_bid {
quote_atoms_traded.as_u64()
} else {
base_atoms_traded.as_u64()
} else {
quote_atoms_traded.as_u64()
}),
)?;

Expand Down
46 changes: 37 additions & 9 deletions programs/manifest/tests/cases/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,22 +226,29 @@ async fn global_match_order() -> anyhow::Result<()> {
let mut test_fixture: TestFixture = TestFixture::new().await;
test_fixture.claim_seat().await?;

test_fixture.global_add_trader().await?;
test_fixture.global_deposit(1_000_000).await?;
test_fixture
.claim_seat_for_keypair(&test_fixture.second_keypair.insecure_clone())
.await?;
test_fixture
.global_add_trader_for_keypair(&test_fixture.second_keypair.insecure_clone())
.await?;
test_fixture
.global_deposit_for_keypair(&test_fixture.second_keypair.insecure_clone(), 1_000_000)
.await?;

test_fixture
.batch_update_with_global_for_keypair(
None,
vec![],
vec![PlaceOrderParams::new(
100,
1,
0,
11,
-1,
true,
OrderType::Global,
NO_EXPIRATION_LAST_VALID_SLOT,
)],
&test_fixture.payer_keypair().insecure_clone(),
&test_fixture.second_keypair.insecure_clone(),
)
.await?;

Expand All @@ -267,27 +274,48 @@ async fn global_match_order() -> anyhow::Result<()> {
let orders: Vec<RestingOrder> = test_fixture.market_fixture.get_resting_orders().await;
assert_eq!(orders.len(), 0, "Order still on orderbook");

// Global buys 100 base for 110 quote
// Local sells 100 base for 90 quote

// Match will leave global: 0 quote, 100 base
// match will leave local: 110 quote, 1_000_000 - 100 base

assert_eq!(
test_fixture
.market_fixture
.get_base_balance_atoms(&test_fixture.second_keypair.pubkey())
.await,
100
);
assert_eq!(
test_fixture
.market_fixture
.get_quote_balance_atoms(&test_fixture.second_keypair.pubkey())
.await,
0
);
assert_eq!(
test_fixture
.market_fixture
.get_base_balance_atoms(&test_fixture.payer())
.await,
1_000_000
1_000_000 - 100
);
assert_eq!(
test_fixture
.market_fixture
.get_quote_balance_atoms(&test_fixture.payer())
.await,
100
110
);
test_fixture.global_fixture.reload().await;
assert_eq!(
test_fixture
.global_fixture
.global
.get_balance_atoms(&test_fixture.payer()),
999_900
.get_balance_atoms(&test_fixture.second_keypair.insecure_clone().pubkey())
.as_u64(),
1_000_000 - 110
);

Ok(())
Expand Down

0 comments on commit 1543575

Please sign in to comment.