github.com/InjectiveLabs/sdk-go@v1.53.0/chain/exchange/types/authz_spot.go (about)

     1  package types
     2  
     3  import (
     4  	"context"
     5  
     6  	sdk "github.com/cosmos/cosmos-sdk/types"
     7  	sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
     8  	"github.com/cosmos/cosmos-sdk/x/authz"
     9  )
    10  
    11  var (
    12  	_ authz.Authorization = &CreateSpotLimitOrderAuthz{}
    13  	_ authz.Authorization = &CreateSpotMarketOrderAuthz{}
    14  	_ authz.Authorization = &BatchCreateSpotLimitOrdersAuthz{}
    15  	_ authz.Authorization = &CancelSpotOrderAuthz{}
    16  	_ authz.Authorization = &BatchCancelSpotOrdersAuthz{}
    17  )
    18  
    19  // CreateSpotLimitOrderAuthz impl
    20  func (a CreateSpotLimitOrderAuthz) MsgTypeURL() string {
    21  	return sdk.MsgTypeURL(&MsgCreateSpotLimitOrder{})
    22  }
    23  
    24  func (a CreateSpotLimitOrderAuthz) Accept(ctx context.Context, msg sdk.Msg) (authz.AcceptResponse, error) {
    25  	spotOrder, ok := msg.(*MsgCreateSpotLimitOrder)
    26  	if !ok {
    27  		return authz.AcceptResponse{}, sdkerrors.ErrInvalidType.Wrap("type mismatch")
    28  	}
    29  	// check authorized subaccount
    30  	if spotOrder.Order.OrderInfo.SubaccountId != a.SubaccountId {
    31  		return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf("requested subaccount is unauthorized")
    32  	}
    33  	// check authorized market
    34  	if !find(a.MarketIds, spotOrder.Order.MarketId) {
    35  		return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf("requested market is unauthorized")
    36  	}
    37  	return authz.AcceptResponse{Accept: true, Delete: false, Updated: nil}, nil
    38  }
    39  
    40  func (a CreateSpotLimitOrderAuthz) ValidateBasic() error {
    41  	if !IsHexHash(a.SubaccountId) {
    42  		return sdkerrors.ErrLogic.Wrap("invalid subaccount id to authorize")
    43  	}
    44  	if len(a.MarketIds) == 0 || len(a.MarketIds) > AuthorizedMarketsLimit {
    45  		return sdkerrors.ErrLogic.Wrapf("invalid markets array length")
    46  	}
    47  	marketsSet := reduceToSet(a.MarketIds)
    48  	if len(a.MarketIds) != len(marketsSet) {
    49  		return sdkerrors.ErrLogic.Wrapf("Cannot have duplicate markets")
    50  	}
    51  	for _, m := range a.MarketIds {
    52  		if !IsHexHash(m) {
    53  			return sdkerrors.ErrLogic.Wrap("invalid market id to authorize")
    54  		}
    55  	}
    56  	return nil
    57  }
    58  
    59  // CreateSpotMarketOrderAuthz impl
    60  func (a CreateSpotMarketOrderAuthz) MsgTypeURL() string {
    61  	return sdk.MsgTypeURL(&MsgCreateSpotMarketOrder{})
    62  }
    63  
    64  func (a CreateSpotMarketOrderAuthz) Accept(ctx context.Context, msg sdk.Msg) (authz.AcceptResponse, error) {
    65  	spotOrder, ok := msg.(*MsgCreateSpotMarketOrder)
    66  	if !ok {
    67  		return authz.AcceptResponse{}, sdkerrors.ErrInvalidType.Wrap("type mismatch")
    68  	}
    69  	// check authorized subaccount
    70  	if spotOrder.Order.OrderInfo.SubaccountId != a.SubaccountId {
    71  		return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf("requested subaccount is unauthorized")
    72  	}
    73  	// check authorized market
    74  	if !find(a.MarketIds, spotOrder.Order.MarketId) {
    75  		return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf("requested market is unauthorized")
    76  	}
    77  	return authz.AcceptResponse{Accept: true, Delete: false, Updated: nil}, nil
    78  }
    79  
    80  func (a CreateSpotMarketOrderAuthz) ValidateBasic() error {
    81  	if !IsHexHash(a.SubaccountId) {
    82  		return sdkerrors.ErrLogic.Wrap("invalid subaccount id to authorize")
    83  	}
    84  	if len(a.MarketIds) == 0 || len(a.MarketIds) > AuthorizedMarketsLimit {
    85  		return sdkerrors.ErrLogic.Wrapf("invalid markets array length")
    86  	}
    87  	marketsSet := reduceToSet(a.MarketIds)
    88  	if len(a.MarketIds) != len(marketsSet) {
    89  		return sdkerrors.ErrLogic.Wrapf("Cannot have duplicate markets")
    90  	}
    91  	for _, m := range a.MarketIds {
    92  		if !IsHexHash(m) {
    93  			return sdkerrors.ErrLogic.Wrap("invalid market id to authorize")
    94  		}
    95  	}
    96  	return nil
    97  }
    98  
    99  // BatchCreateSpotLimitOrdersAuthz impl
   100  func (a BatchCreateSpotLimitOrdersAuthz) MsgTypeURL() string {
   101  	return sdk.MsgTypeURL(&MsgBatchCreateSpotLimitOrders{})
   102  }
   103  
   104  func (a BatchCreateSpotLimitOrdersAuthz) Accept(ctx context.Context, msg sdk.Msg) (authz.AcceptResponse, error) {
   105  	spotOrders, ok := msg.(*MsgBatchCreateSpotLimitOrders)
   106  	if !ok {
   107  		return authz.AcceptResponse{}, sdkerrors.ErrInvalidType.Wrap("type mismatch")
   108  	}
   109  	for _, o := range spotOrders.Orders {
   110  		// check authorized subaccount
   111  		if o.OrderInfo.SubaccountId != a.SubaccountId {
   112  			return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf("requested subaccount is unauthorized")
   113  		}
   114  		// check authorized markets
   115  		if !find(a.MarketIds, o.MarketId) {
   116  			return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf("requested market is unauthorized")
   117  		}
   118  	}
   119  	return authz.AcceptResponse{Accept: true, Delete: false, Updated: nil}, nil
   120  }
   121  
   122  func (a BatchCreateSpotLimitOrdersAuthz) ValidateBasic() error {
   123  	if !IsHexHash(a.SubaccountId) {
   124  		return sdkerrors.ErrLogic.Wrap("invalid subaccount id to authorize")
   125  	}
   126  	if len(a.MarketIds) == 0 || len(a.MarketIds) > AuthorizedMarketsLimit {
   127  		return sdkerrors.ErrLogic.Wrapf("invalid markets array length")
   128  	}
   129  	marketsSet := reduceToSet(a.MarketIds)
   130  	if len(a.MarketIds) != len(marketsSet) {
   131  		return sdkerrors.ErrLogic.Wrapf("Cannot have duplicate markets")
   132  	}
   133  	for _, m := range a.MarketIds {
   134  		if !IsHexHash(m) {
   135  			return sdkerrors.ErrLogic.Wrap("invalid market id to authorize")
   136  		}
   137  	}
   138  	return nil
   139  }
   140  
   141  // CancelSpotOrderAuthz impl
   142  func (a CancelSpotOrderAuthz) MsgTypeURL() string {
   143  	return sdk.MsgTypeURL(&MsgCancelSpotOrder{})
   144  }
   145  
   146  func (a CancelSpotOrderAuthz) Accept(ctx context.Context, msg sdk.Msg) (authz.AcceptResponse, error) {
   147  	orderToCancel, ok := msg.(*MsgCancelSpotOrder)
   148  	if !ok {
   149  		return authz.AcceptResponse{}, sdkerrors.ErrInvalidType.Wrap("type mismatch")
   150  	}
   151  	// check authorized subaccount
   152  	if orderToCancel.SubaccountId != a.SubaccountId {
   153  		return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf("requested subaccount is unauthorized")
   154  	}
   155  	// check authorized market
   156  	if !find(a.MarketIds, orderToCancel.MarketId) {
   157  		return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf("requested market is unauthorized")
   158  	}
   159  	return authz.AcceptResponse{Accept: true, Delete: false, Updated: nil}, nil
   160  }
   161  
   162  func (a CancelSpotOrderAuthz) ValidateBasic() error {
   163  	if !IsHexHash(a.SubaccountId) {
   164  		return sdkerrors.ErrLogic.Wrap("invalid subaccount id to authorize")
   165  	}
   166  	if len(a.MarketIds) == 0 || len(a.MarketIds) > AuthorizedMarketsLimit {
   167  		return sdkerrors.ErrLogic.Wrapf("invalid markets array length")
   168  	}
   169  	marketsSet := reduceToSet(a.MarketIds)
   170  	if len(a.MarketIds) != len(marketsSet) {
   171  		return sdkerrors.ErrLogic.Wrapf("Cannot have duplicate markets")
   172  	}
   173  	for _, m := range a.MarketIds {
   174  		if !IsHexHash(m) {
   175  			return sdkerrors.ErrLogic.Wrap("invalid market id to authorize")
   176  		}
   177  	}
   178  	return nil
   179  }
   180  
   181  // BatchCancelSpotOrdersAuthz impl
   182  func (a BatchCancelSpotOrdersAuthz) MsgTypeURL() string {
   183  	return sdk.MsgTypeURL(&MsgBatchCancelSpotOrders{})
   184  }
   185  
   186  func (a BatchCancelSpotOrdersAuthz) Accept(ctx context.Context, msg sdk.Msg) (authz.AcceptResponse, error) {
   187  	ordersToCancel, ok := msg.(*MsgBatchCancelSpotOrders)
   188  	if !ok {
   189  		return authz.AcceptResponse{}, sdkerrors.ErrInvalidType.Wrap("type mismatch")
   190  	}
   191  	for _, o := range ordersToCancel.Data {
   192  		// check authorized subaccount
   193  		if o.SubaccountId != a.SubaccountId {
   194  			return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf("requested subaccount is unauthorized")
   195  		}
   196  		// check authorized markets
   197  		if !find(a.MarketIds, o.MarketId) {
   198  			return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf("requested market is unauthorized")
   199  		}
   200  	}
   201  	return authz.AcceptResponse{Accept: true, Delete: false, Updated: nil}, nil
   202  }
   203  
   204  func (a BatchCancelSpotOrdersAuthz) ValidateBasic() error {
   205  	if !IsHexHash(a.SubaccountId) {
   206  		return sdkerrors.ErrLogic.Wrap("invalid subaccount id to authorize")
   207  	}
   208  	if len(a.MarketIds) == 0 || len(a.MarketIds) > AuthorizedMarketsLimit {
   209  		return sdkerrors.ErrLogic.Wrapf("invalid markets array length")
   210  	}
   211  	marketsSet := reduceToSet(a.MarketIds)
   212  	if len(a.MarketIds) != len(marketsSet) {
   213  		return sdkerrors.ErrLogic.Wrapf("Cannot have duplicate markets")
   214  	}
   215  	for _, m := range a.MarketIds {
   216  		if !IsHexHash(m) {
   217  			return sdkerrors.ErrLogic.Wrap("invalid market id to authorize")
   218  		}
   219  	}
   220  	return nil
   221  }