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

feat: use swapping queue when swapping network fee for burn #4584

Merged
merged 8 commits into from
Mar 7, 2024

Conversation

j4m1ef0rd
Copy link
Contributor

@j4m1ef0rd j4m1ef0rd commented Feb 29, 2024

Pull Request

Closes: PRO-1214

Checklist

Please conduct a thorough self-review before opening the PR.

  • I am confident that the code works.
  • I have updated documentation where appropriate.

Summary

  • Created a SwapQueueApi so the pools pallet can add the swap to the queue
  • Created a new swap type that is only used for this network fee swap.
  • Moved the FlipToBurn storage item to the swapping pallet.
    • Added migration for this and tested it against perseverance.

@j4m1ef0rd j4m1ef0rd requested review from msgmaxim and kylezs February 29, 2024 06:00
@j4m1ef0rd j4m1ef0rd self-assigned this Feb 29, 2024
@j4m1ef0rd j4m1ef0rd requested a review from dandanlen as a code owner February 29, 2024 06:00
Copy link

codecov bot commented Feb 29, 2024

Codecov Report

Attention: Patch coverage is 69.23077% with 36 lines in your changes are missing coverage. Please review.

Project coverage is 72%. Comparing base (4540356) to head (9f1cdd0).
Report is 1 commits behind head on main.

Files Patch % Lines
state-chain/runtime/src/lib.rs 0% 21 Missing ⚠️
state-chain/traits/src/liquidity.rs 8% 8 Missing and 4 partials ⚠️
state-chain/pallets/cf-swapping/src/lib.rs 94% 3 Missing ⚠️
Additional details and impacted files
@@          Coverage Diff           @@
##            main   #4584    +/-   ##
======================================
- Coverage     73%     72%    -0%     
======================================
  Files        402     402            
  Lines      67087   66987   -100     
  Branches   67087   66987   -100     
======================================
- Hits       48664   48493   -171     
- Misses     16083   16157    +74     
+ Partials    2340    2337     -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

amount: AssetAmount,
swap_type: SwapType,
) -> (u64, Self::BlockNumber) {
Self::schedule_swap_internal(from, to, amount, swap_type)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to do the MaximumSwapAmount checks for a network fee swap

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we add it to the SwapQueue and use Self::swap_into_stable_taking_network_fee(&mut swaps)?; which is what's happening now, then we'll be taking a network fee for the network fee swap. which we don't want either.
We should add a test for this case - after the network swap goes through then we should have nothing left to swap in the Network fee storage

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if it makes sense to have a second queue here. What matters isn't that it's in the SwapQueue specifically - but that it is queued by the delay and it shows in the subscription. I think this is probably the cleaner solution than trying to retrofit this into the existing flow which takes network fees etc. Also means we get the ability to emit a separate event for the network swap (if we wanted to).

CC: @dandanlen ^

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we really care that much if the network fee swap also pays a fee. Adding another queue raises more questions than it answers (do bundle it with other swaps? why/why not? what about delays, slippage limits etc...)

Much simpler to treat it like a normal swap - notify the LPs, bundle it with everything else, recover like everything else.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I guess it's not a big deal

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we really care about this, we can address it in the swap bundling / unbundling logic. Fee Swaps are always USDC->FLIP, so they happen on the second leg. We could just add the network fee swap to the bundle after we take the network fee.

This would be less invasive than adding a whole new queue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it not as simple as just checking swap_type in swap_into_stable_taking_network_fee to see if the fee should be taken? Something like:

fn swap_into_stable_taking_network_fee(
	swaps: &mut Vec<Swap>,
) -> Result<(), BatchExecutionError> {
	Self::do_group_and_swap(swaps, SwapLeg::ToStable)?;

	// Take NetworkFee for all swaps
	for swap in swaps.iter_mut() {
                 // ***** NEW CODE BEGINS *****
		if swap.swap_type == SwapType::NetworkFee {
			// Don't take network fee for network fee swaps
			continue;
		}
                 // ***** NEW CODE END *****
		debug_assert!(
			swap.stable_amount.is_some(),
			"All swaps should have Stable amount set here"
		);
		let stable_amount = swap.stable_amount.get_or_insert_with(Default::default);
		*stable_amount = T::SwappingApi::take_network_fee(*stable_amount);
	}

	Ok(())
}

Comment on lines 1058 to 1062
fn add_flip_to_burn(amount: AssetAmount) {
FlipToBurn::<T>::mutate(|total| {
total.saturating_accrue(amount);
});
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be easier/cleaner to just move the FLipToBurn storage into the Swapping pallet rather than extend the SwappingApi with this arbitrary add_flip_to_burn method.

Copy link
Collaborator

@dandanlen dandanlen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine to me, will leave final approval to @kylezs / @msgmaxim

FWIW (and this is out of scope of this PR) I think we should consider moving all of the network fee stuff into the swapping pallet.

We now seem to have two redundant interfaces:

  • SwapQueueApi: only used for network fee swaps?
  • SwappingApi::take_network_fee: used to calculate the network fee.

If we make it the responsibility of the swapping pallet to trigger the fee swaps, we can remove both of the above: the swapping pallet can calculate the USDC fee, store it, and then schedule a swap (or even schedule an immediate swap every time we process a batch).

It's not quite trivial because we have swap_with_network_fee which is used in some places for price estimation, and this would need a bit of a refactor. But IMO is the right direction in the longer term. Taking the network fee is something we do for swaps, it should be in the swapping pallet. It's not really a concern of the pools. (@AlastairHolmes maybe you have an opinion here)

@kylezs kylezs enabled auto-merge (squash) March 7, 2024 08:19
@kylezs kylezs merged commit 8f3b9c7 into main Mar 7, 2024
43 checks passed
@kylezs kylezs deleted the feat/network-fee-swaps-use-queue branch March 7, 2024 08:19
syan095 added a commit that referenced this pull request Mar 10, 2024
…ncoding

* origin/main:
  fix "get_proposed_runtime" command (#4624)
  fix: allow OldAsset to support unambiguously encoding Arb USDC and Eth USDC, while maintaining backcompat (PRO-1237) (#4614)
  fix: start localnet using correct commit (#4623)
  chore: migration housekeeping (#4612)
  feat: add arbitrum support (PRO-1154) (#4486)
  fix: swap subscription amounts as hex (#4611)
  feat: use swapping queue when swapping network fee for burn (#4584)

# Conflicts:
#	state-chain/custom-rpc/src/lib.rs
syan095 added a commit that referenced this pull request Mar 10, 2024
…utxo

* origin/main:
  fix "get_proposed_runtime" command (#4624)
  fix: allow OldAsset to support unambiguously encoding Arb USDC and Eth USDC, while maintaining backcompat (PRO-1237) (#4614)
  fix: start localnet using correct commit (#4623)
  chore: migration housekeeping (#4612)
  feat: add arbitrum support (PRO-1154) (#4486)
  fix: swap subscription amounts as hex (#4611)
  feat: use swapping queue when swapping network fee for burn (#4584)
  chore: update chainspecs (#4615)
  feat: Add channel opening fee to *DepositAddressReady Events (#4609)
  refactor: pass out CFE incompatibility exit information to main (#4563)
  feat: Introduce tx fee multiplier storage item (#4594)
  fix: debug cli  (#4605)
  fix: patch 1.2 broker test (#4607)
  feat: add block height and deposit details to PrewitnessedDeposit (#4606)
  chore: add myself as codeowner to upgrade-test (#4603)
  fix: RUSTSEC-2024-0019 (#4604)

# Conflicts:
#	state-chain/pallets/cf-environment/src/lib.rs
dandanlen pushed a commit that referenced this pull request Mar 22, 2024
* feat: use swapping queue when burning network fee

* refactor: address PR comments

* fix: try-runtime and fix migration

* docs: add comment about pre-requisites

* fix: incorrect alias for migration

* chore: spec version to 130

---------

Co-authored-by: kylezs <zsembery.kyle@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants