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

refactor(x/gamm, x/swaprouter): multihop and estimate swap move #3764

Merged
merged 28 commits into from
Dec 21, 2022

Conversation

p0mvn
Copy link
Member

@p0mvn p0mvn commented Dec 16, 2022

Closes: #XXX

What is the purpose of the change

This PR moves multihop and estimate swaps keeper methods from x/gamm to x/swaprouter.

No messages or queries are broken.

Old messages and queries relying on the x/gamm keeper methods now directly call the respective methods in x/swaprouter.

Testing and Verifying

All pre-existing tests have been moved to x/swaprouter. New tests added where applicable.

Documentation and Release Note

  • Does this pull request introduce a new feature or user-facing behavior changes? yes
  • Is a relevant changelog entry added to the Unreleased section in CHANGELOG.md? no
  • How is the feature or change documented? not applicable

@github-actions github-actions bot added C:app-wiring Changes to the app folder C:x/gamm Changes, features and bugs related to the gamm module. C:x/swaprouter C:x/txfees labels Dec 16, 2022
@p0mvn p0mvn added the V:state/breaking State machine breaking PR label Dec 16, 2022
@p0mvn p0mvn force-pushed the roman/swaprouter-multihops branch from 7a0d6d0 to 01df0c3 Compare December 17, 2022 21:58
app/keepers/keepers.go Outdated Show resolved Hide resolved
@@ -49,6 +30,9 @@ func (k Keeper) swapExactAmountIn(
if tokenIn.Denom == tokenOutDenom {
return sdk.Int{}, errors.New("cannot trade same denomination in and out")
}
if !pool.IsActive(ctx) {
Copy link
Member Author

Choose a reason for hiding this comment

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

TODO: consider moving this to swaprotuer

Copy link
Member Author

Choose a reason for hiding this comment

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

Comment on lines +425 to +426
// TODO: remove this redundancy after making routes be shared between x/gamm and x/swaprouter.
swaprouterRoutes := types.ConvertAmountInRoutes(req.Routes)
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: this implies proto-generating routes only in swaprouter and importing them in x/gamm.

According to earlier discussions, this should not cause breakage and will be done in a future PR

Copy link
Member

Choose a reason for hiding this comment

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

SG if were confident it wont break stuff. Is there an issue or something written to track our testing strategy? (Not PR merge blocking)

Copy link
Member Author

Choose a reason for hiding this comment

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

According to our earlier discussions, this should not be state-breaking.

The only concern was breaking telescope. I'm planning to make a follow-up PR and tag Dan Lynch to help with looking into this.

I can also add a test to serialize both old and new routes to make sure that they are valid

Copy link
Member Author

Choose a reason for hiding this comment

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

}

func (k Keeper) MultihopEstimateOutGivenExactAmountIn(
ctx sdk.Context,
routes []types.SwapAmountInRoute,
tokenIn sdk.Coin,
) (tokenOutAmount sdk.Int, err error) {
panic("not implemented")
Copy link
Member Author

@p0mvn p0mvn Dec 19, 2022

Choose a reason for hiding this comment

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

Note: confirmed that for all methods in this file the followin is true:

Copied with minimal changes, the only additional changes are one of:

  • new routing logic
  • avoid using sdkerrors

@@ -1,365 +0,0 @@
package keeper
Copy link
Member Author

@p0mvn p0mvn Dec 19, 2022

Choose a reason for hiding this comment

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

Note: diffed this whole file against x/swaprouter/router.go. The few changes are summarized in x/swaprouter/router.go per method.

x/swaprouter/router_test.go Outdated Show resolved Hide resolved
@@ -1,846 +0,0 @@
package keeper_test
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: confirmed that every test case removed in this file has been ported to x/swaprouter/router_test.go.

The test fixtures might look different due to clean up. However, every test case is still present

// tokenInDenom string,
// swapFee sdk.Dec,
// ) (tokenIn sdk.Coin, err error)
GetPool(ctx sdk.Context, poolId uint64) (PoolI, error)
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: adding godocs to the rest of these is tracked via: #3556

@p0mvn p0mvn marked this pull request as ready for review December 19, 2022 23:57
Copy link
Member

@czarcas7ic czarcas7ic left a comment

Choose a reason for hiding this comment

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

Well done, only minor comments on my end 👍

x/gamm/keeper/keeper.go Outdated Show resolved Hide resolved
x/gamm/keeper/swap.go Outdated Show resolved Hide resolved
}
}

// TestCalcInAmtGivenOut only tests that balancer and stableswap pools are type casted correctly while concentratedliquidity pools fail
Copy link
Member

Choose a reason for hiding this comment

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

Can we either remove the concentratedliquidity portion from the description, or add a todo to add this to this test as well as the previous one?

Copy link
Member Author

Choose a reason for hiding this comment

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

added TODO, including here: #3556

x/txfees/keeper/keeper.go Outdated Show resolved Hide resolved
@p0mvn p0mvn mentioned this pull request Dec 20, 2022
9 tasks
x/gamm/keeper/pool.go Outdated Show resolved Hide resolved
tokenOutAmount = exitCoins.AmountOf(tokenOutDenom)
for _, coin := range exitCoins {
if coin.Denom == tokenOutDenom {
continue
}
swapOut, err := k.SwapExactAmountIn(ctx, sender, poolId, coin, tokenOutDenom, sdk.ZeroInt())
swapOut, err := k.SwapExactAmountIn(ctx, sender, pool, coin, tokenOutDenom, sdk.ZeroInt(), swapFee)
Copy link
Member

@ValarDragon ValarDragon Dec 20, 2022

Choose a reason for hiding this comment

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

Wait, theres been an important abstraction issue thats changed here wrt swap fee.

I get why its being done here, but am concerned about:

  • Whats the public/private guarantee
  • Ensuring we keep dynamic swap fees possible.

The latter is still the case, if AMMs can "ignore" the swapFee arg.

Maybe we add a panic for now, if swapFee arg less than .5 ctx.SwapFee() ?

I think its good to merge if we add that safety check, and add to our mental checklist, that modules shouldn't import gamm, only swaprouter.

Copy link
Member Author

Choose a reason for hiding this comment

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

I have the same concern and this is tracked here: #3130

I've added the suggested check and tests for it in: ace13f9

@@ -130,7 +105,12 @@ func (k Keeper) swapExactAmountOut(
"can't get more tokens out than there are tokens in the pool")
}

tokenIn, err := pool.SwapInAmtGivenOut(ctx, sdk.Coins{tokenOut}, tokenInDenom, swapFee)
cfmmPool, err := convertToCFMMPool(pool)
Copy link
Member

Choose a reason for hiding this comment

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

nbd, but why's this a function instead of just a typecast?

Copy link
Member Author

Choose a reason for hiding this comment

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

This type cast happens frequently so made sense to abstract it away

Copy link
Member

@ValarDragon ValarDragon left a comment

Choose a reason for hiding this comment

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

LGTM, aside from my comments.

I am assuming the router.go and router_test.go are pure moves, but I have not in-depth verified this.

@p0mvn p0mvn merged commit d1349db into main Dec 21, 2022
@p0mvn p0mvn deleted the roman/swaprouter-multihops branch December 21, 2022 01:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:app-wiring Changes to the app folder C:x/gamm Changes, features and bugs related to the gamm module. C:x/swaprouter C:x/txfees V:state/breaking State machine breaking PR
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants