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

     1  package types
     2  
     3  import (
     4  	sdk "github.com/cosmos/cosmos-sdk/types"
     5  )
     6  
     7  var (
     8  	_ sdk.Msg = (*MsgCreatePool)(nil)
     9  	_ sdk.Msg = (*MsgDepositWithinBatch)(nil)
    10  	_ sdk.Msg = (*MsgWithdrawWithinBatch)(nil)
    11  	_ sdk.Msg = (*MsgSwapWithinBatch)(nil)
    12  )
    13  
    14  // Message types for the liquidity module
    15  //
    16  //nolint:gosec
    17  const (
    18  	TypeMsgCreatePool          = "create_pool"
    19  	TypeMsgDepositWithinBatch  = "deposit_within_batch"
    20  	TypeMsgWithdrawWithinBatch = "withdraw_within_batch"
    21  	TypeMsgSwapWithinBatch     = "swap_within_batch"
    22  )
    23  
    24  // NewMsgCreatePool creates a new MsgCreatePool.
    25  func NewMsgCreatePool(poolCreator sdk.AccAddress, poolTypeID uint32, depositCoins sdk.Coins) *MsgCreatePool {
    26  	return &MsgCreatePool{
    27  		PoolCreatorAddress: poolCreator.String(),
    28  		PoolTypeId:         poolTypeID,
    29  		DepositCoins:       depositCoins,
    30  	}
    31  }
    32  
    33  func (msg MsgCreatePool) Route() string { return RouterKey }
    34  
    35  func (msg MsgCreatePool) Type() string { return TypeMsgCreatePool }
    36  
    37  func (msg MsgCreatePool) ValidateBasic() error {
    38  	if 1 > msg.PoolTypeId {
    39  		return ErrBadPoolTypeID
    40  	}
    41  	if _, err := sdk.AccAddressFromBech32(msg.PoolCreatorAddress); err != nil {
    42  		return ErrInvalidPoolCreatorAddr
    43  	}
    44  	if err := msg.DepositCoins.Validate(); err != nil {
    45  		return err
    46  	}
    47  	if n := uint32(len(msg.DepositCoins)); n > MaxReserveCoinNum || n < MinReserveCoinNum {
    48  		return ErrNumOfReserveCoin
    49  	}
    50  	return nil
    51  }
    52  
    53  func (msg MsgCreatePool) GetSignBytes() []byte {
    54  	return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
    55  }
    56  
    57  func (msg MsgCreatePool) GetSigners() []sdk.AccAddress {
    58  	addr, err := sdk.AccAddressFromBech32(msg.PoolCreatorAddress)
    59  	if err != nil {
    60  		panic(err)
    61  	}
    62  	return []sdk.AccAddress{addr}
    63  }
    64  
    65  func (msg MsgCreatePool) GetPoolCreator() sdk.AccAddress {
    66  	addr, err := sdk.AccAddressFromBech32(msg.PoolCreatorAddress)
    67  	if err != nil {
    68  		panic(err)
    69  	}
    70  	return addr
    71  }
    72  
    73  // NewMsgDepositWithinBatch creates a new MsgDepositWithinBatch.
    74  func NewMsgDepositWithinBatch(depositor sdk.AccAddress, poolID uint64, depositCoins sdk.Coins) *MsgDepositWithinBatch {
    75  	return &MsgDepositWithinBatch{
    76  		DepositorAddress: depositor.String(),
    77  		PoolId:           poolID,
    78  		DepositCoins:     depositCoins,
    79  	}
    80  }
    81  
    82  func (msg MsgDepositWithinBatch) Route() string { return RouterKey }
    83  
    84  func (msg MsgDepositWithinBatch) Type() string { return TypeMsgDepositWithinBatch }
    85  
    86  func (msg MsgDepositWithinBatch) ValidateBasic() error {
    87  	if _, err := sdk.AccAddressFromBech32(msg.DepositorAddress); err != nil {
    88  		return ErrInvalidDepositorAddr
    89  	}
    90  	if err := msg.DepositCoins.Validate(); err != nil {
    91  		return err
    92  	}
    93  	if !msg.DepositCoins.IsAllPositive() {
    94  		return ErrBadDepositCoinsAmount
    95  	}
    96  	if n := uint32(len(msg.DepositCoins)); n > MaxReserveCoinNum || n < MinReserveCoinNum {
    97  		return ErrNumOfReserveCoin
    98  	}
    99  	return nil
   100  }
   101  
   102  func (msg MsgDepositWithinBatch) GetSignBytes() []byte {
   103  	return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
   104  }
   105  
   106  func (msg MsgDepositWithinBatch) GetSigners() []sdk.AccAddress {
   107  	addr, err := sdk.AccAddressFromBech32(msg.DepositorAddress)
   108  	if err != nil {
   109  		panic(err)
   110  	}
   111  	return []sdk.AccAddress{addr}
   112  }
   113  
   114  func (msg MsgDepositWithinBatch) GetDepositor() sdk.AccAddress {
   115  	addr, err := sdk.AccAddressFromBech32(msg.DepositorAddress)
   116  	if err != nil {
   117  		panic(err)
   118  	}
   119  	return addr
   120  }
   121  
   122  // NewMsgWithdrawWithinBatch creates a new MsgWithdrawWithinBatch.
   123  func NewMsgWithdrawWithinBatch(withdrawer sdk.AccAddress, poolID uint64, poolCoin sdk.Coin) *MsgWithdrawWithinBatch {
   124  	return &MsgWithdrawWithinBatch{
   125  		WithdrawerAddress: withdrawer.String(),
   126  		PoolId:            poolID,
   127  		PoolCoin:          poolCoin,
   128  	}
   129  }
   130  
   131  func (msg MsgWithdrawWithinBatch) Route() string { return RouterKey }
   132  
   133  func (msg MsgWithdrawWithinBatch) Type() string { return TypeMsgWithdrawWithinBatch }
   134  
   135  func (msg MsgWithdrawWithinBatch) ValidateBasic() error {
   136  	if _, err := sdk.AccAddressFromBech32(msg.WithdrawerAddress); err != nil {
   137  		return ErrInvalidWithdrawerAddr
   138  	}
   139  	if err := msg.PoolCoin.Validate(); err != nil {
   140  		return err
   141  	}
   142  	if !msg.PoolCoin.IsPositive() {
   143  		return ErrBadPoolCoinAmount
   144  	}
   145  	return nil
   146  }
   147  
   148  func (msg MsgWithdrawWithinBatch) GetSignBytes() []byte {
   149  	return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
   150  }
   151  
   152  func (msg MsgWithdrawWithinBatch) GetSigners() []sdk.AccAddress {
   153  	addr, err := sdk.AccAddressFromBech32(msg.WithdrawerAddress)
   154  	if err != nil {
   155  		panic(err)
   156  	}
   157  	return []sdk.AccAddress{addr}
   158  }
   159  
   160  func (msg MsgWithdrawWithinBatch) GetWithdrawer() sdk.AccAddress {
   161  	addr, err := sdk.AccAddressFromBech32(msg.WithdrawerAddress)
   162  	if err != nil {
   163  		panic(err)
   164  	}
   165  	return addr
   166  }
   167  
   168  // NewMsgSwapWithinBatch creates a new MsgSwapWithinBatch.
   169  func NewMsgSwapWithinBatch(
   170  	swapRequester sdk.AccAddress,
   171  	poolID uint64,
   172  	swapTypeID uint32,
   173  	offerCoin sdk.Coin,
   174  	demandCoinDenom string,
   175  	orderPrice sdk.Dec,
   176  	swapFeeRate sdk.Dec,
   177  ) *MsgSwapWithinBatch {
   178  	return &MsgSwapWithinBatch{
   179  		SwapRequesterAddress: swapRequester.String(),
   180  		PoolId:               poolID,
   181  		SwapTypeId:           swapTypeID,
   182  		OfferCoin:            offerCoin,
   183  		OfferCoinFee:         GetOfferCoinFee(offerCoin, swapFeeRate),
   184  		DemandCoinDenom:      demandCoinDenom,
   185  		OrderPrice:           orderPrice,
   186  	}
   187  }
   188  
   189  func (msg MsgSwapWithinBatch) Route() string { return RouterKey }
   190  
   191  func (msg MsgSwapWithinBatch) Type() string { return TypeMsgSwapWithinBatch }
   192  
   193  func (msg MsgSwapWithinBatch) ValidateBasic() error {
   194  	if _, err := sdk.AccAddressFromBech32(msg.SwapRequesterAddress); err != nil {
   195  		return ErrInvalidSwapRequesterAddr
   196  	}
   197  	if err := msg.OfferCoin.Validate(); err != nil {
   198  		return err
   199  	}
   200  	if !msg.OfferCoin.IsPositive() {
   201  		return ErrBadOfferCoinAmount
   202  	}
   203  	if !msg.OrderPrice.IsPositive() {
   204  		return ErrBadOrderPrice
   205  	}
   206  	if !msg.OfferCoin.Amount.GTE(MinOfferCoinAmount) {
   207  		return ErrLessThanMinOfferAmount
   208  	}
   209  	return nil
   210  }
   211  
   212  func (msg MsgSwapWithinBatch) GetSignBytes() []byte {
   213  	return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
   214  }
   215  
   216  func (msg MsgSwapWithinBatch) GetSigners() []sdk.AccAddress {
   217  	addr, err := sdk.AccAddressFromBech32(msg.SwapRequesterAddress)
   218  	if err != nil {
   219  		panic(err)
   220  	}
   221  	return []sdk.AccAddress{addr}
   222  }
   223  
   224  func (msg MsgSwapWithinBatch) GetSwapRequester() sdk.AccAddress {
   225  	addr, err := sdk.AccAddressFromBech32(msg.SwapRequesterAddress)
   226  	if err != nil {
   227  		panic(err)
   228  	}
   229  	return addr
   230  }