2024-07-17 19:49:19 +00:00
|
|
|
"""# Automatic Crypto Seller.
|
|
|
|
|
|
|
|
Description TODO.
|
|
|
|
"""
|
|
|
|
|
2024-07-17 20:21:58 +00:00
|
|
|
__all__ = ['__version__', 'run_auto_sell']
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
import dataclasses
|
|
|
|
import random
|
|
|
|
from typing import Any
|
|
|
|
import logging
|
|
|
|
from decimal import Decimal
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
import fin_defs
|
2024-07-17 19:49:19 +00:00
|
|
|
|
|
|
|
from ._version import __version__
|
2024-07-17 20:21:58 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logger.setLevel('INFO')
|
|
|
|
|
|
|
|
@dataclasses.dataclass
|
|
|
|
class AutoSellConfig:
|
|
|
|
sell_amount_range: tuple[Decimal, Decimal]
|
|
|
|
sell_interval_range: tuple[datetime.timedelta, datetime.timedelta]
|
|
|
|
token_input: fin_defs.Asset
|
|
|
|
token_output: fin_defs.Asset
|
|
|
|
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 determine_available_tokens(token: fin_defs.Asset) -> Decimal:
|
|
|
|
# TODO
|
|
|
|
return Decimal(10000)
|
|
|
|
|
|
|
|
def sell_tokens(token_input: fin_defs.Asset, tokens_to_sell: Decimal, token_output: fin_defs.Asset):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def run_auto_sell(config: AutoSellConfig):
|
|
|
|
rng = random.SystemRandom()
|
|
|
|
|
|
|
|
while True:
|
|
|
|
# Check that account has tokens.
|
|
|
|
num_tokens_available = determine_available_tokens(config.token_input)
|
|
|
|
logger.info('Currently own %s %s', num_tokens_available, config.token_input)
|
|
|
|
|
|
|
|
if num_tokens_available > 0:
|
|
|
|
tokens_to_sell = sample_from_range(rng, config.sell_amount_range)
|
|
|
|
tokens_to_sell = min(num_tokens_available, tokens_to_sell)
|
|
|
|
logger.info('Attempting to sell %s %s', tokens_to_sell, config.token_input)
|
|
|
|
|
|
|
|
sell_tokens(config.token_input, tokens_to_sell, config.token_output)
|
|
|
|
|
|
|
|
del tokens_to_sell
|
|
|
|
elif config.exit_when_empty:
|
|
|
|
break
|
|
|
|
|
|
|
|
# Time out
|
|
|
|
time_to_sleep = sample_from_range(rng, config.sell_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)
|
|
|
|
|
|
|
|
del num_tokens_available
|