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

Merged
merged 5 commits 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 @@ -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
Loading