Skip to content

feat: oracle swaps#6039

Merged
j4m1ef0rd merged 8 commits intomainfrom
feat/oracle-swaps-jamie
Aug 15, 2025
Merged

feat: oracle swaps#6039
j4m1ef0rd merged 8 commits intomainfrom
feat/oracle-swaps-jamie

Conversation

@j4m1ef0rd
Copy link
Contributor

@j4m1ef0rd j4m1ef0rd commented Aug 7, 2025

Pull Request

Closes: PRO-2331

Checklist

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

  • I am confident that the code works.
  • I have written sufficient tests.
  • I have written and tested required migrations.
  • I have updated documentation where appropriate.

Summary

  • Replaced min_price param with a new SwapLimits struct that has the new oracle price field.
  • Added logic to the swapping code to check the oracle price and give a price violation if the output of the swap is not enough.
  • Moved some of the. oracle price feed api stuff.
  • Added support to the btc vault swap to take a u8 for the new oracle price slippage bps.
    • Used u8::MAX as a special value to set None
    • Added a new version of the data struct to allow for backwards compatibility
  • Added 2 tests for the new oracle price protection

TODO: Need some input from @dandanlen

  • Possible breaking change to request_swap_deposit_address_with_affiliates and request_swap_deposit_address extrinsics.
  • Breaking change to cf_request_swap_parameter_encoding. This will most likely need a new RPC, can be done in another PR.
  • Breaking change to schedule_swap and lp_schedule_swap (aka internal swap). should we make another extrinsic instead?
  • Should we return an error when opening a deposit channel or encoding vault swap params if the oracle price is None for one of the swap assets? Catch it early so they do not think they are protected when they are not.

@j4m1ef0rd j4m1ef0rd requested a review from syan095 August 7, 2025 07:07
@j4m1ef0rd j4m1ef0rd requested review from a team, dandanlen, msgmaxim and nmammeri as code owners August 7, 2025 07:07
@coderabbitai
Copy link

coderabbitai bot commented Aug 7, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/oracle-swaps-jamie

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

/// and refund CCM metadata.
pub type VaultSwapParametersV0<RefundAddress> =
VaultSwapParameters<ChannelRefundParameters<RefundAddress, ()>>;
VaultSwapParameters<ChannelRefundParameters<RefundAddress, (), ()>>;
Copy link
Contributor

Choose a reason for hiding this comment

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

the () design has caused some tricky problems. I would strongly advise using a field with Option<> instead of this () pattern.

Copy link
Contributor Author

@j4m1ef0rd j4m1ef0rd Aug 8, 2025

Choose a reason for hiding this comment

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

I tried. Spent a day trying to change both of them to options and gave up in the end. Might re-visit it in another PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah better to keep the previous one as is, and just add the new one as Option.
I considered changing this last time in the Solana refund parameter the refactor is too much

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah I will give that a try

Ord,
)]
pub struct ChannelRefundParameters<A, CcmRefundDetails> {
pub struct ChannelRefundParameters<A, CcmRefundDetails, OracleSlippage> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same point as in the cf_parameter. If possible, avoid this and just use a field. Do we expect different types for the OracleSlippage?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the problem is that we have a ton of different types that are derived from ChannelRefundParameters and if we create a separate "v2" type (which we would need to we can still decode the old type) without using generics, you will end up duplicating a lot of code. I'm also not a fan of using generics this way, but I would leave it to @j4m1ef0rd to investigate if there is a better way.

@syan095
Copy link
Contributor

syan095 commented Aug 8, 2025

It might be a good idea to add an integration test as well, which can cover the entire workflow from requesting a swap all the way to egress.

Ord,
)]
pub struct ChannelRefundParameters<A, CcmRefundDetails> {
pub struct ChannelRefundParameters<A, CcmRefundDetails, OracleSlippage> {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the problem is that we have a ton of different types that are derived from ChannelRefundParameters and if we create a separate "v2" type (which we would need to we can still decode the old type) without using generics, you will end up duplicating a lot of code. I'm also not a fan of using generics this way, but I would leave it to @j4m1ef0rd to investigate if there is a better way.

@j4m1ef0rd j4m1ef0rd force-pushed the feat/oracle-swaps-jamie branch 2 times, most recently from afccdff to 610c911 Compare August 12, 2025 05:43
@j4m1ef0rd j4m1ef0rd force-pushed the feat/oracle-swaps-jamie branch from 610c911 to 1f7c917 Compare August 12, 2025 07:24
pub affiliates: BoundedVec<AffiliateAndFee, ConstU32<MAX_AFFILIATES>>,
}

impl From<BtcCfParametersV0> for BtcCfParameters {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: In order to keep it consistent with the other chain's cf_parameters I'd suggest renaming BtcCfParameters to BtcCfParametersV1.

VaultSwapParameters<ChannelRefundParameters<RefundAddress, ()>>;
VaultSwapParameters<ChannelRefundParametersV0<RefundAddress>>;

/// New version of `VaultSwapParameters` that includes refund CCM metadata.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// New version of `VaultSwapParameters` that includes refund CCM metadata.
/// New version of `VaultSwapParameters` includes refund CCM and oracle price slippage.


#[cfg(test)]
pub fn build_and_encode_v0_cf_parameters<RefundAddress: Encode>(
refund_parameters: crate::ChannelRefundParametersV0<RefundAddress>, /* crate::ChannelRefundParametersUncheckedV0<RefundAddress>, */
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove commented code.

);

let min_output =
output_amount_ceil(swap.swap.input_amount.into(), min_price)
Copy link
Contributor

Choose a reason for hiding this comment

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

Any particular reason why we use output_amount_ceil instead of output_amount_floor? I don't think it matters here but normally the rounding tends to be done in "favor" of the protocol per say, just to cover any edge cases.
Since it's not AMM logic that's almost certainly not relevant though, so it's just curiosity.

Copy link
Contributor

Choose a reason for hiding this comment

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

No particular reason. Like you said, this shouldn't matter, but I'm not against changing this to output_amount_floor.

gas_budget: fillOrKillParams?.refundCcmMetadata.gasBudget,
ccm_additional_data: fillOrKillParams?.refundCcmMetadata.ccmAdditionalData,
},
max_oracle_price_slippage: undefined, // TODO: update FillOrKillParamsX128 with max_oracle_price_slippage
Copy link
Contributor

Choose a reason for hiding this comment

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

Right now the bouncer is using "@chainflip/cli": "1.9.0-assethub.0-ccm-refunds", which is a little patch I made so FillOrKillParamsX128 contains the refund_ccm_metadata. Then the web team will add that to the 1.11 SDK.
We can add max_oracle_price_slippage in that same branch, as it's a little patch anyway, so we can use this in the bouncer. @j4m1ef0rd lmk if you'd want me to help out with that.
Then I think we should have a oracle swap FoK test in tests/fill_or_kill.ts the same way that we have it for regular and CCM refunds.

Copy link
Contributor Author

@j4m1ef0rd j4m1ef0rd Aug 15, 2025

Choose a reason for hiding this comment

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

I made the changes to your branch but I dont have permissions to push it. So I will do this bouncer stuff in another PR so we can merge this now.

@j4m1ef0rd j4m1ef0rd force-pushed the feat/oracle-swaps-jamie branch from a1c131d to 93b5f8b Compare August 15, 2025 04:35
@j4m1ef0rd j4m1ef0rd added this pull request to the merge queue Aug 15, 2025
Merged via the queue into main with commit ea97dce Aug 15, 2025
54 checks passed
@j4m1ef0rd j4m1ef0rd deleted the feat/oracle-swaps-jamie branch August 15, 2025 07:38
@j4m1ef0rd j4m1ef0rd mentioned this pull request Aug 21, 2025
3 tasks
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.

4 participants