def handle_add_asset(): merkatos = get_all_merkatos() complete_merkato_configs = generate_complete_merkato_configs(merkatos) print('Select Merkato from available IDs') for counter, complete_config in enumerate(complete_merkato_configs): exchange_name = complete_config['configuration'][ 'exchange'] + '_' + complete_config[ 'base'] + '_' + complete_config['coin'] print('{} -> {}'.format(counter + 1, exchange_name)) selection = input('Selection: ') num_selection = int(selection) - 1 selection_exists = len(complete_merkato_configs) > num_selection + 1 if selection_exists: complete_config = complete_merkato_configs[num_selection] asset_to_add = input('Do you want to add to {} or {}: '.format( complete_config['coin'], complete_config['base'])) while asset_to_add != complete_config[ 'coin'] and asset_to_add != complete_config['base']: print('Wrong Asset, try again') asset_to_add = input('Do you want to add to {} or {}: '.format( complete_config['coin'], complete_config['base'])) amount_to_add = input( 'How much {} do you want to add: '.format(asset_to_add)) password = getpass.getpass('Enter password for merkato: ') decrypt_keys(config=complete_config['configuration'], password=password) merkato = Merkato(**complete_config) merkato.update_orders(asset_to_add, amount_to_add) else: handle_add_asset()
class Bot(ttk.Frame): def __init__(self, app, parent, owner, persist=None, stub=False, auto_start=False, starting_stats=None, *args, **kwargs): ttk.Frame.__init__(self, parent, style="app.TFrame", *args, **kwargs) self.app = app # root self.parent = parent # containing frame self.owner = owner self.stub = stub self.is_active = False self.title_var = tk.StringVar() self.coin_title = "coin" self.base_title = "base" self.exchange_title = "<exchange>" if self.stub: self.name = "XMR/BTC Stub" else: self.name = "New Merkato" self.title_var.set(str(self.name)) # merkato args #self.auth_frame = ttk.Frame(self, style="app.TFrame") self.bot = None if persist: self.name = persist["coin"] + "/" + persist[ "base"] + " " + persist["configuration"]["exchange"] self.title_var.set(str(self.name)) self.bot = Merkato(**persist) #presumably from db # -------------------- self.exchange_frame = ttk.Frame(self, style="app.TFrame") self.exchange_menu, self.exchange_index = database_utils.get_all_exchanges( ) self.exchange_name = MyWidget(self.app, self.exchange_frame, handle="exchange", startVal="test", choices=self.exchange_menu) self.coin = MyWidget(self.app, self.exchange_frame, handle="coin", startVal="XMR", choices=["XMR", "LTC", "ETH", "DOGE", "PEPECASH"]) self.base = MyWidget(self.app, self.exchange_frame, handle="base", startVal="BTC", choices=["BTC", "USDT"]) #self.public_key = MyWidget(self.app, self.exchange_frame, handle="pub. key", choices="entry") #self.private_key = MyWidget(self.app, self.exchange_frame, handle="priv. key", is_password=True, choices="entry") self.ask_budget = MyWidget(self.app, self.exchange_frame, handle="coin reserve", startVal=636, choices="entry") self.bid_budget = MyWidget(self.app, self.exchange_frame, handle="base reserve", startVal=10, choices="entry") self.spread = MyWidget(self.app, self.exchange_frame, handle="spread [%]", startVal=5.0, choices="entry") self.margin = MyWidget(self.app, self.exchange_frame, handle="profit margin [%]", startVal="0", choices=[str(i) for i in range(101)]) self.execute = ttk.Button(self.exchange_frame, text="Launch", cursor="shuttle", command=self.start) self.exchange_name.grid(row=0, column=0, sticky=tk.NE, padx=(10, 5), pady=(5, 5)) self.coin.grid(row=1, column=0, sticky=tk.NE, padx=(10, 5), pady=(5, 5)) self.base.grid(row=2, column=0, sticky=tk.NE, padx=(10, 5), pady=(5, 5)) #self.public_key.grid(row=3, column=0, sticky=tk.NE, padx=(10, 5), pady=(5, 5)) #self.private_key.grid(row=4, column=0, sticky=tk.NE, padx=(10, 5), pady=(5, 5)) self.ask_budget.grid(row=5, column=0, sticky=tk.NE, padx=(10, 5), pady=(5, 5)) self.bid_budget.grid(row=6, column=0, sticky=tk.NE, padx=(10, 5), pady=(5, 5)) self.spread.grid(row=7, column=0, sticky=tk.NE, padx=(10, 5), pady=(5, 5)) self.margin.grid(row=8, column=0, sticky=tk.NE, padx=(10, 5), pady=(5, 5)) self.execute.grid(row=9, column=0, sticky=tk.NE, padx=(10, 5), pady=(15, 5)) # -------------------- self.util_frame = ttk.Frame(self, style="app.TFrame") self.kill_button = ttk.Button(self.util_frame, text="Kill", cursor="shuttle", command=self.kill) self.kill_button.grid(row=0, column=0, sticky=tk.SE, padx=(10, 5), pady=(15, 5)) self.add_assets_type = MyWidget( self.app, self.util_frame, handle="Add Asset", startVal="XMR", choices=["XMR", "BTC", "ETH", "DOGE", "PEPECASH"]) self.add_assets_type.grid(row=1, column=0, sticky=tk.SE, padx=(10, 5), pady=(15)) self.add_assets_amount = MyWidget(self.app, self.util_frame, handle="amount to add", startVal=1.0, choices="entry") self.add_assets_amount.grid(row=2, column=0, sticky=tk.SE, padx=(10, 5), pady=(15)) self.add_assets_button = ttk.Button(self.util_frame, text="Add Assets", cursor="shuttle", command=self.add_assets) self.add_assets_button.grid(row=3, column=0, sticky=tk.SE, padx=(10, 5), pady=(15)) # -------------------- if not starting_stats: starting_stats = {"price_x": []} self.graph = Graph(self.app, self, stub=self.stub, **starting_stats) self.graph.grid(row=0, column=0, rowspan=3, padx=(5, 10)) #self.auth_frame.grid(row = 0, column=1, sticky=tk.NE, padx=(10,10), pady=(20,5)) if not self.bot: self.exchange_frame.grid(row=0, column=1, sticky=tk.NE, padx=(10, 10), pady=(20, 5)) self.util_frame.grid(row=1, column=1, sticky=tk.SE, padx=(10, 10), pady=(10, 5)) def add_assets(self): add_assets_amount = self.add_assets_amount.get()[0] add_assets_type = self.add_assets_type.get()[0] self.bot.update_orders(add_assets_type, add_assets_amount) def update(self, initial_update=False): context = {} if self.stub or self.bot: # then we have something to update print("--------------- updating %s ----------------------" % self.name) if not self.stub: if self.bot.initialized: if initial_update == False: context = self.bot.update() if initial_update == True: print('doing initial update') context = self.bot.get_context_history() print('adding bid_reserved_balance') context['ask_reserved_balance'] = float( self.bot.ask_reserved_balance) context['bid_reserved_balance'] = float( self.bot.bid_reserved_balance) context['quote_volume'] = float(self.bot.quote_volume) context['base_volume'] = float(self.bot.base_volume) context['spread'] = float(self.bot.spread) else: context = self.graph.fake_data() if context: self.graph.refresh(data=context, active=self.is_active) try: self.title_var.set(str(self.name)) except: self.title_var.set(str(self.name) + " err") def start(self): for widget in [ self.exchange_name, self.coin, self.base, self.ask_budget, self.bid_budget ]: print("{}:\t\t{}".format(widget.handle, widget.get()[0])) self.merk_args = {} self.merk_args["configuration"] = konfig.decrypt_keys( self.exchange_index[self.exchange_name.get()[0]], self.owner.password) self.merk_args["coin"] = self.coin.get()[0] self.merk_args["base"] = self.base.get()[0] self.merk_args["ask_reserved_balance"] = float( self.ask_budget.get()[0]) self.merk_args["bid_reserved_balance"] = float( self.bid_budget.get()[0]) self.merk_args["spread"] = float(self.spread.get()[0]) / 100.0 self.merk_args["profit_margin"] = float(self.margin.get()[0]) / 100.0 self.merk_args["user_interface"] = self self.coin_title = self.merk_args["coin"] self.base_title = self.merk_args["base"] self.graph.profit_base.config(text="%s \u0394bal:" % self.base_title[:4]) self.graph.profit_alt.config(text="%s \u0394bal:" % self.coin_title[:4]) self.exchange_title = self.merk_args["configuration"]["exchange"] self.name = str(self.merk_args["coin"]) + "/" + str( self.merk_args["base"]) + " " + self.exchange_title self.title_var.set(str(self.name)) if not self.stub: try: self.bot = Merkato(**self.merk_args) self.update(initial_update=True) except Exception as e: raise Exception('bot failed to start', e) e2 = traceback.format_exc() safe_show = self.merk_args.copy() safe_show["configuration"] = "obfuscated: " + str( safe_show["configuration"]["exchange"]) MessageBox.showerror("Bot Start Fail", str(e2) + "\n" + repr(self.merk_args)) else: self.exchange_frame.destroy() def kill(self): # TODO: tell self.bot to cancel all orders and delete merkato from DB if self.bot: self.bot.kill() # merkato.kill() self.bot = None # garbage collect mMerkato object, basically self._root().after(10, self.owner.kill_screen, self) # remove from gui def confirm_price(self, price): self.confirmation = popupWindow(self.app, "Confirm starting price: %s" % price, price) self.app.wait_window(self.confirmation.top) price = self.confirmation.value return price # if not exchange_config: # # get args via gui # pass #self.bot = Merkato(exchange_config) """