/
routable_concentrated_pool.go
192 lines (158 loc) · 6.85 KB
/
routable_concentrated_pool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package pools
import (
"fmt"
"cosmossdk.io/math"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/osmosis-labs/osmosis/osmomath"
"github.com/osmosis-labs/osmosis/v21/ingest/sqs/domain"
clmath "github.com/osmosis-labs/osmosis/v21/x/concentrated-liquidity/math"
concentratedmodel "github.com/osmosis-labs/osmosis/v21/x/concentrated-liquidity/model"
"github.com/osmosis-labs/osmosis/v21/x/concentrated-liquidity/swapstrategy"
"github.com/osmosis-labs/osmosis/v21/x/poolmanager"
poolmanagertypes "github.com/osmosis-labs/osmosis/v21/x/poolmanager/types"
)
var _ domain.RoutablePool = &routableConcentratedPoolImpl{}
type routableConcentratedPoolImpl struct {
ChainPool *concentratedmodel.Pool "json:\"cl_pool\""
TickModel *domain.TickModel "json:\"tick_model\""
TokenOutDenom string "json:\"token_out_denom\""
TakerFee osmomath.Dec "json:\"taker_fee\""
}
// GetPoolDenoms implements domain.RoutablePool.
func (r *routableConcentratedPoolImpl) GetPoolDenoms() []string {
return r.ChainPool.GetPoolDenoms(sdk.Context{})
}
// GetType implements domain.RoutablePool.
func (r *routableConcentratedPoolImpl) GetType() poolmanagertypes.PoolType {
return poolmanagertypes.Concentrated
}
// GetId implements domain.RoutablePool.
func (r *routableConcentratedPoolImpl) GetId() uint64 {
return r.ChainPool.Id
}
// GetSpreadFactor implements domain.RoutablePool.
func (r *routableConcentratedPoolImpl) GetSpreadFactor() math.LegacyDec {
return r.ChainPool.SpreadFactor
}
// GetTakerFee implements domain.RoutablePool.
func (r *routableConcentratedPoolImpl) GetTakerFee() math.LegacyDec {
return r.TakerFee
}
// CalculateTokenOutByTokenIn implements domain.RoutablePool.
// It calculates the amount of token out given the amount of token in for a concentrated liquidity pool.
// Fails if:
// - the underlying chain pool set on the routable pool is not of concentrated type
// - fails to retrieve the tick model for the pool
// - the current tick is not within the specified current bucket range
// - tick model has no liquidity flag set
// - the current sqrt price is zero
// - rans out of ticks during swap (token in is too high for liquidity in the pool)
func (r *routableConcentratedPoolImpl) CalculateTokenOutByTokenIn(tokenIn sdk.Coin) (sdk.Coin, error) {
concentratedPool := r.ChainPool
tickModel := r.TickModel
if tickModel == nil {
return sdk.Coin{}, domain.ConcentratedPoolNoTickModelError{
PoolId: r.ChainPool.Id,
}
}
// Ensure pool has liquidity.
if tickModel.HasNoLiquidity {
return sdk.Coin{}, domain.ConcentratedNoLiquidityError{
PoolId: concentratedPool.Id,
}
}
// Ensure that the current bucket is within the available bucket range.
currentBucketIndex := tickModel.CurrentTickIndex
if currentBucketIndex < 0 || currentBucketIndex >= int64(len(tickModel.Ticks)) {
return sdk.Coin{}, domain.ConcentratedCurrentTickNotWithinBucketError{
PoolId: concentratedPool.Id,
CurrentBucketIndex: currentBucketIndex,
TotalBuckets: int64(len(tickModel.Ticks)),
}
}
currentBucket := tickModel.Ticks[currentBucketIndex]
isCurrentTickWithinBucket := concentratedPool.IsCurrentTickInRange(currentBucket.LowerTick, currentBucket.UpperTick)
if !isCurrentTickWithinBucket {
return sdk.Coin{}, domain.ConcentratedCurrentTickAndBucketMismatchError{
CurrentTick: concentratedPool.CurrentTick,
LowerTick: currentBucket.LowerTick,
UpperTick: currentBucket.UpperTick,
}
}
// Set the appropriate token out denom.
isZeroForOne := tokenIn.Denom == concentratedPool.Token0
tokenOutDenom := concentratedPool.Token0
if isZeroForOne {
tokenOutDenom = concentratedPool.Token1
}
// Initialize the swap strategy.
swapStrategy := swapstrategy.New(isZeroForOne, osmomath.ZeroBigDec(), &storetypes.KVStoreKey{}, concentratedPool.SpreadFactor)
var (
// Swap state
currentSqrtPrice = concentratedPool.GetCurrentSqrtPrice()
amountRemainingIn = tokenIn.Amount.ToLegacyDec()
amountOutTotal = osmomath.ZeroDec()
)
if currentSqrtPrice.IsZero() {
return sdk.Coin{}, domain.ConcentratedZeroCurrentSqrtPriceError{
PoolId: concentratedPool.Id,
}
}
// Compute swap over all buckets.
for amountRemainingIn.GT(osmomath.ZeroDec()) {
if currentBucketIndex >= int64(len(tickModel.Ticks)) || currentBucketIndex < 0 {
// This happens when there is not enough liquidity in the pool to complete the swap
// for a given amount of token in.
return sdk.Coin{}, domain.ConcentratedNotEnoughLiquidityToCompleteSwapError{
PoolId: concentratedPool.Id,
AmountIn: sdk.NewCoins(tokenIn).String(),
}
}
currentBucket = tickModel.Ticks[currentBucketIndex]
// Compute the next initialized tick index depending on the swap direction.
// Zero for one - in the lower tick direction.
// One for zero - in the upper tick direction.
var nextInitializedTickIndex int64
if isZeroForOne {
nextInitializedTickIndex = currentBucket.LowerTick
currentBucketIndex--
} else {
nextInitializedTickIndex = currentBucket.UpperTick
currentBucketIndex++
}
// Get the sqrt price for the next initialized tick index.
sqrtPriceTarget, err := clmath.TickToSqrtPrice(nextInitializedTickIndex)
if err != nil {
return sdk.Coin{}, err
}
// Compute the swap within current bucket
sqrtPriceNext, amountInConsumed, amountOutComputed, spreadRewardChargeTotal := swapStrategy.ComputeSwapWithinBucketOutGivenIn(currentSqrtPrice, sqrtPriceTarget, currentBucket.LiquidityAmount, amountRemainingIn)
// Update swap state for next iteration
amountRemainingIn = amountRemainingIn.SubMut(amountInConsumed).SubMut(spreadRewardChargeTotal)
amountOutTotal = amountOutTotal.AddMut(amountOutComputed)
// Update current sqrt price
currentSqrtPrice = sqrtPriceNext
}
// Return the total amount out.
return sdk.NewCoin(tokenOutDenom, amountOutTotal.TruncateInt()), nil
}
// GetTokenOutDenom implements RoutablePool.
func (r *routableConcentratedPoolImpl) GetTokenOutDenom() string {
return r.TokenOutDenom
}
// String implements domain.RoutablePool.
func (r *routableConcentratedPoolImpl) String() string {
concentratedPool := r.ChainPool
return fmt.Sprintf("pool (%d), pool type (%d), pool denoms (%v), token out (%s)", concentratedPool.Id, poolmanagertypes.Concentrated, concentratedPool.GetPoolDenoms(sdk.Context{}), r.TokenOutDenom)
}
// ChargeTakerFee implements domain.RoutablePool.
// Charges the taker fee for the given token in and returns the token in after the fee has been charged.
func (r *routableConcentratedPoolImpl) ChargeTakerFeeExactIn(tokenIn sdk.Coin) (tokenInAfterFee sdk.Coin) {
tokenInAfterTakerFee, _ := poolmanager.CalcTakerFeeExactIn(tokenIn, r.GetTakerFee())
return tokenInAfterTakerFee
}
// SetTokenOutDenom implements domain.RoutablePool.
func (r *routableConcentratedPoolImpl) SetTokenOutDenom(tokenOutDenom string) {
r.TokenOutDenom = tokenOutDenom
}