Skip to content

feat: add router capabilities for swapping with the sdk #66

Merged
baroooo merged 7 commits intomainfrom
feat/router
Feb 17, 2025
Merged

feat: add router capabilities for swapping with the sdk #66
baroooo merged 7 commits intomainfrom
feat/router

Conversation

@bowd
Copy link
Copy Markdown
Contributor

@bowd bowd commented Feb 14, 2025

Description

This PR integrates the Mento Router to implement simple routes between assets in the Mento protocol. The tl;dr of the implementation is:

  • Create a TradablePair interface the represents a pair that can be traded either directly or through multiple swaps.
  • Extend the getTradablePairs to return an array of TradablePairs limited to 1 or 2 swaps (because one hop is enough to get between any two assets).
  • Change the interface of all getAmount* and swap* functions to receive the tradable pair and depending on the size of the path choose to use either the Broker directly or the Router.

This as a base worked well, as long as the clients will use getTradablePairs for discoverability and then have that instance with the path to pass back into the router.

What this doesn't do: Find the best route. In our setup it's redundant. All paths will be through cUSD ultimately. No need to implement a full router logic on our end.

I had to do some additional work because in practice mento-web didn't rely on getTradablePairs for performance reasons but kept a local index of hardcoded exchange ids and assets to determine possible swaps. Because the performance concerns were real I decided to implement a helper to cache all tradable pairs in the SDK (instead of them living in mento-web). This way we get the performance boost across implementations and consumers of the SDK have the option to query without cache.
This means that when new tokens go live we would still need to rebuild the cache here and redeploy mento-web but that was the case in the past as well.

Other changes

I've also added a script for testing swaps and a script to list tradable pairs to make it easier to test.

Tested

Tests updated to reflect the router situation.

Related issues

N/A

Backwards compatibility

I've made the interface backwards compatible so no major version push required 🎉

Documentation

N/A

@bowd bowd changed the title Feat/router feat: add router capabilities for swapping with the sdk Feb 14, 2025
Co-authored-by: Bayological <6872903+bayological@users.noreply.github.com>
)

// Calculate minAmountOut with 5% slippage
const minAmountOut = amountOut.mul(95).div(100)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: would be nice to make this an optional param

philbow61 and others added 2 commits February 17, 2025 15:40
Co-authored-by: baroooo <baranseltekin@gmail.com>
@baroooo baroooo merged commit 0fe7aaa into main Feb 17, 2025
1 check passed
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.

5 participants