Skip to content

Commit

Permalink
fix: take fee on to usdc (#4801)
Browse files Browse the repository at this point in the history
* fix: take network fee from output amount when swapping to stable

* chore: set value directly

* chore: bump runtime version

* chore: clippy

* chore: cleaner
  • Loading branch information
kylezs committed Apr 26, 2024
1 parent 340cf13 commit 9efaaf4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
4 changes: 4 additions & 0 deletions state-chain/pallets/cf-swapping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
40 changes: 40 additions & 0 deletions state-chain/pallets/cf-swapping/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2244,3 +2245,42 @@ fn network_fee_swap_gets_burnt() {
assert_eq!(FlipToBurn::<Test>::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::<AnyChain>::get_scheduled_egresses(),
vec![MockEgressParameter::Swap {
asset: to,
amount: expected_output,
fee: 0,
destination_address: ForeignChainAddress::Eth(H160::zero()),
},]
);
}
});
}
}
2 changes: 1 addition & 1 deletion state-chain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 9efaaf4

Please sign in to comment.