Ejemplo n.º 1
0
def save_all_currency():
    """
    Sends leftover MB and (T)BTC to the predefined global wallet
    """
    wallet = wallet_controller.TriblerWallet(
        plebnet_settings.get_instance().wallets_testnet_created())
    wallet.pay(settings.wallets_btc_global(),
               satoshi_to_btc(wallet.get_balance()))
Ejemplo n.º 2
0
 def create_offer(self, amount_mb, timeout):
     """
     Retrieve the price of the chosen server to buy and make a new offer on the Tribler marketplace.
     :param amount_mb:
     :param timeout: offer to
     :return: None
     """
     if not self.config.get('chosen_provider'):
         return
     (provider, option, _) = self.config.get('chosen_provider')
     last_price = self.calculate_price(self.transactions[-1])
     btc_price = satoshi_to_btc(last_price * amount_mb)
     return self.place_offer(amount_mb, btc_price, timeout, self.config)
Ejemplo n.º 3
0
 def create_offer(self, amount_mb, timeout):
     """
     Retrieve the price of the chosen server to buy and make a new offer on the Tribler marketplace.
     :param amount_mb:
     :param timeout: offer to
     :return: None
     """
     if not self.config.get('chosen_provider'):
         return
     wallet = wallet_controller.TriblerWallet(plebnet_settings.get_instance().wallets_testnet_created())
     (provider, option, _) = self.config.get('chosen_provider')
     btc_balance = satoshi_to_btc(wallet.get_balance())
     btc_price = max(self.get_replication_price(provider, option) * self.target_vps_count - btc_balance, 0)
     self.place_offer(amount_mb, btc_price, timeout, self.config)
Ejemplo n.º 4
0
def make_ask(first_amount, first_type, second_amount, second_type):
    '''
    Makes an ask with the same arguments as a bid.
    '''
    logging.info("[Making an ask]:  %s %s / %s %s" %
                 (str(satoshi_to_btc(first_amount)), first_type,
                  str(second_amount), second_type))

    # make a bid
    data = {
        'first_asset_amount': first_amount,
        'first_asset_type': first_type,
        'second_asset_amount': second_amount,
        'second_asset_type': second_type
    }
    r = requests.put('http://localhost:8085/market/asks', data=data)
    logging.info(r.json())
Ejemplo n.º 5
0
def attempt_purchase_vpn():
    """
    Attempts to purchase a VPN, checks first if balance is sufficient
    The success message is stored to prevent further unecessary purchases.
    """
    provider = settings.vpn_host()
    if settings.wallets_testnet():
        domain = 'TBTC'
    else:
        domain = 'BTC'
    btc_balance = satoshi_to_btc(market_controller.get_balance(domain))
    vpn_price = cloudomate_controller.calculate_price_vpn(provider)
    if btc_balance >= vpn_price:
        logger.log("Try to buy a new VPN from %s" % provider, log_name)
        success = cloudomate_controller.purchase_choice_vpn(config)
        if success == plebnet_settings.SUCCESS:
            logger.success("Purchasing VPN succesful!", log_name)
        elif success == plebnet_settings.FAILURE:
            logger.error("Error purchasing vpn", log_name)
Ejemplo n.º 6
0
def attempt_purchase():
    """
    Check if enough money to buy a server, and if so, do so,
    """
    (provider, option, _) = config.get('chosen_provider')
    provider_offer_ID = str(provider).lower() + "_" + str(option).lower()
    if settings.wallets_testnet():
        domain = 'TBTC'
    else:
        domain = 'BTC'
    btc_balance = satoshi_to_btc(market_controller.get_balance(domain))
    vps_price = cloudomate_controller.calculate_price(provider, option)
    vpn_price = cloudomate_controller.calculate_price_vpn()
    logger.log('Selected VPS: %s (%s), %s BTC' % (provider, option, vps_price),
               log_name)
    logger.log('Selected VPN: %s, %s BTC' % ("mullvad", vpn_price), log_name)
    logger.log("Balance: %s %s" % (btc_balance, domain), log_name)
    if btc_balance >= vps_price + vpn_price:
        logger.log(
            "Before trying to purchase VPS share current QTable with other agents"
        )
        qtable.share_qtable()
        logger.log("Try to buy a new server from %s" % provider, log_name)
        success = cloudomate_controller.purchase_choice(config)
        if success == plebnet_settings.SUCCESS:
            # Update qtable yourself positively if you are successful
            qtable.update_qtable(remote_tables, provider_offer_ID, True,
                                 get_reward_qlearning())
            # purchase VPN with same config if server allows for it
            # purchase VPN with same config if server allows for it
            if cloudomate_controller.get_vps_providers(
            )[provider].TUN_TAP_SETTINGS:
                attempt_purchase_vpn()
        elif success == plebnet_settings.FAILURE:
            # Update qtable provider negatively if not successful
            qtable.update_qtable(remote_tables, provider_offer_ID, False,
                                 get_reward_qlearning())

        qtable.write_dictionary()
        config.increment_child_index()
        fake_generator.generate_child_account()
        config.set('chosen_provider', None)
        config.save()
Ejemplo n.º 7
0
def check_bids(limit):
    '''
    Periodically checks for bids in the market, and places an ask against it.
    Currently, accepts all bids regardless of bidder.
    '''

    logging.info('Coin type is: %s' % coin)
    logging.info('Limit price is: %s BTC/MB' % limit)

    r = requests.get('http://localhost:8085/market/bids')

    # evaluate each
    asks = r.json()['bids']

    # we focus only on MB-for-BTC bids
    for ask in asks:
        if ask['asset1'] == coin and ask['asset2'] == 'MB':
            ticks = sorted(ask['ticks'], key=tick_price)

            for tick in ticks:
                first_amount = tick['assets']['first'][
                    'amount']  # should be in BTC
                first_type = tick['assets']['first']['type']
                second_amount = tick['assets']['second'][
                    'amount']  # should be in MB
                second_type = tick['assets']['second']['type']
                price = tick_price(tick)

                logging.info('[Bid]: %s %s / %s %s (%s)' %
                             (satoshi_to_btc(first_amount), first_type,
                              second_amount, second_type, price))

                if price < limit:
                    make_ask(first_amount, first_type, second_amount,
                             second_type)
                    time.sleep(10)
Ejemplo n.º 8
0
def tick_price(tick):
    return satoshi_to_btc(
        tick['assets']['first']['amount']) / tick['assets']['second']['amount']