github.com/InjectiveLabs/sdk-go@v1.53.0/chain/exchange/types/exchange.go (about) 1 package types 2 3 import ( 4 "fmt" 5 6 "cosmossdk.io/math" 7 sdk "github.com/cosmos/cosmos-sdk/types" 8 "github.com/ethereum/go-ethereum/common" 9 ) 10 11 type MatchedMarketDirection struct { 12 MarketId common.Hash 13 BuysExists bool 14 SellsExists bool 15 } 16 17 type TriggeredOrdersInMarket struct { 18 Market *DerivativeMarket 19 MarkPrice math.LegacyDec 20 MarketOrders []*DerivativeMarketOrder 21 LimitOrders []*DerivativeLimitOrder 22 HasLimitBuyOrders bool 23 HasLimitSellOrders bool 24 } 25 26 func (e ExecutionType) IsMarket() bool { 27 return e == ExecutionType_Market 28 } 29 30 func (e ExecutionType) IsMaker() bool { 31 return !e.IsTaker() 32 } 33 34 func (e ExecutionType) IsTaker() bool { 35 return e == ExecutionType_Market || e == ExecutionType_LimitMatchNewOrder 36 } 37 38 func (s MarketStatus) SupportsOrderCancellations() bool { 39 switch s { 40 case MarketStatus_Active, MarketStatus_Demolished, MarketStatus_Expired, MarketStatus_Paused: 41 return true 42 default: 43 return false 44 } 45 } 46 47 type TradingRewardAccountPoints struct { 48 Account sdk.AccAddress 49 Points math.LegacyDec 50 } 51 52 func (p *PointsMultiplier) GetMultiplier(e ExecutionType) math.LegacyDec { 53 if e.IsMaker() { 54 return p.MakerPointsMultiplier 55 } 56 57 return p.TakerPointsMultiplier 58 } 59 60 type IOrder interface { 61 GetPrice() math.LegacyDec 62 GetQuantity() math.LegacyDec 63 GetFillable() math.LegacyDec 64 IsBuy() bool 65 GetSubaccountID() common.Hash 66 } 67 68 // IDerivativeOrder proto interface for wrapping all different variations of representations of derivative orders. Methods can be added as needed (make sure to add to every implementor) 69 type IDerivativeOrder interface { 70 IOrder 71 GetMargin() math.LegacyDec 72 IsReduceOnly() bool 73 IsVanilla() bool 74 } 75 76 type IMutableDerivativeOrder interface { 77 IDerivativeOrder 78 SetPrice(math.LegacyDec) 79 SetQuantity(math.LegacyDec) 80 SetMargin(math.LegacyDec) 81 } 82 83 // DerivativeOrder - IMutableDerivativeOrder implementation 84 85 func (o *DerivativeOrder) GetPrice() math.LegacyDec { 86 return o.OrderInfo.Price 87 } 88 89 func (o *DerivativeOrder) GetQuantity() math.LegacyDec { 90 return o.OrderInfo.Quantity 91 } 92 func (o *DerivativeOrder) GetFillable() math.LegacyDec { 93 return o.GetQuantity() 94 } 95 96 func (o *DerivativeOrder) GetMargin() math.LegacyDec { 97 return o.Margin 98 } 99 func (o *DerivativeOrder) GetSubaccountID() common.Hash { 100 return o.OrderInfo.SubaccountID() 101 } 102 103 func (o *DerivativeOrder) SetPrice(price math.LegacyDec) { 104 o.OrderInfo.Price = price 105 } 106 107 func (o *DerivativeOrder) SetQuantity(quantity math.LegacyDec) { 108 o.OrderInfo.Quantity = quantity 109 } 110 111 func (o *DerivativeOrder) SetMargin(margin math.LegacyDec) { 112 o.Margin = margin 113 } 114 115 // DerivativeLimitOrder - IMutableDerivativeOrder implementation 116 117 func (o *DerivativeLimitOrder) GetPrice() math.LegacyDec { 118 return o.OrderInfo.Price 119 } 120 121 func (o *DerivativeLimitOrder) GetQuantity() math.LegacyDec { 122 return o.OrderInfo.Quantity 123 } 124 func (o *DerivativeLimitOrder) GetFillable() math.LegacyDec { 125 return o.Fillable 126 } 127 func (o *DerivativeLimitOrder) GetMargin() math.LegacyDec { 128 return o.Margin 129 } 130 131 func (o *DerivativeLimitOrder) GetSubaccountID() common.Hash { 132 return o.OrderInfo.SubaccountID() 133 } 134 135 func (o *DerivativeLimitOrder) SetPrice(price math.LegacyDec) { 136 o.OrderInfo.Price = price 137 } 138 139 func (o *DerivativeLimitOrder) SetQuantity(quantity math.LegacyDec) { 140 o.OrderInfo.Quantity = quantity 141 o.Fillable = quantity 142 } 143 144 func (o *DerivativeLimitOrder) SetMargin(margin math.LegacyDec) { 145 o.Margin = margin 146 } 147 148 // DerivativeMarketOrder - IMutableDerivativeOrder implementation 149 150 func (o *DerivativeMarketOrder) GetPrice() math.LegacyDec { 151 return o.OrderInfo.Price 152 } 153 154 func (o *DerivativeMarketOrder) GetQuantity() math.LegacyDec { 155 return o.OrderInfo.Quantity 156 } 157 func (o *DerivativeMarketOrder) GetFillable() math.LegacyDec { 158 return o.OrderInfo.Quantity 159 } 160 func (o *DerivativeMarketOrder) GetMargin() math.LegacyDec { 161 return o.Margin 162 } 163 func (o *DerivativeMarketOrder) GetSubaccountID() common.Hash { 164 return o.OrderInfo.SubaccountID() 165 } 166 func (o *DerivativeMarketOrder) SetPrice(price math.LegacyDec) { 167 o.OrderInfo.Price = price 168 } 169 170 func (o *DerivativeMarketOrder) SetQuantity(quantity math.LegacyDec) { 171 o.OrderInfo.Quantity = quantity 172 } 173 174 func (o *DerivativeMarketOrder) SetMargin(margin math.LegacyDec) { 175 o.Margin = margin 176 } 177 178 func (o *DerivativeMarketOrder) DebugString() string { 179 return fmt.Sprintf("(q:%v, p:%v, m:%v, isLong: %v)", o.Quantity(), o.Price(), o.Margin, o.IsBuy()) 180 } 181 182 // spot orders 183 184 func (o *SpotOrder) GetPrice() math.LegacyDec { 185 return o.OrderInfo.Price 186 } 187 func (o *SpotLimitOrder) GetPrice() math.LegacyDec { 188 return o.OrderInfo.Price 189 } 190 func (o *SpotMarketOrder) GetPrice() math.LegacyDec { 191 return o.OrderInfo.Price 192 } 193 194 func (o *SpotOrder) GetQuantity() math.LegacyDec { 195 return o.OrderInfo.Quantity 196 } 197 func (o *SpotLimitOrder) GetQuantity() math.LegacyDec { 198 return o.OrderInfo.Quantity 199 } 200 201 func (o *SpotMarketOrder) GetQuantity() math.LegacyDec { 202 return o.OrderInfo.Quantity 203 } 204 func (o *SpotMarketOrder) IsBuy() bool { 205 return o.OrderType.IsBuy() 206 } 207 func (o *SpotOrder) GetFillable() math.LegacyDec { 208 return o.OrderInfo.Quantity 209 } 210 func (o *SpotMarketOrder) GetFillable() math.LegacyDec { 211 // no fillable for market order, but quantity works same in this case 212 return o.OrderInfo.Quantity 213 } 214 func (o *SpotLimitOrder) GetFillable() math.LegacyDec { 215 return o.Fillable 216 } 217 218 func (o *SpotOrder) GetSubaccountID() common.Hash { 219 return o.OrderInfo.SubaccountID() 220 } 221 func (o *SpotMarketOrder) GetSubaccountID() common.Hash { 222 return o.OrderInfo.SubaccountID() 223 } 224 func (o *SpotLimitOrder) GetSubaccountID() common.Hash { 225 return o.OrderInfo.SubaccountID() 226 } 227 228 func (al AtomicMarketOrderAccessLevel) IsValid() bool { 229 switch al { 230 case AtomicMarketOrderAccessLevel_Nobody, 231 AtomicMarketOrderAccessLevel_SmartContractsOnly, 232 AtomicMarketOrderAccessLevel_BeginBlockerSmartContractsOnly, 233 AtomicMarketOrderAccessLevel_Everyone: 234 return true 235 default: 236 return false 237 } 238 }