diff --git a/state-chain/pallets/cf-swapping/src/lib.rs b/state-chain/pallets/cf-swapping/src/lib.rs index b9bb4fc6b8..3c9f3d4cf7 100644 --- a/state-chain/pallets/cf-swapping/src/lib.rs +++ b/state-chain/pallets/cf-swapping/src/lib.rs @@ -746,6 +746,10 @@ pub mod pallet { ); let stable_amount = swap.stable_amount.get_or_insert_with(Default::default); *stable_amount = T::SwappingApi::take_network_fee(*stable_amount); + + if swap.to == STABLE_ASSET { + swap.final_output = Some(*stable_amount); + } } Ok(()) diff --git a/state-chain/pallets/cf-swapping/src/tests.rs b/state-chain/pallets/cf-swapping/src/tests.rs index 2b3ede27e9..782ff9d84d 100644 --- a/state-chain/pallets/cf-swapping/src/tests.rs +++ b/state-chain/pallets/cf-swapping/src/tests.rs @@ -27,6 +27,7 @@ use frame_support::{ use itertools::Itertools; use sp_arithmetic::Permill; use sp_core::H160; +use sp_runtime::Percent; use sp_std::iter; const GAS_BUDGET: AssetAmount = 1_000u128; @@ -2244,3 +2245,42 @@ fn network_fee_swap_gets_burnt() { assert_eq!(FlipToBurn::::get(), AMOUNT); }); } + +#[test] +fn swap_output_amounts_correctly_account_for_fees() { + for (from, to) in + // non-stable to non-stable, non-stable to stable, stable to non-stable + [(Asset::Btc, Asset::Eth), (Asset::Btc, Asset::Usdc), (Asset::Usdc, Asset::Eth)] + { + new_test_ext().execute_with(|| { + const SWAPPED_AMOUNT: AssetAmount = 1000; + + let network_fee = Percent::from_percent(1); + NetworkFee::set(network_fee); + + let expected_output: AssetAmount = + (SWAPPED_AMOUNT as u32 - (network_fee * SWAPPED_AMOUNT as u32)).into(); + + { + Swapping::schedule_swap( + from, + to, + SWAPPED_AMOUNT, + SwapType::Swap(ForeignChainAddress::Eth(H160::zero())), + ); + + Swapping::on_finalize(System::block_number() + SWAP_DELAY_BLOCKS as u64); + + assert_eq!( + MockEgressHandler::::get_scheduled_egresses(), + vec![MockEgressParameter::Swap { + asset: to, + amount: expected_output, + fee: 0, + destination_address: ForeignChainAddress::Eth(H160::zero()), + },] + ); + } + }); + } +} diff --git a/state-chain/runtime/src/lib.rs b/state-chain/runtime/src/lib.rs index ceb1c9cb98..7034352e36 100644 --- a/state-chain/runtime/src/lib.rs +++ b/state-chain/runtime/src/lib.rs @@ -163,7 +163,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("chainflip-node"), impl_name: create_runtime_str!("chainflip-node"), authoring_version: 1, - spec_version: 136, + spec_version: 137, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 12,