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: ExitSwapExternAmountOut #1244

Merged
merged 28 commits into from
Apr 18, 2022
Merged

refactor: ExitSwapExternAmountOut #1244

merged 28 commits into from
Apr 18, 2022

Conversation

p0mvn
Copy link
Member

@p0mvn p0mvn commented Apr 13, 2022

Closes: #1130

Description

  • Re-implemented ExitSwapExternAmountOut with similar logic to v7
  • introduced PoolExitSwapExternAmountOutExtension

Risks

I was confused about the following part:

// TODO: `balancer` contract sends the exit fee to the `factory` contract.
// But, it is unclear that how the exit fees in the `factory` contract are handled.
// And, it seems to be not good way to send the exit fee to the pool,
// because the pool doesn't have the PoolAsset about exit fee.
// So, temporarily, just burn the exit fee.
if exitFee.IsPositive() {
err = k.BurnPoolShareFromAccount(ctx, pool, sender, exitFee)
if err != nil {
return sdk.Int{}, err
}
}

I think that instead of doing that, it is possible to call k.applyExitPoolStateChange(...) so that it takes care of the fee since it does k.BurnPoolShareFromAccount(ctx, pool, exiter, numShares) where numShares do not have the exitFee subtracted.

It seems to me that in the old logic subtracting the fee only to burn it separately was redundant, and I'm not sure if I'm missing some algebra reasoning behind it.

Next Steps

I'm planning to spend some time writing more unit tests for this in a separate PR. Then, spearhead more e2e tests by joining a swap and exiting it using this method.


For contributor use:

  • Targeted PR against correct branch (see CONTRIBUTING.md)
  • Updated relevant documentation (docs/) or specification (x/<module>/spec/)
  • Added a relevant changelog entry to the Unreleased section in CHANGELOG.md
  • Re-reviewed Files changed in the Github PR explorer

@codecov-commenter
Copy link

codecov-commenter commented Apr 13, 2022

Codecov Report

Merging #1244 (685f439) into main (b6d342a) will decrease coverage by 0.75%.
The diff coverage is 9.32%.

@@            Coverage Diff             @@
##             main    #1244      +/-   ##
==========================================
- Coverage   20.90%   20.14%   -0.76%     
==========================================
  Files         196      203       +7     
  Lines       25425    26824    +1399     
==========================================
+ Hits         5316     5405      +89     
- Misses      19118    20412    +1294     
- Partials      991     1007      +16     
Impacted Files Coverage Δ
x/claim/abci.go 0.00% <ø> (ø)
x/claim/client/cli/query.go 34.45% <ø> (ø)
x/claim/client/cli/tx.go 0.00% <ø> (ø)
x/claim/handler.go 0.00% <ø> (ø)
x/claim/keeper/claim.go 64.33% <ø> (ø)
x/claim/keeper/grpc_query.go 2.50% <ø> (ø)
x/claim/keeper/hooks.go 28.00% <ø> (ø)
x/claim/keeper/keeper.go 87.50% <ø> (ø)
x/claim/keeper/params.go 81.81% <ø> (ø)
x/claim/module.go 58.06% <ø> (ø)
... and 106 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1530e4a...685f439. Read the comment docs.

x/gamm/types/pool.go Outdated Show resolved Hide resolved
@p0mvn p0mvn marked this pull request as ready for review April 13, 2022 01:08
@alexanderbez alexanderbez added the C:x/gamm Changes, features and bugs related to the gamm module. label Apr 13, 2022
CHANGELOG.md Outdated
@@ -43,6 +43,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Features

* [#1244](https://github.com/osmosis-labs/osmosis/pull/1244) refactor: ExitSwapExternAmountOut
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* [#1244](https://github.com/osmosis-labs/osmosis/pull/1244) refactor: ExitSwapExternAmountOut
* [#1244](https://github.com/osmosis-labs/osmosis/pull/1244) Refactor `x/gamm`'s `ExitSwapExternAmountOut`.

x/gamm/types/pool.go Outdated Show resolved Hide resolved
x/gamm/keeper/pool_service.go Show resolved Hide resolved
@@ -375,3 +375,62 @@ func (p *Pool) CalcExitPoolShares(ctx sdk.Context, exitingShares sdk.Int, exitFe
}
return exitedCoins, nil
}

// balancer notation: pAi - poolshares amount in, given single out
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// balancer notation: pAi - poolshares amount in, given single out
// balancer notation: pAi - pool shares amount in, given single out

Copy link
Contributor

Choose a reason for hiding this comment

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

What is "single out"? I'm not sure how to read this godoc :P

Copy link
Member Author

Choose a reason for hiding this comment

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

Missed "asset" in "single out". Changed to:

// balancer notation: pAi - pool shares amount in, given single asset out
// the second argument requires the tokenWeightOut / total token weight.

Also, there is a similar function for the converse:

// balancer notation: pAo - poolshares amount out, given single asset in
// the second argument requires the tokenWeightIn / total token weight.
func calcPoolOutGivenSingleIn(

x/gamm/pool-models/balancer/amm.go Outdated Show resolved Hide resolved
x/gamm/pool-models/balancer/amm.go Show resolved Hide resolved
x/gamm/pool-models/balancer/amm.go Outdated Show resolved Hide resolved
p.GetSwapFee(ctx),
p.GetExitFee(ctx))

if poolAmountInBeforeFee.LTE(sdk.ZeroInt().ToDec()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Pretty sure there is a sdk.ZeroDec that we can use.

return sdk.Int{}, sdkerrors.Wrapf(types.ErrInvalidMathApprox, "token amount is zero or negative")
}

if poolAmountInBeforeFee.GT(shareInMaxAmount.ToDec()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not super familiar with the balancer amm arithmetic yet, but why do we compare balances to shares here?

Copy link
Member

@ValarDragon ValarDragon Apr 14, 2022

Choose a reason for hiding this comment

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

We should rename calcPoolOutGivenSingleIn to calcPoolSharesOutGivenSingleIn and the same for the variable, then this makes sense / is correct.

Thanks for catching this

Copy link
Member Author

Choose a reason for hiding this comment

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

Although renaming calcPoolOutGivenSingleIn to calcPoolSharesOutGivenSingleIn makes sebse, I'm assuming that you are referring to renaming calcPoolInGivenSingleOut to calcPoolSharesInGivenSingleAssetOut in this particular example of ExitSwapExternAmountOut?

if err != nil {
return sdk.Int{}, err
}

Copy link
Member

Choose a reason for hiding this comment

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

We need to update the total shares as well, ala

https://github.com/osmosis-labs/osmosis/pull/1244/files#diff-897e45c36167c79b080379402216acbe7c118711dcaa23ca7852cd2a70aca3c1R346

Maybe we refactor share updating + token balance updating for exits into a single re-used function between the two?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea, I extracted this functionality from ExitPool into exitPool

Copy link
Member

Choose a reason for hiding this comment

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

Nice! That change LGTM

@p0mvn p0mvn marked this pull request as draft April 15, 2022 16:06
@p0mvn p0mvn marked this pull request as ready for review April 15, 2022 23:24
@ValarDragon ValarDragon merged commit 298ad00 into main Apr 18, 2022
@ValarDragon ValarDragon deleted the roman/1130 branch April 18, 2022 17:25
@faddat faddat mentioned this pull request Apr 19, 2022
4 tasks
@github-actions github-actions bot mentioned this pull request Jan 15, 2024
@github-actions github-actions bot mentioned this pull request May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:CLI 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.

[x/gamm] Re-implement ExitSwapExternAmountOut generically
4 participants