-
Notifications
You must be signed in to change notification settings - Fork 44
/
pool_join_pool_no_swap.go
195 lines (169 loc) · 6.47 KB
/
pool_join_pool_no_swap.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
193
194
195
package types
import (
fmt "fmt"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type PoolAssetUSDValue struct {
Asset string
Value sdk.Dec
}
type InternalSwapRequest struct {
InAmount sdk.Coin
OutToken string
}
func (p *Pool) CalcJoinValueWithoutSlippage(ctx sdk.Context, oracleKeeper OracleKeeper, accountedPoolKeeper AccountedPoolKeeper, tokensIn sdk.Coins) (math.LegacyDec, error) {
joinValue := sdk.ZeroDec()
for _, asset := range tokensIn {
tokenPrice := oracleKeeper.GetAssetPriceFromDenom(ctx, asset.Denom)
if tokenPrice.IsZero() {
return sdk.ZeroDec(), fmt.Errorf("token price not set: %s", asset.Denom)
}
v := tokenPrice.Mul(sdk.NewDecFromInt(asset.Amount))
joinValue = joinValue.Add(v)
}
return joinValue, nil
// Note: Disable slippage handling for oracle pool due to 1 hour lockup on oracle lp
// // weights := NormalizedWeights(p.PoolAssets)
// weights, err := OraclePoolNormalizedWeights(ctx, oracleKeeper, p.PoolAssets)
// if err != nil {
// return sdk.ZeroDec(), err
// }
// inAmounts := []PoolAssetUSDValue{}
// outAmounts := []PoolAssetUSDValue{}
// for _, weight := range weights {
// targetAmount := joinValue.Mul(weight.Weight)
// tokenPrice := oracleKeeper.GetAssetPriceFromDenom(ctx, weight.Asset)
// if tokenPrice.IsZero() {
// return sdk.ZeroDec(), fmt.Errorf("token price not set: %s", weight.Asset)
// }
// inAmount := tokenPrice.Mul(sdk.NewDecFromInt(tokensIn.AmountOf(weight.Asset)))
// if targetAmount.GT(inAmount) {
// outAmounts = append(outAmounts, PoolAssetUSDValue{
// Asset: weight.Asset,
// Value: targetAmount.Sub(inAmount),
// })
// }
// if targetAmount.LT(inAmount) {
// inAmounts = append(inAmounts, PoolAssetUSDValue{
// Asset: weight.Asset,
// Value: inAmount.Sub(targetAmount),
// })
// }
// }
// internalSwapRequests := []InternalSwapRequest{}
// for i, j := 0, 0; i < len(inAmounts) && j < len(outAmounts); {
// inTokenPrice := oracleKeeper.GetAssetPriceFromDenom(ctx, inAmounts[i].Asset)
// if inTokenPrice.IsZero() {
// return sdk.ZeroDec(), fmt.Errorf("token price not set: %s", inAmounts[i].Asset)
// }
// inAsset := inAmounts[i].Asset
// outAsset := outAmounts[j].Asset
// inAmount := sdk.ZeroInt()
// if inAmounts[i].Value.GT(outAmounts[j].Value) {
// inAmount = outAmounts[j].Value.Quo(inTokenPrice).RoundInt()
// j++
// } else if inAmounts[i].Value.LT(outAmounts[j].Value) {
// inAmount = inAmounts[i].Value.Quo(inTokenPrice).RoundInt()
// i++
// } else {
// inAmount = inAmounts[i].Value.Quo(inTokenPrice).RoundInt()
// i++
// j++
// }
// internalSwapRequests = append(internalSwapRequests, InternalSwapRequest{
// InAmount: sdk.NewCoin(inAsset, inAmount),
// OutToken: outAsset,
// })
// }
// slippageValue := sdk.ZeroDec()
// for _, req := range internalSwapRequests {
// inTokenPrice := oracleKeeper.GetAssetPriceFromDenom(ctx, req.InAmount.Denom)
// if inTokenPrice.IsZero() {
// return sdk.ZeroDec(), fmt.Errorf("token price not set: %s", req.InAmount.Denom)
// }
// resizedAmount := sdk.NewDecFromInt(req.InAmount.Amount).
// Quo(p.PoolParams.ExternalLiquidityRatio).RoundInt()
// slippageAmount, err := p.CalcGivenInSlippage(
// ctx,
// oracleKeeper,
// p,
// sdk.Coins{sdk.NewCoin(req.InAmount.Denom, resizedAmount)},
// req.OutToken,
// accountedPoolKeeper,
// )
// if err != nil {
// return sdk.ZeroDec(), err
// }
// slippageValue = slippageValue.Add(slippageAmount.Mul(inTokenPrice))
// }
// joinValueWithoutSlippage := joinValue.Sub(slippageValue)
// return joinValueWithoutSlippage, nil
}
// JoinPool calculates the number of shares needed for an all-asset join given tokensIn with swapFee applied.
// It updates the liquidity if the pool is joined successfully. If not, returns error.
func (p *Pool) JoinPool(ctx sdk.Context, oracleKeeper OracleKeeper, accountedPoolKeeper AccountedPoolKeeper, tokensIn sdk.Coins) (numShares math.Int, err error) {
// if it's not single sided liquidity, add at pool ratio
if len(tokensIn) != 1 {
numShares, tokensJoined, err := p.CalcJoinPoolNoSwapShares(tokensIn)
if err != nil {
return math.Int{}, err
}
// update pool with the calculated share and liquidity needed to join pool
p.IncreaseLiquidity(numShares, tokensJoined)
return numShares, nil
}
if !p.PoolParams.UseOracle {
numShares, tokensJoined, err := p.CalcSingleAssetJoinPoolShares(tokensIn)
if err != nil {
return math.Int{}, err
}
// update pool with the calculated share and liquidity needed to join pool
p.IncreaseLiquidity(numShares, tokensJoined)
return numShares, nil
}
joinValueWithoutSlippage, err := p.CalcJoinValueWithoutSlippage(ctx, oracleKeeper, accountedPoolKeeper, tokensIn)
if err != nil {
return sdk.ZeroInt(), err
}
initialWeightDistance := p.WeightDistanceFromTarget(ctx, oracleKeeper, p.PoolAssets)
tvl, err := p.TVL(ctx, oracleKeeper)
if err != nil {
return sdk.ZeroInt(), err
}
// Ensure tvl is not zero to avoid division by zero
if tvl.IsZero() {
return sdk.ZeroInt(), ErrAmountTooLow
}
newAssetPools, err := p.NewPoolAssetsAfterSwap(tokensIn, sdk.Coins{})
if err != nil {
return sdk.ZeroInt(), err
}
weightDistance := p.WeightDistanceFromTarget(ctx, oracleKeeper, newAssetPools)
distanceDiff := weightDistance.Sub(initialWeightDistance)
weightBreakingFee := sdk.ZeroDec()
if distanceDiff.IsPositive() {
// old weight breaking fee implementation
// weightBreakingFee = p.PoolParams.WeightBreakingFeeMultiplier.Mul(distanceDiff)
// we only allow
tokenInDenom := tokensIn[0].Denom
// target weight
targetWeightIn := NormalizedWeight(ctx, p.PoolAssets, tokenInDenom)
targetWeightOut := sdk.OneDec().Sub(targetWeightIn)
// weight breaking fee as in Plasma pool
weightIn := OracleAssetWeight(ctx, oracleKeeper, newAssetPools, tokenInDenom)
weightOut := sdk.OneDec().Sub(weightIn)
weightBreakingFee = GetWeightBreakingFee(weightIn, weightOut, targetWeightIn, targetWeightOut, p.PoolParams)
}
weightBalanceBonus := sdk.ZeroDec()
if initialWeightDistance.GT(p.PoolParams.ThresholdWeightDifference) && distanceDiff.IsNegative() {
weightBalanceBonus = p.PoolParams.WeightBreakingFeeMultiplier.Mul(distanceDiff).Abs()
}
totalShares := p.GetTotalShares()
numSharesDec := sdk.NewDecFromInt(totalShares.Amount).
Mul(joinValueWithoutSlippage).Quo(tvl).
Mul(sdk.OneDec().Add(weightBalanceBonus).Sub(weightBreakingFee))
numShares = numSharesDec.RoundInt()
p.IncreaseLiquidity(numShares, tokensIn)
return numShares, nil
}