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

WT-1662 Bridge and swap route #907

Merged
merged 27 commits into from
Sep 28, 2023
Merged

WT-1662 Bridge and swap route #907

merged 27 commits into from
Sep 28, 2023

Conversation

imx-mikhala
Copy link
Contributor

@imx-mikhala imx-mikhala commented Sep 27, 2023

Summary

This PR adds the bridge -> swap route.

  • The bridge -> swap route reuse the bridge & swap routes but have some additional logic.
  • Firstly, the bridge -> swap route filters all the swappable tokens by ones that can also be bridged by checking against the indexer for L1 addresses and then filteriing these using the bridgeable tokens.
  • Next, we get all the quotes from the DEX for swappable tokens.
    • Using this we construct all the possible bridge requirements, which is basically an array of all the tokens we want to check for bridging and the amount of each token to bridge.
    • The amount checks for things like fees and balance requirements to ensure we bridge enough across if the fees and requirements include the same token to ensure we bridge enough for swapping and covering any fees etc.
    • We are applying a 1% slippage to the DEX to accommodate token pairing price flucs
      • May need to adjust the % to something higher but just using this for now
  • Using the bridge requirements we perform the bridge route n times.
  • After this we check all the bridges that were successful and fake the balances as if the bridge occurred (so when we run the swap route it uses the balance as if we bridged)
  • The swap route is then called against each of the tokens that could be bridged to see what can be swapped.
  • Once we have the swap routes we then reset the balances on the routes to the original balances.
  • All of the bridge->swap routes are then returned

Demo

This demo has a requirement of zkYEET. The wallet I am testing with has a small about of IMX on L2 but not quite enough to perform the swap into zkYEET. The wallet also has enough ETH to pay for bridge fees. However, the wallet has enough IMX on L1 that can be bridged across and then swapped into zkYEET. Smart checkout suggests a Bridge->Swap route since I can bridge across enough IMX to perform the swap and cover all fees.

Screen.Recording.2023-09-28.at.8.51.57.am.mov

@imx-mikhala imx-mikhala marked this pull request as ready for review September 27, 2023 23:15
@imx-mikhala imx-mikhala requested a review from a team as a code owner September 27, 2023 23:15
Copy link
Contributor

@dreamoftrees dreamoftrees left a comment

Choose a reason for hiding this comment

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

Awesome work @imx-mikhala 🎉 Just some minor comments.


const ownerAddress = '0xOWNER';

const getTestDexQuotes = (): DexQuotes => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I like this work, at some future point I think worth considering to refactor into mockUtils or mockDexQuotes as mocking functions we can share across checkout.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

100%

);
});

it('should return no bridge and swap routes', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think it's worth expanding on what the logic/business rule is here that is being validated, rather than the focus only on the response.. for example

Suggested change
it('should return no bridge and swap routes', async () => {
it('should return no bridge and swap routes if no bridgeable and swappable tokens available', async () => {

import { L1ToL2TokenAddressMapping } from '../indexer/fetchL1Representation';
import { getDexQuotes } from './getDexQuotes';

export const abortBridgeAndSwap = (
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice encapsulation here, looks nice and clean 🌮

Copy link
Contributor Author

Choose a reason for hiding this comment

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

got the idea from a comment you left on another pr haha

@imx-mikhala imx-mikhala merged commit f0998ab into main Sep 28, 2023
6 checks passed
@imx-mikhala imx-mikhala deleted the WT-1662 branch September 28, 2023 06:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants