Skip to content

Commit a6202cf

Browse files
committed
feat: add conditional orders and order_mask
1 parent cc4aae3 commit a6202cf

File tree

3 files changed

+75
-9
lines changed

3 files changed

+75
-9
lines changed

pyinjective/async_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
DEFAULT_BLOCK_TIME = 3 # seconds
6767

6868

69-
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
69+
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)
7070

7171
class AsyncClient:
7272
def __init__(

pyinjective/composer.py

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,31 @@ def __init__(self, network: str):
4141
def Coin(self, amount: int, denom: str):
4242
return cosmos_base_coin_pb.Coin(amount=str(amount), denom=denom)
4343

44-
def OrderData(self, market_id: str, subaccount_id: str, order_hash: str):
44+
def OrderData(self, market_id: str, subaccount_id: str, order_hash: str, order_mask: str = "any"):
45+
46+
if order_mask == "any":
47+
order_mask = injective_exchange_pb.OrderMask.ANY
48+
49+
elif order_mask == "regular":
50+
order_mask = injective_exchange_pb.OrderMask.REGULAR
51+
52+
elif order_mask == "conditional":
53+
order_mask = injective_exchange_pb.OrderMask.CONDITIONAL
54+
55+
elif order_mask == "direction_buy_or_higher":
56+
order_mask = injective_exchange_pb.OrderMask.DIRECTION_BUY_OR_HIGHER
57+
58+
elif order_mask == "direction_sell_or_lower":
59+
order_mask = injective_exchange_pb.OrderMask.DIRECTION_SELL_OR_LOWER
60+
61+
elif order_mask == "type_market":
62+
order_mask = injective_exchange_pb.OrderMask.TYPE_MARKET
63+
64+
elif order_mask == "type_limit":
65+
order_mask = injective_exchange_pb.OrderMask.TYPE_LIMIT
66+
4567
return injective_exchange_tx_pb.OrderData(
46-
market_id=market_id, subaccount_id=subaccount_id, order_hash=order_hash
68+
market_id=market_id, subaccount_id=subaccount_id, order_hash=order_hash, order_mask=order_mask
4769
)
4870

4971
def SpotOrder(
@@ -95,6 +117,7 @@ def DerivativeOrder(
95117
fee_recipient: str,
96118
price: float,
97119
quantity: float,
120+
trigger_price: float = 0,
98121
**kwargs
99122
):
100123
# load denom metadata
@@ -114,7 +137,7 @@ def DerivativeOrder(
114137

115138
# prepare values
116139
price = derivative_price_to_backend(price, denom)
117-
trigger_price = derivative_price_to_backend(0, denom)
140+
trigger_price = derivative_price_to_backend(trigger_price, denom)
118141
quantity = derivative_quantity_to_backend(quantity, denom)
119142

120143
if kwargs.get("is_buy") and not kwargs.get("is_po"):
@@ -129,6 +152,18 @@ def DerivativeOrder(
129152
elif not kwargs.get("is_buy") and kwargs.get("is_po"):
130153
order_type = injective_exchange_pb.OrderType.SELL_PO
131154

155+
elif kwargs.get("stop_buy"):
156+
order_type = injective_exchange_pb.OrderType.STOP_BUY
157+
158+
elif kwargs.get("stop_sell"):
159+
order_type = injective_exchange_pb.OrderType.STOP_SEll
160+
161+
elif kwargs.get("take_buy"):
162+
order_type = injective_exchange_pb.OrderType.TAKE_BUY
163+
164+
elif kwargs.get("take_sell"):
165+
order_type = injective_exchange_pb.OrderType.TAKE_SELL
166+
132167
return injective_exchange_pb.DerivativeOrder(
133168
market_id=market_id,
134169
order_info=injective_exchange_pb.OrderInfo(
@@ -290,7 +325,7 @@ def MsgCancelSpotOrder(
290325
sender=sender,
291326
market_id=market_id,
292327
subaccount_id=subaccount_id,
293-
order_hash=order_hash,
328+
order_hash=order_hash
294329
)
295330

296331
def MsgBatchCreateSpotLimitOrders(self, sender: str, orders: List):
@@ -509,13 +544,36 @@ def MsgInstantBinaryOptionsMarketLaunch(
509544
)
510545

511546
def MsgCancelDerivativeOrder(
512-
self, market_id: str, sender: str, subaccount_id: str, order_hash: str
547+
self, market_id: str, sender: str, subaccount_id: str, order_hash: str, order_mask: str = "any"
513548
):
549+
550+
if order_mask == "any":
551+
order_mask = injective_exchange_pb.OrderMask.ANY
552+
553+
elif order_mask == "regular":
554+
order_mask = injective_exchange_pb.OrderMask.REGULAR
555+
556+
elif order_mask == "conditional":
557+
order_mask = injective_exchange_pb.OrderMask.CONDITIONAL
558+
559+
elif order_mask == "direction_buy_or_higher":
560+
order_mask = injective_exchange_pb.OrderMask.DIRECTION_BUY_OR_HIGHER
561+
562+
elif order_mask == "direction_sell_or_lower":
563+
order_mask = injective_exchange_pb.OrderMask.DIRECTION_SELL_OR_LOWER
564+
565+
elif order_mask == "type_market":
566+
order_mask = injective_exchange_pb.OrderMask.TYPE_MARKET
567+
568+
elif order_mask == "type_limit":
569+
order_mask = injective_exchange_pb.OrderMask.TYPE_LIMIT
570+
514571
return injective_exchange_tx_pb.MsgCancelDerivativeOrder(
515572
sender=sender,
516573
market_id=market_id,
517574
subaccount_id=subaccount_id,
518575
order_hash=order_hash,
576+
order_mask=order_mask
519577
)
520578

521579
def MsgBatchCreateDerivativeLimitOrders(self, sender: str, orders: List):

pyinjective/constant.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ class Network:
6363
def __init__(
6464
self,
6565
lcd_endpoint: str ,
66+
tm_websocket_endpoint: str,
6667
grpc_endpoint: str ,
6768
grpc_exchange_endpoint: str ,
6869
chain_id: str ,
6970
fee_denom: str ,
7071
env: str
7172
):
7273
self.lcd_endpoint = lcd_endpoint
74+
self.tm_websocket_endpoint = tm_websocket_endpoint
7375
self.grpc_endpoint = grpc_endpoint
7476
self.grpc_exchange_endpoint = grpc_exchange_endpoint
7577
self.chain_id = chain_id
@@ -80,6 +82,7 @@ def __init__(
8082
def devnet(cls):
8183
return cls(
8284
lcd_endpoint='https://devnet.lcd.injective.dev',
85+
tm_websocket_endpoint='wss://devnet.tm.injective.dev/websocket',
8386
grpc_endpoint='devnet.injective.dev:9900',
8487
grpc_exchange_endpoint='devnet.injective.dev:9910',
8588
chain_id='injective-777',
@@ -91,6 +94,7 @@ def devnet(cls):
9194
def testnet(cls):
9295
return cls(
9396
lcd_endpoint='https://k8s.testnet.lcd.injective.network',
97+
tm_websocket_endpoint='wss://k8s.testnet.tm.injective.network/websocket',
9498
grpc_endpoint='k8s.testnet.chain.grpc.injective.network:443',
9599
grpc_exchange_endpoint='k8s.testnet.exchange.grpc.injective.network:443',
96100
chain_id='injective-888',
@@ -102,7 +106,6 @@ def testnet(cls):
102106
def mainnet(cls, node='k8s'):
103107
nodes = [
104108
'k8s',
105-
'lb',
106109
'sentry0', # us, prod
107110
'sentry1', # us, prod
108111
'sentry2', # us, staging
@@ -111,17 +114,20 @@ def mainnet(cls, node='k8s'):
111114
if node not in nodes:
112115
raise ValueError('Must be one of {}'.format(nodes))
113116

114-
if node == 'lb' or node == 'k8s':
117+
if node == 'k8s':
115118
lcd_endpoint='https://k8s.mainnet.lcd.injective.network'
119+
tm_websocket_endpoint='wss://k8s.mainnet.tm.injective.network/websocket'
116120
grpc_endpoint='k8s.mainnet.chain.grpc.injective.network:443'
117121
grpc_exchange_endpoint='k8s.mainnet.exchange.grpc.injective.network:443'
118122
else:
119123
lcd_endpoint='https://lcd.injective.network'
124+
tm_websocket_endpoint=f'ws://{node}.injective.network:26657/websocket'
120125
grpc_endpoint=f'{node}.injective.network:9900'
121126
grpc_exchange_endpoint=f'{node}.injective.network:9910'
122127

123128
return cls(
124129
lcd_endpoint=lcd_endpoint,
130+
tm_websocket_endpoint=tm_websocket_endpoint,
125131
grpc_endpoint=grpc_endpoint,
126132
grpc_exchange_endpoint=grpc_exchange_endpoint,
127133
chain_id='injective-1',
@@ -133,6 +139,7 @@ def mainnet(cls, node='k8s'):
133139
def local(cls):
134140
return cls(
135141
lcd_endpoint='http://localhost:10337',
142+
tm_websocket_endpoint='ws://localost:26657/websocket',
136143
grpc_endpoint='localhost:9900',
137144
grpc_exchange_endpoint='localhost:9910',
138145
chain_id='injective-1',
@@ -141,9 +148,10 @@ def local(cls):
141148
)
142149

143150
@classmethod
144-
def custom(cls, lcd_endpoint, grpc_endpoint, grpc_exchange_endpoint, chain_id, env):
151+
def custom(cls, lcd_endpoint, tm_websocket_endpoint, grpc_endpoint, grpc_exchange_endpoint, chain_id, env):
145152
return cls(
146153
lcd_endpoint=lcd_endpoint,
154+
tm_websocket_endpoint=tm_websocket_endpoint,
147155
grpc_endpoint=grpc_endpoint,
148156
grpc_exchange_endpoint=grpc_exchange_endpoint,
149157
chain_id=chain_id,

0 commit comments

Comments
 (0)