-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathcomposer.py
More file actions
1887 lines (1673 loc) · 76.3 KB
/
composer.py
File metadata and controls
1887 lines (1673 loc) · 76.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
from decimal import Decimal
from enum import IntFlag
from time import time
from typing import Any, Dict, List, Optional
from warnings import warn
from google.protobuf import any_pb2, json_format, timestamp_pb2
from pyinjective.constant import ADDITIONAL_CHAIN_FORMAT_DECIMALS, INJ_DECIMALS, INJ_DENOM
from pyinjective.core.market import BinaryOptionMarket, DerivativeMarket, SpotMarket
from pyinjective.core.token import Token
from pyinjective.ofac import OfacChecker
from pyinjective.proto.cosmos.authz.v1beta1 import authz_pb2 as cosmos_authz_pb, tx_pb2 as cosmos_authz_tx_pb
from pyinjective.proto.cosmos.bank.v1beta1 import bank_pb2 as bank_pb, tx_pb2 as cosmos_bank_tx_pb
from pyinjective.proto.cosmos.base.v1beta1 import coin_pb2 as base_coin_pb
from pyinjective.proto.cosmos.distribution.v1beta1 import (
distribution_pb2 as cosmos_distribution_pb2,
tx_pb2 as cosmos_distribution_tx_pb,
)
from pyinjective.proto.cosmos.gov.v1beta1 import tx_pb2 as cosmos_gov_tx_pb
from pyinjective.proto.cosmos.staking.v1beta1 import tx_pb2 as cosmos_staking_tx_pb
from pyinjective.proto.cosmwasm.wasm.v1 import tx_pb2 as wasm_tx_pb
from pyinjective.proto.exchange import injective_explorer_rpc_pb2 as explorer_pb2
from pyinjective.proto.ibc.applications.transfer.v1 import tx_pb2 as ibc_transfer_tx_pb
from pyinjective.proto.ibc.core.client.v1 import client_pb2 as ibc_core_client_pb
from pyinjective.proto.injective.auction.v1beta1 import tx_pb2 as injective_auction_tx_pb
from pyinjective.proto.injective.exchange.v1beta1 import (
authz_pb2 as injective_authz_pb,
exchange_pb2 as injective_exchange_pb,
tx_pb2 as injective_exchange_tx_pb,
)
from pyinjective.proto.injective.insurance.v1beta1 import tx_pb2 as injective_insurance_tx_pb
from pyinjective.proto.injective.oracle.v1beta1 import (
oracle_pb2 as injective_oracle_pb,
tx_pb2 as injective_oracle_tx_pb,
)
from pyinjective.proto.injective.peggy.v1 import msgs_pb2 as injective_peggy_tx_pb
from pyinjective.proto.injective.permissions.v1beta1 import (
permissions_pb2 as injective_permissions_pb,
tx_pb2 as injective_permissions_tx_pb,
)
from pyinjective.proto.injective.stream.v1beta1 import query_pb2 as chain_stream_query
from pyinjective.proto.injective.tokenfactory.v1beta1 import tx_pb2 as token_factory_tx_pb
from pyinjective.proto.injective.wasmx.v1 import tx_pb2 as wasmx_tx_pb
from pyinjective.utils.denom import Denom
REQUEST_TO_RESPONSE_TYPE_MAP = {
"MsgCreateSpotLimitOrder": injective_exchange_tx_pb.MsgCreateSpotLimitOrderResponse,
"MsgCreateSpotMarketOrder": injective_exchange_tx_pb.MsgCreateSpotMarketOrderResponse,
"MsgCreateDerivativeLimitOrder": injective_exchange_tx_pb.MsgCreateDerivativeLimitOrderResponse,
"MsgCreateDerivativeMarketOrder": injective_exchange_tx_pb.MsgCreateDerivativeMarketOrderResponse,
"MsgCancelSpotOrder": injective_exchange_tx_pb.MsgCancelSpotOrderResponse,
"MsgCancelDerivativeOrder": injective_exchange_tx_pb.MsgCancelDerivativeOrderResponse,
"MsgBatchCancelSpotOrders": injective_exchange_tx_pb.MsgBatchCancelSpotOrdersResponse,
"MsgBatchCancelDerivativeOrders": injective_exchange_tx_pb.MsgBatchCancelDerivativeOrdersResponse,
"MsgBatchCreateSpotLimitOrders": injective_exchange_tx_pb.MsgBatchCreateSpotLimitOrdersResponse,
"MsgBatchCreateDerivativeLimitOrders": injective_exchange_tx_pb.MsgBatchCreateDerivativeLimitOrdersResponse,
"MsgBatchUpdateOrders": injective_exchange_tx_pb.MsgBatchUpdateOrdersResponse,
"MsgDeposit": injective_exchange_tx_pb.MsgDepositResponse,
"MsgWithdraw": injective_exchange_tx_pb.MsgWithdrawResponse,
"MsgSubaccountTransfer": injective_exchange_tx_pb.MsgSubaccountTransferResponse,
"MsgLiquidatePosition": injective_exchange_tx_pb.MsgLiquidatePositionResponse,
"MsgIncreasePositionMargin": injective_exchange_tx_pb.MsgIncreasePositionMarginResponse,
"MsgCreateBinaryOptionsLimitOrder": injective_exchange_tx_pb.MsgCreateBinaryOptionsLimitOrderResponse,
"MsgCreateBinaryOptionsMarketOrder": injective_exchange_tx_pb.MsgCreateBinaryOptionsMarketOrderResponse,
"MsgCancelBinaryOptionsOrder": injective_exchange_tx_pb.MsgCancelBinaryOptionsOrderResponse,
"MsgAdminUpdateBinaryOptionsMarket": injective_exchange_tx_pb.MsgAdminUpdateBinaryOptionsMarketResponse,
"MsgInstantBinaryOptionsMarketLaunch": injective_exchange_tx_pb.MsgInstantBinaryOptionsMarketLaunchResponse,
}
GRPC_MESSAGE_TYPE_TO_CLASS_MAP = {
"/injective.exchange.v1beta1.MsgCreateSpotLimitOrder": injective_exchange_tx_pb.MsgCreateSpotLimitOrder,
"/injective.exchange.v1beta1.MsgCreateSpotMarketOrder": injective_exchange_tx_pb.MsgCreateSpotMarketOrder,
"/injective.exchange.v1beta1.MsgCreateDerivativeLimitOrder": injective_exchange_tx_pb.MsgCreateDerivativeLimitOrder,
"/injective.exchange.v1beta1.MsgCreateDerivativeMarketOrder": injective_exchange_tx_pb.MsgCreateDerivativeMarketOrder, # noqa: 121
"/injective.exchange.v1beta1.MsgCancelSpotOrder": injective_exchange_tx_pb.MsgCancelSpotOrder,
"/injective.exchange.v1beta1.MsgCancelDerivativeOrder": injective_exchange_tx_pb.MsgCancelDerivativeOrder,
"/injective.exchange.v1beta1.MsgBatchCancelSpotOrders": injective_exchange_tx_pb.MsgBatchCancelSpotOrders,
"/injective.exchange.v1beta1.MsgBatchCancelDerivativeOrders": injective_exchange_tx_pb.MsgBatchCancelDerivativeOrders, # noqa: 121
"/injective.exchange.v1beta1.MsgBatchCreateSpotLimitOrders": injective_exchange_tx_pb.MsgBatchCreateSpotLimitOrders,
"/injective.exchange.v1beta1.MsgBatchCreateDerivativeLimitOrders": injective_exchange_tx_pb.MsgBatchCreateDerivativeLimitOrders, # noqa: 121
"/injective.exchange.v1beta1.MsgBatchUpdateOrders": injective_exchange_tx_pb.MsgBatchUpdateOrders,
"/injective.exchange.v1beta1.MsgDeposit": injective_exchange_tx_pb.MsgDeposit,
"/injective.exchange.v1beta1.MsgWithdraw": injective_exchange_tx_pb.MsgWithdraw,
"/injective.exchange.v1beta1.MsgSubaccountTransfer": injective_exchange_tx_pb.MsgSubaccountTransfer,
"/injective.exchange.v1beta1.MsgLiquidatePosition": injective_exchange_tx_pb.MsgLiquidatePosition,
"/injective.exchange.v1beta1.MsgIncreasePositionMargin": injective_exchange_tx_pb.MsgIncreasePositionMargin,
"/injective.auction.v1beta1.MsgBid": injective_auction_tx_pb.MsgBid,
"/injective.exchange.v1beta1.MsgCreateBinaryOptionsLimitOrder": injective_exchange_tx_pb.MsgCreateBinaryOptionsLimitOrder, # noqa: 121
"/injective.exchange.v1beta1.MsgCreateBinaryOptionsMarketOrder": injective_exchange_tx_pb.MsgCreateBinaryOptionsMarketOrder, # noqa: 121
"/injective.exchange.v1beta1.MsgCancelBinaryOptionsOrder": injective_exchange_tx_pb.MsgCancelBinaryOptionsOrder,
"/injective.exchange.v1beta1.MsgAdminUpdateBinaryOptionsMarket": injective_exchange_tx_pb.MsgAdminUpdateBinaryOptionsMarket, # noqa: 121
"/injective.exchange.v1beta1.MsgInstantBinaryOptionsMarketLaunch": injective_exchange_tx_pb.MsgInstantBinaryOptionsMarketLaunch, # noqa: 121
"/cosmos.bank.v1beta1.MsgSend": cosmos_bank_tx_pb.MsgSend,
"/cosmos.authz.v1beta1.MsgGrant": cosmos_authz_tx_pb.MsgGrant,
"/cosmos.authz.v1beta1.MsgExec": cosmos_authz_tx_pb.MsgExec,
"/cosmos.authz.v1beta1.MsgRevoke": cosmos_authz_tx_pb.MsgRevoke,
"/injective.oracle.v1beta1.MsgRelayPriceFeedPrice": injective_oracle_tx_pb.MsgRelayPriceFeedPrice,
"/injective.oracle.v1beta1.MsgRelayProviderPrices": injective_oracle_tx_pb.MsgRelayProviderPrices,
}
class Composer:
PERMISSIONS_ACTION = IntFlag(
"PERMISSIONS_ACTION",
[
(permission_name, injective_permissions_pb.Action.Value(permission_name))
for permission_name in injective_permissions_pb.Action.keys()
],
)
def __init__(
self,
network: str,
spot_markets: Optional[Dict[str, SpotMarket]] = None,
derivative_markets: Optional[Dict[str, DerivativeMarket]] = None,
binary_option_markets: Optional[Dict[str, BinaryOptionMarket]] = None,
tokens: Optional[Dict[str, Token]] = None,
):
"""Composer is used to create the requests to send to the nodes using the Client
:param network: the name of the network to use (mainnet, testnet, devnet)
:type network: str
:param spot_markets: a dictionary containing all spot markets
:type spot_markets: Dictionary
:param derivative_markets: a dictionary containing all derivative markets
:type derivative_markets: Dictionary
:param binary_option_markets: a dictionary containing all derivative markets
:type binary_option_markets: Dictionary
:param tokens: a dictionary with all the possible tokens
:type tokens: Dictionary
"""
warn(
"Composer from pyinjective.composer is deprecated. "
"Please use Composer from pyinjective.composer_v2 instead.",
DeprecationWarning,
stacklevel=2,
)
self.network = network
self.spot_markets = spot_markets or dict()
self.derivative_markets = derivative_markets or dict()
self.binary_option_markets = binary_option_markets or dict()
self.tokens = tokens or dict()
self._ofac_checker = OfacChecker()
def coin(self, amount: int, denom: str) -> base_coin_pb.Coin:
"""
This method create an instance of Coin gRPC type, considering the amount is already expressed in chain format
"""
formatted_amount_string = str(int(amount))
return base_coin_pb.Coin(amount=formatted_amount_string, denom=denom)
def create_coin_amount(self, amount: Decimal, token_name: str) -> base_coin_pb.Coin:
"""
This method create an instance of Coin gRPC type, considering the amount is already expressed in chain format
"""
token = self.tokens[token_name]
chain_amount = token.chain_formatted_value(human_readable_value=amount)
return self.coin(amount=int(chain_amount), denom=token.denom)
def order_data(
self,
market_id: str,
subaccount_id: str,
order_hash: Optional[str] = None,
cid: Optional[str] = None,
is_conditional: Optional[bool] = False,
is_buy: Optional[bool] = False,
is_market_order: Optional[bool] = False,
) -> injective_exchange_tx_pb.OrderData:
order_mask = self._order_mask(is_conditional=is_conditional, is_buy=is_buy, is_market_order=is_market_order)
return injective_exchange_tx_pb.OrderData(
market_id=market_id,
subaccount_id=subaccount_id,
order_hash=order_hash,
order_mask=order_mask,
cid=cid,
)
def order_data_without_mask(
self,
market_id: str,
subaccount_id: str,
order_hash: Optional[str] = None,
cid: Optional[str] = None,
) -> injective_exchange_tx_pb.OrderData:
return injective_exchange_tx_pb.OrderData(
market_id=market_id,
subaccount_id=subaccount_id,
order_hash=order_hash,
order_mask=1,
cid=cid,
)
def spot_order(
self,
market_id: str,
subaccount_id: str,
fee_recipient: str,
price: Decimal,
quantity: Decimal,
order_type: str,
cid: Optional[str] = None,
trigger_price: Optional[Decimal] = None,
) -> injective_exchange_pb.SpotOrder:
market = self.spot_markets[market_id]
chain_quantity = f"{market.quantity_to_chain_format(human_readable_value=quantity).normalize():f}"
chain_price = f"{market.price_to_chain_format(human_readable_value=price).normalize():f}"
trigger_price = trigger_price or Decimal(0)
chain_trigger_price = f"{market.price_to_chain_format(human_readable_value=trigger_price).normalize():f}"
chain_order_type = injective_exchange_pb.OrderType.Value(order_type)
return injective_exchange_pb.SpotOrder(
market_id=market_id,
order_info=injective_exchange_pb.OrderInfo(
subaccount_id=subaccount_id,
fee_recipient=fee_recipient,
price=chain_price,
quantity=chain_quantity,
cid=cid,
),
order_type=chain_order_type,
trigger_price=chain_trigger_price,
)
def calculate_margin(
self, quantity: Decimal, price: Decimal, leverage: Decimal, is_reduce_only: bool = False
) -> Decimal:
if is_reduce_only:
margin = Decimal(0)
else:
margin = quantity * price / leverage
return margin
def derivative_order(
self,
market_id: str,
subaccount_id: str,
fee_recipient: str,
price: Decimal,
quantity: Decimal,
margin: Decimal,
order_type: str,
cid: Optional[str] = None,
trigger_price: Optional[Decimal] = None,
) -> injective_exchange_pb.DerivativeOrder:
market = self.derivative_markets[market_id]
chain_quantity = market.quantity_to_chain_format(human_readable_value=quantity)
chain_price = market.price_to_chain_format(human_readable_value=price)
chain_margin = market.margin_to_chain_format(human_readable_value=margin)
trigger_price = trigger_price or Decimal(0)
chain_trigger_price = market.price_to_chain_format(human_readable_value=trigger_price)
return self._basic_derivative_order(
market_id=market.id,
subaccount_id=subaccount_id,
fee_recipient=fee_recipient,
chain_price=chain_price,
chain_quantity=chain_quantity,
chain_margin=chain_margin,
order_type=order_type,
cid=cid,
chain_trigger_price=chain_trigger_price,
)
def binary_options_order(
self,
market_id: str,
subaccount_id: str,
fee_recipient: str,
price: Decimal,
quantity: Decimal,
margin: Decimal,
order_type: str,
cid: Optional[str] = None,
trigger_price: Optional[Decimal] = None,
denom: Optional[Denom] = None,
) -> injective_exchange_pb.DerivativeOrder:
market = self.binary_option_markets[market_id]
chain_quantity = market.quantity_to_chain_format(human_readable_value=quantity, special_denom=denom)
chain_price = market.price_to_chain_format(human_readable_value=price, special_denom=denom)
chain_margin = market.margin_to_chain_format(human_readable_value=margin, special_denom=denom)
trigger_price = trigger_price or Decimal(0)
chain_trigger_price = market.price_to_chain_format(human_readable_value=trigger_price, special_denom=denom)
return self._basic_derivative_order(
market_id=market.id,
subaccount_id=subaccount_id,
fee_recipient=fee_recipient,
chain_price=chain_price,
chain_quantity=chain_quantity,
chain_margin=chain_margin,
order_type=order_type,
cid=cid,
chain_trigger_price=chain_trigger_price,
)
def create_grant_authorization(self, grantee: str, amount: Decimal) -> injective_exchange_pb.GrantAuthorization:
chain_formatted_amount = int(amount * Decimal(f"1e{INJ_DECIMALS}"))
return injective_exchange_pb.GrantAuthorization(grantee=grantee, amount=str(chain_formatted_amount))
# region Auction module
def MsgBid(self, sender: str, bid_amount: float, round: float) -> injective_auction_tx_pb.MsgBid:
"""
This method is deprecated and will be removed soon. Please use `msg_bid` instead
"""
warn("This method is deprecated. Use msg_bid instead", DeprecationWarning, stacklevel=2)
return self.msg_bid(sender=sender, bid_amount=bid_amount, round=round)
def msg_bid(self, sender: str, bid_amount: float, round: float) -> injective_auction_tx_pb.MsgBid:
be_amount = Decimal(str(bid_amount)) * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
return injective_auction_tx_pb.MsgBid(
sender=sender,
round=round,
bid_amount=self.coin(amount=int(be_amount), denom=INJ_DENOM),
)
# endregion
# region Authz module
def MsgGrantGeneric(self, granter: str, grantee: str, msg_type: str, expire_in: int) -> cosmos_authz_tx_pb.MsgGrant:
"""
This method is deprecated and will be removed soon. Please use `msg_grant_generic` instead
"""
warn("This method is deprecated. Use msg_grant_generic instead", DeprecationWarning, stacklevel=2)
return self.msg_grant_generic(granter=granter, grantee=grantee, msg_type=msg_type, expire_in=expire_in)
def msg_grant_generic(
self, granter: str, grantee: str, msg_type: str, expire_in: int
) -> cosmos_authz_tx_pb.MsgGrant:
if self._ofac_checker.is_blacklisted(granter):
raise Exception(f"Address {granter} is in the OFAC list")
auth = cosmos_authz_pb.GenericAuthorization(msg=msg_type)
any_auth = any_pb2.Any()
any_auth.Pack(auth, type_url_prefix="")
grant = cosmos_authz_pb.Grant(
authorization=any_auth,
expiration=timestamp_pb2.Timestamp(seconds=(int(time()) + expire_in)),
)
return cosmos_authz_tx_pb.MsgGrant(granter=granter, grantee=grantee, grant=grant)
def MsgExec(self, grantee: str, msgs: List):
"""
This method is deprecated and will be removed soon. Please use `msg_exec` instead
"""
warn("This method is deprecated. Use msg_exec instead", DeprecationWarning, stacklevel=2)
return self.msg_exec(grantee=grantee, msgs=msgs)
def msg_exec(self, grantee: str, msgs: List):
any_msgs: List[any_pb2.Any] = []
for msg in msgs:
any_msg = any_pb2.Any()
any_msg.Pack(msg, type_url_prefix="")
any_msgs.append(any_msg)
return cosmos_authz_tx_pb.MsgExec(grantee=grantee, msgs=any_msgs)
def MsgRevoke(self, granter: str, grantee: str, msg_type: str) -> cosmos_authz_tx_pb.MsgRevoke:
"""
This method is deprecated and will be removed soon. Please use `msg_revoke` instead
"""
warn("This method is deprecated. Use msg_revoke instead", DeprecationWarning, stacklevel=2)
return self.msg_revoke(granter=granter, grantee=grantee, msg_type=msg_type)
def msg_revoke(self, granter: str, grantee: str, msg_type: str) -> cosmos_authz_tx_pb.MsgRevoke:
return cosmos_authz_tx_pb.MsgRevoke(granter=granter, grantee=grantee, msg_type_url=msg_type)
def msg_execute_contract_compat(self, sender: str, contract: str, msg: str, funds: str):
return wasmx_tx_pb.MsgExecuteContractCompat(
sender=sender,
contract=contract,
msg=msg,
funds=funds,
)
# endregion
# region Bank module
def MsgSend(self, from_address: str, to_address: str, amount: float, denom: str) -> cosmos_bank_tx_pb.MsgSend:
"""
This method is deprecated and will be removed soon. Please use `msg_send` instead
"""
warn("This method is deprecated. Use msg_send instead", DeprecationWarning, stacklevel=2)
return self.msg_send(from_address=from_address, to_address=to_address, amount=amount, denom=denom)
def msg_send(self, from_address: str, to_address: str, amount: float, denom: str) -> cosmos_bank_tx_pb.MsgSend:
coin = self.create_coin_amount(amount=Decimal(str(amount)), token_name=denom)
return cosmos_bank_tx_pb.MsgSend(
from_address=from_address,
to_address=to_address,
amount=[coin],
)
# endregion
# region Chain Exchange module
def msg_deposit(self, sender: str, subaccount_id: str, amount: Decimal, denom: str):
coin = self.create_coin_amount(amount=amount, token_name=denom)
return injective_exchange_tx_pb.MsgDeposit(
sender=sender,
subaccount_id=subaccount_id,
amount=coin,
)
def msg_withdraw(self, sender: str, subaccount_id: str, amount: Decimal, denom: str):
be_amount = self.create_coin_amount(amount=amount, token_name=denom)
return injective_exchange_tx_pb.MsgWithdraw(
sender=sender,
subaccount_id=subaccount_id,
amount=be_amount,
)
def msg_instant_spot_market_launch(
self,
sender: str,
ticker: str,
base_denom: str,
quote_denom: str,
min_price_tick_size: Decimal,
min_quantity_tick_size: Decimal,
min_notional: Decimal,
base_decimals: int,
quote_decimals: int,
) -> injective_exchange_tx_pb.MsgInstantSpotMarketLaunch:
base_token = self.tokens[base_denom]
quote_token = self.tokens[quote_denom]
chain_min_price_tick_size = (
quote_token.chain_formatted_value(min_price_tick_size) / base_token.chain_formatted_value(Decimal("1"))
) * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
chain_min_quantity_tick_size = base_token.chain_formatted_value(min_quantity_tick_size) * Decimal(
f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}"
)
chain_min_notional = quote_token.chain_formatted_value(min_notional) * Decimal(
f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}"
)
return injective_exchange_tx_pb.MsgInstantSpotMarketLaunch(
sender=sender,
ticker=ticker,
base_denom=base_token.denom,
quote_denom=quote_token.denom,
min_price_tick_size=f"{chain_min_price_tick_size.normalize():f}",
min_quantity_tick_size=f"{chain_min_quantity_tick_size.normalize():f}",
min_notional=f"{chain_min_notional.normalize():f}",
base_decimals=base_decimals,
quote_decimals=quote_decimals,
)
def msg_create_spot_limit_order(
self,
market_id: str,
sender: str,
subaccount_id: str,
fee_recipient: str,
price: Decimal,
quantity: Decimal,
order_type: str,
cid: Optional[str] = None,
trigger_price: Optional[Decimal] = None,
) -> injective_exchange_tx_pb.MsgCreateSpotLimitOrder:
return injective_exchange_tx_pb.MsgCreateSpotLimitOrder(
sender=sender,
order=self.spot_order(
market_id=market_id,
subaccount_id=subaccount_id,
fee_recipient=fee_recipient,
price=price,
quantity=quantity,
order_type=order_type,
cid=cid,
trigger_price=trigger_price,
),
)
def msg_batch_create_spot_limit_orders(
self, sender: str, orders: List[injective_exchange_pb.SpotOrder]
) -> injective_exchange_tx_pb.MsgBatchCreateSpotLimitOrders:
return injective_exchange_tx_pb.MsgBatchCreateSpotLimitOrders(sender=sender, orders=orders)
def msg_create_spot_market_order(
self,
market_id: str,
sender: str,
subaccount_id: str,
fee_recipient: str,
price: Decimal,
quantity: Decimal,
order_type: str,
cid: Optional[str] = None,
trigger_price: Optional[Decimal] = None,
) -> injective_exchange_tx_pb.MsgCreateSpotMarketOrder:
return injective_exchange_tx_pb.MsgCreateSpotMarketOrder(
sender=sender,
order=self.spot_order(
market_id=market_id,
subaccount_id=subaccount_id,
fee_recipient=fee_recipient,
price=price,
quantity=quantity,
order_type=order_type,
cid=cid,
trigger_price=trigger_price,
),
)
def msg_cancel_spot_order(
self,
market_id: str,
sender: str,
subaccount_id: str,
order_hash: Optional[str] = None,
cid: Optional[str] = None,
) -> injective_exchange_tx_pb.MsgCancelSpotOrder:
return injective_exchange_tx_pb.MsgCancelSpotOrder(
sender=sender,
market_id=market_id,
subaccount_id=subaccount_id,
order_hash=order_hash,
cid=cid,
)
def msg_batch_cancel_spot_orders(
self, sender: str, orders_data: List[injective_exchange_tx_pb.OrderData]
) -> injective_exchange_tx_pb.MsgBatchCancelSpotOrders:
return injective_exchange_tx_pb.MsgBatchCancelSpotOrders(sender=sender, data=orders_data)
def msg_batch_update_orders(
self,
sender: str,
subaccount_id: Optional[str] = None,
spot_market_ids_to_cancel_all: Optional[List[str]] = None,
derivative_market_ids_to_cancel_all: Optional[List[str]] = None,
spot_orders_to_cancel: Optional[List[injective_exchange_tx_pb.OrderData]] = None,
derivative_orders_to_cancel: Optional[List[injective_exchange_tx_pb.OrderData]] = None,
spot_orders_to_create: Optional[List[injective_exchange_pb.SpotOrder]] = None,
derivative_orders_to_create: Optional[List[injective_exchange_pb.DerivativeOrder]] = None,
binary_options_orders_to_cancel: Optional[List[injective_exchange_tx_pb.OrderData]] = None,
binary_options_market_ids_to_cancel_all: Optional[List[str]] = None,
binary_options_orders_to_create: Optional[List[injective_exchange_pb.DerivativeOrder]] = None,
) -> injective_exchange_tx_pb.MsgBatchUpdateOrders:
return injective_exchange_tx_pb.MsgBatchUpdateOrders(
sender=sender,
subaccount_id=subaccount_id,
spot_market_ids_to_cancel_all=spot_market_ids_to_cancel_all,
derivative_market_ids_to_cancel_all=derivative_market_ids_to_cancel_all,
spot_orders_to_cancel=spot_orders_to_cancel,
derivative_orders_to_cancel=derivative_orders_to_cancel,
spot_orders_to_create=spot_orders_to_create,
derivative_orders_to_create=derivative_orders_to_create,
binary_options_orders_to_cancel=binary_options_orders_to_cancel,
binary_options_market_ids_to_cancel_all=binary_options_market_ids_to_cancel_all,
binary_options_orders_to_create=binary_options_orders_to_create,
)
def msg_privileged_execute_contract(
self,
sender: str,
contract_address: str,
data: str,
funds: Optional[str] = None,
) -> injective_exchange_tx_pb.MsgPrivilegedExecuteContract:
# funds is a string of Coin strings, comma separated, e.g. 100000inj,20000000000usdt
return injective_exchange_tx_pb.MsgPrivilegedExecuteContract(
sender=sender,
contract_address=contract_address,
data=data,
funds=funds,
)
def msg_create_derivative_limit_order(
self,
market_id: str,
sender: str,
subaccount_id: str,
fee_recipient: str,
price: Decimal,
quantity: Decimal,
margin: Decimal,
order_type: str,
cid: Optional[str] = None,
trigger_price: Optional[Decimal] = None,
) -> injective_exchange_tx_pb.MsgCreateDerivativeLimitOrder:
return injective_exchange_tx_pb.MsgCreateDerivativeLimitOrder(
sender=sender,
order=self.derivative_order(
market_id=market_id,
subaccount_id=subaccount_id,
fee_recipient=fee_recipient,
price=price,
quantity=quantity,
margin=margin,
order_type=order_type,
cid=cid,
trigger_price=trigger_price,
),
)
def msg_batch_create_derivative_limit_orders(
self,
sender: str,
orders: List[injective_exchange_pb.DerivativeOrder],
) -> injective_exchange_tx_pb.MsgBatchCreateDerivativeLimitOrders:
return injective_exchange_tx_pb.MsgBatchCreateDerivativeLimitOrders(sender=sender, orders=orders)
def msg_create_derivative_market_order(
self,
market_id: str,
sender: str,
subaccount_id: str,
fee_recipient: str,
price: Decimal,
quantity: Decimal,
margin: Decimal,
order_type: str,
cid: Optional[str] = None,
trigger_price: Optional[Decimal] = None,
) -> injective_exchange_tx_pb.MsgCreateDerivativeMarketOrder:
return injective_exchange_tx_pb.MsgCreateDerivativeMarketOrder(
sender=sender,
order=self.derivative_order(
market_id=market_id,
subaccount_id=subaccount_id,
fee_recipient=fee_recipient,
price=price,
quantity=quantity,
margin=margin,
order_type=order_type,
cid=cid,
trigger_price=trigger_price,
),
)
def msg_cancel_derivative_order(
self,
market_id: str,
sender: str,
subaccount_id: str,
order_hash: Optional[str] = None,
cid: Optional[str] = None,
is_conditional: Optional[bool] = False,
is_buy: Optional[bool] = False,
is_market_order: Optional[bool] = False,
) -> injective_exchange_tx_pb.MsgCancelDerivativeOrder:
order_mask = self._order_mask(is_conditional=is_conditional, is_buy=is_buy, is_market_order=is_market_order)
return injective_exchange_tx_pb.MsgCancelDerivativeOrder(
sender=sender,
market_id=market_id,
subaccount_id=subaccount_id,
order_hash=order_hash,
order_mask=order_mask,
cid=cid,
)
def msg_batch_cancel_derivative_orders(
self, sender: str, orders_data: List[injective_exchange_tx_pb.OrderData]
) -> injective_exchange_tx_pb.MsgBatchCancelDerivativeOrders:
return injective_exchange_tx_pb.MsgBatchCancelDerivativeOrders(sender=sender, data=orders_data)
def msg_instant_binary_options_market_launch(
self,
sender: str,
ticker: str,
oracle_symbol: str,
oracle_provider: str,
oracle_type: str,
oracle_scale_factor: int,
maker_fee_rate: Decimal,
taker_fee_rate: Decimal,
expiration_timestamp: int,
settlement_timestamp: int,
admin: str,
quote_denom: str,
min_price_tick_size: Decimal,
min_quantity_tick_size: Decimal,
min_notional: Decimal,
) -> injective_exchange_tx_pb.MsgInstantBinaryOptionsMarketLaunch:
quote_token = self.tokens[quote_denom]
chain_min_price_tick_size = min_price_tick_size * Decimal(
f"1e{quote_token.decimals + ADDITIONAL_CHAIN_FORMAT_DECIMALS}"
)
chain_min_quantity_tick_size = min_quantity_tick_size * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
chain_maker_fee_rate = maker_fee_rate * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
chain_taker_fee_rate = taker_fee_rate * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
chain_min_notional = quote_token.chain_formatted_value(min_notional) * Decimal(
f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}"
)
return injective_exchange_tx_pb.MsgInstantBinaryOptionsMarketLaunch(
sender=sender,
ticker=ticker,
oracle_symbol=oracle_symbol,
oracle_provider=oracle_provider,
oracle_type=injective_oracle_pb.OracleType.Value(oracle_type),
oracle_scale_factor=oracle_scale_factor,
maker_fee_rate=f"{chain_maker_fee_rate.normalize():f}",
taker_fee_rate=f"{chain_taker_fee_rate.normalize():f}",
expiration_timestamp=expiration_timestamp,
settlement_timestamp=settlement_timestamp,
admin=admin,
quote_denom=quote_token.denom,
min_price_tick_size=f"{chain_min_price_tick_size.normalize():f}",
min_quantity_tick_size=f"{chain_min_quantity_tick_size.normalize():f}",
min_notional=f"{chain_min_notional.normalize():f}",
)
def msg_create_binary_options_limit_order(
self,
market_id: str,
sender: str,
subaccount_id: str,
fee_recipient: str,
price: Decimal,
quantity: Decimal,
margin: Decimal,
order_type: str,
cid: Optional[str] = None,
trigger_price: Optional[Decimal] = None,
denom: Optional[Denom] = None,
) -> injective_exchange_tx_pb.MsgCreateDerivativeLimitOrder:
return injective_exchange_tx_pb.MsgCreateDerivativeLimitOrder(
sender=sender,
order=self.binary_options_order(
market_id=market_id,
subaccount_id=subaccount_id,
fee_recipient=fee_recipient,
price=price,
quantity=quantity,
margin=margin,
order_type=order_type,
cid=cid,
trigger_price=trigger_price,
denom=denom,
),
)
def msg_create_binary_options_market_order(
self,
market_id: str,
sender: str,
subaccount_id: str,
fee_recipient: str,
price: Decimal,
quantity: Decimal,
margin: Decimal,
order_type: str,
cid: Optional[str] = None,
trigger_price: Optional[Decimal] = None,
denom: Optional[Denom] = None,
):
return injective_exchange_tx_pb.MsgCreateBinaryOptionsMarketOrder(
sender=sender,
order=self.binary_options_order(
market_id=market_id,
subaccount_id=subaccount_id,
fee_recipient=fee_recipient,
price=price,
quantity=quantity,
margin=margin,
order_type=order_type,
cid=cid,
trigger_price=trigger_price,
denom=denom,
),
)
def msg_cancel_binary_options_order(
self,
market_id: str,
sender: str,
subaccount_id: str,
order_hash: Optional[str] = None,
cid: Optional[str] = None,
is_conditional: Optional[bool] = False,
is_buy: Optional[bool] = False,
is_market_order: Optional[bool] = False,
) -> injective_exchange_tx_pb.MsgCancelBinaryOptionsOrder:
order_mask = self._order_mask(is_conditional=is_conditional, is_buy=is_buy, is_market_order=is_market_order)
return injective_exchange_tx_pb.MsgCancelBinaryOptionsOrder(
sender=sender,
market_id=market_id,
subaccount_id=subaccount_id,
order_hash=order_hash,
order_mask=order_mask,
cid=cid,
)
def msg_subaccount_transfer(
self,
sender: str,
source_subaccount_id: str,
destination_subaccount_id: str,
amount: Decimal,
denom: str,
) -> injective_exchange_tx_pb.MsgSubaccountTransfer:
be_amount = self.create_coin_amount(amount=amount, token_name=denom)
return injective_exchange_tx_pb.MsgSubaccountTransfer(
sender=sender,
source_subaccount_id=source_subaccount_id,
destination_subaccount_id=destination_subaccount_id,
amount=be_amount,
)
def msg_external_transfer(
self,
sender: str,
source_subaccount_id: str,
destination_subaccount_id: str,
amount: Decimal,
denom: str,
) -> injective_exchange_tx_pb.MsgExternalTransfer:
coin = self.create_coin_amount(amount=amount, token_name=denom)
return injective_exchange_tx_pb.MsgExternalTransfer(
sender=sender,
source_subaccount_id=source_subaccount_id,
destination_subaccount_id=destination_subaccount_id,
amount=coin,
)
def msg_liquidate_position(
self,
sender: str,
subaccount_id: str,
market_id: str,
order: Optional[injective_exchange_pb.DerivativeOrder] = None,
) -> injective_exchange_tx_pb.MsgLiquidatePosition:
return injective_exchange_tx_pb.MsgLiquidatePosition(
sender=sender, subaccount_id=subaccount_id, market_id=market_id, order=order
)
def msg_emergency_settle_market(
self,
sender: str,
subaccount_id: str,
market_id: str,
) -> injective_exchange_tx_pb.MsgEmergencySettleMarket:
return injective_exchange_tx_pb.MsgEmergencySettleMarket(
sender=sender, subaccount_id=subaccount_id, market_id=market_id
)
def msg_increase_position_margin(
self,
sender: str,
source_subaccount_id: str,
destination_subaccount_id: str,
market_id: str,
amount: Decimal,
):
market = self.derivative_markets[market_id]
additional_margin = market.margin_to_chain_format(human_readable_value=amount)
return injective_exchange_tx_pb.MsgIncreasePositionMargin(
sender=sender,
source_subaccount_id=source_subaccount_id,
destination_subaccount_id=destination_subaccount_id,
market_id=market_id,
amount=str(int(additional_margin)),
)
def msg_rewards_opt_out(self, sender: str) -> injective_exchange_tx_pb.MsgRewardsOptOut:
return injective_exchange_tx_pb.MsgRewardsOptOut(sender=sender)
def msg_admin_update_binary_options_market(
self,
sender: str,
market_id: str,
status: str,
settlement_price: Optional[Decimal] = None,
expiration_timestamp: Optional[int] = None,
settlement_timestamp: Optional[int] = None,
) -> injective_exchange_tx_pb.MsgAdminUpdateBinaryOptionsMarket:
market = self.binary_option_markets[market_id]
if settlement_price is not None:
chain_settlement_price = market.price_to_chain_format(human_readable_value=settlement_price)
price_parameter = f"{chain_settlement_price.normalize():f}"
else:
price_parameter = None
return injective_exchange_tx_pb.MsgAdminUpdateBinaryOptionsMarket(
sender=sender,
market_id=market_id,
settlement_price=price_parameter,
expiration_timestamp=expiration_timestamp,
settlement_timestamp=settlement_timestamp,
status=status,
)
def msg_decrease_position_margin(
self,
sender: str,
source_subaccount_id: str,
destination_subaccount_id: str,
market_id: str,
amount: Decimal,
) -> injective_exchange_tx_pb.MsgDecreasePositionMargin:
market = self.derivative_markets[market_id]
additional_margin = market.margin_to_chain_format(human_readable_value=amount)
return injective_exchange_tx_pb.MsgDecreasePositionMargin(
sender=sender,
source_subaccount_id=source_subaccount_id,
destination_subaccount_id=destination_subaccount_id,
market_id=market_id,
amount=str(int(additional_margin)),
)
def msg_update_spot_market(
self,
admin: str,
market_id: str,
new_ticker: str,
new_min_price_tick_size: Decimal,
new_min_quantity_tick_size: Decimal,
new_min_notional: Decimal,
) -> injective_exchange_tx_pb.MsgUpdateSpotMarket:
market = self.spot_markets[market_id]
chain_min_price_tick_size = new_min_price_tick_size * Decimal(
f"1e{market.quote_token.decimals - market.base_token.decimals + ADDITIONAL_CHAIN_FORMAT_DECIMALS}"
)
chain_min_quantity_tick_size = new_min_quantity_tick_size * Decimal(
f"1e{market.base_token.decimals + ADDITIONAL_CHAIN_FORMAT_DECIMALS}"
)
chain_min_notional = new_min_notional * Decimal(
f"1e{market.quote_token.decimals + ADDITIONAL_CHAIN_FORMAT_DECIMALS}"
)
return injective_exchange_tx_pb.MsgUpdateSpotMarket(
admin=admin,
market_id=market_id,
new_ticker=new_ticker,
new_min_price_tick_size=f"{chain_min_price_tick_size.normalize():f}",
new_min_quantity_tick_size=f"{chain_min_quantity_tick_size.normalize():f}",
new_min_notional=f"{chain_min_notional.normalize():f}",
)
def msg_update_derivative_market(
self,
admin: str,
market_id: str,
new_ticker: str,
new_min_price_tick_size: Decimal,
new_min_quantity_tick_size: Decimal,
new_min_notional: Decimal,
new_initial_margin_ratio: Decimal,
new_maintenance_margin_ratio: Decimal,
) -> injective_exchange_tx_pb.MsgUpdateDerivativeMarket:
market = self.derivative_markets[market_id]
chain_min_price_tick_size = new_min_price_tick_size * Decimal(
f"1e{market.quote_token.decimals + ADDITIONAL_CHAIN_FORMAT_DECIMALS}"
)
chain_min_quantity_tick_size = new_min_quantity_tick_size * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
chain_min_notional = new_min_notional * Decimal(
f"1e{market.quote_token.decimals + ADDITIONAL_CHAIN_FORMAT_DECIMALS}"
)
chain_initial_margin_ratio = new_initial_margin_ratio * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
chain_maintenance_margin_ratio = new_maintenance_margin_ratio * Decimal(f"1e{ADDITIONAL_CHAIN_FORMAT_DECIMALS}")
return injective_exchange_tx_pb.MsgUpdateDerivativeMarket(
admin=admin,
market_id=market_id,
new_ticker=new_ticker,
new_min_price_tick_size=f"{chain_min_price_tick_size.normalize():f}",