<s> import json <EOL> import hashlib <EOL> import logging <EOL> from decimal import Decimal , getcontext <EOL> SATOSHI = <NUM_LIT> <EOL> class BaseHandler : <EOL> def __init__ ( self , oracle ) : <EOL> self . oracle = oracle <EOL> self . btc = oracle . btc <EOL> getcontext ( ) . prec = <NUM_LIT:8> <EOL> def handle_request ( self , request ) : <EOL> raise NotImplementedError ( ) <EOL> def handle_task ( self , task ) : <EOL> raise NotImplementedError ( ) <EOL> def get_observed_addresses ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ ] <EOL> def handle_new_transactions ( self , transactions ) : <EOL> pass <EOL> def valid_task ( self , task ) : <EOL> return True <EOL> def get_tx_hash ( self , tx ) : <EOL> inputs , outputs = self . btc . get_inputs_outputs ( tx ) <EOL> request_dict = { <EOL> "<STR_LIT>" : inputs , <EOL> "<STR_LIT>" : outputs , <EOL> } <EOL> return hashlib . sha256 ( json . dumps ( request_dict ) ) . hexdigest ( ) <EOL> def input_addresses ( self , prevtxs ) : <EOL> addresses = set ( ) <EOL> for prevtx in prevtxs : <EOL> if not '<STR_LIT>' in prevtx : <EOL> return False <EOL> script = prevtx [ '<STR_LIT>' ] <EOL> address = self . btc . decode_script ( script ) [ '<STR_LIT>' ] <EOL> addresses . add ( address ) <EOL> return list ( addresses ) <EOL> def try_prepare_raw_transaction_full_node ( self , message ) : <EOL> value = message [ '<STR_LIT:value>' ] <EOL> return_address = message [ '<STR_LIT>' ] <EOL> txid = message [ '<STR_LIT>' ] <EOL> n = message [ '<STR_LIT:n>' ] <EOL> oracle_fees = message [ '<STR_LIT>' ] <EOL> outputs = { } <EOL> left_cash = value - ( SATOSHI * message [ '<STR_LIT>' ] ) <EOL> has_my_fee = False <EOL> for oracle , fee in oracle_fees . iteritems ( ) : <EOL> outputs [ oracle ] = fee <EOL> left_cash -= fee <EOL> if self . oracle . is_fee_sufficient ( oracle , fee ) : <EOL> has_my_fee = True <EOL> if not has_my_fee : <EOL> logging . debug ( "<STR_LIT>" ) <EOL> return None <EOL> if left_cash < <NUM_LIT:0> : <EOL> logging . debug ( "<STR_LIT>" ) <EOL> return None <EOL> outputs [ return_address ] = left_cash <EOL> inputs = [ ( txid , n ) ] <EOL> transaction = self . btc . create_raw_transaction ( inputs , outputs ) <EOL> return transaction <EOL> def try_prepare_raw_transaction ( self , message ) : <EOL> inputs = [ ] <EOL> logging . debug ( message ) <EOL> for tx in message [ '<STR_LIT>' ] : <EOL> inputs . append ( { '<STR_LIT>' : tx [ '<STR_LIT>' ] , '<STR_LIT>' : tx [ '<STR_LIT>' ] } ) <EOL> if len ( self . input_addresses ( message [ '<STR_LIT>' ] ) ) > <NUM_LIT:1> : <EOL> logging . debug ( "<STR_LIT>" ) <EOL> return False <EOL> cash_back = Decimal ( Decimal ( SATOSHI ) * ( message [ '<STR_LIT>' ] - message [ '<STR_LIT>' ] ) ) <EOL> logging . debug ( cash_back ) <EOL> outputs = { } <EOL> has_my_fee = False <EOL> for oracle , fee in message [ '<STR_LIT>' ] . iteritems ( ) : <EOL> outputs [ oracle ] = fee <EOL> cash_back -= Decimal ( fee ) <EOL> if self . oracle . is_fee_sufficient ( oracle , fee ) : <EOL> has_my_fee = True <EOL> if not has_my_fee : <EOL> logging . debug ( "<STR_LIT>" ) <EOL> return None <EOL> if cash_back < <NUM_LIT:0> : <EOL> logging . debug ( "<STR_LIT>" ) <EOL> return None <EOL> outputs [ message [ '<STR_LIT>' ] ] = float ( cash_back ) <EOL> logging . debug ( "<STR_LIT>" % outputs ) <EOL> for address in outputs : <EOL> outputs [ address ] = float ( outputs [ address ] ) <EOL> logging . debug ( outputs ) <EOL> transaction = self . btc . create_raw_transaction ( inputs , outputs ) <EOL> return transaction </s>
