2018-06-26 19:00:12 -07:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-06-26 19:00:12 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
2019-02-07 17:41:23 -08:00
|
|
|
// Pool - tracking bonded and not-bonded token supply of the bond denomination
|
2018-06-26 19:00:12 -07:00
|
|
|
type Pool struct {
|
2019-02-07 17:41:23 -08:00
|
|
|
NotBondedTokens sdk.Int `json:"not_bonded_tokens"` // tokens which are not bonded to a validator (unbonded or unbonding)
|
|
|
|
BondedTokens sdk.Int `json:"bonded_tokens"` // tokens which are currently bonded to a validator
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// nolint
|
2019-02-07 17:41:23 -08:00
|
|
|
// TODO: This is slower than comparing struct fields directly
|
2018-06-26 19:00:12 -07:00
|
|
|
func (p Pool) Equal(p2 Pool) bool {
|
2018-11-04 18:28:38 -08:00
|
|
|
bz1 := MsgCdc.MustMarshalBinaryLengthPrefixed(&p)
|
|
|
|
bz2 := MsgCdc.MustMarshalBinaryLengthPrefixed(&p2)
|
2018-06-26 19:00:12 -07:00
|
|
|
return bytes.Equal(bz1, bz2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// initial pool for testing
|
|
|
|
func InitialPool() Pool {
|
|
|
|
return Pool{
|
2019-01-22 09:28:39 -08:00
|
|
|
NotBondedTokens: sdk.ZeroInt(),
|
|
|
|
BondedTokens: sdk.ZeroInt(),
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sum total of all staking tokens in the pool
|
2019-01-02 12:29:47 -08:00
|
|
|
func (p Pool) TokenSupply() sdk.Int {
|
2019-01-21 16:52:03 -08:00
|
|
|
return p.NotBondedTokens.Add(p.BondedTokens)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2019-02-07 17:41:23 -08:00
|
|
|
// Get the fraction of the staking token which is currently bonded
|
2018-08-14 17:15:02 -07:00
|
|
|
func (p Pool) BondedRatio() sdk.Dec {
|
2018-07-13 13:46:14 -07:00
|
|
|
supply := p.TokenSupply()
|
2019-01-02 12:29:47 -08:00
|
|
|
if supply.IsPositive() {
|
2019-02-21 00:05:31 -08:00
|
|
|
return p.BondedTokens.ToDec().QuoInt(supply)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
2018-08-14 17:15:02 -07:00
|
|
|
return sdk.ZeroDec()
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2019-01-21 16:52:03 -08:00
|
|
|
func (p Pool) notBondedTokensToBonded(bondedTokens sdk.Int) Pool {
|
2018-07-13 13:46:14 -07:00
|
|
|
p.BondedTokens = p.BondedTokens.Add(bondedTokens)
|
2019-01-21 16:52:03 -08:00
|
|
|
p.NotBondedTokens = p.NotBondedTokens.Sub(bondedTokens)
|
|
|
|
if p.NotBondedTokens.IsNegative() {
|
|
|
|
panic(fmt.Sprintf("sanity check: not-bonded tokens negative, pool: %v", p))
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
2018-07-13 13:46:14 -07:00
|
|
|
return p
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2019-01-21 16:52:03 -08:00
|
|
|
func (p Pool) bondedTokensToNotBonded(bondedTokens sdk.Int) Pool {
|
2018-07-13 13:46:14 -07:00
|
|
|
p.BondedTokens = p.BondedTokens.Sub(bondedTokens)
|
2019-01-21 16:52:03 -08:00
|
|
|
p.NotBondedTokens = p.NotBondedTokens.Add(bondedTokens)
|
2019-01-02 12:29:47 -08:00
|
|
|
if p.BondedTokens.IsNegative() {
|
2018-07-13 13:46:14 -07:00
|
|
|
panic(fmt.Sprintf("sanity check: bonded tokens negative, pool: %v", p))
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
2018-07-13 13:46:14 -07:00
|
|
|
return p
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2019-01-22 09:28:39 -08:00
|
|
|
// String returns a human readable string representation of a pool.
|
|
|
|
func (p Pool) String() string {
|
|
|
|
return fmt.Sprintf(`Pool:
|
2019-01-25 03:12:04 -08:00
|
|
|
Loose Tokens: %s
|
2019-01-22 09:28:39 -08:00
|
|
|
Bonded Tokens: %s
|
2019-01-25 03:12:04 -08:00
|
|
|
Token Supply: %s
|
|
|
|
Bonded Ratio: %v`, p.NotBondedTokens,
|
2019-01-22 09:28:39 -08:00
|
|
|
p.BondedTokens, p.TokenSupply(),
|
|
|
|
p.BondedRatio())
|
2018-08-21 08:32:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshal the current pool value from store key or panics
|
2018-09-13 11:17:32 -07:00
|
|
|
func MustUnmarshalPool(cdc *codec.Codec, value []byte) Pool {
|
2018-08-21 08:32:54 -07:00
|
|
|
pool, err := UnmarshalPool(cdc, value)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return pool
|
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshal the current pool value from store key
|
2018-09-13 11:17:32 -07:00
|
|
|
func UnmarshalPool(cdc *codec.Codec, value []byte) (pool Pool, err error) {
|
2018-11-04 18:28:38 -08:00
|
|
|
err = cdc.UnmarshalBinaryLengthPrefixed(value, &pool)
|
2018-08-21 08:32:54 -07:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|