Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: take fee on to usdc (#4801) #4804

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -769,6 +769,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 @@ -28,6 +28,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 @@ -2302,3 +2303,42 @@ fn register_and_deregister_account() {
.expect_err("ALICE should be deregistered.");
});
}

#[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()),
},]
);
}
});
}
}
Loading