-
Notifications
You must be signed in to change notification settings - Fork 44
/
pool_calc_join_pool_no_swap_shares.go
115 lines (99 loc) · 4.84 KB
/
pool_calc_join_pool_no_swap_shares.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
package types
import (
"errors"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// MaximalExactRatioJoin calculates the maximal amount of tokens that can be joined whilst maintaining pool asset's ratio
// returning the number of shares that'd be and how many coins would be left over.
//
// e.g) suppose we have a pool of 10 foo tokens and 10 bar tokens, with the total amount of 100 shares.
// if `tokensIn` provided is 1 foo token and 2 bar tokens, `MaximalExactRatioJoin`
// would be returning (10 shares, 1 bar token, nil)
//
// This can be used when `tokensIn` are not guaranteed the same ratio as assets in the pool.
// Calculation for this is done in the following steps.
// 1. iterate through all the tokens provided as an argument, calculate how much ratio it accounts for the asset in the pool
// 2. get the minimal share ratio that would work as the benchmark for all tokens.
// 3. calculate the number of shares that could be joined (total share * min share ratio), return the remaining coins
func MaximalExactRatioJoin(p *Pool, tokensIn sdk.Coins) (numShares math.Int, remCoins sdk.Coins, err error) {
coinShareRatios := make([]sdk.Dec, len(tokensIn))
minShareRatio := sdk.MaxSortableDec
maxShareRatio := sdk.ZeroDec()
poolLiquidity := p.GetTotalPoolLiquidity()
totalShares := p.GetTotalShares()
for i, coin := range tokensIn {
// Note: QuoInt implements floor division, unlike Quo
// This is because it calls the native golang routine big.Int.Quo
// https://pkg.go.dev/math/big#Int.Quo
shareRatio := sdk.NewDecFromBigInt(coin.Amount.BigInt()).QuoInt(poolLiquidity.AmountOfNoDenomValidation(coin.Denom))
if shareRatio.LT(minShareRatio) {
minShareRatio = shareRatio
}
if shareRatio.GT(maxShareRatio) {
maxShareRatio = shareRatio
}
coinShareRatios[i] = shareRatio
}
if minShareRatio.Equal(sdk.MaxSortableDec) {
return numShares, remCoins, errors.New("unexpected error in MaximalExactRatioJoin")
}
remCoins = sdk.Coins{}
// critically we round down here (TruncateInt), to ensure that the returned LP shares
// are always less than or equal to % liquidity added.
numShares = minShareRatio.MulInt(totalShares.Amount).TruncateInt()
// if we have multiple share values, calculate remainingCoins
if !minShareRatio.Equal(maxShareRatio) {
// we have to calculate remCoins
for i, coin := range tokensIn {
// if coinShareRatios[i] == minShareRatio, no remainder
if coinShareRatios[i].Equal(minShareRatio) {
continue
}
usedAmount := minShareRatio.MulInt(poolLiquidity.AmountOfNoDenomValidation(coin.Denom)).Ceil().TruncateInt()
newAmt := coin.Amount.Sub(usedAmount)
// if newAmt is non-zero, add to RemCoins. (It could be zero due to rounding)
if !newAmt.IsZero() {
remCoins = remCoins.Add(sdk.Coin{Denom: coin.Denom, Amount: newAmt})
}
}
}
return numShares, remCoins, nil
}
// CalcJoinPoolNoSwapShares calculates the number of shares created to execute an all-asset pool join with the provided amount of `tokensIn`.
// The input tokens must contain the same tokens as in the pool.
//
// Returns the number of shares created, the amount of coins actually joined into the pool, (in case of not being able to fully join),
// and the remaining tokens in `tokensIn` after joining. If an all-asset join is not possible, returns an error.
//
// Since CalcJoinPoolNoSwapShares is non-mutative, the steps for updating pool shares / liquidity are
// more complex / don't just alter the state.
// We should simplify this logic further in the future using multi-join equations.
func (p *Pool) CalcJoinPoolNoSwapShares(tokensIn sdk.Coins) (numShares math.Int, tokensJoined sdk.Coins, err error) {
// get all 'pool assets' (aka current pool liquidity + balancer weight)
poolAssetsByDenom, err := GetPoolAssetsByDenom(p.GetAllPoolAssets())
if err != nil {
return sdk.ZeroInt(), sdk.NewCoins(), err
}
err = EnsureDenomInPool(poolAssetsByDenom, tokensIn)
if err != nil {
return sdk.ZeroInt(), sdk.NewCoins(), err
}
// ensure that there aren't too many or too few assets in `tokensIn`
if tokensIn.Len() != len(p.PoolAssets) {
return sdk.ZeroInt(), sdk.NewCoins(), errors.New("no-swap joins require LP'ing with all assets in pool")
}
// execute a no-swap join with as many tokens as possible given a perfect ratio:
// * numShares is how many shares are perfectly matched.
// * remainingTokensIn is how many coins we have left to join that have not already been used.
numShares, remainingTokensIn, err := MaximalExactRatioJoin(p, tokensIn)
if err != nil {
return sdk.ZeroInt(), sdk.NewCoins(), err
}
// ensure that no more tokens have been joined than is possible with the given `tokensIn`
tokensJoined = tokensIn.Sub(remainingTokensIn...)
if tokensJoined.IsAnyGT(tokensIn) {
return sdk.ZeroInt(), sdk.NewCoins(), errors.New("an error has occurred, more coins joined than token In")
}
return numShares, tokensJoined, nil
}