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: Change Pool API to have separate methods for JoinPool and JoinPoolNoSwap #2133

Merged
merged 31 commits into from
Sep 7, 2022

Conversation

AlpinYukseloglu
Copy link
Contributor

@AlpinYukseloglu AlpinYukseloglu commented Jul 19, 2022

Closes: #2128

What is the purpose of the change

Add JoinPoolNoSwap and CalcJoinPoolNoSwapShares to our GAMM pool interface to have a clear and accessible flow for no-swap joins.

Brief Changelog

  • Add JoinPoolNoSwap and CalcJoinPoolNoSwapShares to GAMM pool interface
  • Implement JoinPoolNoSwap and CalcJoinPoolNoSwapShares in balancer package
  • Route JoinPoolNoSwap in pool_service.go to the new JoinPoolNoSwap in our pool interface
  • Add tests for new balancer methods

TODO:

  • Create issue for stableswap implementation

Testing and Verifying

This change added tests and can be verified as follows:

  • TestCalcJoinPoolNoSwapShares in balancer's pool_test.go

Documentation and Release Note

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

@AlpinYukseloglu AlpinYukseloglu added the C:x/gamm Changes, features and bugs related to the gamm module. label Jul 19, 2022
@AlpinYukseloglu AlpinYukseloglu marked this pull request as ready for review July 19, 2022 02:09
@AlpinYukseloglu AlpinYukseloglu requested a review from a team July 19, 2022 02:09
return numShares, tokensJoined, nil
}

// 4) Still more coins to join, so we update the effective pool state here to account for
// join that just happened.
// * We add the joined coins to our "current pool liquidity" object (poolAssetsByDenom)
// * We increment a variable for our "newTotalShares" to add in the shares that've been added.
tokensJoined = tokensIn.Sub(remainingTokensIn)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Context: CalcJoinPoolNoSwapShares includes this calculation and returns the resulting tokensJoined above

x/gamm/pool-models/balancer/pool.go Show resolved Hide resolved
@github-actions github-actions bot added the C:docs Improvements or additions to documentation label Jul 19, 2022
Copy link
Member

@mattverse mattverse left a comment

Choose a reason for hiding this comment

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

Feature LGTM!
Left some comments, please take a look!

x/gamm/pool-models/balancer/pool_test.go Show resolved Hide resolved
x/gamm/pool-models/balancer/pool.go Outdated Show resolved Hide resolved
Copy link
Member

@p0mvn p0mvn left a comment

Choose a reason for hiding this comment

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

LGTM overall - would like to suggest adding new test cases

x/gamm/pool-models/balancer/pool_test.go Show resolved Hide resolved
x/gamm/pool-models/balancer/pool.go Show resolved Hide resolved
@ValarDragon ValarDragon added the A:backport/v10.x backport patches to v10.x branch label Jul 20, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Aug 5, 2022

This pull request has been automatically marked as stale because it has not had any recent activity. It will be closed if no further activity occurs. Thank you!

@github-actions github-actions bot added the Stale label Aug 5, 2022
p.IncreaseLiquidity(numShares, newLiquidity)
return numShares, nil
}

// CalcJoinPoolShares calculates the number of shares created to join pool with the provided amount of `tokenIn`.
Copy link
Member

Choose a reason for hiding this comment

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

So now this method is basically not called in the codebase anywhere, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

CalcJoinPoolShares? Do you mean calls outside of the module? Otherwise it's called in JoinPool

Copy link
Member

Choose a reason for hiding this comment

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

But JoinPool is no longer called in the state machien right?

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 might be missing your point here but I believe JoinPool is still called in JoinSwapExactAmountIn:

sharesOut, err := pool.JoinPool(ctx, tokensIn, pool.GetSwapFee(ctx))

x/gamm/pool-models/balancer/pool_test.go Outdated Show resolved Hide resolved
// get all 'pool assets' (aka current pool liquidity + balancer weight)
poolAssetsByDenom, err := getPoolAssetsByDenom(p.GetAllPoolAssets())
if err != nil {
return sdk.ZeroInt(), sdk.NewCoins(), tokensIn, err
Copy link
Member

Choose a reason for hiding this comment

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

I'm just going to point out which branches are not covered by tests.

@AlpinYukseloglu do you mind confirming please if it is possible to introduce test cases to cover these

This is the first branch

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This one should actually not be possible as it only fails when there are duplicate assets in a pool and it's not possible to have duplicate denoms in a valid coin set. The error check is just here as a best practice.

Copy link
Member

Choose a reason for hiding this comment

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

Is it due to using a balancer pool constructor in TestCalcJoinPoolNoSwapShares where the constructor performs this validation?

What if we avoid it using it to trigger an invalid state and achieve full coverage?

We should be able to:

balancerPool := balancer.Pool{
  Address:            types.NewPoolAddress(defaultPoolId).String(),
  Id:                 defaultPoolId,
  PoolParams:         balancer.PoolParams{SwapFee: defaultSwapFee, ExitFee: defaultExitFee},
  PoolAssets:         test.poolAssets,
  FuturePoolGovernor: defaultFutureGovernor,
  TotalShares:        sdk.NewCoin(types.GetPoolShareDenom(defaultPoolId), types.InitPoolSharesSupply),
}

Now, we can force duplicate pool assets

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please see the most recent case I added. It is still technically a trivial test case because duplicate pool assets are caught in multiple places throughout this function and ultimately end up in a division by zero even if you comment out all of them, but it should be covered now.

x/gamm/pool-models/balancer/pool.go Outdated Show resolved Hide resolved
x/gamm/pool-models/balancer/pool.go Outdated Show resolved Hide resolved
x/gamm/pool-models/balancer/pool.go Show resolved Hide resolved
x/gamm/pool-models/balancer/pool.go Show resolved Hide resolved
x/gamm/pool-models/balancer/pool.go Show resolved Hide resolved
x/gamm/pool-models/balancer/pool.go Outdated Show resolved Hide resolved
x/gamm/pool-models/balancer/pool.go Show resolved Hide resolved
@czarcas7ic czarcas7ic added this to the V12 Blockers milestone Aug 30, 2022
@p0mvn p0mvn self-assigned this Aug 30, 2022
Copy link
Member

@p0mvn p0mvn left a comment

Choose a reason for hiding this comment

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

LGTM. Great work on this @AlpinYukseloglu . Thank you for maximizing test coverage.

The remaining comments are non-blocking. However, could you please double-check the following:

  • the error message about "no swap joins requiring all assets in pool". I just want to ensure that this is not exclusive of "joins with swaps" because CalcJoinPoolNoSwapShares is called from CalcJoinPoolShares.
  • the new empty sdk.Context being passed in to MaximalExactRatioJoin from CalcJoinPoolNoSwapShares

x/gamm/pool-models/balancer/amm.go Show resolved Hide resolved
x/gamm/pool-models/balancer/pool_test.go Outdated Show resolved Hide resolved
x/gamm/pool-models/balancer/pool_test.go Outdated Show resolved Hide resolved
x/gamm/pool-models/balancer/amm.go Outdated Show resolved Hide resolved
x/gamm/pool-models/balancer/pool.go Outdated Show resolved Hide resolved

// ensure that there aren't too many or too few assets in `tokensIn`
if tokensIn.Len() != p.NumAssets() {
return sdk.ZeroInt(), sdk.NewCoins(), errors.New("no-swap joins require LP'ing with all assets in pool")
Copy link
Member

Choose a reason for hiding this comment

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

Just trying to understand:

Why do you emphasize "no-swap joins"?

CalcJoinPoolNoSwapShares is called from CalcJoinPoolShares which is called from regular JoinPool that does swaps. I also noticed a similar check n CalcJoinPoolShares.

My point is that it seems that this is relevant for joins with the swaps as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This specific function is for no-swap joins, so the errors returned by it specify that it is a no-swap pool join (this specific line wouldn't error if this was a join with swaps)

Even though CalcJoinPoolNoSwapShares is still called in CalcJoinPoolShares, it is called in the "no-swap"/"all-asset"/"exact ratio" join component of it, and all pool join logic related to swaps is handled elsewhere in that function

Copy link
Member

Choose a reason for hiding this comment

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

Awesome, thank you! Just wanted to make sure that this check was intentional to have on the path for regular joins as well

x/gamm/pool-models/balancer/pool.go Outdated Show resolved Hide resolved
x/gamm/pool-models/balancer/pool_test.go Show resolved Hide resolved
@p0mvn p0mvn added the A:backport/v12.x backport patches to v12.x branch label Sep 7, 2022
@p0mvn p0mvn merged commit 4de8bdf into main Sep 7, 2022
@p0mvn p0mvn deleted the separate-join-pool branch September 7, 2022 17:03
mergify bot pushed a commit that referenced this pull request Sep 7, 2022
… `JoinPoolNoSwap` (#2133)

* split out no swap joins on pool interface and implement changes for balancer

* minor refactor and comment changes for clarity

* minor comment changes for review clarity

* add tests for new calcs

* fix merge-related issues

* add changelog entry

* add failure case test and make existing tests more robust

* clean up validation logic

* factor out common denom check and add tests for it

* add additional test cases

* apply new internal fx where relevant and ensure proper testing

* add test case and improve test readability

* add tests for edge cases and clean up existing tests

* add duplicate pool edge case test

* minor comment updates from code review

Co-authored-by: Roman <[email protected]>

* remove remainingCoins return value from internal no-swap calcs

* update mocks

* move ensuredenominpool test to amm tests

* minor comment updates from code review

Co-authored-by: Roman <[email protected]>

* pass ctx through no-swap pool joins

* add multi asset and edge case tests to calcnoswap

Co-authored-by: Adam Tucker <[email protected]>
Co-authored-by: Roman <[email protected]>
(cherry picked from commit 4de8bdf)
AlpinYukseloglu added a commit that referenced this pull request Sep 8, 2022
… `JoinPoolNoSwap` (#2133) (#2634)

* split out no swap joins on pool interface and implement changes for balancer

* minor refactor and comment changes for clarity

* minor comment changes for review clarity

* add tests for new calcs

* fix merge-related issues

* add changelog entry

* add failure case test and make existing tests more robust

* clean up validation logic

* factor out common denom check and add tests for it

* add additional test cases

* apply new internal fx where relevant and ensure proper testing

* add test case and improve test readability

* add tests for edge cases and clean up existing tests

* add duplicate pool edge case test

* minor comment updates from code review

Co-authored-by: Roman <[email protected]>

* remove remainingCoins return value from internal no-swap calcs

* update mocks

* move ensuredenominpool test to amm tests

* minor comment updates from code review

Co-authored-by: Roman <[email protected]>

* pass ctx through no-swap pool joins

* add multi asset and edge case tests to calcnoswap

Co-authored-by: Adam Tucker <[email protected]>
Co-authored-by: Roman <[email protected]>
(cherry picked from commit 4de8bdf)

Co-authored-by: alpo <[email protected]>
Co-authored-by: alpo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A:backport/v12.x backport patches to v12.x branch C:docs Improvements or additions to documentation C:x/gamm Changes, features and bugs related to the gamm module.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Refactor: Change Pool API to have separate methods for JoinPool and JoinPoolNoSwap
5 participants