-
Notifications
You must be signed in to change notification settings - Fork 609
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
Add mutative, non mutative method for PoolI
#1147
Conversation
Ah, I think we need the PR to delete |
Yeah agreed that we need to remove ApplySwap, was planning to make a second PR but seems that we could do it all in this PR! |
Codecov Report
@@ Coverage Diff @@
## main #1147 +/- ##
==========================================
- Coverage 20.21% 19.97% -0.25%
==========================================
Files 199 199
Lines 25062 25459 +397
==========================================
+ Hits 5066 5085 +19
- Misses 19069 19445 +376
- Partials 927 929 +2
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
@@ -28,21 +28,22 @@ type PoolI interface { | |||
GetTotalShares() sdk.Int | |||
|
|||
CalcOutAmtGivenIn(ctx sdk.Context, tokenIn sdk.Coins, tokenOutDenom string, swapFee sdk.Dec) (tokenOut sdk.DecCoin, err error) | |||
CalcInAmtGivenOut(ctx sdk.Context, tokenOut sdk.Coins, tokenInDenom string, swapFee sdk.Dec) (tokenIn sdk.DecCoin, err error) | |||
SwapOutAmtGivenIn(ctx sdk.Context, tokenIn sdk.Coins, tokenOutDenom string, swapFee sdk.Dec) (tokenOut sdk.Coin, err error) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: not that is has to be done in these PRs, but I believe that these methods are the meat n bones of the AMM state machine, or are at least a large part of it. Being so, I think each of these methods in the interface here should be very very well documented with godocs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created #1183 to track
x/gamm/pool-models/balancer/amm.go
Outdated
newShares, err := p.singleAssetJoin(coin, swapFee) | ||
newShares, err := p.calcSingleAssetJoin(coin, swapFee) | ||
if err != nil { | ||
return sdk.ZeroInt(), err | ||
return sdk.ZeroInt(), sdk.NewCoins(), err | ||
} | ||
p.updateLiquidity(newShares, sdk.Coins{coin}) | ||
newLiquidity = newLiquidity.Add(coin) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So these single asset joins actual need the liquidity updated in order to work :/
This is because the pool reserves and therefore correct amounts out change at each of these steps.
Because the JoinPool and ExitPool methods have a lot of internal steps pool liquidity updates for correctness, I'd suggest doing this in one of two ways:
- Keeping JoinPool and ExitPool as before, but then make the calc methods do a deep copy of the struct. (Probably easier) I don't know if deepcopy / clone methods are going to be straight forward to make, or we'd have to make a function called "clonePoolAssets" to do it.
- Making a common method, that does all of this logic as before, but with pool assets passed in as an argument rather than read off the pool. (It can mutate this pool assets) Then make the calc method use a cloned pool assets struct, and the join pool use the 'real' pool assets struct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha! How is this passing tests right now tho :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uhhhh that's a very good question.
On first thought, maybe it's that we previously didn't support lping at arbitrary ratios, and therefore dont have tests for this? (Which is then concerning/ my bad for not adding tests in the refactoring)
So I took the approach where we keep the original structure, but pass in poolAssets, poolShares, as a parameter for calculating calcSingleAssetJoin. This way we can use the updated values of poolAssets and poolShares without mutating the original values, then mutate the original value only when joiningpool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
||
SpotPrice(ctx sdk.Context, baseAssetDenom string, quoteAssetDenom string) (sdk.Dec, error) | ||
|
||
// JoinPool joins the pool, and uses all of the tokensIn provided. | ||
// The AMM swaps to whatever the ratio should be and returns the number of shares created. | ||
// Internally the pool updates its count for the number of shares in this function. | ||
// If the function errors, or should not be mutative, then state must be reverted after this call. | ||
CalcJoinPoolShares(ctx sdk.Context, tokensIn sdk.Coins, swapFee sdk.Dec) (numShares sdk.Int, newLiquidity sdk.Coins, err error) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: seems that the comment above applies to JoinPool
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah we would have to go through the comments for PoolI
. Many of them seemed to be like temporary comments for @ValarDragon . I'll leave it to @ValarDragon to go through / organize the final struct for PoolI
: doesn't have to be in this PR
@@ -28,21 +28,22 @@ type PoolI interface { | |||
GetTotalShares() sdk.Int | |||
|
|||
CalcOutAmtGivenIn(ctx sdk.Context, tokenIn sdk.Coins, tokenOutDenom string, swapFee sdk.Dec) (tokenOut sdk.DecCoin, err error) | |||
CalcInAmtGivenOut(ctx sdk.Context, tokenOut sdk.Coins, tokenInDenom string, swapFee sdk.Dec) (tokenIn sdk.DecCoin, err error) | |||
SwapOutAmtGivenIn(ctx sdk.Context, tokenIn sdk.Coins, tokenOutDenom string, swapFee sdk.Dec) (tokenOut sdk.Coin, err error) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created #1183 to track
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Can you reslolve conflicts then we can merge?
@ValarDragon Sweet! Can you review the resolved conflicts? |
Closes: #1142
Description
This PR separates out mutative methods for joinPool, exitPool and swap functions for
PoolI
.Further things to address in the future: changing the existing methods(ex.
SwapExactAmountIn
in swap.go ) so that they use the mutative methods.For contributor use:
docs/
) or specification (x/<module>/spec/
)Unreleased
section inCHANGELOG.md
Files changed
in the Github PR explorer