-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b3fa0a5
commit c69ba97
Showing
3 changed files
with
73 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ use penumbra_num::Amount; | |
use rand_core::OsRng; | ||
|
||
use crate::lp::action::PositionOpen; | ||
use crate::lp::{position, SellOrder}; | ||
use crate::DexParameters; | ||
use crate::{ | ||
component::{ | ||
|
@@ -257,6 +258,59 @@ async fn single_limit_order() -> anyhow::Result<()> { | |
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
/// Builds a simple order book with a two orders, fills against them both, | ||
/// and checks that one of the orders is auto-closed. | ||
async fn check_close_on_fill() -> anyhow::Result<()> { | ||
let storage = TempStorage::new().await?.apply_minimal_genesis().await?; | ||
let mut state = Arc::new(StateDelta::new(storage.latest_snapshot())); | ||
let mut state_tx = state.try_begin_transaction().unwrap(); | ||
|
||
let gm = asset::Cache::with_known_assets().get_unit("gm").unwrap(); | ||
|
||
let mut position_1 = SellOrder::parse_str("100gm@1gn")?.into_position(OsRng); | ||
position_1.close_on_fill = true; | ||
let position_2 = SellOrder::parse_str("[email protected]")?.into_position(OsRng); | ||
|
||
let position_1_id = position_1.id(); | ||
let position_2_id = position_2.id(); | ||
|
||
state_tx.open_position(position_1.clone()).await.unwrap(); | ||
state_tx.open_position(position_2.clone()).await.unwrap(); | ||
|
||
// Now we have the following liquidity: | ||
// | ||
// 100gm@1gn (auto-closing) | ||
// [email protected] | ||
// | ||
// We therefore expect that trading 100gn + 110gn will exhaust both positions. | ||
// Attempting to trade a bit more than that ensures we completely fill both, | ||
// without worrying about rounding. | ||
// Because we're just testing the DEX internals, we need to trigger fill_route manually. | ||
let input = "220gn".parse::<Value>().unwrap(); | ||
let route = [gm.id()]; | ||
let execution = FillRoute::fill_route(&mut state_tx, input, &route, None).await?; | ||
|
||
let unfilled = input.amount.checked_sub(&execution.input.amount).unwrap(); | ||
|
||
// Check that we got the execution we expected. | ||
assert_eq!(unfilled, "10gn".parse::<Value>().unwrap().amount); | ||
assert_eq!(execution.output, "200gm".parse::<Value>().unwrap()); | ||
|
||
// Now grab both position states: | ||
let position_1_post_exec = state_tx.position_by_id(&position_1_id).await?.unwrap(); | ||
let position_2_post_exec = state_tx.position_by_id(&position_2_id).await?.unwrap(); | ||
|
||
dbg!(&position_1_post_exec); | ||
dbg!(&position_2_post_exec); | ||
|
||
// Check that position 1 was auto-closed but position 2 wasn't: | ||
assert_eq!(position_1_post_exec.state, position::State::Closed); | ||
assert_eq!(position_2_post_exec.state, position::State::Opened); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
/// Try to execute against multiple positions, mainly testing that the order-book traversal | ||
/// is done correctly. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters