-
Notifications
You must be signed in to change notification settings - Fork 50
/
pool_calc_join_pool_shares.go
63 lines (56 loc) · 2.21 KB
/
pool_calc_join_pool_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
package types
import (
"errors"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// calcPoolOutGivenSingleIn - balance pAo.
func (p *Pool) calcSingleAssetJoin(tokenIn sdk.Coin, spreadFactor sdk.Dec, tokenInPoolAsset PoolAsset, totalShares math.Int) (numShares math.Int, err error) {
totalWeight := p.TotalWeight
if totalWeight.IsZero() {
return sdk.ZeroInt(), errors.New("pool misconfigured, total weight = 0")
}
normalizedWeight := sdk.NewDecFromInt(tokenInPoolAsset.Weight).Quo(sdk.NewDecFromInt(totalWeight))
poolShares, err := calcPoolSharesOutGivenSingleAssetIn(
sdk.NewDecFromInt(tokenInPoolAsset.Token.Amount),
normalizedWeight,
sdk.NewDecFromInt(totalShares),
sdk.NewDecFromInt(tokenIn.Amount),
spreadFactor,
)
if err != nil {
return sdk.ZeroInt(), err
}
return poolShares.TruncateInt(), nil
}
// CalcJoinPoolShares calculates the number of shares created to join pool with the provided amount of `tokenIn`.
// The input tokens must either be:
// - a single token
// - contain exactly the same tokens as the pool contains
//
// It returns the number of shares created, the amount of coins actually joined into the pool
// (in case of not being able to fully join), or an error.
func (p *Pool) CalcSingleAssetJoinPoolShares(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() != 1 {
return sdk.ZeroInt(), sdk.NewCoins(), errors.New("pool only supports LP'ing with one asset")
}
// 2) Single token provided, so do single asset join and exit.
totalShares := p.GetTotalShares()
numShares, err = p.calcSingleAssetJoin(tokensIn[0], p.PoolParams.SwapFee, poolAssetsByDenom[tokensIn[0].Denom], totalShares.Amount)
if err != nil {
return sdk.ZeroInt(), sdk.NewCoins(), err
}
// we join all the tokens.
tokensJoined = tokensIn
return numShares, tokensJoined, nil
}