-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool_asset.go
116 lines (97 loc) · 3.09 KB
/
pool_asset.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
package balancer
import (
"fmt"
"sort"
"strings"
"gopkg.in/yaml.v2"
sdkioerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/nephirim/blackfury/osmosis-types/gamm"
)
type poolAssetPretty struct {
Token sdk.Coin `json:"token" yaml:"token"`
Weight sdk.Dec `json:"weight" yaml:"weight"`
}
// validates a pool asset, to check if it has a valid weight.
func (pa PoolAsset) validateWeight() error {
if pa.Weight.LTE(sdk.ZeroInt()) {
return fmt.Errorf("a token's weight in the pool must be greater than 0")
}
// TODO: add validation for asset weight overflow:
// https://github.com/osmosis-labs/osmosis/issues/1958
return nil
}
func (pa PoolAsset) prettify() poolAssetPretty {
return poolAssetPretty{
Weight: sdk.NewDecFromInt(pa.Weight).QuoInt64(GuaranteedWeightPrecision),
Token: pa.Token,
}
}
// MarshalYAML returns the YAML representation of a PoolAsset.
// This is assumed to not be called on a stand-alone instance, so it removes the first marshalled line.
func (pa PoolAsset) MarshalYAML() (interface{}, error) {
bz, err := yaml.Marshal(pa.prettify())
if err != nil {
return nil, err
}
s := string(bz)
return s, nil
}
// sortPoolAssetsOutOfPlaceByDenom sorts pool assets in place, by weight
// Doesn't deep copy the underlying weights, but it does place the assets
// into a new slice.
func sortPoolAssetsOutOfPlaceByDenom(assets []PoolAsset) []PoolAsset {
assets_copy := make([]PoolAsset, len(assets))
copy(assets_copy, assets)
sortPoolAssetsByDenom(assets_copy)
return assets_copy
}
// sortPoolAssetsByDenom sorts pool assets in place, by weight.
func sortPoolAssetsByDenom(assets []PoolAsset) {
sort.Slice(assets, func(i, j int) bool {
PoolAssetA := assets[i]
PoolAssetB := assets[j]
return strings.Compare(PoolAssetA.Token.Denom, PoolAssetB.Token.Denom) == -1
})
}
func validateUserSpecifiedPoolAssets(assets []PoolAsset) error {
// The pool must be swapping between at least two assets
if len(assets) < 2 {
return gamm.ErrTooFewPoolAssets
}
if len(assets) > 8 {
return sdkioerrors.Wrapf(gamm.ErrTooManyPoolAssets, "%d", len(assets))
}
assetExistsMap := map[string]bool{}
for _, asset := range assets {
err := ValidateUserSpecifiedWeight(asset.Weight)
if err != nil {
return err
}
if !asset.Token.IsValid() || !asset.Token.IsPositive() {
return sdkioerrors.Wrap(sdkerrors.ErrInvalidCoins, asset.Token.String())
}
if _, exists := assetExistsMap[asset.Token.Denom]; exists {
return sdkioerrors.Wrapf(gamm.ErrTooFewPoolAssets, "pool asset %s already exists", asset.Token.Denom)
}
assetExistsMap[asset.Token.Denom] = true
}
return nil
}
// poolAssetsCoins returns all the coins corresponding to a slice of pool assets.
func poolAssetsCoins(assets []PoolAsset) sdk.Coins {
coins := sdk.Coins{}
for _, asset := range assets {
coins = coins.Add(asset.Token)
}
return coins
}
func getPoolAssetByDenom(assets []PoolAsset, denom string) (PoolAsset, bool) {
for _, asset := range assets {
if asset.Token.Denom == denom {
return asset, true
}
}
return PoolAsset{}, false
}