Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kraken.exceptions.KrakenException.KrakenInvalidNonceError: An invalid nonce was supplied. #114

Closed
andyDoucette opened this issue Jun 5, 2023 · 5 comments · Fixed by #106
Assignees
Labels
Bug Something isn't working Spot Topic related to Spot trading

Comments

@andyDoucette
Copy link

I'm getting InvalidNonceErrors when I'm calling Trade().create_order().

  File "/cs/crypto_sage/kraken/spot/trade/__init__.py", line 337, in create_order
    return self._request(  # type: ignore[return-value]
  File "/cs/crypto_sage/kraken/base_api/__init__.py", line 245, in _request
    return self.__check_response_data(
  File "/cs/crypto_sage/kraken/base_api/__init__.py", line 303, in __check_response_data
    return self.__err_handler.check(data)  # type: ignore[arg-type]
  File "/cs/crypto_sage/kraken/base_api/__init__.py", line 60, in check
    raise exception(data)
kraken.exceptions.KrakenException.KrakenInvalidNonceError: An invalid nonce was supplied.

I am running a single thread, single process for this API key. I have another process using a different API key but they should be independent.

The sdk code I'm using is based off of 1fce745. I'm not sure if the nonce logic has changed since then or not.

Could there be an issue with the nonce logic? Are you using system time to the microsecond?

@andyDoucette andyDoucette added the Bug Something isn't working label Jun 5, 2023
@btschwertfeger
Copy link
Owner

According to the commit you provided, this is where the nonce is calculated:

self.__nonce = (self.__nonce + 1) % 1
params["nonce"] = str(int(time.time() * 1000)) + str(self.__nonce).zfill(4)

Indeed, the nonce is always API key independent and must be the highest ever sent for this key.

I'm running several deployments using different API keys and did not experienced any nonce related errors, these were only a problem in a very early version of this tool.

You can check the behaviour by running multiple instances using different and some with the same API keys and access endpoints that need authentication. Only those with the same key that are executed in nearly the same time but with some internal delay could lead to this error.

@andyDoucette
Copy link
Author

andyDoucette commented Jun 7, 2023

That looks to me to just be millisecond accuracy. Maybe two calls are happening within 1ms and getting the same nonce? I remember from some earlier projects that ms accuracy wasn't enough for my logging, I needed microsecond accuracy to not have re-use of timestamps.

@andyDoucette
Copy link
Author

Do you have any concerns about using *1000000 instead of *1000?

@btschwertfeger
Copy link
Owner

I increased the accuracy of the nonce calculation for Spot and Futures in https://github.com/btschwertfeger/python-kraken-sdk/pull/106)](https://github.com/btschwertfeger/python-kraken-sdk/pull/106. This will be included in the upcoming release (milestone/7).

@btschwertfeger btschwertfeger added the Spot Topic related to Spot trading label Jun 10, 2023
@btschwertfeger btschwertfeger self-assigned this Jun 10, 2023
@andyDoucette
Copy link
Author

Thanks so much! :) I appreciate how well you're managing this project. You really seem on top of things. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Spot Topic related to Spot trading
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants