dydxprotocol / dydx-v3-python

Python client for dYdX (API v3)
Apache License 2.0
306 stars 176 forks source link

Problem with starkex when creating order #150

Open gabrielfior opened 2 years ago

gabrielfior commented 2 years ago

I tried to create an order using the dydx3 python package, but I receive an error with my signature. Using the UI the order creation process works fine.

~\code\deepb-ai\trading_engine\trading_coordinator.py in execute_order(self, order_container)
    169     def execute_order(self, order_container: OrderContainer):
    170 
--> 171         create_order_result = self.client.private.create_order(**dataclasses.asdict(order_container))
    172 
    173         order_id = create_order_result.data['order']['id']

~\miniconda3\envs\alpaca-env\lib\site-packages\dydx3\modules\private.py in create_order(self, position_id, market, side, order_type, post_only, size, price, limit_fee, time_in_force, cancel_id, trigger_price, trailing_percent, client_id, expiration, expiration_epoch_seconds, signature)
    556                 expiration_epoch_seconds=expiration_epoch_seconds,
    557             )
--> 558             order_signature = order_to_sign.sign(self.stark_private_key)
    559 
    560         order = {

~\miniconda3\envs\alpaca-env\lib\site-packages\dydx3\starkex\signable.py in sign(self, private_key_hex)
     30     def sign(self, private_key_hex):
     31         """Sign the hash of the object using the given private key."""
---> 32         r, s = sign(self.hash, int(private_key_hex, 16))
     33         return serialize_signature(r, s)
     34 

~\miniconda3\envs\alpaca-env\lib\site-packages\dydx3\starkex\signable.py in hash(self)
     25         """Get the hash of the object."""
     26         if self._hash is None:
---> 27             self._hash = self._calculate_hash()
     28         return self._hash
     29 

~\miniconda3\envs\alpaca-env\lib\site-packages\dydx3\starkex\order.py in _calculate_hash(self)
    166         )
    167         return get_hash(
--> 168             get_hash(
    169                 assets_hash,
    170                 part_1,

~\miniconda3\envs\alpaca-env\lib\site-packages\dydx3\starkex\starkex_resources\proxy.py in get_hash(*elements)
     37         return cpp_hash(*elements)
     38 
---> 39     return py_pedersen_hash(*elements)

~\miniconda3\envs\alpaca-env\lib\site-packages\dydx3\starkex\starkex_resources\python_signature.py in py_pedersen_hash(*elements)
    249 
    250 def py_pedersen_hash(*elements: int) -> int:
--> 251     return pedersen_hash_as_point(*elements)[0]
    252 
    253 

~\miniconda3\envs\alpaca-env\lib\site-packages\dydx3\starkex\starkex_resources\python_signature.py in pedersen_hash_as_point(*elements)
    259     point = SHIFT_POINT
    260     for i, x in enumerate(elements):
--> 261         assert 0 <= x < FIELD_PRIME
    262         point_list = CONSTANT_POINTS[2 + i * N_ELEMENT_BITS_HASH:2 + (i + 1) * N_ELEMENT_BITS_HASH]
    263         assert len(point_list) == N_ELEMENT_BITS_HASH

AssertionError: 
naman-doshi commented 2 years ago

I have the same issue. Any thoughts on how to fix it?

saleh-mir commented 1 year ago

I have the same problem but not always. I'm not sure what is causing it. Here's my error's traceback:

562, in submit_order
res = self.client.private.create_order(
File "/opt/homebrew/lib/python3.9/site-packages/dydx3/modules/private.py", line 571, in create_order
order_signature = order_to_sign.sign(self.stark_private_key)
File "/opt/homebrew/lib/python3.9/site-packages/dydx3/starkex/signable.py", line 32, in sign
r, s = sign(self.hash, int(private_key_hex, 16))
File "/opt/homebrew/lib/python3.9/site-packages/dydx3/starkex/signable.py", line 27, in hash
self._hash = self._calculate_hash()
File "/opt/homebrew/lib/python3.9/site-packages/dydx3/starkex/order.py", line 168, in _calculate_hash
get_hash(
File "/opt/homebrew/lib/python3.9/site-packages/dydx3/starkex/starkex_resources/proxy.py", line 39, in get_hash
return py_pedersen_hash(*elements)
File "/opt/homebrew/lib/python3.9/site-packages/dydx3/starkex/starkex_resources/python_signature.py", line 251, in py_pedersen_hash
return pedersen_hash_as_point(*elements)[0]
File "/opt/homebrew/lib/python3.9/site-packages/dydx3/starkex/starkex_resources/python_signature.py", line 261, in pedersen_hash_as_point
assert 0 <= x < FIELD_PRIME
AssertionError

[UPDATE]: It works fine with buy/entry orders, but I get the error with sell/closing orders.

greenfield-br commented 1 year ago

hi there. i had it and solved it. the problem is that when you sell, size should be positive, not negative, the ORDER_BUY_SIDE, ORDER_SELL_SIDE makes up for the signal in size. that is confusing at first, because when you get the account/openpositions/market data, selling side come as negative. so, don rely on negative sizes for expressing selling, only for interpreting them. best regards to all the community.

saleh-mir commented 1 year ago

@greenfield-br god bless you! That was it