package types
import (
"strconv"
"strings"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func PoolName(reserveCoinDenoms []string, poolTypeID uint32) string {
return strings.Join(append(SortDenoms(reserveCoinDenoms), strconv.FormatUint(uint64(poolTypeID), 10)), "/")
}
func (pool Pool) Name() string {
return PoolName(pool.ReserveCoinDenoms, pool.TypeId)
}
func (pool Pool) Validate() error {
if pool.Id == 0 {
return ErrPoolNotExists
}
if pool.TypeId == 0 {
return ErrPoolTypeNotExists
}
if pool.ReserveCoinDenoms == nil || len(pool.ReserveCoinDenoms) == 0 {
return ErrNumOfReserveCoinDenoms
}
if uint32(len(pool.ReserveCoinDenoms)) > MaxReserveCoinNum || uint32(len(pool.ReserveCoinDenoms)) < MinReserveCoinNum {
return ErrNumOfReserveCoinDenoms
}
sortedDenomA, sortedDenomB := AlphabeticalDenomPair(pool.ReserveCoinDenoms[0], pool.ReserveCoinDenoms[1])
if sortedDenomA != pool.ReserveCoinDenoms[0] || sortedDenomB != pool.ReserveCoinDenoms[1] {
return ErrBadOrderingReserveCoinDenoms
}
if pool.ReserveAccountAddress == "" {
return ErrEmptyReserveAccountAddress
}
if pool.ReserveAccountAddress != GetPoolReserveAcc(pool.Name(), false).String() {
return ErrBadReserveAccountAddress
}
if pool.PoolCoinDenom == "" {
return ErrEmptyPoolCoinDenom
}
if pool.PoolCoinDenom != pool.Name() {
return ErrBadPoolCoinDenom
}
return nil
}
func NewPoolBatch(poolID, batchIndex uint64) PoolBatch {
return PoolBatch{
PoolId: poolID,
Index: batchIndex,
BeginHeight: 0,
DepositMsgIndex: 1,
WithdrawMsgIndex: 1,
SwapMsgIndex: 1,
Executed: false,
}
}
func MustMarshalPool(cdc codec.BinaryCodec, liquidityPool Pool) []byte {
return cdc.MustMarshal(&liquidityPool)
}
func MustUnmarshalPool(cdc codec.BinaryCodec, value []byte) Pool {
liquidityPool, err := UnmarshalPool(cdc, value)
if err != nil {
panic(err)
}
return liquidityPool
}
func UnmarshalPool(cdc codec.BinaryCodec, value []byte) (liquidityPool Pool, err error) {
err = cdc.Unmarshal(value, &liquidityPool)
return liquidityPool, err
}
func (pool Pool) GetReserveAccount() sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(pool.ReserveAccountAddress)
if err != nil {
panic(err)
}
return addr
}
func (pool Pool) GetPoolCoinDenom() string { return pool.PoolCoinDenom }
func (pool Pool) GetId() uint64 { return pool.Id }
type Pools []Pool
func (pools Pools) String() (out string) {
for _, del := range pools {
out += del.String() + "\n"
}
return strings.TrimSpace(out)
}
func MustMarshalPoolBatch(cdc codec.BinaryCodec, poolBatch PoolBatch) []byte {
return cdc.MustMarshal(&poolBatch)
}
func UnmarshalPoolBatch(cdc codec.BinaryCodec, value []byte) (poolBatch PoolBatch, err error) {
err = cdc.Unmarshal(value, &poolBatch)
return poolBatch, err
}
func MustUnmarshalPoolBatch(cdc codec.BinaryCodec, value []byte) PoolBatch {
poolBatch, err := UnmarshalPoolBatch(cdc, value)
if err != nil {
panic(err)
}
return poolBatch
}
func MustMarshalDepositMsgState(cdc codec.BinaryCodec, msg DepositMsgState) []byte {
return cdc.MustMarshal(&msg)
}
func UnmarshalDepositMsgState(cdc codec.BinaryCodec, value []byte) (msg DepositMsgState, err error) {
err = cdc.Unmarshal(value, &msg)
return msg, err
}
func MustUnmarshalDepositMsgState(cdc codec.BinaryCodec, value []byte) DepositMsgState {
msg, err := UnmarshalDepositMsgState(cdc, value)
if err != nil {
panic(err)
}
return msg
}
func MustMarshalWithdrawMsgState(cdc codec.BinaryCodec, msg WithdrawMsgState) []byte {
return cdc.MustMarshal(&msg)
}
func UnmarshalWithdrawMsgState(cdc codec.BinaryCodec, value []byte) (msg WithdrawMsgState, err error) {
err = cdc.Unmarshal(value, &msg)
return msg, err
}
func MustUnmarshalWithdrawMsgState(cdc codec.BinaryCodec, value []byte) WithdrawMsgState {
msg, err := UnmarshalWithdrawMsgState(cdc, value)
if err != nil {
panic(err)
}
return msg
}
func MustMarshalSwapMsgState(cdc codec.BinaryCodec, msg SwapMsgState) []byte {
return cdc.MustMarshal(&msg)
}
func UnmarshalSwapMsgState(cdc codec.BinaryCodec, value []byte) (msg SwapMsgState, err error) {
err = cdc.Unmarshal(value, &msg)
return msg, err
}
func MustUnmarshalSwapMsgState(cdc codec.BinaryCodec, value []byte) SwapMsgState {
msg, err := UnmarshalSwapMsgState(cdc, value)
if err != nil {
panic(err)
}
return msg
}