# Path: deploy/utils.py
# def check_succesful_tx(web3, txid, timeout=180) -> dict:
#     receipt = wait_for_transaction_receipt(web3, txid, timeout=timeout)
#     txinfo = web3.eth.getTransaction(txid)
#     return receipt, txinfo["gas"] != receipt["gasUsed"]
# 
# def get_expected_tokens(amount, token_multiplier, final_price):
#     return (token_multiplier * amount) // final_price
# 
# Path: tests/utils_logs.py
# class LogFilter:
#     def __init__(self,
#         web3,
#         abi,
#         address,
#         event_name,
#         from_block=0,
#         to_block='latest',
#         filters=None,
#         callback=None):
#         self.web3 = web3
#         self.event_name = event_name
# 
#         # Callback for every registered log
#         self.callback = callback
# 
#         filter_kwargs = {
#             'fromBlock': from_block,
#             'toBlock': to_block,
#             'address': address
#         }
# 
#         event_abi = [i for i in abi if i['type'] == 'event' and i['name'] == event_name]
#         if len(event_abi) == 0:
#             return None
# 
#         self.event_abi = event_abi[0]
#         assert self.event_abi
# 
#         filters = filters if filters else {}
#         self.filter = construct_event_filter_params(
#             self.event_abi,
#             argument_filters=filters,
#             **filter_kwargs)[1]
#         filter_params = input_filter_params_formatter(self.filter)
#         self.filter = web3.eth.filter(filter_params)
# 
#     def init(self, post_callback=None):
#         for log in self.get_logs():
#             log['event'] = self.event_name
#             self.callback(log)
#         if post_callback:
#             post_callback()
# 
#     def watch(self):
#         def log_callback(log):
#             log = self.set_log_data(log)
#             self.callback(log)
# 
#         self.filter.watch(log_callback)
# 
#     def stop(self):
#         if self.filter.running:
#             self.filter.stop_watching()
# 
#     def get_logs(self):
#         response = self.web3.eth.getFilterLogs(self.filter.filter_id)
#         logs = log_array_formatter(response)
#         formatted_logs = []
#         for log in [dict(log) for log in logs]:
#             formatted_logs.append(self.set_log_data(log))
#         return formatted_logs
# 
#     def set_log_data(self, log):
#         log = dict(log_array_formatter([log])[0])
#         log['args'] = get_event_data(self.event_abi, log)['args']
#         log['event'] = self.event_name
#         return log


from time import time
from web3.utils.compat import (
    Timeout,
)
from deploy.utils import (
    check_succesful_tx,
    get_expected_tokens
)
from tests.utils_logs import LogFilter
import logging
        # 87380 gas / claimTokens
        # We need to calculate from gas estimation
        if unclaimed_number > 0 and not self.batch_number:
            valid_bid_address = self.addresses_unclaimed[0]
            claim_tx_gas = self.auction.estimateGas({'from': self.account}).proxyClaimTokens(valid_bid_address)
            # claim_tx_gas = 50000
            log.info('ESTIMATED claimTokens tx GAS: %s', (claim_tx_gas))

            self.batch_number = int(self.total_distribute_tx_gas // claim_tx_gas)
            log.info('BATCH number: %s', (self.batch_number))

        # Call the distributor contract with batches of bidder addresses
        while len(self.addresses_unclaimed):
            batch_number = min(self.batch_number, len(self.addresses_unclaimed))
            batch = self.addresses_unclaimed[:batch_number]
            self.addresses_unclaimed = self.addresses_unclaimed[batch_number:]
            self.addresses_claimed = self.addresses_claimed + batch

            log.info('Distributing tokens to %s addresses: %s' % (batch_number, ','.join(batch)))

            # Send the distribute transaction
            tx = {
                'from': self.account,
                'gas': int(self.total_distribute_tx_gas)
            }
            if self.gas_price:
                tx['gas_price'] = int(self.gas_price)
            txhash = self.distributor.transact(tx).distribute(batch)

            if self.wait:

# Path: deploy/utils.py
# def check_succesful_tx(web3, txid, timeout=180) -> dict:
#     receipt = wait_for_transaction_receipt(web3, txid, timeout=timeout)
#     txinfo = web3.eth.getTransaction(txid)
#     return receipt, txinfo["gas"] != receipt["gasUsed"]
# 
# def get_expected_tokens(amount, token_multiplier, final_price):
#     return (token_multiplier * amount) // final_price
# 
# Path: tests/utils_logs.py
# class LogFilter:
#     def __init__(self,
#         web3,
#         abi,
#         address,
#         event_name,
#         from_block=0,
#         to_block='latest',
#         filters=None,
#         callback=None):
#         self.web3 = web3
#         self.event_name = event_name
# 
#         # Callback for every registered log
#         self.callback = callback
# 
#         filter_kwargs = {
#             'fromBlock': from_block,
#             'toBlock': to_block,
#             'address': address
#         }
# 
#         event_abi = [i for i in abi if i['type'] == 'event' and i['name'] == event_name]
#         if len(event_abi) == 0:
#             return None
# 
#         self.event_abi = event_abi[0]
#         assert self.event_abi
# 
#         filters = filters if filters else {}
#         self.filter = construct_event_filter_params(
#             self.event_abi,
#             argument_filters=filters,
#             **filter_kwargs)[1]
#         filter_params = input_filter_params_formatter(self.filter)
#         self.filter = web3.eth.filter(filter_params)
# 
#     def init(self, post_callback=None):
#         for log in self.get_logs():
#             log['event'] = self.event_name
#             self.callback(log)
#         if post_callback:
#             post_callback()
# 
#     def watch(self):
#         def log_callback(log):
#             log = self.set_log_data(log)
#             self.callback(log)
# 
#         self.filter.watch(log_callback)
# 
#     def stop(self):
#         if self.filter.running:
#             self.filter.stop_watching()
# 
#     def get_logs(self):
#         response = self.web3.eth.getFilterLogs(self.filter.filter_id)
#         logs = log_array_formatter(response)
#         formatted_logs = []
#         for log in [dict(log) for log in logs]:
#             formatted_logs.append(self.set_log_data(log))
#         return formatted_logs
# 
#     def set_log_data(self, log):
#         log = dict(log_array_formatter([log])[0])
#         log['args'] = get_event_data(self.event_abi, log)['args']
#         log['event'] = self.event_name
#         return log


# Path: distributor/distributor.py
from time import time
from web3.utils.compat import (
    Timeout,
)
from deploy.utils import (
    check_succesful_tx,
    get_expected_tokens
)
from tests.utils_logs import LogFilter
import logging

        # 87380 gas / claimTokens
        # We need to calculate from gas estimation
        if unclaimed_number > 0 and not self.batch_number:
            valid_bid_address = self.addresses_unclaimed[0]
            claim_tx_gas = self.auction.estimateGas({'from': self.account}).proxyClaimTokens(valid_bid_address)
            # claim_tx_gas = 50000
            log.info('ESTIMATED claimTokens tx GAS: %s', (claim_tx_gas))

            self.batch_number = int(self.total_distribute_tx_gas // claim_tx_gas)
            log.info('BATCH number: %s', (self.batch_number))

        # Call the distributor contract with batches of bidder addresses
        while len(self.addresses_unclaimed):
            batch_number = min(self.batch_number, len(self.addresses_unclaimed))
            batch = self.addresses_unclaimed[:batch_number]
            self.addresses_unclaimed = self.addresses_unclaimed[batch_number:]
            self.addresses_claimed = self.addresses_claimed + batch

            log.info('Distributing tokens to %s addresses: %s' % (batch_number, ','.join(batch)))

            # Send the distribute transaction
            tx = {
                'from': self.account,
                'gas': int(self.total_distribute_tx_gas)
            }
            if self.gas_price:
                tx['gas_price'] = int(self.gas_price)
            txhash = self.distributor.transact(tx).distribute(batch)

            if self.wait:

                receipt, success = check_succesful_tx(self.web3, txhash)