diff --git a/crypto_seller/__init__.py b/crypto_seller/__init__.py index 1a21e76..0114779 100644 --- a/crypto_seller/__init__.py +++ b/crypto_seller/__init__.py @@ -76,7 +76,7 @@ import random import time from decimal import Decimal from pathlib import Path -from typing import Any +from typing import Any,Callable import fin_defs import fin_depo @@ -96,27 +96,28 @@ PATH_TRADES_FILE = PATH_OUTPUT / 'trades.csv' # Main code # -@dataclasses.dataclass +@dataclasses.dataclass(frozen = True) class AutoSellConfig: interval_range: tuple[datetime.timedelta, datetime.timedelta] input_asset: fin_defs.Asset input_amount_range: tuple[Decimal, Decimal] output_asset: fin_defs.Asset + seller: fin_depo.defi_kucoin.KucoinDepoFetcher + sleep: Callable[[float],None] exit_when_empty: bool = True - def sample_from_range(rng: random.Random, range: tuple[Any, Any]) -> Any: multiplier = rng.random() if isinstance(range[0], Decimal): multiplier = Decimal(multiplier) return range[0] + (range[1] - range[0]) * multiplier -def run_auto_sell(seller: fin_depo.defi_kucoin.KucoinDepoFetcher, config: AutoSellConfig): +def run_auto_sell(config: AutoSellConfig): rng = random.SystemRandom() while True: # Check that account has tokens. - input_amount_available = seller.get_depo().get_amount_of_asset(config.input_asset) + input_amount_available = config.seller.get_depo().get_amount_of_asset(config.input_asset) logger.info('Currently own %s %s', input_amount_available, config.input_asset) if input_amount_available > 0: @@ -124,7 +125,7 @@ def run_auto_sell(seller: fin_depo.defi_kucoin.KucoinDepoFetcher, config: AutoSe amount_to_sell = min(input_amount_available, amount_to_sell) logger.info('Attempting to sell %s %s', amount_to_sell, config.input_asset) - order_details = seller.place_market_order( + order_details = config.seller.place_market_order( config.input_asset, amount_to_sell, config.output_asset) print(order_details) @@ -138,6 +139,6 @@ def run_auto_sell(seller: fin_depo.defi_kucoin.KucoinDepoFetcher, config: AutoSe time_to_sleep = sample_from_range(rng, config.interval_range) time_to_sleep_secs = time_to_sleep.total_seconds() logger.info('Sleeping %s (%d seconds)', time_to_sleep, time_to_sleep_secs) - time.sleep(time_to_sleep_secs) + config.sleep(time_to_sleep_secs) del input_amount_available diff --git a/crypto_seller/__main__.py b/crypto_seller/__main__.py index 0f119bc..98c525c 100644 --- a/crypto_seller/__main__.py +++ b/crypto_seller/__main__.py @@ -2,6 +2,7 @@ import datetime import logging import logging.handlers from decimal import Decimal +import time import fin_defs @@ -41,8 +42,10 @@ def main(): input_asset=fin_defs.MPC, output_asset=fin_defs.USDT, exit_when_empty=True, + seller = seller_backend, + sleep = time.sleep ) - run_auto_sell(seller_backend, auto_sell_config) + run_auto_sell(auto_sell_config) if __name__ == '__main__': diff --git a/test/test_auto_sell.py b/test/test_auto_sell.py index 5271cc3..d87f4a7 100644 --- a/test/test_auto_sell.py +++ b/test/test_auto_sell.py @@ -32,17 +32,31 @@ class SellerMock(fin_depo.data.DepoFetcher): ) +class SleepMock: + + def __init__(self): + self.time_slept = 0.0 + + def __call__(self, amount: float): + self.time_slept = self.time_slept + amount + def test_auto_run(): + + sleep_mock = SleepMock() + seller_mock = SellerMock(fin_defs.USDT, Decimal('1000')) + config = crypto_seller.AutoSellConfig( input_amount_range=(Decimal('1'), Decimal('1')), - interval_range=(datetime.timedelta(seconds=0), datetime.timedelta(seconds=0)), + interval_range=(datetime.timedelta(seconds=1), datetime.timedelta(seconds=1)), input_asset=fin_defs.USDT, output_asset=fin_defs.USD, exit_when_empty=True, + seller = seller_mock, + sleep = sleep_mock , ) - seller = SellerMock(fin_defs.USDT, Decimal('1000')) - crypto_seller.run_auto_sell(seller, config) + crypto_seller.run_auto_sell(config) - assert seller.amount_left == 0 + assert seller_mock.amount_left == 0 + assert sleep_mock.time_slept == 1000