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: on chain swaps #5595

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

feat: on chain swaps #5595

wants to merge 9 commits into from

Conversation

msgmaxim
Copy link
Contributor

@msgmaxim msgmaxim commented Jan 30, 2025

PRO-1998

Summary

  • Added extrinsic to the swapping pallet to initiate a swap with free balance (do we need to restrict it to account role == LP?)
  • Updated SwapRequestType (part of SwapRequests storage) where I replaced the output address with SwapOutputAction that has two variants (Egress - for "traditional" swaps, and CreditOnChain - for on chain swaps). Added migration for this.
  • Extended refund parameters so that the destination can also now be an on-chain account. The externally facing refund parameters struct remains unchanged (since we want to be able to reject/refund any ingress, and so internal account is not allowed as a refund destination).

Breaking changes

  • Changes to SwapRequested event:
    • request_type in the Regular variant now can contain account_id instead of output_address.
    • refund_params now contain refund_desitnation (instead of refund_address), which can be either an address (as before) or an account_id (in case of an internal swap).

Discussion

Do we need new events for when an account is credited as a result of a swap?
- Currently we are expecting the following sequence of events:

`SwapRequested` → `SwapScheduled` → `SwapExecuted` → `SwapEgressScheduled` (or `RefundEgressScheduled`) → `SwapRequestCompleted` 

Presumably we want new events for internal swaps to match SwapEgressScheduled and RefundEgressScheduled.

@msgmaxim msgmaxim force-pushed the feat/on-chain-swaps branch from 8d4b190 to a876c17 Compare January 31, 2025 02:36
@msgmaxim msgmaxim changed the title WIP: on chain swaps feat: on chain swaps Jan 31, 2025
@@ -89,57 +89,6 @@ impl<T: Config> UncheckedOnRuntimeUpgrade for Migration<T> {
}

fn on_runtime_upgrade() -> Weight {
crate::SwapRequests::<T>::translate_values::<old::SwapRequest<T>, _>(|old_swap_requests| {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need to branch 1.8 and remove this migration (unless we want this PR in 1.8, in which case I can fix this migration properly).

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.

I think, for now at least, we probably do need to restrict this to LPs - LPs are likely to be the only parties with enough funds anyway.

state-chain/chains/src/lib.rs Show resolved Hide resolved
state-chain/pallets/cf-swapping/src/lib.rs Outdated Show resolved Hide resolved
state-chain/pallets/cf-swapping/src/lib.rs Outdated Show resolved Hide resolved
);
},
SwapOutputAction::CreditOnChain { account_id } => {
// TODO: a new event for when LP is credited after a swap?
Copy link
Collaborator

Choose a reason for hiding this comment

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

An event would make sense... we have an event for SwapExecuted, why not for SwapFailed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added CreditedOnChain and RefundedOnChain events.

We could also add SwapFailed though this isn't relevant to this PR and maybe should be discussed a little first? (e.g. do we emit one alongside every SwapRescheduled or only when finally refunding?)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes I agree, not part of this, PR, was just curious as to why we don't already have it - if we have an event for when a swap request succeeds, why not for when one fails? Did we just forget this or was there a reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did we just forget this or was there a reason?

No reason IIRC other than needing some discussion. Because the frontend can use egress/refund events for this purpose, this hasn't been a priority. We should do it.

@msgmaxim msgmaxim force-pushed the feat/on-chain-swaps branch from 482c012 to 8154860 Compare February 3, 2025 04:35
@msgmaxim msgmaxim marked this pull request as ready for review February 3, 2025 04:37
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.

Now that the extrinsic is lp-specific, I would suggest refactoring such internal_swap is an interface method on SwapRequestHandler, and moving the extrinsic, account check, refund address check and sweep to the lp pallet.

Then the lp pallet can depend on the swapping interface rather than the swapping pallet depending on the lp and pools interfaces. This is more in line with how things are currently structured, with lp-specific calls and checks in the lp pallet. The swapping pallet has accumulated enough responsibilities already.

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.

Btw. I noticed we already have a type AccountOrAddress in the lp pallet, it's very similar to RefundDestination, would be good to merge/deduplicate these.

@msgmaxim msgmaxim force-pushed the feat/on-chain-swaps branch from 4fffb16 to 2fab474 Compare February 12, 2025 01:50
@msgmaxim msgmaxim force-pushed the feat/on-chain-swaps branch from 2fab474 to d464d68 Compare February 12, 2025 01:50
/// zero becomes more valuable relative to asset one the price's literal value goes up, and vice
/// versa. This ratio is represented as a fixed point number with `PRICE_FRACTIONAL_BITS` fractional
/// bits.
pub type Price = U256;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I've mentioned this many times - i'm not a fan of exposing a type alias like this as a 'primitive' when it's loaded with so much meaning...

I guess this is better than duplicating the definition though, so 🤷

Created PRO-2044 to address this once and for all.

min_price,
}),
dca_params,
SwapOrigin::OnChainAccount(account_id),
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: It might have been a bit cleaner to define a trait method like internal_swap_request(..), then all these types like SwapOrigin, SwapRequestType, SwapOutputAction and RefundDestination (even RefundParametersExtended?) could remain internal to the swapping pallet rather than leaking them in the public interface. And we would not have to pass empty broker fees (which makes little sense for an internal swap anyway).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants