Skip to content

Commit

Permalink
Add test for and fix arbitrage swap execution output amount
Browse files Browse the repository at this point in the history
  • Loading branch information
zbuc committed Apr 29, 2024
1 parent c84338f commit 0a6c792
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/core/component/dex/src/component/arb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub trait Arbitrage: StateWrite + Sized {
amount: filled_input,
},
output: Value {
amount: arb_profit,
amount: filled_input + arb_profit,
asset_id: arb_token,
},
};
Expand Down
44 changes: 44 additions & 0 deletions crates/core/component/dex/src/component/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,9 @@ async fn basic_cycle_arb() -> anyhow::Result<()> {
/// The issue was that we did not treat the spill price as a strict
/// upper bound, which is necessary to ensure that the arbitrage logic
/// terminates.
///
/// This test also ensures that the created `SwapExecution` has the
/// correct data.
async fn reproduce_arbitrage_loop_testnet_53() -> anyhow::Result<()> {
let _ = tracing_subscriber::fmt::try_init();
let storage = TempStorage::new().await?.apply_minimal_genesis().await?;
Expand Down Expand Up @@ -953,5 +956,46 @@ async fn reproduce_arbitrage_loop_testnet_53() -> anyhow::Result<()> {
tracing::info!("fetching the `ArbExecution`");
let arb_execution = state.arb_execution(0).await?.expect("arb was performed");
tracing::info!(?arb_execution, "fetched arb execution!");

// Validate that the arb execution has the correct data:
// Validate the traces.
assert_eq!(
arb_execution.traces,
vec![
vec![
penumbra.value(1u32.into()),
test_usd.value(110u32.into()),
Value {
amount: 1099999u64.into(),
asset_id: penumbra.id()
}
],
vec![
penumbra.value(1u32.into()),
test_usd.value(100u32.into()),
Value {
amount: 999999u64.into(),
asset_id: penumbra.id()
}
]
]
);

// Validate the input/output of the arb execution:
assert_eq!(
arb_execution.input,
Value {
amount: 2000000u64.into(),
asset_id: penumbra.id(),
}
);
assert_eq!(
arb_execution.output,
Value {
amount: 2099998u64.into(),
asset_id: penumbra.id(),
}
);

Ok(())
}

0 comments on commit 0a6c792

Please sign in to comment.