# Path: deploy/utils.py
# def amount_format(web3, wei):
# def createWallet():
# def check_succesful_tx(web3, txid, timeout=180) -> dict:
#     def __init__(self,
#                  web3,
#                  abi,
#                  address,
#                  event_name,
#                  from_block=0,
#                  to_block='latest',
#                  filters=None,
#                  callback=None):
#     def get_logs(self):
#     def set_log_data(self, log):
#     def watch_logs(self, callback):
#         def log_callback(log):
#     def stop(self):
# def watch_logs(contract, event, callback, params={}):
# def print_logs(contract, event, name=''):
# def returnFundsToOwner(web3, owner, bidder):
# def sendFunds(web3, owner, bidder, max_bid):
# def assignFundsToBidders(web3, owner: str, bidders: list, distribution_limit: int=None):
# def set_connection_pool_size(web3, pool_connections, pool_size):
# def get_expected_tokens(amount, token_multiplier, final_price):
# class LogFilter:


import logging
import random
import gevent
from deploy.utils import (
    passphrase,
    amount_format,
    check_succesful_tx
)
        if missing_funds <= self.last_missing_funds:
            log.warning('missing funds <= last missing: %d < %d'
                        % (missing_funds, self.last_missing_funds))
        self.last_missing_funds = missing_funds
        balance = self.web3.eth.getBalance(self.address)
        unlocked = self.web3.personal.unlockAccount(self.address, passphrase)
        assert unlocked is True
        amount = self.get_random_bid(missing_funds, balance)
        log.info('BID bidder=%s, missing_funds=%.2e, balance=%d, amount=%s' %
                 (self.address, missing_funds, balance, amount_format(self.web3, amount)))
        try:
            txhash = self.auction_contract.transact({'from': self.address, "value": amount}).bid()
            receipt, success = check_succesful_tx(self.web3, txhash)
        except ValueError as e:
            log.warn(str(e))
            if self.retries >= self.max_retries:
                raise e
            self.retries += 1

    def get_random_bid(self, missing_funds, balance):
        # cap missing funds to percentage defined by max_bid_ceiling
        max_bid = int(missing_funds * self.max_bid_ceiling)
        amount = int(max(0, min(balance - self.approx_bid_txn_cost, max_bid)))
        # cap to max bid price
        amount = min(amount, self.max_bid_price)
        # randomize how much will be used
        amount = int(amount * random.random())
        # make sure we bid at least min_bid_price
        amount = max(amount, min(self.min_bid_price, missing_funds))
        if amount == 0:

# Path: deploy/utils.py
# def amount_format(web3, wei):
# def createWallet():
# def check_succesful_tx(web3, txid, timeout=180) -> dict:
#     def __init__(self,
#                  web3,
#                  abi,
#                  address,
#                  event_name,
#                  from_block=0,
#                  to_block='latest',
#                  filters=None,
#                  callback=None):
#     def get_logs(self):
#     def set_log_data(self, log):
#     def watch_logs(self, callback):
#         def log_callback(log):
#     def stop(self):
# def watch_logs(contract, event, callback, params={}):
# def print_logs(contract, event, name=''):
# def returnFundsToOwner(web3, owner, bidder):
# def sendFunds(web3, owner, bidder, max_bid):
# def assignFundsToBidders(web3, owner: str, bidders: list, distribution_limit: int=None):
# def set_connection_pool_size(web3, pool_connections, pool_size):
# def get_expected_tokens(amount, token_multiplier, final_price):
# class LogFilter:


# Path: deploy/bidder.py
import logging
import random
import gevent
from deploy.utils import (
    passphrase,
    amount_format,
    check_succesful_tx
)

        if missing_funds <= self.last_missing_funds:
            log.warning('missing funds <= last missing: %d < %d'
                        % (missing_funds, self.last_missing_funds))
        self.last_missing_funds = missing_funds
        balance = self.web3.eth.getBalance(self.address)
        unlocked = self.web3.personal.unlockAccount(self.address, passphrase)
        assert unlocked is True
        amount = self.get_random_bid(missing_funds, balance)
        log.info('BID bidder=%s, missing_funds=%.2e, balance=%d, amount=%s' %
                 (self.address, missing_funds, balance, amount_format(self.web3, amount)))
        try:
            txhash = self.auction_contract.transact({'from': self.address, "value": amount}).bid()
            receipt, success = check_succesful_tx(self.web3, txhash)
        except ValueError as e:
            log.warn(str(e))
            if self.retries >= self.max_retries:
                raise e
            self.retries += 1

    def get_random_bid(self, missing_funds, balance):
        # cap missing funds to percentage defined by max_bid_ceiling
        max_bid = int(missing_funds * self.max_bid_ceiling)
        amount = int(max(0, min(balance - self.approx_bid_txn_cost, max_bid)))
        # cap to max bid price
        amount = min(amount, self.max_bid_price)
        # randomize how much will be used
        amount = int(amount * random.random())
        # make sure we bid at least min_bid_price
        amount = max(amount, min(self.min_bid_price, missing_funds))
        if amount == 0:

            amount = 1