diff --git a/programs/manifest/src/state/market.rs b/programs/manifest/src/state/market.rs index 0666d5ac5..5adf0c644 100644 --- a/programs/manifest/src/state/market.rs +++ b/programs/manifest/src/state/market.rs @@ -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() }), )?; diff --git a/programs/manifest/tests/cases/global.rs b/programs/manifest/tests/cases/global.rs index 4d920403d..2ab875ee9 100644 --- a/programs/manifest/tests/cases/global.rs +++ b/programs/manifest/tests/cases/global.rs @@ -226,8 +226,15 @@ 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( @@ -235,13 +242,13 @@ async fn global_match_order() -> anyhow::Result<()> { 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?; @@ -267,27 +274,48 @@ async fn global_match_order() -> anyhow::Result<()> { let orders: Vec = 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(())