Beispiel #1
0
 def process_withdrawals(self):
     # Set wallet from relevant currency according to side
     to_wallet = self.buda.wallets.base if self.side == Side.BUY else self.buda.wallets.quote
     for deposit in self.deposits.values():
         # Filter deposits already converted and pending withdrawal
         deposit_confirmed = deposit['state'] == 'confirmed'
         has_pending_withdrawal = deposit['pending_withdrawal']
         converted_all = deposit['amounts']['original_amount'] == deposit[
             'amounts']['converted_amount']
         if deposit_confirmed and has_pending_withdrawal and converted_all:
             withdrawal_amount = truncate_to(
                 deposit['amounts']['converted_value'], self.to_currency)
             available = to_wallet.get_available()
             if withdrawal_amount <= available:  # We cannot withdraw more than available balance
                 self.notifier.notify(
                     f'Withdrawing {withdrawal_amount} {self.to_currency}')
                 withdrawal = to_wallet.request_withdrawal(
                     withdrawal_amount, self.to_address, subtract_fee=True)
                 if withdrawal.state == 'pending_preparation':  # Check state to set and store updated values
                     self.log.info(
                         f'{self.to_currency} withdrawal request received, updating store values'
                     )
                     deposit['pending_withdrawal'] = False
                     self.store.set(self.from_currency + '_deposits',
                                    self.deposits)
                     self.notifier.notify(
                         f'Success!, withdrawal id: {withdrawal.id}')
                 else:
                     msg = 'Withdrawal failed'
                     self.log.warning(msg)
                     self.notifier.notify(f'{msg}, :shame:')
             else:
                 msg = f'Available balance not enough for withdrawal amount {withdrawal_amount} {self.to_currency}'
                 self.log.warning(msg)
                 self.notifier.notify(msg)
Beispiel #2
0
 def process_conversions(self):
     # Get deposits
     for deposit in self.deposits.values():
         # Calculate remaining amount to convert
         original_amount = deposit['amounts']['original_amount']
         converted_amount = deposit['amounts']['converted_amount']
         converted_value = deposit['amounts']['converted_value']
         remaining = original_amount - converted_amount
         if converted_amount / original_amount >= 0.99:
             deposit['amounts']['converted_amount'] = original_amount
             self.store_deposits()
         elif deposit['state'] == 'confirmed' and remaining > 0:
             if self.side == Side.BUY:  # Change amount to base currency for order creation purposes
                 quotation = self.buda.client.quotation_market(
                     market_id=self.buda.market_id,
                     quotation_type='bid_given_spent_quote',
                     amount=remaining)
                 remaining = quotation.order_amount.amount
             remaining = truncate_to(remaining, self.market.base)
             # Convert remaining amount using market order
             order = self.buda.place_market_order(self.side, remaining)
             self.notifier.notify(
                 f'{self.side.value}ing {remaining} {self.market.base} at market rate'
             )
             # Wait for traded state to set updated values
             if order:
                 self.log.info(
                     f'{self.side} market order placed, waiting for traded state'
                 )
                 while order.state != 'traded':
                     order = self.buda.client.order_details(order.id)
                     time.sleep(1)
                 self.log.info(
                     f'{self.side} order traded, updating store values')
                 # Update amounts
                 if self.side == Side.BUY and order.state == 'traded':
                     converted_amount += order.total_exchanged.amount
                     converted_value += order.traded_amount.amount
                 else:
                     converted_amount += order.traded_amount.amount
                     converted_value += order.total_exchanged.amount
                 converted_value -= order.paid_fee.amount  # Fee deducted so it wont interfere with withdrawal
                 deposit['orders'].append(
                     order.id)  # Save related orders for debugging
                 self.notifier.notify(
                     f'Success!, converted value: {converted_value} {self.to_currency}'
                 )
             # Save new values
             deposit['amounts']['converted_amount'] = converted_amount
             deposit['amounts']['converted_value'] = converted_value
             self.store_deposits()
Beispiel #3
0
 def truncate_price(self, value):
     return truncate_to(value, self.buda.market.quote)
Beispiel #4
0
 def truncate_amount(self, value):
     return truncate_to(value, self.buda.market.base)