From 113f1ca15874a8b763224b5c98dc4f985a3a915a Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Sun, 21 Jul 2024 12:00:55 +0200 Subject: [PATCH] Base functionality mostly implemented. Just need logging and output files. --- crypto_seller/__init__.py | 58 ++++++++++++++++++++++++++------------- crypto_seller/__main__.py | 9 +++--- crypto_seller/config.py | 7 +++++ 3 files changed, 51 insertions(+), 23 deletions(-) create mode 100644 crypto_seller/config.py diff --git a/crypto_seller/__init__.py b/crypto_seller/__init__.py index 90be2b9..a787dd1 100644 --- a/crypto_seller/__init__.py +++ b/crypto_seller/__init__.py @@ -15,7 +15,9 @@ import time import fin_defs +import fin_depo +from . import config from ._version import __version__ logger = logging.getLogger(__name__) @@ -23,10 +25,10 @@ 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 + interval_range: tuple[datetime.timedelta, datetime.timedelta] + input_asset: fin_defs.Asset + input_amount_range: tuple[Decimal, Decimal] + output_asset: fin_defs.Asset exit_when_empty: bool = True def sample_from_range(rng: random.Random, range: tuple[Any, Any]) -> Any: @@ -35,36 +37,54 @@ def sample_from_range(rng: random.Random, range: tuple[Any, Any]) -> Any: multiplier = Decimal(multiplier) return range[0] + (range[1] - range[0]) * multiplier -def determine_available_tokens(token: fin_defs.Asset) -> Decimal: - # TODO - return Decimal(10000) +SELLER = fin_depo.defi_kucoin.KucoinDepoFetcher( + kucoin_secret = config.KUCOIN_SECRET, + kucoin_key = config.KUCOIN_KEY, + kucoin_pass = config.KUCOIN_PASS, +) -def sell_tokens(token_input: fin_defs.Asset, tokens_to_sell: Decimal, token_output: fin_defs.Asset): - pass +def determine_available_tokens(token: fin_defs.Asset) -> Decimal: + return SELLER.get_depo().get_amount_of_asset(token) + +def sell_tokens(input_asset: fin_defs.Asset, input_amount: Decimal, + output_asset: fin_defs.Asset) -> fin_depo.data.TradeOrderDetails: + #return SELLER.place_market_order(input_asset, input_amount, output_asset) + return fin_depo.data.TradeOrderDetails( + input_asset= input_asset, + input_amount= input_amount, + output_asset= output_asset, + output_amount= input_amount, + fee_asset = input_asset, + fee_amount = input_amount, + order_id = 10001, + raw_order_details = {'TEST': 1, 'DATA': 2}, + ) 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) + input_amount_available = determine_available_tokens(config.input_asset) + logger.info('Currently own %s %s', input_amount_available, config.input_asset) - 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) + if input_amount_available > 0: + amount_to_sell = sample_from_range(rng, config.input_amount_range) + amount_to_sell = min(input_amount_available, amount_to_sell) + logger.info('Attempting to sell %s %s', amount_to_sell, config.input_asset) - sell_tokens(config.token_input, tokens_to_sell, config.token_output) + order_details = sell_tokens(config.input_asset, amount_to_sell, config.output_asset) + print(order_details) + # TODO: Write order details to file. - del tokens_to_sell + del amount_to_sell elif config.exit_when_empty: break # Time out - time_to_sleep = sample_from_range(rng, config.sell_interval_range) + 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) - del num_tokens_available + del input_amount_available diff --git a/crypto_seller/__main__.py b/crypto_seller/__main__.py index 6b32e47..bc07469 100644 --- a/crypto_seller/__main__.py +++ b/crypto_seller/__main__.py @@ -13,10 +13,11 @@ logger.setLevel('INFO') def main(): logger.info('Initializing crypto_seller') config = AutoSellConfig( - sell_amount_range= (Decimal(100), Decimal(1000)), - sell_interval_range= (datetime.timedelta(minutes=20), datetime.timedelta(minutes=60)), - token_input= fin_defs.BTC, - token_output= fin_defs.USDT, + input_amount_range= (Decimal('0.5'), Decimal('1')), + interval_range= (datetime.timedelta(seconds=2), + datetime.timedelta(seconds=6)), + input_asset = fin_defs.MPC, + output_asset = fin_defs.USDT, exit_when_empty = True, ) run_auto_sell(config) diff --git a/crypto_seller/config.py b/crypto_seller/config.py new file mode 100644 index 0000000..77a4cb6 --- /dev/null +++ b/crypto_seller/config.py @@ -0,0 +1,7 @@ +import secret_loader + +load_secret = secret_loader.SecretLoader().load + +KUCOIN_KEY = load_secret('KUCOIN_KEY') +KUCOIN_SECRET = load_secret('KUCOIN_SECRET') +KUCOIN_PASS = load_secret('KUCOIN_PASS')