-
Notifications
You must be signed in to change notification settings - Fork 200
/
pool_asset.go
50 lines (39 loc) · 1.25 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
package types
import (
"errors"
fmt "fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Validates a PoolAsset amount and weights.
func (poolAsset PoolAsset) Validate() error {
if poolAsset.Token.Amount.LTE(sdk.ZeroInt()) {
return fmt.Errorf("can't add the zero or negative balance of token")
}
if poolAsset.Weight.LTE(sdk.ZeroInt()) {
return fmt.Errorf("a token's weight in the pool must be greater than 0")
}
if poolAsset.Weight.GTE(MaxUserSpecifiedWeight.MulRaw(GuaranteedWeightPrecision)) {
return fmt.Errorf("a token's weight in the pool must be less than 1^50")
}
return nil
}
/*
Subtracts an amount of coins from a pool's assets.
Throws an error if the final amount is less than zero.
*/
func (pool *Pool) SubtractPoolAssetBalance(assetDenom string, subAmt sdk.Int) (err error) {
if subAmt.LT(sdk.ZeroInt()) {
return errors.New("can't subtract a negative amount")
}
index, poolAsset, err := pool.getPoolAssetAndIndex(assetDenom)
if err != nil {
return err
}
// Update the supply of the asset
poolAsset.Token.Amount = poolAsset.Token.Amount.Sub(subAmt)
if poolAsset.Token.Amount.LT(sdk.ZeroInt()) {
return errors.New("can't set the pool's balance of a token to be zero or negative")
}
pool.PoolAssets[index] = poolAsset
return nil
}