From cc50cba21025a986a40a23ca8412d88e96d74034 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Mon, 22 Jul 2024 22:18:17 +0200 Subject: [PATCH] Retry get order because we might too fast to ask for it after placing order --- fin_depo/defi_kucoin.py | 27 +++++++++++++++++++++++++-- test/secrets.py | 6 +++--- test/test_kucoin.py | 7 ++++++- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/fin_depo/defi_kucoin.py b/fin_depo/defi_kucoin.py index e5ade01..2efdf02 100644 --- a/fin_depo/defi_kucoin.py +++ b/fin_depo/defi_kucoin.py @@ -4,6 +4,7 @@ import datetime import logging from decimal import Decimal +import time import fin_defs import kucoin.client @@ -135,8 +136,17 @@ class KucoinDepoFetcher(DepoFetcher): del symbol, side, size, funds, input_amount # Determine order details - order_id = response['orderId'] - order_details = self.kucoin_client.get_order(order_id) + return self._get_order_details(response['orderId'], input_asset, output_asset) + + def _get_order_details(self, order_id: str, + input_asset: fin_defs.Asset, + output_asset: fin_defs.Asset) -> TradeOrderDetails: + """Determine the order details for the order with the given id. + + Retries the order a few times, as KuCoin might not have propagated the + order through their systems. + """ + order_details = self._get_order_with_retries(order_id, num_retries=10) # Convert from kucoin results if input_asset == fin_defs.USDT: @@ -159,3 +169,16 @@ class KucoinDepoFetcher(DepoFetcher): order_id=order_id, raw_order_details=order_details, ) + + def _get_order_with_retries(self, order_id: str, *, num_retries: int, sleep_between_tries:float = 1.0) -> dict: + """Get the order details from KuCoin backend. + + Retries the order a few times, as KuCoin might not have propagated the + order through their systems since it was sent. + """ + for _ in range(num_retries): + try: + return self.kucoin_client.get_order(order_id) + except kucoin.exceptions.KucoinAPIException as e: # noqa + time.sleep(sleep_between_tries) + return self.kucoin_client.get_order(order_id) diff --git a/test/secrets.py b/test/secrets.py index 73f2d90..a5a8e14 100644 --- a/test/secrets.py +++ b/test/secrets.py @@ -15,6 +15,6 @@ NORDNET_PASSWORD = load_secret('NORDNET_PASSWORD') KRAKEN_KEY = load_secret('KRAKEN_KEY') KRAKEN_SECRET = load_secret('KRAKEN_SECRET') -KUCOIN_KEY = load_secret('KUCOIN_KEY') -KUCOIN_SECRET = load_secret('KUCOIN_SECRET') -KUCOIN_PASS = load_secret('KUCOIN_PASS') +KUCOIN_KEY = load_secret('KUCOIN_KEY_TRADING') +KUCOIN_SECRET = load_secret('KUCOIN_SECRET_TRADING') +KUCOIN_PASS = load_secret('KUCOIN_PASS_TRADING') diff --git a/test/test_kucoin.py b/test/test_kucoin.py index 6d60d89..e0185e0 100644 --- a/test/test_kucoin.py +++ b/test/test_kucoin.py @@ -2,12 +2,13 @@ from decimal import Decimal import fin_defs import pytest +import datetime import fin_depo from . import secrets -TEST_MARKET_ORDERS = False +TEST_MARKET_ORDERS = True needs_secrets = pytest.mark.skipif( not secrets.KUCOIN_KEY, @@ -16,6 +17,7 @@ needs_secrets = pytest.mark.skipif( fin_depo.defi_kucoin.logger.setLevel('INFO') +NOW = datetime.datetime.now(tz=datetime.UTC) @needs_secrets def test_get_depo(): @@ -82,6 +84,7 @@ def test_place_buy_side_order(): assert order_details.fee_asset == fin_defs.USDT assert order_details.fee_amount <= Decimal('0.0002') + assert NOW <= order_details.executed_time <= NOW + datetime.timedelta(minutes=10) @needs_secrets def test_place_sell_side_order(): @@ -115,3 +118,5 @@ def test_place_sell_side_order(): assert order_details.fee_asset == fin_defs.USDT assert order_details.fee_amount is not None + + assert NOW <= order_details.executed_time <= NOW + datetime.timedelta(minutes=10)