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

     1  package types
     2  
     3  import (
     4  	"cosmossdk.io/math"
     5  	"github.com/ethereum/go-ethereum/common"
     6  )
     7  
     8  type PositionTransfer struct {
     9  	MarketID                common.Hash    `json:"market_id"`
    10  	SourceSubaccountID      common.Hash    `json:"source_subaccount_id"`
    11  	DestinationSubaccountID common.Hash    `json:"destination_subaccount_id"`
    12  	Quantity                math.LegacyDec `json:"quantity"`
    13  }
    14  
    15  func (p *PositionTransfer) ValidateBasic() error {
    16  	if p.Quantity.IsNil() || !p.Quantity.IsPositive() {
    17  		return ErrInvalidQuantity
    18  	}
    19  
    20  	if p.SourceSubaccountID == p.DestinationSubaccountID {
    21  		return ErrBadSubaccountID
    22  	}
    23  
    24  	return nil
    25  }
    26  
    27  type SyntheticTradeAction struct {
    28  	UserTrades     []*SyntheticTrade `json:"user_trades"`
    29  	ContractTrades []*SyntheticTrade `json:"contract_trades"`
    30  }
    31  
    32  func (a *SyntheticTradeAction) ValidateBasic() error {
    33  	if len(a.UserTrades) == 0 || len(a.ContractTrades) == 0 {
    34  		return ErrInvalidTrade
    35  	}
    36  
    37  	for _, t := range a.UserTrades {
    38  		if err := t.Validate(); err != nil {
    39  			return err
    40  		}
    41  	}
    42  
    43  	for _, t := range a.ContractTrades {
    44  		if err := t.Validate(); err != nil {
    45  			return err
    46  		}
    47  	}
    48  
    49  	return nil
    50  }
    51  
    52  type SyntheticTrade struct {
    53  	MarketID     common.Hash    `json:"market_id"`
    54  	SubaccountID common.Hash    `json:"subaccount_id"`
    55  	IsBuy        bool           `json:"is_buy"`
    56  	Quantity     math.LegacyDec `json:"quantity"`
    57  	Price        math.LegacyDec `json:"price"`
    58  	Margin       math.LegacyDec `json:"margin"`
    59  }
    60  
    61  func (t *SyntheticTrade) Validate() error {
    62  	if t.Quantity.IsNil() || !t.Quantity.IsPositive() {
    63  		return ErrInvalidQuantity
    64  	}
    65  
    66  	if t.Price.IsNil() || !t.Price.IsPositive() {
    67  		return ErrInvalidPrice
    68  	}
    69  
    70  	// Margin can be 0 or even negative!
    71  	if t.Margin.IsNil() {
    72  		return ErrInvalidMargin
    73  	}
    74  
    75  	return nil
    76  }
    77  
    78  func (t *SyntheticTrade) ToPositionDelta() *PositionDelta {
    79  	return &PositionDelta{
    80  		IsLong:            t.IsBuy,
    81  		ExecutionQuantity: t.Quantity,
    82  		ExecutionMargin:   t.Margin,
    83  		ExecutionPrice:    t.Price,
    84  	}
    85  }