From 419051ffe85defebd5374a7f3b4a5d3faa100450 Mon Sep 17 00:00:00 2001 From: Chris Czub Date: Mon, 29 Apr 2024 16:12:28 -0400 Subject: [PATCH] Add test for and fix arbitrage swap execution output amount --- .../core/component/dex/src/component/arb.rs | 2 +- .../core/component/dex/src/component/tests.rs | 52 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/crates/core/component/dex/src/component/arb.rs b/crates/core/component/dex/src/component/arb.rs index b802ca2279..8017d348ca 100644 --- a/crates/core/component/dex/src/component/arb.rs +++ b/crates/core/component/dex/src/component/arb.rs @@ -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, }, }; diff --git a/crates/core/component/dex/src/component/tests.rs b/crates/core/component/dex/src/component/tests.rs index cfebe53716..5ef251cff3 100644 --- a/crates/core/component/dex/src/component/tests.rs +++ b/crates/core/component/dex/src/component/tests.rs @@ -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?; @@ -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(()) }