Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

Trade supports multiple routes #55

Merged
merged 15 commits into from
Jul 26, 2021
Merged

Trade supports multiple routes #55

merged 15 commits into from
Jul 26, 2021

Conversation

willpote
Copy link
Contributor

@willpote willpote commented Jul 23, 2021

Trade updated to support multiple routes. Change is back in a backward compatible way to avoid a major version bump: you will hit an error if you try and access the 'route' property when your Trade consists of multiple routes

I previously made a change to the SwapRouter to support multiple Trade objects being provided to swapCallParameters. With this change that is no longer necessary, but I left it in anyway.

Copy link
Contributor

@NoahZinsmeister NoahZinsmeister left a comment

Choose a reason for hiding this comment

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

just did a brief first pass, seems generally sound, let me take a bit more time to sit with it

src/entities/trade.ts Outdated Show resolved Hide resolved
src/entities/trade.ts Outdated Show resolved Hide resolved
src/entities/trade.ts Outdated Show resolved Hide resolved
@willpote
Copy link
Contributor Author

I've changed the approach here, decided to remove all percentages and just use the actual amounts for each split

The routing api will handle potential loss of precision from doing splits and return amounts per route s.t. they add up correctly

exactOut
.maximumAmountIn(new Percent(JSBI.BigInt(200), JSBI.BigInt(100)))
.equalTo(CurrencyAmount.fromRawAmount(token0, 46464))
).toBeTruthy()
Copy link
Contributor

Choose a reason for hiding this comment

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

could maybe also just define a matcher, not that it matters

src/entities/trade.ts Outdated Show resolved Hide resolved
src/entities/trade.ts Outdated Show resolved Hide resolved
let spotOutputAmount = CurrencyAmount.fromRawAmount(this.outputAmount.currency, 0)
for (const { route, inputAmount } of this.routes) {
const midPrice = route.midPrice
spotOutputAmount = spotOutputAmount.add(midPrice.quote(inputAmount))
Copy link
Contributor

Choose a reason for hiding this comment

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

mmmm is this correct... i don't really have an intuition about the meaning of price impact across routes

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 think so, and I think @NoahZinsmeister agrees?

Copy link
Contributor

Choose a reason for hiding this comment

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

this is what i suggested, it seems consistent with how we were calculating it in the single-route case, and works out to a weighted average price impact across routes, where the weights are the quote amounts and the values are the individual price impact

@NoahZinsmeister NoahZinsmeister self-requested a review July 26, 2021 19:05
Copy link
Contributor

@NoahZinsmeister NoahZinsmeister left a comment

Choose a reason for hiding this comment

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

LGTM

@willpote willpote merged commit 7c3aedd into main Jul 26, 2021
@willpote willpote deleted the trade-multiple-routes branch July 26, 2021 19:15
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants