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

Sdk: include rfq protocol fees #1871

Merged
merged 4 commits into from
Jan 19, 2024
Merged

Sdk: include rfq protocol fees #1871

merged 4 commits into from
Jan 19, 2024

Conversation

ChiTimesChi
Copy link
Collaborator

@ChiTimesChi ChiTimesChi commented Jan 16, 2024

Description
This PR adds support for protocol fees in the FastBridge contract.

Summary by CodeRabbit

  • New Features
    • Introduced a feature to retrieve the protocol fee rate within the routing system.
    • Updated routing calculations to include protocol fee adjustments, ensuring accurate cost representation during transactions.

Copy link
Contributor

coderabbitai bot commented Jan 16, 2024

Walkthrough

The FastBridgeRouter and FastBridgeRouterSet within an SDK have been enhanced to handle protocol fees. A new method for fetching the fee rate has been added to the FastBridgeRouter, and the FastBridgeRouterSet now includes logic to apply this fee rate, affecting the calculated destination amount after a swap.

Changes

File Path Change Summary
.../rfq/fastBridgeRouter.test.ts
.../rfq/fastBridgeRouter.ts
Added getProtocolFeeRate to fetch and return the protocol fee rate as a BigNumber.
.../rfq/fastBridgeRouterSet.test.ts
.../rfq/fastBridgeRouterSet.ts
Included applyProtocolFeeRate to calculate amount after fee, and updated destAmountOut logic to factor in the protocol fee rate.

Poem

🐇 "In the land of code and byte,"
"A fee was set to make things right."
"With numbers big and logic tight,"
"We bridge the swap into the night." 🌉✨

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

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>.
    • Generate unit-tests for this file.
  • 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. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • 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 generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

codecov bot commented Jan 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (744d57d) 51.18525% compared to head (5a06feb) 51.19794%.
Report is 1 commits behind head on master.

Additional details and impacted files
@@                 Coverage Diff                 @@
##              master       #1871         +/-   ##
===================================================
+ Coverage   51.18525%   51.19794%   +0.01268%     
===================================================
  Files            397         397                 
  Lines          27125       27130          +5     
  Branches         307         307                 
===================================================
+ Hits           13884       13890          +6     
  Misses         11888       11888                 
+ Partials        1353        1352          -1     
Flag Coverage Δ
packages 91.02167% <100.00000%> (+0.15030%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

@ChiTimesChi ChiTimesChi marked this pull request as ready for review January 16, 2024 18:56
@ChiTimesChi ChiTimesChi requested a review from mikeyrf January 16, 2024 18:56
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 31c3a4c and 5a06feb.
Files selected for processing (4)
  • packages/sdk-router/src/rfq/fastBridgeRouter.test.ts (2 hunks)
  • packages/sdk-router/src/rfq/fastBridgeRouter.ts (1 hunks)
  • packages/sdk-router/src/rfq/fastBridgeRouterSet.test.ts (1 hunks)
  • packages/sdk-router/src/rfq/fastBridgeRouterSet.ts (2 hunks)
Additional comments: 7
packages/sdk-router/src/rfq/fastBridgeRouterSet.test.ts (1)
  • 105-119: The tests for the applyProtocolFeeRate method are correctly checking the behavior of applying 0 bps and 10 bps fee rates. The expectations are set correctly to match the amount when no fee is applied and to match the reduced amount when a 10 bps fee rate is applied.
packages/sdk-router/src/rfq/fastBridgeRouter.ts (1)
  • 142-148: The getProtocolFeeRate method has been correctly added to the FastBridgeRouter class. It retrieves the protocol fee rate from the fastBridgeContract asynchronously and returns it as a BigNumber, which aligns with the PR objectives.
packages/sdk-router/src/rfq/fastBridgeRouterSet.ts (2)
  • 115-127: The applyProtocolFeeRate method is correctly integrated into the getBridgeRoutes method. The protocol fee rate is fetched and applied to the originQuery.minAmountOut before applying the quote, which is consistent with the PR objectives to handle protocol fees.
  • 237-248: The applyProtocolFeeRate method is correctly implemented to calculate the amount after the protocol fee is applied. The method uses the correct formula to calculate the fee and subtracts it from the amount, which is consistent with the expected behavior.
packages/sdk-router/src/rfq/fastBridgeRouter.test.ts (3)
  • 229-293: The tests for the getOriginAmountOut method are correctly implemented. They mock the contract's behavior and verify that the method returns the expected values when the protocol fee rate is 0 bps.
  • 295-318: The tests for the getOriginAmountOut method with a protocol fee rate of 10 bps are correctly implemented. The mock implementation of the protocolFeeRate and the expectations are set up correctly to ensure that the method's behavior is as expected.
  • 321-329: The test for the getProtocolFeeRate method is correctly implemented. It mocks the protocolFeeRate method of the fastBridgeContractCache and asserts that the getProtocolFeeRate method of the FastBridgeRouter returns the correct value.

Copy link
Contributor

@mikeyrf mikeyrf left a comment

Choose a reason for hiding this comment

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

One question but otherwise lgtm

destAmountOut: applyQuote(quote, originQuery.minAmountOut),
destAmountOut: applyQuote(
quote,
this.applyProtocolFeeRate(originQuery.minAmountOut, protocolFeeRate)
Copy link
Contributor

Choose a reason for hiding this comment

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

Applied on origin amount tho so is it relevant for FE as only used by caller of claim() which will be relayer?

Copy link
Contributor

Choose a reason for hiding this comment

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

Other consideration would be on refund(), bridge sends back to origin sender transaction.originAmount + transaction.originFeeAmount so protocol doesn't take a fee on unsuccessful bridges, if that affects SDK in any way.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am replicating the internal logic of FastBrdige.bridge() wrt the origin amount used for the cross-chain swap:

originAmount: originAmount,

This should be the amount that the off-chain Relayer will recognize, and therefore to get an accurate final quote for the user, the Relayer's quote is applied to the amount after the protocol fees.

Does this make sense?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yup! That makes sense then

@ChiTimesChi ChiTimesChi merged commit 8245ea5 into master Jan 19, 2024
35 checks passed
@ChiTimesChi ChiTimesChi deleted the sdk/rfq-protocol-fees branch January 19, 2024 11:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants