@@ -38,20 +38,65 @@ class DerivativeOrder(EIP712Struct):
3838class OrderHashes :
3939 def __init__ (
4040 self ,
41- spot : [str ],
42- derivative : [str ],
41+ spot : [str ] = None ,
42+ derivative : [str ] = None ,
43+ nonce : str = None
4344 ):
4445 self .spot = spot
4546 self .derivative = derivative
47+ self .nonce = nonce
48+
49+ @classmethod
50+ def get_subaccount_nonce (self , network , subaccount_id ) -> int :
51+ url = network .lcd_endpoint + '/injective/exchange/v1beta1/exchange/' + subaccount_id
52+ res = requests .get (url = url )
53+ nonce = res .json ()["nonce" ]
54+ self .nonce = nonce + 1
55+ return self .nonce
56+
57+ @classmethod
58+ def compute_order_hashes (self , spot_orders , derivative_orders ) -> [str ]:
59+ if len (spot_orders ) + len (derivative_orders ) == 0 :
60+ return []
61+
62+ order_hashes = OrderHashes (spot = [], derivative = [])
63+
64+ subaccount_id = None
65+ if len (spot_orders ) > 0 :
66+ subaccount_id = spot_orders [0 ].order_info .subaccount_id
67+ else :
68+ subaccount_id = derivative_orders [0 ].order_info .subaccount_id
69+
70+ for o in spot_orders :
71+ msg = build_eip712_msg (o , self .nonce )
72+ typed_data_hash = msg .hash_struct ()
73+ typed_bytes = b'\x19 \x01 ' + domain_separator + typed_data_hash
74+ keccak256 = sha3 .keccak_256 ()
75+ keccak256 .update (typed_bytes )
76+ order_hash = keccak256 .hexdigest ()
77+ order_hashes .spot .append ('0x' + order_hash )
78+ self .nonce += 1
79+
80+ for o in derivative_orders :
81+ msg = build_eip712_msg (o , self .nonce )
82+ typed_data_hash = msg .hash_struct ()
83+ typed_bytes = b'\x19 \x01 ' + domain_separator + typed_data_hash
84+ keccak256 = sha3 .keccak_256 ()
85+ keccak256 .update (typed_bytes )
86+ order_hash = keccak256 .hexdigest ()
87+ order_hashes .derivative .append ('0x' + order_hash )
88+ self .nonce += 1
89+
90+ return order_hashes
4691
4792def param_to_backend_go (param ) -> int :
4893 go_param = Decimal (param ) / pow (10 , 18 )
4994 return format (go_param , '.18f' )
5095
51- def get_subaccount_nonce ( network , subaccount_id ) -> int :
52- url = network . lcd_endpoint + '/injective/exchange/v1beta1/exchange/' + subaccount_id
53- res = requests . get ( url = url )
54- return res . json ()[ "nonce" ]
96+ def increment_nonce ( self ) :
97+ current_nonce = self . nonce
98+ self . nonce += 1
99+ return current_nonce
55100
56101def parse_order_type (order ):
57102 return order_type_dict [order .order_type ]
@@ -95,40 +140,4 @@ def build_eip712_msg(order, nonce):
95140 )
96141
97142# only support msgs from single subaccount
98- def compute_order_hashes (network , spot_orders , derivative_orders ) -> [str ]:
99- if len (spot_orders ) + len (derivative_orders ) == 0 :
100- return []
101-
102- order_hashes = OrderHashes (spot = [], derivative = [])
103-
104- subaccount_id = None
105- if len (spot_orders ) > 0 :
106- subaccount_id = spot_orders [0 ].order_info .subaccount_id
107- else :
108- subaccount_id = derivative_orders [0 ].order_info .subaccount_id
109-
110- # get starting nonce
111- nonce = get_subaccount_nonce (network , subaccount_id )
112- nonce += 1
113-
114- for o in spot_orders :
115- msg = build_eip712_msg (o , nonce )
116- typed_data_hash = msg .hash_struct ()
117- typed_bytes = b'\x19 \x01 ' + domain_separator + typed_data_hash
118- keccak256 = sha3 .keccak_256 ()
119- keccak256 .update (typed_bytes )
120- order_hash = keccak256 .hexdigest ()
121- order_hashes .spot .append ('0x' + order_hash )
122- nonce += 1
123-
124- for o in derivative_orders :
125- msg = build_eip712_msg (o , nonce )
126- typed_data_hash = msg .hash_struct ()
127- typed_bytes = b'\x19 \x01 ' + domain_separator + typed_data_hash
128- keccak256 = sha3 .keccak_256 ()
129- keccak256 .update (typed_bytes )
130- order_hash = keccak256 .hexdigest ()
131- order_hashes .derivative .append ('0x' + order_hash )
132- nonce += 1
133-
134- return order_hashes
143+
0 commit comments