-
Notifications
You must be signed in to change notification settings - Fork 611
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
[CL] Change calcOutAmtGivenIn
and calcInAmtGivenOut
to be non-mutative
#3910
Conversation
x/concentrated-liquidity/swaps.go
Outdated
@@ -210,6 +210,7 @@ func (k Keeper) calcOutAmtGivenIn(ctx sdk.Context, | |||
priceLimit sdk.Dec, | |||
poolId uint64, | |||
) (tokenIn, tokenOut sdk.Coin, updatedTick sdk.Int, updatedLiquidity, updatedSqrtPrice sdk.Dec, err error) { | |||
ctx, _ = ctx.CacheContext() |
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.
We should return the second return of CacheContext
from the caller (calcOutAmtGivenIn
) itself. The second return is a callback to write the cache context.
The reason for this requirement is that if calcOutAmtGivenIn
is called from swapOtAmtGivenIn
, we actually do want to write the cache context to storage.
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.
I also noticed that part in the original issue, but did not quite understand.
What's the reason we want to use write cache context here? Do we not store in state anyways using the results from calling the calc method anyways within the swap method?(https://github.com/osmosis-labs/osmosis/blob/main/x/concentrated-liquidity/swaps.go#L167)
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.
I am not sure if this is what you are saying Roman, but shouldnt we pass this cache ctx as a return value and then write in the swap function that calls this? If that is what you are saying, I agree. Without doing this, despite us calling applySwap, the tick boundaries that got passed wont be updated.
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.
Yes, that's what I meant
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, this makes sense
@p0mvn @czarcas7ic Would it be possible to give a quick conceptACK on the direction we're going that I put in da1c910 before I make the changes further on? |
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.
This is what I meant in an earlier discussion
x/concentrated-liquidity/swaps.go
Outdated
@@ -209,10 +209,11 @@ func (k Keeper) calcOutAmtGivenIn(ctx sdk.Context, | |||
swapFee sdk.Dec, | |||
priceLimit sdk.Dec, | |||
poolId uint64, | |||
) (tokenIn, tokenOut sdk.Coin, updatedTick sdk.Int, updatedLiquidity, updatedSqrtPrice sdk.Dec, err error) { | |||
) (cacheCtx sdk.Context, tokenIn, tokenOut sdk.Coin, updatedTick sdk.Int, updatedLiquidity, updatedSqrtPrice sdk.Dec, err error) { | |||
ctx, _ = ctx.CacheContext() |
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.
ctx, _ = ctx.CacheContext() | |
ctx, writeCtx := ctx.CacheContext() |
x/concentrated-liquidity/swaps.go
Outdated
@@ -335,7 +336,7 @@ func (k Keeper) calcOutAmtGivenIn(ctx sdk.Context, | |||
tokenIn = sdk.NewCoin(tokenInMin.Denom, amt0) | |||
tokenOut = sdk.NewCoin(tokenOutDenom, amt1) | |||
|
|||
return tokenIn, tokenOut, swapState.tick, swapState.liquidity, swapState.sqrtPrice, nil | |||
return ctx, tokenIn, tokenOut, swapState.tick, swapState.liquidity, swapState.sqrtPrice, nil |
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.
return ctx, tokenIn, tokenOut, swapState.tick, swapState.liquidity, swapState.sqrtPrice, nil | |
return ctx, tokenIn, tokenOut, swapState.tick, swapState.liquidity, swapState.sqrtPrice, nil |
return ctx, tokenIn, tokenOut, swapState.tick, swapState.liquidity, swapState.sqrtPrice, nil | |
return writeCtx, tokenIn, tokenOut, swapState.tick, swapState.liquidity, swapState.sqrtPrice, nil |
x/concentrated-liquidity/swaps.go
Outdated
@@ -178,12 +178,12 @@ func (k Keeper) CalcOutAmtGivenIn( | |||
tokenIn sdk.Coin, | |||
tokenOutDenom string, | |||
swapFee sdk.Dec, | |||
) (tokenOut sdk.Coin, err error) { | |||
_, tokenOut, _, _, _, err = k.calcOutAmtGivenIn(ctx, tokenIn, tokenOutDenom, swapFee, sdk.ZeroDec(), poolI.GetId()) | |||
) (newCtx sdk.Context, 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.
) (newCtx sdk.Context, tokenOut sdk.Coin, err error) { | |
) (writeCache func(), tokenOut sdk.Coin, err error) { |
This reverts commit da1c910.
@p0mvn Thanks for the clarification. I'm not knowledgable of writeCtx() function that much, but AFAIU, writeCtx works in pair with the cached context returned by |
What are you trying to test in the linked example? It seems that you're trying to assert that the pool has been updated. However, it would not be because we write pool data to store in osmosis/x/concentrated-liquidity/swaps.go Line 146 in 794e3b1
In fact, currently, we do not write anything to store inside This trick this |
I see, then do you have suggestion upon testing on state change by calling the writeCtx function as linked in my original question? If not, would pass on the testings for this specific case until PR #3890 is implemented |
I think it's fine to pass testing until #3890 is implemented. Could you please make sure that we track adding these tests in #3890? |
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
@@ -203,16 +205,18 @@ func (k Keeper) CalcInAmtGivenOut( | |||
// calcOutAmtGivenIn calculates tokens to be swapped out given the provided amount and fee deducted. It also returns | |||
// what the updated tick, liquidity, and currentSqrtPrice for the pool would be after this swap. | |||
// Note this method is non-mutative, so the values returned by CalcOutAmtGivenIn do not get stored | |||
// Instead, we return writeCtx function so that the caller of this method can decide to write the cached ctx to store or not. |
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.
Do you mind adding spec for calcInAmtGivenOut
as a drive by change in this PR please?
Just noticed it's missing
Co-authored-by: Roman <roman@osmosis.team>
Co-authored-by: Roman <roman@osmosis.team>
Closes: #3889
What is the purpose of the change
This PR changes the two calc methods, namely
calcOutAmtGivenIn
andcalcInAmtGivenOut
to be non mutative methods, as previously they have been mutating ticks of the pool when upon crossing tick during iteration process of the calculation.Also added existing checks to existing test cases that ensures that pool has not been changed after performing calculation method.
Testing and Verifying
Changed existing test to cover changes made within this PR