Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Register
Sign in
Toggle navigation
Menu
Open sidebar
Tiger Ton
osmosis
Commits
aab9ac6a
Commit
aab9ac6a
authored
2 years ago
by
mattverse
Browse files
Options
Download
Email Patches
Plain Diff
WIP
parent
a35002c9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
x/concentrated-liquidity/pool.go
+8
-6
x/concentrated-liquidity/pool.go
x/concentrated-liquidity/pool_test.go
+49
-45
x/concentrated-liquidity/pool_test.go
with
57 additions
and
51 deletions
+57
-51
x/concentrated-liquidity/pool.go
+
8
-
6
View file @
aab9ac6a
...
...
@@ -200,7 +200,7 @@ func (k Keeper) CalcInAmtGivenOut(ctx sdk.Context, tokenOut sdk.Coin, tokenInDen
p
:=
k
.
getPoolbyId
(
ctx
,
poolId
)
asset0
:=
p
.
Token0
asset1
:=
p
.
Token1
zeroForOne
:=
token
Out
.
Denom
==
asset0
zeroForOne
:=
token
In
.
Denom
==
asset0
// get current sqrt price from pool
// curSqrtPrice := sdk.NewDecWithPrec(int64(p.CurrentSqrtPrice.Uint64()), 6)
...
...
@@ -255,6 +255,7 @@ func (k Keeper) CalcInAmtGivenOut(ctx sdk.Context, tokenOut sdk.Coin, tokenInDen
// TODO: This should be GT 0 but some instances have very small remainder
// need to look into fixing this
for
swapState
.
amountSpecifiedRemaining
.
GT
(
sdk
.
NewDecWithPrec
(
1
,
6
))
{
// amountRemaining := sdk.NewDec(99999999999)
nextTick
,
_
:=
k
.
NextInitializedTick
(
ctx
,
poolId
,
swapState
.
tick
.
Int64
(),
zeroForOne
)
// TODO: we can enable this error checking once we fix tick initialization
// if !ok {
...
...
@@ -266,6 +267,7 @@ func (k Keeper) CalcInAmtGivenOut(ctx sdk.Context, tokenOut sdk.Coin, tokenInDen
}
// TODO: In and out get flipped based on if we are calculating for in or out, need to fix this
// Matt thinks we should use a different equation here
sqrtPrice
,
amountIn
,
amountOut
:=
computeSwapStep
(
swapState
.
sqrtPrice
,
nextSqrtPrice
,
...
...
@@ -274,22 +276,22 @@ func (k Keeper) CalcInAmtGivenOut(ctx sdk.Context, tokenOut sdk.Coin, tokenInDen
zeroForOne
,
)
swapState
.
amountSpecifiedRemaining
=
swapState
.
amountSpecifiedRemaining
.
Sub
(
amount
In
)
swapState
.
amountCalculated
=
swapState
.
amountCalculated
.
Add
(
amount
Out
.
Quo
(
sdk
.
OneDec
()
.
Sub
(
swapFee
)))
swapState
.
amountSpecifiedRemaining
=
swapState
.
amountSpecifiedRemaining
.
Sub
(
amount
Out
)
swapState
.
amountCalculated
=
swapState
.
amountCalculated
.
Add
(
amount
In
.
Quo
(
sdk
.
OneDec
()
.
Sub
(
swapFee
)))
if
swapState
.
s
qrtPrice
.
Equal
(
sqrtPrice
)
{
if
nextS
qrtPrice
.
Equal
(
sqrtPrice
)
{
liquidityDelta
,
err
:=
k
.
crossTick
(
ctx
,
p
.
Id
,
nextTick
)
if
err
!=
nil
{
return
sdk
.
Coin
{},
sdk
.
Coin
{},
err
}
if
!
zeroForOne
{
if
zeroForOne
{
liquidityDelta
=
liquidityDelta
.
Neg
()
}
swapState
.
liquidity
=
swapState
.
liquidity
.
Add
(
liquidityDelta
.
ToDec
())
if
swapState
.
liquidity
.
LTE
(
sdk
.
ZeroDec
())
||
swapState
.
liquidity
.
IsNil
()
{
return
sdk
.
Coin
{},
sdk
.
Coin
{},
fmt
.
Errorf
(
"no liquidity available, cannot swap"
)
}
if
!
zeroForOne
{
if
zeroForOne
{
swapState
.
tick
=
sdk
.
NewInt
(
nextTick
-
1
)
}
else
{
swapState
.
tick
=
sdk
.
NewInt
(
nextTick
)
...
...
This diff is collapsed.
Click to expand it.
x/concentrated-liquidity/pool_test.go
+
49
-
45
View file @
aab9ac6a
package
concentrated_liquidity_test
import
(
fmt
"fmt"
sdk
"github.com/cosmos/cosmos-sdk/types"
cl
"github.com/osmosis-labs/osmosis/v12/x/concentrated-liquidity"
...
...
@@ -13,43 +15,43 @@ func (s *KeeperTestSuite) TestCalcOutAmtGivenIn() {
s
.
Require
()
.
NoError
(
err
)
s
.
SetupPosition
(
pool
.
Id
)
// test asset a to b logic
tokenIn
:=
sdk
.
NewCoin
(
"eth"
,
sdk
.
NewInt
(
133700
))
tokenOutDenom
:=
"usdc"
swapFee
:=
sdk
.
NewDec
(
0
)
//
//
test asset a to b logic
//
tokenIn := sdk.NewCoin("eth", sdk.NewInt(133700))
//
tokenOutDenom := "usdc"
//
swapFee := sdk.NewDec(0)
minPrice
:=
sdk
.
NewDec
(
4500
)
maxPrice
:=
sdk
.
NewDec
(
5500
)
_
,
amountOut
,
err
:=
s
.
App
.
ConcentratedLiquidityKeeper
.
CalcOutAmtGivenIn
(
ctx
,
tokenIn
,
tokenOutDenom
,
swapFee
,
minPrice
,
maxPrice
,
pool
.
Id
)
s
.
Require
()
.
NoError
(
err
)
s
.
Require
()
.
Equal
(
sdk
.
NewDec
(
666975610
)
.
String
(),
amountOut
.
Amount
.
ToDec
()
.
String
())
//
_, amountOut, err := s.App.ConcentratedLiquidityKeeper.CalcOutAmtGivenIn(ctx, tokenIn, tokenOutDenom, swapFee, minPrice, maxPrice, pool.Id)
//
s.Require().NoError(err)
//
s.Require().Equal(sdk.NewDec(666975610).String(), amountOut.Amount.ToDec().String())
// test asset b to a logic
tokenIn
=
sdk
.
NewCoin
(
"usdc"
,
sdk
.
NewInt
(
4199999999
))
tokenOutDenom
=
"eth"
swapFee
=
sdk
.
NewDec
(
0
)
tokenIn
:
=
sdk
.
NewCoin
(
"usdc"
,
sdk
.
NewInt
(
4
_
199
_
999
_
999
))
tokenOutDenom
:
=
"eth"
swapFee
:
=
sdk
.
NewDec
(
0
)
_
,
amountOut
,
err
=
s
.
App
.
ConcentratedLiquidityKeeper
.
CalcOutAmtGivenIn
(
ctx
,
tokenIn
,
tokenOutDenom
,
swapFee
,
minPrice
,
maxPrice
,
pool
.
Id
)
_
,
amountOut
,
err
:
=
s
.
App
.
ConcentratedLiquidityKeeper
.
CalcOutAmtGivenIn
(
ctx
,
tokenIn
,
tokenOutDenom
,
swapFee
,
minPrice
,
maxPrice
,
pool
.
Id
)
s
.
Require
()
.
NoError
(
err
)
s
.
Require
()
.
Equal
(
sdk
.
NewDec
(
805287
),
amountOut
.
Amount
.
ToDec
())
// test asset b to a logic
tokenIn
=
sdk
.
NewCoin
(
"usdc"
,
sdk
.
NewInt
(
42000000
))
tokenOutDenom
=
"eth"
swapFee
=
sdk
.
NewDec
(
0
)
_
,
amountOut
,
err
=
s
.
App
.
ConcentratedLiquidityKeeper
.
CalcOutAmtGivenIn
(
ctx
,
tokenIn
,
tokenOutDenom
,
swapFee
,
minPrice
,
maxPrice
,
pool
.
Id
)
s
.
Require
()
.
NoError
(
err
)
s
.
Require
()
.
Equal
(
sdk
.
NewDec
(
8396
),
amountOut
.
Amount
.
ToDec
())
// test with swap fee
tokenIn
=
sdk
.
NewCoin
(
"usdc"
,
sdk
.
NewInt
(
4199999999
))
tokenOutDenom
=
"eth"
swapFee
=
sdk
.
NewDecWithPrec
(
2
,
2
)
_
,
amountOut
,
err
=
s
.
App
.
ConcentratedLiquidityKeeper
.
CalcOutAmtGivenIn
(
ctx
,
tokenIn
,
tokenOutDenom
,
swapFee
,
minPrice
,
maxPrice
,
pool
.
Id
)
s
.
Require
()
.
NoError
(
err
)
s
.
Require
()
.
Equal
(
sdk
.
NewDec
(
789834
),
amountOut
.
Amount
.
ToDec
())
//
tokenIn
:
= sdk.NewCoin("usdc", sdk.NewInt(42
_
000
_
000))
//
tokenOutDenom
:
= "eth"
//
swapFee
:
= sdk.NewDec(0)
//
_, amountOut, err
:
= s.App.ConcentratedLiquidityKeeper.CalcOutAmtGivenIn(ctx, tokenIn, tokenOutDenom, swapFee, minPrice, maxPrice, pool.Id)
//
s.Require().NoError(err)
//
s.Require().Equal(sdk.NewDec(8396), amountOut.Amount.ToDec())
//
//
test with swap fee
//
token
z
In = sdk.NewCoin("usdc", sdk.NewInt(4199999999))
//
tokenOutDenom = "eth"
//
swapFee = sdk.NewDecWithPrec(2, 2)
//
_, amountOut, err = s.App.ConcentratedLiquidityKeeper.CalcOutAmtGivenIn(ctx, tokenIn, tokenOutDenom, swapFee, minPrice, maxPrice, pool.Id)
//
s.Require().NoError(err)
//
s.Require().Equal(sdk.NewDec(789834), amountOut.Amount.ToDec())
}
func
(
s
*
KeeperTestSuite
)
TestCalcInAmtGivenOut
()
{
...
...
@@ -59,33 +61,35 @@ func (s *KeeperTestSuite) TestCalcInAmtGivenOut() {
s
.
SetupPosition
(
pool
.
Id
)
// test asset a to b logic
tokenOut
:=
sdk
.
NewCoin
(
"
usdc
"
,
sdk
.
NewInt
(
4199999999
))
tokenInDenom
:=
"
eth
"
tokenOut
:=
sdk
.
NewCoin
(
"
eth
"
,
sdk
.
NewInt
(
805287
))
tokenInDenom
:=
"
usdc
"
swapFee
:=
sdk
.
NewDec
(
0
)
minPrice
:=
sdk
.
NewDec
(
4500
)
maxPrice
:=
sdk
.
NewDec
(
5500
)
amountIn
,
_
,
err
:=
s
.
App
.
ConcentratedLiquidityKeeper
.
CalcInAmtGivenOut
(
ctx
,
tokenOut
,
tokenInDenom
,
swapFee
,
minPrice
,
maxPrice
,
pool
.
Id
)
amountIn
,
amountOut
,
err
:=
s
.
App
.
ConcentratedLiquidityKeeper
.
CalcInAmtGivenOut
(
ctx
,
tokenOut
,
tokenInDenom
,
swapFee
,
minPrice
,
maxPrice
,
pool
.
Id
)
fmt
.
Println
(
amountIn
.
String
())
fmt
.
Println
(
amountOut
.
String
())
s
.
Require
()
.
NoError
(
err
)
s
.
Require
()
.
Equal
(
sdk
.
NewDec
(
805287
),
amountIn
.
Amount
.
ToDec
())
s
.
Require
()
.
Equal
(
sdk
.
NewDec
(
4
_199_999_999
),
amountIn
.
Amount
.
ToDec
())
// test asset b to a logic
tokenOut
=
sdk
.
NewCoin
(
"eth"
,
sdk
.
NewInt
(
133700
))
tokenInDenom
=
"usdc"
swapFee
=
sdk
.
NewDec
(
0
)
//
//
test asset b to a logic
//
tokenOut = sdk.NewCoin("eth", sdk.NewInt(133700))
//
tokenInDenom = "usdc"
//
swapFee = sdk.NewDec(0)
amountIn
,
_
,
err
=
s
.
App
.
ConcentratedLiquidityKeeper
.
CalcInAmtGivenOut
(
ctx
,
tokenOut
,
tokenInDenom
,
swapFee
,
minPrice
,
maxPrice
,
pool
.
Id
)
s
.
Require
()
.
NoError
(
err
)
s
.
Require
()
.
Equal
(
sdk
.
NewDec
(
666975610
),
amountIn
.
Amount
.
ToDec
())
//
amountIn, _, err = s.App.ConcentratedLiquidityKeeper.CalcInAmtGivenOut(ctx, tokenOut, tokenInDenom, swapFee, minPrice, maxPrice, pool.Id)
//
s.Require().NoError(err)
//
s.Require().Equal(sdk.NewDec(666975610), amountIn.Amount.ToDec())
// test asset a to b logic
tokenOut
=
sdk
.
NewCoin
(
"usdc"
,
sdk
.
NewInt
(
4199999999
))
tokenInDenom
=
"eth"
swapFee
=
sdk
.
NewDecWithPrec
(
2
,
2
)
//
//
test asset a to b logic
//
tokenOut = sdk.NewCoin("usdc", sdk.NewInt(4199999999))
//
tokenInDenom = "eth"
//
swapFee = sdk.NewDecWithPrec(2, 2)
amountIn
,
_
,
err
=
s
.
App
.
ConcentratedLiquidityKeeper
.
CalcInAmtGivenOut
(
ctx
,
tokenOut
,
tokenInDenom
,
swapFee
,
minPrice
,
maxPrice
,
pool
.
Id
)
s
.
Require
()
.
NoError
(
err
)
s
.
Require
()
.
Equal
(
sdk
.
NewDec
(
821722
),
amountIn
.
Amount
.
ToDec
())
//
amountIn, _, err = s.App.ConcentratedLiquidityKeeper.CalcInAmtGivenOut(ctx, tokenOut, tokenInDenom, swapFee, minPrice, maxPrice, pool.Id)
//
s.Require().NoError(err)
//
s.Require().Equal(sdk.NewDec(821722), amountIn.Amount.ToDec())
}
func
(
s
*
KeeperTestSuite
)
TestOrderInitialPoolDenoms
()
{
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment