github.com/gravity-devs/liquidity@v1.5.3/x/liquidity/types/liquidity_pool.go (about)

     1  package types
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"github.com/cosmos/cosmos-sdk/codec"
     8  	sdk "github.com/cosmos/cosmos-sdk/types"
     9  )
    10  
    11  // PoolName returns unique name of the pool consists of given reserve coin denoms and type id.
    12  func PoolName(reserveCoinDenoms []string, poolTypeID uint32) string {
    13  	return strings.Join(append(SortDenoms(reserveCoinDenoms), strconv.FormatUint(uint64(poolTypeID), 10)), "/")
    14  }
    15  
    16  // Name returns the pool's name.
    17  func (pool Pool) Name() string {
    18  	return PoolName(pool.ReserveCoinDenoms, pool.TypeId)
    19  }
    20  
    21  // Validate validates Pool.
    22  func (pool Pool) Validate() error {
    23  	if pool.Id == 0 {
    24  		return ErrPoolNotExists
    25  	}
    26  	if pool.TypeId == 0 {
    27  		return ErrPoolTypeNotExists
    28  	}
    29  	if pool.ReserveCoinDenoms == nil || len(pool.ReserveCoinDenoms) == 0 {
    30  		return ErrNumOfReserveCoinDenoms
    31  	}
    32  	if uint32(len(pool.ReserveCoinDenoms)) > MaxReserveCoinNum || uint32(len(pool.ReserveCoinDenoms)) < MinReserveCoinNum {
    33  		return ErrNumOfReserveCoinDenoms
    34  	}
    35  	sortedDenomA, sortedDenomB := AlphabeticalDenomPair(pool.ReserveCoinDenoms[0], pool.ReserveCoinDenoms[1])
    36  	if sortedDenomA != pool.ReserveCoinDenoms[0] || sortedDenomB != pool.ReserveCoinDenoms[1] {
    37  		return ErrBadOrderingReserveCoinDenoms
    38  	}
    39  	if pool.ReserveAccountAddress == "" {
    40  		return ErrEmptyReserveAccountAddress
    41  	}
    42  	if pool.ReserveAccountAddress != GetPoolReserveAcc(pool.Name(), false).String() {
    43  		return ErrBadReserveAccountAddress
    44  	}
    45  	if pool.PoolCoinDenom == "" {
    46  		return ErrEmptyPoolCoinDenom
    47  	}
    48  	if pool.PoolCoinDenom != pool.Name() {
    49  		return ErrBadPoolCoinDenom
    50  	}
    51  	return nil
    52  }
    53  
    54  // NewPoolBatch creates a new PoolBatch object.
    55  func NewPoolBatch(poolID, batchIndex uint64) PoolBatch {
    56  	return PoolBatch{
    57  		PoolId:           poolID,
    58  		Index:            batchIndex,
    59  		BeginHeight:      0,
    60  		DepositMsgIndex:  1,
    61  		WithdrawMsgIndex: 1,
    62  		SwapMsgIndex:     1,
    63  		Executed:         false,
    64  	}
    65  }
    66  
    67  // MustMarshalPool returns the Pool bytes. Panics if fails.
    68  func MustMarshalPool(cdc codec.BinaryCodec, liquidityPool Pool) []byte {
    69  	return cdc.MustMarshal(&liquidityPool)
    70  }
    71  
    72  // MustUnmarshalPool returns the Pool from bytes. Panics if fails.
    73  func MustUnmarshalPool(cdc codec.BinaryCodec, value []byte) Pool {
    74  	liquidityPool, err := UnmarshalPool(cdc, value)
    75  	if err != nil {
    76  		panic(err)
    77  	}
    78  	return liquidityPool
    79  }
    80  
    81  // UnmarshalPool returns the Pool from bytes.
    82  func UnmarshalPool(cdc codec.BinaryCodec, value []byte) (liquidityPool Pool, err error) {
    83  	err = cdc.Unmarshal(value, &liquidityPool)
    84  	return liquidityPool, err
    85  }
    86  
    87  // GetReserveAccount returns sdk.AccAddress of the pool's reserve account.
    88  func (pool Pool) GetReserveAccount() sdk.AccAddress {
    89  	addr, err := sdk.AccAddressFromBech32(pool.ReserveAccountAddress)
    90  	if err != nil {
    91  		panic(err)
    92  	}
    93  	return addr
    94  }
    95  
    96  // GetPoolCoinDenom returns the pool coin's denom.
    97  func (pool Pool) GetPoolCoinDenom() string { return pool.PoolCoinDenom }
    98  
    99  // GetId returns id of the pool.
   100  func (pool Pool) GetId() uint64 { return pool.Id } //nolint:revive
   101  
   102  // Pools is a collection of pools.
   103  type Pools []Pool
   104  
   105  func (pools Pools) String() (out string) {
   106  	for _, del := range pools {
   107  		out += del.String() + "\n"
   108  	}
   109  	return strings.TrimSpace(out)
   110  }
   111  
   112  // MustMarshalPoolBatch returns the PoolBatch bytes. Panics if fails.
   113  func MustMarshalPoolBatch(cdc codec.BinaryCodec, poolBatch PoolBatch) []byte {
   114  	return cdc.MustMarshal(&poolBatch)
   115  }
   116  
   117  // UnmarshalPoolBatch returns the PoolBatch from bytes.
   118  func UnmarshalPoolBatch(cdc codec.BinaryCodec, value []byte) (poolBatch PoolBatch, err error) {
   119  	err = cdc.Unmarshal(value, &poolBatch)
   120  	return poolBatch, err
   121  }
   122  
   123  // MustUnmarshalPoolBatch returns the PoolBatch from bytes. Panics if fails.
   124  func MustUnmarshalPoolBatch(cdc codec.BinaryCodec, value []byte) PoolBatch {
   125  	poolBatch, err := UnmarshalPoolBatch(cdc, value)
   126  	if err != nil {
   127  		panic(err)
   128  	}
   129  	return poolBatch
   130  }
   131  
   132  // MustMarshalDepositMsgState returns the DepositMsgState bytes. Panics if fails.
   133  func MustMarshalDepositMsgState(cdc codec.BinaryCodec, msg DepositMsgState) []byte {
   134  	return cdc.MustMarshal(&msg)
   135  }
   136  
   137  // UnmarshalDepositMsgState returns the DepositMsgState from bytes.
   138  func UnmarshalDepositMsgState(cdc codec.BinaryCodec, value []byte) (msg DepositMsgState, err error) {
   139  	err = cdc.Unmarshal(value, &msg)
   140  	return msg, err
   141  }
   142  
   143  // MustUnmarshalDepositMsgState returns the DepositMsgState from bytes. Panics if fails.
   144  func MustUnmarshalDepositMsgState(cdc codec.BinaryCodec, value []byte) DepositMsgState {
   145  	msg, err := UnmarshalDepositMsgState(cdc, value)
   146  	if err != nil {
   147  		panic(err)
   148  	}
   149  	return msg
   150  }
   151  
   152  // MustMarshalWithdrawMsgState returns the WithdrawMsgState bytes. Panics if fails.
   153  func MustMarshalWithdrawMsgState(cdc codec.BinaryCodec, msg WithdrawMsgState) []byte {
   154  	return cdc.MustMarshal(&msg)
   155  }
   156  
   157  // UnmarshalWithdrawMsgState returns the WithdrawMsgState from bytes.
   158  func UnmarshalWithdrawMsgState(cdc codec.BinaryCodec, value []byte) (msg WithdrawMsgState, err error) {
   159  	err = cdc.Unmarshal(value, &msg)
   160  	return msg, err
   161  }
   162  
   163  // MustUnmarshalWithdrawMsgState returns the WithdrawMsgState from bytes. Panics if fails.
   164  func MustUnmarshalWithdrawMsgState(cdc codec.BinaryCodec, value []byte) WithdrawMsgState {
   165  	msg, err := UnmarshalWithdrawMsgState(cdc, value)
   166  	if err != nil {
   167  		panic(err)
   168  	}
   169  	return msg
   170  }
   171  
   172  // MustMarshalSwapMsgState returns the SwapMsgState bytes. Panics if fails.
   173  func MustMarshalSwapMsgState(cdc codec.BinaryCodec, msg SwapMsgState) []byte {
   174  	return cdc.MustMarshal(&msg)
   175  }
   176  
   177  // UnmarshalSwapMsgState returns the UnmarshalSwapMsgState from bytes.
   178  func UnmarshalSwapMsgState(cdc codec.BinaryCodec, value []byte) (msg SwapMsgState, err error) {
   179  	err = cdc.Unmarshal(value, &msg)
   180  	return msg, err
   181  }
   182  
   183  // MustUnmarshalSwapMsgState returns the SwapMsgState from bytes. Panics if fails.
   184  func MustUnmarshalSwapMsgState(cdc codec.BinaryCodec, value []byte) SwapMsgState {
   185  	msg, err := UnmarshalSwapMsgState(cdc, value)
   186  	if err != nil {
   187  		panic(err)
   188  	}
   189  	return msg
   190  }