github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/ammswap/types/msgs.go (about)

     1  package types
     2  
     3  import (
     4  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     5  )
     6  
     7  // PoolSwap message types and routes
     8  const (
     9  	TypeMsgAddLiquidity = "add_liquidity"
    10  	TypeMsgTokenSwap    = "token_swap"
    11  )
    12  
    13  // MsgAddLiquidity Deposit quote_amount and base_amount at current ratio to mint pool tokens.
    14  type MsgAddLiquidity struct {
    15  	MinLiquidity  sdk.Dec        `json:"min_liquidity"`   // Minimum number of sender will mint if total pool token supply is greater than 0.
    16  	MaxBaseAmount sdk.SysCoin    `json:"max_base_amount"` // Maximum number of tokens deposited. Deposits max amount if total pool token supply is 0.
    17  	QuoteAmount   sdk.SysCoin    `json:"quote_amount"`    // Quote token amount
    18  	Deadline      int64          `json:"deadline"`        // Time after which this transaction can no longer be executed.
    19  	Sender        sdk.AccAddress `json:"sender"`          // Sender
    20  }
    21  
    22  // NewMsgAddLiquidity is a constructor function for MsgAddLiquidity
    23  func NewMsgAddLiquidity(minLiquidity sdk.Dec, maxBaseAmount, quoteAmount sdk.SysCoin, deadline int64, sender sdk.AccAddress) MsgAddLiquidity {
    24  	return MsgAddLiquidity{
    25  		MinLiquidity:  minLiquidity,
    26  		MaxBaseAmount: maxBaseAmount,
    27  		QuoteAmount:   quoteAmount,
    28  		Deadline:      deadline,
    29  		Sender:        sender,
    30  	}
    31  }
    32  
    33  // Route should return the name of the module
    34  func (msg MsgAddLiquidity) Route() string { return RouterKey }
    35  
    36  // Type should return the action
    37  func (msg MsgAddLiquidity) Type() string { return "add_liquidity" }
    38  
    39  // ValidateBasic runs stateless checks on the message
    40  func (msg MsgAddLiquidity) ValidateBasic() sdk.Error {
    41  	if msg.Sender.Empty() {
    42  		return ErrAddressIsRequire("sender")
    43  	}
    44  	if msg.MinLiquidity.IsNegative() {
    45  		return ErrMinLiquidityIsNegative()
    46  	}
    47  	if !(msg.MaxBaseAmount.IsPositive() && msg.QuoteAmount.IsPositive()) {
    48  		return ErrMaxBaseAmountOrQuoteAmountIsNegative()
    49  	}
    50  	if !msg.MaxBaseAmount.IsValid() {
    51  		return ErrMaxBaseAmount()
    52  	}
    53  	if !msg.QuoteAmount.IsValid() {
    54  		return ErrQuoteAmount()
    55  	}
    56  	err := ValidateBaseAndQuoteAmount(msg.MaxBaseAmount.Denom, msg.QuoteAmount.Denom)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	return nil
    62  }
    63  
    64  // GetSignBytes encodes the message for signing
    65  func (msg MsgAddLiquidity) GetSignBytes() []byte {
    66  	return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
    67  }
    68  
    69  // GetSigners defines whose signature is required
    70  func (msg MsgAddLiquidity) GetSigners() []sdk.AccAddress {
    71  	return []sdk.AccAddress{msg.Sender}
    72  }
    73  
    74  // GetSwapTokenPair defines token pair
    75  func (msg MsgAddLiquidity) GetSwapTokenPairName() string {
    76  	return GetSwapTokenPairName(msg.MaxBaseAmount.Denom, msg.QuoteAmount.Denom)
    77  }
    78  
    79  // MsgRemoveLiquidity burns pool tokens to withdraw fibo and Tokens at current ratio.
    80  type MsgRemoveLiquidity struct {
    81  	Liquidity      sdk.Dec        `json:"liquidity"`        // Amount of pool token burned.
    82  	MinBaseAmount  sdk.SysCoin    `json:"min_base_amount"`  // Minimum base amount.
    83  	MinQuoteAmount sdk.SysCoin    `json:"min_quote_amount"` // Minimum quote amount.
    84  	Deadline       int64          `json:"deadline"`         // Time after which this transaction can no longer be executed.
    85  	Sender         sdk.AccAddress `json:"sender"`           // Sender
    86  }
    87  
    88  // NewMsgRemoveLiquidity is a constructor function for MsgAddLiquidity
    89  func NewMsgRemoveLiquidity(liquidity sdk.Dec, minBaseAmount, minQuoteAmount sdk.SysCoin, deadline int64, sender sdk.AccAddress) MsgRemoveLiquidity {
    90  	return MsgRemoveLiquidity{
    91  		Liquidity:      liquidity,
    92  		MinBaseAmount:  minBaseAmount,
    93  		MinQuoteAmount: minQuoteAmount,
    94  		Deadline:       deadline,
    95  		Sender:         sender,
    96  	}
    97  }
    98  
    99  // Route should return the name of the module
   100  func (msg MsgRemoveLiquidity) Route() string { return RouterKey }
   101  
   102  // Type should return the action
   103  func (msg MsgRemoveLiquidity) Type() string { return "remove_liquidity" }
   104  
   105  // ValidateBasic runs stateless checks on the message
   106  func (msg MsgRemoveLiquidity) ValidateBasic() sdk.Error {
   107  	if msg.Sender.Empty() {
   108  		return ErrAddressIsRequire("sender")
   109  	}
   110  	if !(msg.Liquidity.IsPositive()) {
   111  		return ErrMinLiquidityIsNegative()
   112  	}
   113  	if !msg.MinBaseAmount.IsValid() {
   114  		return ErrMinBaseAmount()
   115  	}
   116  	if !msg.MinQuoteAmount.IsValid() {
   117  		return ErrMinQuoteAmount()
   118  	}
   119  	err := ValidateBaseAndQuoteAmount(msg.MinBaseAmount.Denom, msg.MinQuoteAmount.Denom)
   120  	if err != nil {
   121  		return err
   122  	}
   123  	return nil
   124  }
   125  
   126  // GetSignBytes encodes the message for signing
   127  func (msg MsgRemoveLiquidity) GetSignBytes() []byte {
   128  	return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
   129  }
   130  
   131  // GetSigners defines whose signature is required
   132  func (msg MsgRemoveLiquidity) GetSigners() []sdk.AccAddress {
   133  	return []sdk.AccAddress{msg.Sender}
   134  }
   135  
   136  // GetSwapTokenPair defines token pair
   137  func (msg MsgRemoveLiquidity) GetSwapTokenPairName() string {
   138  	return GetSwapTokenPairName(msg.MinBaseAmount.Denom, msg.MinQuoteAmount.Denom)
   139  }
   140  
   141  // MsgCreateExchange creates a new exchange with token
   142  type MsgCreateExchange struct {
   143  	Token0Name string         `json:"token0_name"`
   144  	Token1Name string         `json:"token1_name"`
   145  	Sender     sdk.AccAddress `json:"sender"` // Sender
   146  }
   147  
   148  // NewMsgCreateExchange create a new exchange with token
   149  func NewMsgCreateExchange(token0Name string, token1Name string, sender sdk.AccAddress) MsgCreateExchange {
   150  	return MsgCreateExchange{
   151  		Token0Name: token0Name,
   152  		Token1Name: token1Name,
   153  		Sender:     sender,
   154  	}
   155  }
   156  
   157  // Route should return the name of the module
   158  func (msg MsgCreateExchange) Route() string { return RouterKey }
   159  
   160  // Type should return the action
   161  func (msg MsgCreateExchange) Type() string { return "create_exchange" }
   162  
   163  // ValidateBasic runs stateless checks on the message
   164  func (msg MsgCreateExchange) ValidateBasic() sdk.Error {
   165  	if msg.Sender.Empty() {
   166  		return ErrAddressIsRequire("sender")
   167  	}
   168  	if err := ValidateSwapAmountName(msg.Token0Name); err != nil {
   169  		return err
   170  	}
   171  
   172  	if err := ValidateSwapAmountName(msg.Token1Name); err != nil {
   173  		return err
   174  	}
   175  
   176  	if msg.Token0Name == msg.Token1Name {
   177  		return ErrToken0NameEqualToken1Name()
   178  	}
   179  	return nil
   180  }
   181  
   182  // GetSignBytes encodes the message for signing
   183  func (msg MsgCreateExchange) GetSignBytes() []byte {
   184  	return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
   185  }
   186  
   187  // GetSigners defines whose signature is required
   188  func (msg MsgCreateExchange) GetSigners() []sdk.AccAddress {
   189  	return []sdk.AccAddress{msg.Sender}
   190  }
   191  
   192  // GetSwapTokenPair defines token pair
   193  func (msg MsgCreateExchange) GetSwapTokenPairName() string {
   194  	return GetSwapTokenPairName(msg.Token0Name, msg.Token1Name)
   195  }
   196  
   197  // MsgTokenToToken define the message for swap between token and DefaultBondDenom
   198  type MsgTokenToToken struct {
   199  	SoldTokenAmount      sdk.SysCoin    `json:"sold_token_amount"`       // Amount of Tokens sold.
   200  	MinBoughtTokenAmount sdk.SysCoin    `json:"min_bought_token_amount"` // Minimum token purchased.
   201  	Deadline             int64          `json:"deadline"`                // Time after which this transaction can no longer be executed.
   202  	Recipient            sdk.AccAddress `json:"recipient"`               // Recipient address,transfer Tokens to recipient.default recipient is sender.
   203  	Sender               sdk.AccAddress `json:"sender"`                  // Sender
   204  }
   205  
   206  // NewMsgTokenToToken is a constructor function for MsgTokenfiboSwap
   207  func NewMsgTokenToToken(
   208  	soldTokenAmount, minBoughtTokenAmount sdk.SysCoin, deadline int64, recipient, sender sdk.AccAddress,
   209  ) MsgTokenToToken {
   210  	return MsgTokenToToken{
   211  		SoldTokenAmount:      soldTokenAmount,
   212  		MinBoughtTokenAmount: minBoughtTokenAmount,
   213  		Deadline:             deadline,
   214  		Recipient:            recipient,
   215  		Sender:               sender,
   216  	}
   217  }
   218  
   219  // Route should return the name of the module
   220  func (msg MsgTokenToToken) Route() string { return RouterKey }
   221  
   222  // Type should return the action
   223  func (msg MsgTokenToToken) Type() string { return TypeMsgTokenSwap }
   224  
   225  // ValidateBasic runs stateless checks on the message
   226  func (msg MsgTokenToToken) ValidateBasic() sdk.Error {
   227  	if msg.Sender.Empty() {
   228  		return ErrAddressIsRequire("sender")
   229  	}
   230  
   231  	if msg.Recipient.Empty() {
   232  		return ErrAddressIsRequire("recipient")
   233  	}
   234  
   235  	if !(msg.SoldTokenAmount.IsPositive()) {
   236  		return ErrSoldTokenAmountIsNegative()
   237  	}
   238  	if !msg.SoldTokenAmount.IsValid() {
   239  		return ErrSoldTokenAmount()
   240  	}
   241  
   242  	if !msg.MinBoughtTokenAmount.IsValid() {
   243  		return ErrMinBoughtTokenAmount()
   244  	}
   245  
   246  	baseAmountName, quoteAmountName := GetBaseQuoteTokenName(msg.SoldTokenAmount.Denom, msg.MinBoughtTokenAmount.Denom)
   247  	err := ValidateBaseAndQuoteAmount(baseAmountName, quoteAmountName)
   248  	if err != nil {
   249  		return err
   250  	}
   251  	return nil
   252  }
   253  
   254  // GetSignBytes encodes the message for signing
   255  func (msg MsgTokenToToken) GetSignBytes() []byte {
   256  	return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
   257  }
   258  
   259  // GetSigners defines whose signature is required
   260  func (msg MsgTokenToToken) GetSigners() []sdk.AccAddress {
   261  	return []sdk.AccAddress{msg.Sender}
   262  }
   263  
   264  // GetSwapTokenPair defines token pair
   265  func (msg MsgTokenToToken) GetSwapTokenPairName() string {
   266  	return GetSwapTokenPairName(msg.MinBoughtTokenAmount.Denom, msg.SoldTokenAmount.Denom)
   267  }