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 419051f
Show file tree
Hide file tree
Showing 2 changed files with 53 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
52 changes: 52 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,54 @@ 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.
// left: [
// [Value { amount: 1000000, asset_id: passet1984fctenw8m2fpl8a9wzguzp7j34d7vravryuhft808nyt9fdggqxmanqm },
// Value { amount: 110000000, asset_id: passet14h46dmcyy6fl5vyz7xx933n8l7jy202ahrxg92kad6yfladsvcyqczcwda },
// Value { amount: 1099999, asset_id: passet1984fctenw8m2fpl8a9wzguzp7j34d7vravryuhft808nyt9fdggqxmanqm }
// ],
// [Value { amount: 1000000, asset_id: passet1984fctenw8m2fpl8a9wzguzp7j34d7vravryuhft808nyt9fdggqxmanqm },
// Value { amount: 100000000, asset_id: passet14h46dmcyy6fl5vyz7xx933n8l7jy202ahrxg92kad6yfladsvcyqczcwda },
// Value { amount: 999999, asset_id: passet1984fctenw8m2fpl8a9wzguzp7j34d7vravryuhft808nyt9fdggqxmanqm }]]
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 419051f

Please sign in to comment.