#NEWLINE# def flatten_txns_by_hash( tx_list , nesting = True ) : #NEWLINE# #INDENT# '\n    Flattens a response from querying a list of address (or wallet) transactions\n\n    If nesting==True then it will return an ordered dictionary where the keys are tranasaction hashes, otherwise it will be a list of dicts.\n    (nesting==False is good for django templates)\n    ' #NEWLINE# nested_cleaned_txs = OrderedDict ( ) #NEWLINE# for tx in tx_list : #NEWLINE# #INDENT# tx_hash = tx . get ( 'tx_hash' ) #NEWLINE# satoshis = tx . get ( 'value' , 0 ) #NEWLINE# if ( tx . get ( 'tx_input_n' ) >= 0 ) : #NEWLINE# #INDENT# satoshis *= ( - 1 ) #NEWLINE# #UNINDENT# if ( tx_hash in nested_cleaned_txs ) : #NEWLINE# #INDENT# nested_cleaned_txs [ tx_hash ] [ 'txns_satoshis_list' ] . append ( satoshis ) #NEWLINE# nested_cleaned_txs [ tx_hash ] [ 'satoshis_net' ] = sum ( nested_cleaned_txs [ tx_hash ] [ 'txns_satoshis_list' ] ) #NEWLINE# if ( tx . get ( 'double_spend' ) and ( not nested_cleaned_txs [ tx_hash ] [ 'double_spend' ] ) ) : #NEWLINE# #INDENT# nested_cleaned_txs [ tx_hash ] [ 'double_spend' ] = True #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# nested_cleaned_txs [ tx_hash ] = { #NEWLINE# #INDENT# 'txns_satoshis_list' : [ satoshis ] , #NEWLINE# 'satoshis_net' : satoshis , #NEWLINE# 'received_at' : tx . get ( 'received' ) , #NEWLINE# 'confirmed_at' : tx . get ( 'confirmed' ) , #NEWLINE# 'confirmations' : tx . get ( 'confirmations' , 0 ) , #NEWLINE# 'block_height' : tx . get ( 'block_height' ) , #NEWLINE# 'double_spend' : tx . get ( 'double_spend' , False ) , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# #UNINDENT# if nesting : #NEWLINE# #INDENT# return nested_cleaned_txs #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# unnested_cleaned_txs = [ ] #NEWLINE# for tx_hash in nested_cleaned_txs : #NEWLINE# #INDENT# tx_cleaned = nested_cleaned_txs [ tx_hash ] #NEWLINE# tx_cleaned [ 'tx_hash' ] = tx_hash #NEWLINE# unnested_cleaned_txs . append ( tx_cleaned ) #NEWLINE# #UNINDENT# return unnested_cleaned_txs